ASSIGNMENT-4
Set A:
Q1) 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 [Link].*;
class CovidPositiveException extends Exception
{
CovidPositiveException(String m)
{
super(m);
}
}
class patient
{
String name;
int age;
int oxylevel;
int HRCTreport;
patient(String name, int age, int oxylevel, int HRCTreport)
{
[Link] = name;
[Link] = age;
[Link] = oxylevel;
[Link] = HRCTreport;
}
}
public class setA1 extends Exception
{
public static void main(String[] args)
{
Scanner sc = new Scanner([Link]);
[Link]("How many patient you want insert:");
int number = [Link]();
patient[] ob = new patient[number];
for(int j=0; j<number; j++)
{
[Link]("Enter Name ");
String name = [Link]();
[Link]("Enter Age ");
int age = [Link]();
[Link]("Enter oxygen level");
int oxylevel = [Link]();
[Link]("Enter HRCT report");
int HRCTreport = [Link]();
ob[j] = new patient(name, age, oxylevel, HRCTreport);
}
for(int j=0; j<number; j++)
{
if(ob[j].oxylevel < 95 && ob[j].HRCTreport > 10)
try
{
throw new CovidPositiveException("List of Patient Covid Positive(+) and
Need to Hospitalized\n");
}
catch(CovidPositiveException e)
{
//[Link]("List of Patient Covid Positive(+) and Need to
Hospitalized\n");
[Link]([Link]());
[Link]("name: "+ob[j].name);
[Link]("age " + ob[j].age);
[Link]("oxygen level " +ob[j].oxylevel);
[Link]("HRCT report " + ob[j].HRCTreport);
[Link]("\n");
}
}
}
}
Q2) Write a program to read a text file “[Link]” and display the
contents of a file in reverse order and also original contents change
the case (display in upper case).
import [Link].*;
import [Link].*;
class setA2
{
public static void main(String args[]) throws IOException
{
FileReader f = new FileReader("[Link]");
Scanner sc = new Scanner(f);
String ch,ch2;
while([Link]())
{
StringBuilder sb = new StringBuilder();
ch = [Link]();
ch2=[Link]();
[Link](ch2);
[Link]();
[Link](sb);
}
[Link]();
}
Q3) 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 [Link].*;
import [Link].*;
class setA3{
public static void main(String arg[]) throws Exception {
Scanner sc = new Scanner([Link]);
[Link]("source file name :\n");
String file1 = [Link]();
[Link]("destination file name :\n");
String file2 = [Link]();
FileReader fin = new FileReader(file1);
FileWriter fout = new FileWriter(file2, true);
int c;
while ((c = [Link]()) != -1) {
[Link](c);
}
[Link]("Copy finish...");
[Link]();
[Link]();
}
}
Q4) Write a java program to read text from a file to a specified index
or skipping some byte using FileInputStream class.
import [Link].*;
public class setA4
{
public static void main(String[] args)
{
File file = new File("[Link]");
try
{
FileInputStream fi = new FileInputStream(file);
int ch;
[Link]("After skipped 5 bytes File's Content is : \n");
[Link](5);
while ((ch = [Link]()) != -1)
{
[Link]((char) ch);
}
}
catch (FileNotFoundException f) {
[Link]("FileNotFoundException : " + [Link]());
}
catch (IOException e1)
{
[Link]("IOException : " + [Link]());
}
catch (Exception e2) {
[Link]("Exception: " + [Link]());
}
}
}
Set B:
Q1) Write a program to read book information (bookid, bookname,
bookprice, bookqty) in file "[Link]". Write program i. Search for a
specific book by name.
import [Link].*;
import [Link].*;
import [Link].*;
class setB1
{
public static void main(String arg[]) throws Exception
{
FileInputStream fi=new FileInputStream("[Link]");
InputStreamReader isr=new InputStreamReader(fi);
BufferedReader br=new BufferedReader(isr);
Scanner sc=new Scanner([Link]);
int c,ch;
String bname,s;
[Link]("Enter Book name: ");
bname=[Link]();
int flag=0;
String line;
while ((line = [Link]()) != null) {
String[] data = [Link](" ");
if ([Link] == 4) {
String bookName = data[1].trim();
if ([Link](bname)) {
[Link]("Book found: ");
[Link]("ID: " + data[0]);
[Link]("Name: " + bookName);
[Link]("Price: " + data[2]);
[Link]("Quantity: " + data[3]);
flag=1;
break;
}
}
}
if(flag==0){
[Link]("Book not found: ");
}
}}
Q2) Define class EmailId with members ,username and [Link]
default and parameterized constructors. Accept values from the command
line Throw user defined exceptions – “InvalidUsernameException” or
“InvalidPasswordException” ifthe username and password are invalid.
import [Link].*;
import [Link];
class EmailId
{
String username,password;
EmailId()
{
username="TYBCS";
password="root123";
}
EmailId(String user,String pass)
{
username=user;
password=pass;
}
}
class setB2
{
public static void main(String[] args)
{
String user,pass;
user=args[0];
pass=args[1];
int usr1=0,psw1=0;
EmailId obj=new EmailId();
EmailId obj1=new EmailId(user,pass);
if(([Link]).equals([Link])){
[Link]("Valid User");
usr1=1;
}
else{
try{
throw new InvalidUsernameException(" Invalid Username");
}
catch (InvalidUsernameException e) {
[Link]([Link]()) ;
}
}
if([Link]([Link])){
[Link]("Valid Password");
psw1=1;
}
else{
try{
throw new InvalidPasswordException(" Invalid Password");
}
catch (InvalidPasswordException e) {
[Link]([Link]()) ;
}
}
if(usr1==1 && psw1==1)
{
[Link]("Login Successful");
}
else{
[Link]("You don't have permission to login ");
}
}
}
class InvalidUsernameException extends Exception{
InvalidUsernameException(String usr) {
super(usr);
// [Link]("InvalidUsernameException") ;
}
}
class InvalidPasswordException extends Exception{
InvalidPasswordException(String psw)
{
super(psw);
//[Link]("InvalidPasswordException") ;
}
}