B.
E 3rd Year 5th Sem 150330107016
Practical 11
Aim: Write a method for computing X ydoing repetitive multiplication. X and
Y are of type integer and are to be given as command line arguments. Raise
and handle exception(s) for invalid values of x and y.
DESCRIPTION:
Exception: It is an abnormal condition that arise at Run-time. An exception is an
unwanted or unexpected event. It disturb normal flow of the [Link] exception is an
object that describe an exceptional condition that occur in piece of code.
Exception Handling:Mechanism to handle the run-time error. It is managed via 5
keywords. try, catch, throw, throws, finally.
Syntax:
try ,catch, finally
try
{
// block of code to monitor for errors
}
catch (ExceptionType1 exOb)
{
// exception handler for ExceptionType1
}
catch (ExceptionType2 exOb)
{
// exception handler for ExceptionType2
}
// ...
finally
{
// block of code to be executed before try block ends
}
Here, ExceptionTypeis the type of exception that has occurred.
throw :In java it is possible to throw an exception explicitly using throw statement.
throw new ExceptionType();
throws:If method generate exception than it does not handle. In JAVA, this done by throws
classes in the method’s declaration.
return-type method-name(parameter-list)throwsExceptionType
{
//body of method;
}
MGITER/CE/OOPJ-2019 Page 1
B.E 3rd Year 5th Sem 150330107016
PROGRAM:
class A1
{
void p1(int x,int y)
{
double z=[Link](x,y);
[Link]("X^Y="+x+"^"+y+"="+z);
}
}
class prg11
{
public static void main(String S[])
{
try
{
int x=[Link](S[0]);
int y=[Link](S[1]);
[Link]("Value of X="+x);
[Link]("Value of Y="+y);
A1 o=new A1();
o.p1(x,y);
}
catch(Exception e)
{
[Link]("Invalid value of X or Y \n Error="+e);
}
}
}
OUTPUT:
MGITER/CE/OOPJ-2019 Page 2
B.E 3rd Year 5th Sem 150330107016
Practical 12
Aim: Write a java program to create and start Multiple threads
independently also use sleep( ) and join( ) methods.
DESCRIPTION:
Thread:A Thread is a smallest unit of executable code that performs a particular
[Link] are sometimes called lightweight processes. Both processes and threads
provide an execution environment. Thread are implemented in the form of object that
contain a method called run( ). The run( ) method is the heart of any thread.
public void run( )
{ ……..
// body of run method;
}
The run( ) method should be invoked by an object of the concerned thread. this can be
achived by vcreating the thread and initiating it with the help of another thread method
called start( ).
There are two ways to create a thread:
1. By extending Thread class
2. By implementing Runnable interface.
Once you have a Thread object, you call its start method to start the [Link] a thread is
started, its run method is [Link] the run method returns or throws an exception, the
thread dies and will be garbage-collected.
Every Thread has a state and a Thread can be in one of these five states.
Newborn State: A state in which a thread has not been started.
Runnable State: A state in which a thread is ready for execution and is waiting for
availability of processor.
Running State: A state in which a processor has given its time to the thread for its
execution.
Blocked State: A state in which a thread is waiting for a lock to access an object.
Dead state: A state in which a thread has exited.
MGITER/CE/OOPJ-2019 Page 3
B.E 3rd Year 5th Sem 150330107016
Multithreading:Multithreading enables you to write very efficient programs that make
maximum use of the CPU, Because idle time can be kept to a minimum.
sleep( ): The sleep( ) method causes the Thread from which it is called to a suspend
execution for the specified period of milliseconds.
void sleep( long milliseconds)
join( ): The join( ) method causes the current thread to wait until the thread on which the
join( ) method is called terminates.
PROGRAM:
class Th extends Thread
{
int i;
public void run()
{
try
{
for(i=1;i<4;i++)
{
[Link]("Thread="+i);
[Link](1000);
}
}
catch(Exception e)
{
[Link]("Error="+e);
}
}
}
class prg12
{
public static void main(String S[])
{
Th o1=new Th();
Th o2=new Th();
Th o3=new Th();
[Link]("1st process");
try
{
[Link]();
[Link]();
}
MGITER/CE/OOPJ-2019 Page 4
B.E 3rd Year 5th Sem 150330107016
catch(Exception e)
{
[Link]("Error="+e);
}
[Link]("2nd process");
[Link]();
[Link]("3rd process");
[Link]();
}
}
OUTPUT:
MGITER/CE/OOPJ-2019 Page 5
B.E 3rd Year 5th Sem 150330107016
Practical 13
Aim::Create a class which asks the user to enter a sentence and it should
display count of Each vowel type in the sentence. The program should
continue till user enters a Word “Quit”. Display the total count of each
vowel for all sentences.
DESCRIPTION:
Reading Console Input:In JAVA console input is done by reading from [Link]
obtain a character-based stream,you wrap [Link] in BufferedReader object
BufferedReader( Readerinputreader );
Here,inputreader is the stream that is linked to the instance of BufferedReader
To obtain an instance an InputReader object which is linked to [Link] use the
following construstor.
InputStreamReader( InputStreaminputstream );
Beacause [Link] refers to an object of type InputStream, it can be used for
inputstream. The following line of code creates a BufferedReader that is connected to the
keyboard.
BufferedReaderbr = new BufferedReader( newInputStreamReader ( [Link] ) );
Here,br is a character based streamthat is linked to the console input through [Link]
To read character from a BufferedReader use read( ).
int read( ) throws IOException
To read a string from the keyboard use readLine( )
String readLine( ) throws IOException
PROGRAM:
import [Link].*;
class prg13
{
public static void main(String S[])throws IOException
{
int i,count=0;
int sum=0;
String s;
BufferedReader BR=new BufferedReader(new InputStreamReader([Link]));
do
{
[Link]("Enter your String=");
s=[Link]();
if([Link]("Quit"))
MGITER/CE/OOPJ-2019 Page 6
B.E 3rd Year 5th Sem 150330107016
{
break;
}
count=0;
char c;
int l=[Link]();
for(i=0;i<l;i++)
{
c=[Link](i);
if(c=='a'||c=='e'||c=='i'||c=='o'||c=='u'||c=='A'||c=='E'||c=='I'||c=='O'||c=='U')
{
count=count+1;
}
}
[Link]("Vowel of the sentence="+count);
sum=sum+count;
[Link]("TotelVowwl="+sum);
}while(true);
}
}
OUTPUT:
MGITER/CE/OOPJ-2019 Page 7
B.E 3rd Year 5th Sem 150330107016
Practical 14
Aim: Write a program to replace all “VBP” by “MGITER” from a file1 and
output is written to file2 file and display the number of replacement.
DESCRIPTION:
File Class:Java File class represents the files and directory pathnames in an abstract
manner. This class is used for creation of files and directories, file searching, file deletion
etc.
File ( Stringdirectorypath )
File (String directorypath, String filename )
File ( File obj, String filename )
FileReader:TheFileReader class creates a Reader that you can use to read the contents of a
file.
FileReader( stringfilepath )
FileReader( Filefileobj )
FileWriter:TheFileReader class creates a Reader that you can use to read the contents of a
file.
FileWriter( stringfilepath )
FileWriter( stringfilepath, Boolean append )
FileWriter( Filefileobj )
FileWriter( Filefileobj, Boolean append )
PROGRAM:
import [Link].*;
class prg14
{
public static void main(String arg[])throws IOException
{
String s1=new String();
FileReader f1=new FileReader("[Link]");
BufferedReader Bi=new BufferedReader(f1);
FileWriter f2=new FileWriter("[Link]");
BufferedWriter Bo=new BufferedWriter(f2);
String s2=new String();
while((s1=[Link]())!=null)
{
MGITER/CE/OOPJ-2019 Page 8
B.E 3rd Year 5th Sem 150330107016
s2=s2+s1;
}
s2=[Link]("VBP","MGITER");
[Link](s2);
[Link]();
[Link]();
[Link]("Data cpoy Successfully");
}
}
OUTPUT:
MGITER/CE/OOPJ-2019 Page 9
B.E 3rd Year 5th Sem 150330107016
Practical 15
Aim: Write a program to demonstrate connection-oriented(TCP) socket
programming for Addition of multiple value which pass as command
line argument.
DESCRIPTION:
JAVA Networking:JAVA Networking is a concept of connecting two or more
computing devices together so we can share resources. The [Link] package provides
support for networking.
Socket Programming:JAVA Socket programming provides facility to share data
between different computing devices. JAVA Socket programming can be connection-
oriented or Connection-less.
Socket&ServerSocket classes are used for connection-oriented socket programming.
DatagramSocket&DatagramPacket classes are used for connection-less socket
Programming.
Socket class:It is used to create Socket. A Socket class is designed to connect to
ServerSocket and initiate protocol change.
Socket ( String hostname, int port );
Socket ( InetAddressipaddress, int port );
ServerSocketclass:It is quite different from normal Socket. When we create a
ServerSocket it will register itself with the system as having an interest in client
connection.
ServerSocket( int port );
ServerSocket( int port, int maxQueue );
ServerSocket (int port, int maxQueue, InetAddresslocaladdress );
ServerSocket has method called accept( ). It returns the socket and establishes connection
between server and client.
Socket accept( );
MGITER/CE/OOPJ-2019 Page 10
B.E 3rd Year 5th Sem 150330107016
PROGRAM:
CLINT:
import [Link].*;
import [Link].*;
class cprg15
{
public static void main(String arg[])throws Exception
{
Socket s=new Socket("localhost",80);
InputStream I=[Link]();
OutputStream O=[Link]();
int n1,n2;
n1=[Link](arg[0]);
int l=[Link];
[Link](l);
for(int i=1;i<l;i++)
{
n2=[Link](arg[i]);
[Link](n1);
[Link](n2);
n1=[Link]();
}
[Link]("Total Sum="+n1);
[Link]();
[Link]();
[Link]();
}
}
SERVER:
import [Link].*;
import [Link].*;
class sprg15
{
public static void main(String arg[])throws Exception
{
ServerSocket S1=new ServerSocket(80);
Socket S=[Link]();
InputStream I=[Link]();
OutputStream O=[Link]();
int n1,n2,sum;
int l=[Link]();
for(int i=1;i<l;i++)
{
n1=[Link]();
MGITER/CE/OOPJ-2019 Page 11
B.E 3rd Year 5th Sem 150330107016
n2=[Link]();
sum=n1+n2;
[Link](sum);
}
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
MGITER/CE/OOPJ-2019 Page 12
B.E 3rd Year 5th Sem 150330107016
Practical 16
Aim: Prepare a class diagram for a given group of classes using multiplicity,
generalization, association concepts. And add at least 4-5 attributes and 2-3
operations for particular class Doctor, Patient, staff, Room, Dept, Ward, and Bill
HOSPITAL
bill char() Name
department char() 1
1
word char()
person char()
room char()
administration()
WARD
word_no:int()
type:char()
bed:int()
staff:char()
1..* capacity:int()
DEPARTMENT maintainance();
deaning();
BILL
type:char
medicine:char() doctors:char
prize:float() patient:char
patient:char() staff:char
bottels:char()
injection:char() add_doctor() ROOM
generate() 1..* remove_doctor()
modified() add_dept() patients:char()
* * fans:int()
paid() PERSON remove_dept
light:int()
* name:char() stool:int()
id:int() *
service wiping();
gender:string()
moping();
address:char()
renovation();
phone_no:int()
appoinment();
give_detail(); Name Name
ask_detail();
Name
Name Name GENERAL ROOM SPECIAL ROOM
1 * bed:int() bed:char()
PATIENT DOCTOR stands:int() AC:int()
STAFF
get_water(); refregerator:char()
disease:char() degree:char() television:char()
joined_date:int() get_medicine();
doctor:char() check speciality:string()
education:int() get_water
age:int time:int()
shift:char() food();
weight:int salary:int()
salary:int() medicine();
admitted(); chech_patient(); duty:char()
get_medicine(); give_medicine
patient_record();
sergery();
wiping();
MGITER/CE/OOPJ-2019 Page 13
B.E 3rd Year 5th Sem 150330107016
Practical 17
Aim: Prepare a state diagram for Photocopier (Xerox) machine from the
description given below. Initially the machine is off. When the operator
switches on the machine, it first warms up during which it performs some
internal tests. Once the tests are over, machine is ready for making copies.
When operator loads a page to be photocopied and press ‘start’ button,
machine starts making copies according to the number of copies selected.
While machine is making copies, machine may go out of paper. Once
operator loads sufficient pages, it can start making copies again. During the
photocopy process, if paper jam occurs in the machine, operator may need to
clean the path by removing the jammed paper to make the machine ready.
MGITER/CE/OOPJ-2019 Page 14
B.E 3rd Year 5th Sem 150330107016
System
Xerox Machine
Off
Switch on
Warmup
Internal test
Ready Copy Succesful
Start button
Load page
Select [Link] copy
Paper loaded
Out of paper Making copy
Paper need
Paper path blocked
Paper jam Start copy
Removed paper jam
Ready
MGITER/CE/OOPJ-2019 Page 15
B.E 3rd Year 5th Sem 150330107016
Practical 18
Aim: Prepare a use-case diagram for Hotel Information System. There are two
types of customers: Tour-group customers and Individual customers. Both
can book, cancel, check-in and check-out of a room by Phone or via the
Internet. There are booking process clerk and reception staff who manages it.
A customer can pay his bill by credit card or pay utility bill.
Hotal information System
search hotel
Book room
Check in
Tour Check out Individuala
group
customer
customer
Cancle
pay bill
Pay utility bill
Check booking
request
Alocate room
Alocate room
Reception
Manage room
Manage
services
Clerk Staff
MGITER/CE/OOPJ-2019 Page 16
B.E 3rd Year 5th Sem 150330107016
Practical 19
Aim: Prepare Scenario and sequence diagram for booking a train ticket on line. Also
Prepare sequence diagram for booking a train ticket on line that fails.
FOR RESERVATION SUCCESSFULLY
Scenario:
=>User login for book train tickets.
=>System establishes secure communication.
=>System asks for arrival station.
=>User gives arrival station.
=>System asks for destination station.
=>User gives destination station.
=>System asks for sits.
=>User enters sits.
=>System asks for date.
=>User selects date.
=>System asks to choose train.
=>User chooses train.
=>System asks for mode of payment.
=>User chooses mode of payment.
=>System sends confirmation letter.
=>System sends good wishes to user.
MGITER/CE/OOPJ-2019 Page 17
B.E 3rd Year 5th Sem 150330107016
:USER :SYSTEM
Login
Secure
communication
Ask arrival
station
Give arrival
station
Ask destination
station
Give destination
station
Ask sits
Enter sits
Ask date
Select date
Ask for choose
train
Choose train
Ask for mode of
payment
Choose payment
mode
Send
confirmation
letter
Send good
wishes for
journey
MGITER/CE/OOPJ-2019 Page 18
B.E 3rd Year 5th Sem 150330107016
FOR UNSUCCESSFULL RESERVATION
:USER :SYSTEM
Login
Secure
communication
Ask arrival
station
Give arrival
station
Ask destination
station
Give destination
station
Ask for sits
Enter sits
Ask date
Give date
Apologized letter
for no train
available on this
date
Request for Re-
using
MGITER/CE/OOPJ-2019 Page 19
B.E 3rd Year 5th Sem 150330107016
Practical 20
Aim: Prepare an activity diagram for awarding marks to regular students. If the student
has attended 80% classes, he is awarded minimum 5 marks. If the student has
attended more than 80% classes, he is awarded minimum 10 marks. The students
who have completed assignments are given 10 marks. Those who have completed
50% are given 5 marks and other are given 0 marks.
Scan Student
Attendance
[more than 80% of class]
Minimum 10
marks
[80% of class]
Minimum 5
marks
Scan students
assignment
[completed all]
Give 10 marks
[completed 50%]
Give 5 marks
[not completed 50%]
Give 0 marks
MGITER/CE/OOPJ-2019 Page 20