JAVA-I Assignment 4- Exception And File handling
Set A
a) Define a class patient (patient_name, patient_age, patient_oxy_level,patient_HRCT_report). Create an object of patient. Handle appropriate exception while patient oxygen level less than 95% and HRCT scan report greater than 10, then throw user defined Exception “Patient is Covid Positive(+) and Need to Hospitalized” otherwise display its information.
import java.util.*;
class HealthException extends Exception {
String msg = "I'm sorry ! Your'e Covid Positive(+) \n Need to Hospitalized\n";
public String toString() {
return msg;
}
}
class patient {
String pname;
byte page;
int p_oxy_level;
int p_HRTC_report;
patient(String p, byte a, int ol, int pr) {
pname = p;
page = a;
p_oxy_level = ol;
p_HRTC_report = pr;
}
public void display() {
System.out.println("\n\t\tPatient Details\n");
System.out.println("\tPatient Name\tPatient Age\tOxygen_level\tHRTC_Report");
System.out.println("\t" + pname + "\t\t" + page + "\t\t" + p_oxy_level + "\t\t" + p_HRTC_report);
}
}
public class Hospital {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
System.out.println("Enter Patient Name: ");
String pn = sc.nextLine();
System.out.println("Enter Patient Age: ");
Byte pa = sc.nextByte();
System.out.println("Enter Oxygen Level: ");
int ol = sc.nextInt();
System.out.println("Enter HRTC Report: ");
int pr = sc.nextInt();
patient p = new patient(pn, pa, ol, pr);
if (p.p_oxy_level < 95 && p.p_HRTC_report > 10) {
throw new HealthException();
}
p.display();
} catch (Exception e) {
System.out.println(e);
}
sc.close();
}
}
/*O/P:
javac Hospital.java
java Hospital
Enter Patient Name: ABC
Enter Patient Age: 67
Enter Oxygen Level:98
Enter HRTC Report:8
Patient Details
Patient Name Patient Age Oxygen_level HRTC_Report
ABC 67 98 8
java Hospital
Enter Patient Name: XYZ
Enter Patient Age:89
Enter Oxygen Level:78
Enter HRTC Report:15
I'm sorry ! Your'e Covid Positive(+)
Need to Hospitalized
*/
b) Write a program to read a text file “sample.txt” and display the contents of a file in reverse order and also original contents change the case (display in upper case).
import java.io.*;
public class FileContentReverse {
public static void main(String[] args) {
try {
FileInputStream fin = new FileInputStream("sample.txt");
String str = "";
int ch;
System.out.println("Original Content of File :\n");
while ((ch = fin.read()) != -1) {
System.out.printf("%c", ch);
str += (char) (ch);
}
// reverse File Content
StringBuffer sbf = new StringBuffer(str);
System.out.println("\n\nReverse File Content: \n" + sbf.reverse());
System.out.println("\nOriginal file content in UpperCase: \n" + str.toUpperCase());
fin.close();
} catch (Exception e) {
System.out.println(e);
}
}
}
/*
// sample.txt file content
Bcs Bca Mca
We are cs Students.
*/
/*
O/P
javac FileContentReverse.java
java FileContentReverse
Original Content of File :
Bcs Bca Mca
We are cs Students.
Reverse File Content:
.stnedutS sc era eW
acM acB scB
Original file content in UpperCase:
BCS BCA MCA
WE ARE CS STUDENTS.
*/
c) Accept the names of two files and copy the contents of the first to the second.
First file having Book name and Author name in file. Second file having the contents
of First file and also add the comment ‘end of file’ at the end.
import java.io.*;
import java.util.*;
public class FileCopy {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name of File 1 : ");
String f1 = sc.nextLine();
System.out.println("Enter Name of File 2 : ");
String f2 = sc.nextLine();
try {
FileInputStream fin = new FileInputStream(f1);
FileOutputStream fout = new FileOutputStream(f2);
int ch;
while ((ch = fin.read()) != -1) {
fout.write(ch);
}
String end = "\nEnd of File";
byte b[] = end.getBytes();
fout.write(b);
System.out.println("File Content Copied\n");
fin.close();
fout.close();
} catch (Exception e) {
System.out.println(e);
}
sc.close();
}
}
/*
// first.txt file content
Book Name : Java
Author Name : Dr.Ms.Manisha Bharambe and Ms.Manisha Gadekar
*/
/*O/P
javac FileCopy.java
java FileCopy
Enter Name of File 1 :
first.txt
Enter Name of File 2 :
second.txt
File Content Copied
*/
/*
// second.txt file content after Execution of program
Book Name : Java
Author Name : Dr.Ms.Manisha Bharambe and Ms.Manisha Gadekar
End of File
*/
Set B
a) Write a program to read book information (bookid, bookname, bookprice, bookqty) in file “book.dat”. Write a menu driven program to perform the following operations using Random access file:
i. Search for a specific book by name.
ii. Display all book and total cost
b) Define class EmailId with members ,username and password. Define default and parameterized constructors. Accept values from the command line Throw user defined exceptions – “InvalidUsernameException” or “InvalidPasswordException” if the username and password are invalid.
class InvalidUsernameException extends Exception {
String msg = "Invalid Username....\nTry Again\n";
public String toString() {
return msg;
}
}
class InvalidPasswordException extends Exception {
String msg = "Invalid Pasword....\nTry Again\n";
public String toString() {
return msg;
}
}
class EmailId {
String username;
String password;
EmailId() {
username = "dsk";
password = "dsk@123";
}
EmailId(String u, String p) {
username = u;
password = p;
}
}// EmailId
public class password {
public static void main(String[] args) {
EmailId e1 = new EmailId(args[0], args[1]);
EmailId e2 = new EmailId();
String s1 = e1.username;
String s2 = e2.username;
String s3 = e1.password;
String s4 = e2.password;
try {
if (s1.equals(s2)) {
System.out.println("Username Matched..!");
} else {
throw new InvalidUsernameException();
}
} catch (InvalidUsernameException e) {
System.out.println(e);
}
try {
if (s3.equals(s4)) {
System.out.println("Password Matched..!");
} else {
throw new InvalidPasswordException();
}
} catch (InvalidPasswordException e) {
System.out.println(e);
}
}
}/*
O/P:
javac password.java
java password dsk dsk@123
Username Matched..!
Password Matched..!
java password dskShubham dsk@123
Invalid Username....
Try Again
Password Matched..!
c) Define a class MyDate (day, month, year) with methods to accept and display a MyDate object. Accept date as dd, mm, yyyy. Throw user defined exception “InvalidDateException” if the date is invalid.
Examples of invalid dates : 03 15 2019, 31 6 2000, 29 2 2021
import java.util.Scanner;
class InvalidDateException extends Exception {
String msg = "Invalid date....\nTry Again\n";
public String toString() {
return msg;
}
}
class MyDate {
int day, mon, yr;
MyDate(int d, int m, int y) {
day = d;
mon = m;
yr = y;
}
void display() {
System.out.println("\n\t\tDate\n");
System.out.println("\t----------------------");
System.out.println("\tDay\tMonth\tYear");
System.out.println("\t----------------------");
System.out.println("\t" + day + "\t" + mon + "\t" + yr);
System.out.println("\t----------------------");
}
}
public class DateException {
public static void main(String arg[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Date : dd mm yyyy ");
int day = sc.nextInt();
int mon = sc.nextInt();
int yr = sc.nextInt();
int flag = 0;
try {
if (mon <= 0 || mon > 12)
throw new InvalidDateException();
else {
if (mon == 1 || mon == 3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon == 12) {
if (day >= 1 && day <= 31)
flag = 1;
else
throw new InvalidDateException();
} else if (mon == 2) {
if (yr % 4 == 0) {
if (day >= 1 && day <= 29)
flag = 1;
else
throw new InvalidDateException();
} else {
if (day >= 1 && day <= 28)
flag = 1;
else
throw new InvalidDateException();
}
} else {
if (mon == 4 || mon == 6 || mon == 9 || mon == 11) {
if (day >= 1 && day <= 30)
flag = 1;
else
throw new InvalidDateException();
}
}
}
if (flag == 1) {
MyDate dt = new MyDate(day, mon, yr);
dt.display();
}
} catch (InvalidDateException e) {
System.out.println(e);
}
}
}
/*O/P:
javac DateException.java
java DateException
Enter Date : dd mm yyyy
17 9 2000
Date
----------------------
Day Month Year
17 9 2000
----------------------
java DateException
Enter Date : dd mm yyyy
31 06 2021
Invalid date....
Try Again
java DateException
Enter Date : dd mm yyyy
29 2 2021
Invalid date....
Try Again
java DateException
Enter Date : dd mm yyyy
29 2 2020
Date
----------------------
Day Month Year
----------------------
29 2 2020
----------------------
*/
Set C
a) Write a menu driven program to perform the following operations on a set of integers as shown in the following figure. A load operation should generate 10 random integers (2 digit) and display the number on screen. The save operation should save the number to a file “number.txt”. The short menu provides various operations and the result is displayed on the screen.
import java.io.*;
import java.util.*;
public class FileMenu {
public static void main(String[] args) {
try {
FileOutputStream fout = new FileOutputStream("number.txt");
FileReader fr = new FileReader("number.txt");
Scanner sc = new Scanner(System.in);
int choice, n;
String num = "";
do {
System.out.println("\n\n\tFile Menu\n");
System.out.println("1. Generate and Disply Numbers");
System.out.println("2. Save Numbers in file");
System.out.println("3. View File Content");
System.out.println("4. Exit");
System.out.println("Enter Your Choice");
choice = sc.nextInt();
switch (choice) {
case 1:
Random r = new Random();
for (int i = 0; i < 10; i++) {
n = r.nextInt(99);
num += n + " ";
System.out.print(n + " ");
}
break;
case 2:
byte b[] = num.getBytes();
fout.write(b);
System.out.println("\n\tFile Content Saved");
fout.close();
break;
case 3:
int ch;
System.out.println("\n\tFile Content\n");
while ((ch = fr.read()) != -1) {
System.out.print((char) ch);
}
fr.close();
break;
case 4:
System.exit(0);
break;
}// switch
} while (choice != 4);
} catch (Exception e) {
System.out.println(e);
}
}
}
/**
O/p
javac FileMenu.java
java FileMenu
File Menu
1. Generate and Disply Numbers
2. Save Numbers in file
3. View File Content
4. Exit
Enter Your Choice
1
58 25 95 33 45 63 97 54 1 75
File Menu
1. Generate and Disply Numbers
2. Save Numbers in file
3. View File Content
4. Exit
Enter Your Choice
2
File Content Saved
File Menu
1. Generate and Disply Numbers
2. Save Numbers in file
3. View File Content
4. Exit
Enter Your Choice
3
File Content
58 25 95 33 45 63 97 54 1 75
File Menu
1. Generate and Disply Numbers
2. Save Numbers in file
3. View File Content
4. Exit
Enter Your Choice
4
*/
b) Write a java program to accept Employee name from the user and check whether it is valid or not. If it is not valid then throw user defined Exception “Name is Invalid” otherwise display it.(Name should contain only characters)
import java.util.Scanner;
class NameisInvalid extends Exception {
String msg = "\nInvalid Name....\nName should contain only characters\nThank You :)\n";
public String toString() {
return msg;
}
}
class Employee {
String name;
Employee(String name) {
this.name = name;
}
}
public class EmployeeException {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter Name :");
String name = sc.nextLine();
try {
for (int i = 0; i < name.length(); i++) {
int ch = (int) name.charAt(i);
if ((ch >= 65 && ch <= 90) || (ch >= 97 && ch <= 122)) {
} else {
throw new NameisInvalid();
}
}
System.out.println("\nEmployee Name:-" + name +"\n");
} catch (NameisInvalid e) {
System.out.println(e);
}
}
}
/*O/P:
javac EmployeeException.java
java EmployeeException
shubham
Employee Name:-shubham
java EmployeeException
Enter Name :
9dsk
Invalid Name....
Name should contain only characters
Thank You :)
java EmployeeException
Enter Name :
dsh/
Invalid Name....
Name should contain only characters
Thank You :)
java EmployeeException
Enter Name :
dsk'shubham'
Invalid Name....
Name should contain only characters
Thank You :)
java EmployeeException
Enter Name :
"Shubham"
Invalid Name....
Name should contain only characters
Thank You :)
java EmployeeException
Enter Name :
SHUBHAM
Employee Name:-SHUBHAM
java EmployeeException
Enter Name :
SHUbham
Employee Name:-SHUbham
*/
No comments:
Post a Comment