//Assignment No:4
Set A:
a)Define a class of patient(patient_name, patient_age, patient_oxy_level, patient_HRTC_report). Create an object of patient handle appropriate exception while patient oxygen level less 95% and HRTC scan report greater than 10, then throw user defined Exception "Patient is Covid Positive(+) and Need to Hospitalized" otherwise display its information.
CODE:
👇
- import java.util.*;
- class HealthException extends Exception
- {
- String msg = "I'm sorry..! Your Covid Positive(+)\nNeed To Hpspital italized\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\tPatitent Details\n");
- System.out.println("Patitent Name\t Patitent Age \t Oxygen_Level\tHRTC_Report");
- System.out.println(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 Patitent Name:");
- String pn = sc.next();
- System.out.println("Enter Patitent 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();
- }
- }
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).
CODE:
👇
- import java.io.*;
- public class FileContentReverse
- {
- public static void main(String[] args)
- {
- try
- {
- FileInputStream fin = new FileInputStream("sample.txt");
- String str = "";
- int ch;
- while((ch = fin.read()) != -1)
- {
- System.out.print(ch);
- str += (char)(ch);
- }//Reverser File Content
- StringBuffer sbf = new StringBuffer(str);
- System.out.println("\nOriginal Content of File:\n"+sbf);
- System.out.println("\nReverse File Content: \n"+ sbf.reverse());
- System.out.println("\nOriginal File Content in UppperCase:"+str.toUpperCase());
- fin.close();
- }
- catch (Exception e)
- {
- System.out.println(e);
- }
- }
- }
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.
CODE:
👇
- 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.next();
- System.out.println("Enter Name Of File 2:");
- String f2 = sc.next();
- 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();
- }
- }
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. //Skipped This Quesetion For Now
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.
CODE:
👇
- class InvalidUsernameException extends Exception
- {
- String msg = "Invalid Username...\n Try Again\n";
- public String toString()
- {
- return msg;
- }
- }
- class InvalidPasswordException extends Exception
- {
- String msg = "Invalid Password..\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;
- }
- }
- 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 Match...!");
- }
- 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);
- }
- }
- }
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
CODE:
👇
- import java.util.*;
- class InvalidDateException extends Exception
- {
- String msg = "Invalid Date...\nTry Again";
- 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("---------------------------");
- System.out.println("\tDay\tMonth\tYear");
- System.out.println("\t"+day+"\t"+mon+"\t"+yr);
- System.out.println("---------------------------");
- }
- }
- public class DateException
- {
- public static void main(String[] args)
- {
- 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);
- }
- }
- }
Sec 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 digits) 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.
CODE:
👇
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 from user defined Exception "Name is invalid" otherwise display it.(Name should contain only characters).
CODE:
👇