OOPS Lab 4,5,6
OOPS Lab 4,5,6
OOPS Lab 4,5,6
Date:
Problem:
Implement exception handling and creation of user defined exceptions.
Aim:
To write a Java program to implement user defined exception handling.
Algorithm:
Step 1:Start.
Step 2:Create a class MyException that extends Exception class
Step 3:Get an input
Step 4:Check whether the input is greater than or equal to zero.
a) If yes, print the number.
b) Otherwise, handle the exception using try - catch block
Step 5:Stop.
Program:
import java.io.*;
class MyException extends Exception
{
MyException(String message)
{
super(message);
}
}
class ExceptMain
{
public static void main(String a[])
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.printf("Enter a positive number : ");
int no=Integer.parseInt(br.readLine());
if(no<0)
{
throw new MyException(" Number must be positive ");
}
System.out.println(" Number:" +no);
}
catch(MyException e)
{
System.out.println("Caught the Exception");
System.out.println(e.getMessage());
System.out.println("Exception Handled ...... !");
}
catch(Exception e)
{
System.out.println("Enter numbers ... Exception Handled!");
}
}
}
Viva Questions:
Result:
Thus the Java program to implement user defined exception handling was implemented and
the output was verified successfully.
Ex. No. FILE HANDLING
Date:
Problem:
Write a Java program to perform file operations.
Aim:
To write a Java program to perform file operations.
Algorithm:
Step 1:Start.
Step 2:Get the input as filename.
Step 3:If the filename exists, then
a) Print the details about the file (Is File?, Is Directory?, Is Readable?, Is Writable?, Type,
Length of the file.
b) Otherwise, print “File does not exist”
Step 4:Stop.
Program:
import java.util.Scanner;
import java.io.File;
class FileInfo
{
public static String getFileExtension(File f1)
{
String fileName = f1.getName();
if(fileName.lastIndexOf(".") != -1 && fileName.lastIndexOf(".") != 0)
return fileName.substring(fileName.lastIndexOf(".")+1);
else return "Folder";
}
public static void main(String[] args)
{
Scanner input=new Scanner(System.in);
System.out.print("\nEnter the filename: ");
String s=input.nextLine();
File f1=new File(s);
if(f1.exists())
{
System.out.println("\nDETAILS ABOUT THE FILE");
System.out.println(" --------------------- ");
System.out.println(" File exists in : "+f1.getAbsolutePath());
System.out.println("\n Is file? :"+f1.isFile());
System.out.println(" Is Directory? :"+f1.isDirectory());
System.out.println("\n Is Readable? :"+f1.canRead());
System.out.println(" Is Writable? :"+f1.canWrite());
System.out.println("\n Type :"+getFileExtension(f1));
System.out.println("\n Length of the File :"+f1.length()+" Bytes");
}
else
{
System.out.println("File does not exist");
}
}
}
Viva Questions:
Result:
Thus the Java program for reading a file name from the user and displaying the information
about the file was written and the output was verified successfully.
Ex. No. MINI PROJECT
Date:
Problem:
Develop a mini project for any application using Java concepts.
Aim:
To develop a Hospital Management System as a mini project using java concepts
Algorithm:
Step 1: Start
Step 2: Create classes that are needed for hospital management system
Step 3: Get the option ( Doctor, Patient, medical, lab,facility, Staff) and perform the operations.
Step 4: Stop
Program:
import java.io.*;
import java.util.*;
import java.util.Calendar;
class staff
{
String sid,sname,desg,sex;
int salary;
void new_staff()
{
Scanner input=new Scanner(System.in);
System.out.print("id:-");sid=input.nextLine();
System.out.print("name:-");sname=input.nextLine();
System.out.print("desigination:-");desg=input.nextLine();
System.out.print("sex:-");sex=input.nextLine();
System.out.print("salary:-");salary=input.nextInt();
}
void staff_info()
{
System.out.println(sid+"\t"+sname+"\t"+sex+"\t"+salary);
}
}
class doctor
{
String did,dname,specilist,appoint,doc_qual;
int droom;
void new_doctor()
{
Scanner input=new Scanner(System.in);
System.out.print("id:-");did=input.nextLine();
System.out.print("name:-");dname=input.nextLine();
System.out.print("specilization:-");specilist=input.nextLine();
System.out.print("work time:-");appoint=input.nextLine();
System.out.print("qualification:-");doc_qual=input.nextLine();
System.out.print("room no.:-");droom=input.nextInt();
}
void doctor_info()
{
System.out.println(did+"\t"+dname+" \t"+specilist+" \t"+appoint+" \t"+doc_qual+"
\t"+droom);
}
}
class patient
{
String pid,pname,disease,sex,admit_status;
int age;
void new_patient()
{
Scanner input=new Scanner(System.in);
System.out.print("id:-");pid=input.nextLine();
System.out.print("name:-");pname=input.nextLine();
System.out.print("disease:-");disease=input.nextLine();
System.out.print("sex:-");sex=input.nextLine();
System.out.print("admit_status:-");admit_status=input.nextLine();
System.out.print("age:-");age=input.nextInt();
}
void patient_info()
{
System.out.println(pid+"\t"+pname+" \t"+disease+" \t"+sex+" \t"+admit_status+"\t"+age);
}
}
class medical
{
String med_name,med_comp,exp_date;
int med_cost,count;
void new_medi()
{
Scanner input=new Scanner(System.in);
System.out.print("name:-");med_name=input.nextLine();
System.out.print("comp:-");med_comp=input.nextLine();
System.out.print("exp_date:-");exp_date=input.nextLine();
System.out.print("cost:-");med_cost=input.nextInt();
System.out.print("no of unit:-");count=input.nextInt();
}
void find_medi()
{
System.out.println(med_name+" \t"+med_comp+" \t"+exp_date+" \t"+med_cost);
}
}
class lab
{
String fecility;
int lab_cost;
void new_feci()
{
Scanner input=new Scanner(System.in);
System.out.print("fecility:-");fecility=input.nextLine();
System.out.print("cost:-");lab_cost=input.nextInt();
}
void feci_list()
{
System.out.println(fecility+"\t\t"+lab_cost);
}
}
class fecility
{
String fec_name;
void add_feci()
{
Scanner input=new Scanner(System.in);
System.out.print("fecility:-");fec_name=input.nextLine();
}
void show_feci()
{
System.out.println(fec_name);
}
}
System.out.println(" --------------------------------------------------------------------------------");
System.out.println(" ***HOSPITAL MANAGEMENT SYATEM***");
System.out.println(" --------------------------------------------------------------------------------");
System.out.print("Date: "+months[calendar.get(Calendar.MONTH)]+" " +
calendar.get(Calendar.DATE) + " "+calendar.get(Calendar.YEAR));
System.out.println("\t\t\t\t\t\tTime: "+calendar.get(Calendar.HOUR) +
":"+calendar.get(Calendar.MINUTE) + ":"+calendar.get(Calendar.SECOND));
f[0].fec_name="ambulane";
f[1].fec_name="admit fec";
f[2].fec_name="canteen";
f[3].fec_name="free camp";
int choice,j,c1,status=1,s1=1,s2=1,s3=1,s4=1,s5=1,s6=1;
while(status==1)
{
System.out.println("\n MAIN MENU");
System.out.println(" --------------------------------------------------------------------------------");
System.out.println("1.DOCTOR 2. PATIENT 3.MEDICAL 4.LAB 5. FACILITY 6.STAFF
7.EXIT");
System.out.println(" --------------------------------------------------------------------------------");
choice=input.nextInt();
switch(choice)
{
case 1:
{
System.out.println(" --------------------------------------------------------------------------------");
System.out.println(" **DOCTOR SECTION**");
System.out.println(" -------------------------------------------------------------------------------- ");
s1=1;
while(s1==1)
{
System.out.println("1.new entry\n2.doctor list");
c1=input.nextInt();
switch(c1)
{
case 1:
{
d[count1].new_doctor();count1++;
break;
}
case 2:
{
System.out.println(" -------------------------------------------------------------------------------- ");
System.out.println("id \t name\t specilist \t timing \t qualification \t room no");
System.out.println(" -------------------------------------------------------------------------------- ");
for(j=0;j<count1;j++)
{
d[j].doctor_info();
}
break;
}
}
String a="nurse",b="worker",c="security";
System.out.println("1.new entry\n2.nurse list\n3.worker list \n4.securuty list");
c1=input.nextInt();
switch(c1)
{
case 1:{s[count6].new_staff();count6++;break;}
case 2:
{
System.out.println(" --------------------------------------------------------------------------------");
1. Is constructor inherited?
2. What is this in java?
3. Why multiple inheritance is not supported in java?
4. Why Java does not support pointers?
5. What is object cloning?
Result:
Thus the Hospital Management System as a mini project using java concepts was done and
executed successfully.