lasses — To find the area and perimeter of a rectangle
Aim:
To find the area and perimeter of a rectangle using classes,
Algorithm:
Class Name = areaperi
To store the dimensions and to calculate area and perimeter of a rectangle
Method Name : void main(String args{])
Step I: Get length and breadth as input
Step 2: Calculate the area using the formula area=length*breadth
Step 3: Calculate the perimeter using the formula perimeter~2*(Length breadth)
Step 4: Print the values of area and perimeter
Result:
The given program was implemented and output obtained was found to be correct.
2. Single dimensional Arrays - To calculate the total marks and
average
Aim:
To store the marks obtained by a student in various subjects using single dimensional arrays
and to calculate the total marks and average
Algorithm:
Class Name_:_arrays
To store the individual subject marks of a student and to calculate the total marks and
average.
Method Name _: void main(String ares{l)
Step 1: Get number of subjects, n as input
Step 2: Using a loop get the individual subject marks and store it in the array marks[]
Step 3: Using a loop calculate the total marks obtained by using the formula
total-total+marks[i] where i ranges from 1 ton
Step 4: Calculate the average using the formula average-total/n
Step 5: Print the values of total marks and average
Result:
The given program was implemented and output obtained was found to be correct.
3. Classes and Objects - Area of a rectangle
To find the area of a rectangle using classes and objects.
Algorithm:
Class Name : rectangle
To store the dimensions and to calculate the area of a rectangle.
Method Name : void getdataQ,
Step 1: Get length and breadth as input
Method Name : void calearea()
Step 1: Calculate the area using the formula area-length*breadth,
Method Name : void display)
Step 1: Print the value of area
Class Name _:_rectmain
To create objects of type ‘rectangle’ and to call appropriate methods.
Method Name_: void main(String args}
Step I: Create objects rl and r2 of type ‘rectangle’
Step 2: Call the method getdata() using object 1 and 12
Aim:Step 3: Call the method calcarea() using object rl and 12
Step 4: Call the method display() using object r1 and 12
Result:
‘The given program was implemented and output obtained was found to be correct.
4. Overloaded Constructors - Area of a rectangle
To initialize objects using overloaded constructors and to find the area ofa rectangle
Algorithm:
Class Name : rectangle
To get the dimensions of a rectangle and to find the area.
Method Name : rectangle()
Step 1: Initialize length and breadth to 0
Method Name : rectangle(int v)
Step 1: Tnitialize length and breadth tov
Method Name ectangle(int 1, int b)
Step I: Tnitialize length to Land breadth to b
Method Name _: void getdata
Step 1: Get length and breadth as input
Method Name : void calearea(
Step 1: To find the area of the rectangle using the formula arealength*breadth
Method Name : void display
Step I: To print the values of length, breadth and area
Class Name _:_rectconst
To create multiple objects of type ‘rectangle’ and to call appropriate methods.
Method Name : void main(String args{})
Step 1 Create an object r1 of type “rectangle” using zero argument constructor
Step 2: Call the method calearea() using the object r1
Step 3: Call the method display() using the object rl
Step 4: Create an object 12 of type “rectangle” using one argument constructor
Step 5: Call the method calcarea() using the object r2
Step 6: Call the method display() using the object 12
Step 7: Create an object 13 of type ‘rectangle’ using two arguments constructor
Step &: Call the method calcarea() using the object 13
Step 9: Call the method display() using the object 13
Step 10: Create an object r4 of type “rectangle” using zero argument constructor
Step 11: Call the method getdata() using the object r4
Step 12 Call the method calearea() using the object r4
Step 13: Call the method display() using the object r4
Result:
The given program was implemented and output obtained was found to be correct.
To create a subclass boxweight from a super class box to reuse super class’s data and code
and to add additional data and code as necessary using the concept of single inheritance.
Algorithm:
Class Name _:_ box
To store the dimensions of a box and to implement appropriate methods.
Method Name : box)
Step 1: Tnitialize the variables length, breadth and height to 0
‘Method Name : box(int val)
Step 1: Tnitialize the variables length, breadth and height to valMethod Name : box(int |, int b, int h)
Step I: Tnitialize the variable length to 1, breadth to b and height to h
Method Name : void getdataQ)
Step I: Get the values of length, breadth and height as input
Method Name : void display)
Step I: Print the values of length, breadth and height
Class Name : boxweight extends box
This class extends the super class “box" to reuse its data and code and also adds its own
specific data and code.
Method Name _: boxweight()
Step 1: Call the super class constructor using super()
Step 2: Initialize the variable weight to 0
Method Name _: boxweight(int val)
Step 1: Call the super class constructor using super(val)
Step 2: Initialize the variable weight to val
Method Name_: boxweight(int |, int b, int h, int w)
Step 1: Call the super class constructor using super(l,b.h)
Step 2: Initialize the variable weight to w
Method Name : void getweightQ.
Step 1: Get the value of weight as input
Method Name_: void displayweight
Step I: Print the value of weight
Class Name_:_boxmain
To create objects of type “boxweight’ and to call appropriate methods of both super class and
subclass.
Method Name : void main(String args{])
Step I: Create an object bl of type ‘boxweight” using zero argument constructor
Step 2: Call the methods display() and displayweight() using the object b1
Step 3: Create an object b2 of type ‘boxweight” using one argument constructor
Step 4: Call the methods display() and displayweight() using the object b2
Step 5: Create an object b3 of type ‘boxweight” using four argument constructor
Step 6: Call the methods display() and displayweight() using the object b3
Step 7: Call the method getdata()using the object bl
Step 8: Call the method getweight(using the object bl
Step 9: Call the method display()using the object bl
Step 10: Call the method displayweight(using the object b1
Result:
The given program was implemented and output obtained was found to be correct.
6. Multilevel Inheritance - Student - Marks — Result
Aim:
To create a sub class result which inherits from marks which in turn inherits from student to
reuse data and code of super classes using the concept of multilevel inheritance.
Algorithm:
Class Name : studdet
To store the details of students such as regno, sname and age.
Method Name : studdet()
Step 1: Thitialize regno to 0
Step 2: Initialize sname to empty string
Step 3: Initialize age to 0
Method Name : void getdata()
Step | Get regno, sname and age as input
Method Name : void displayQ
Step 1 Print the values of regno, name and ageClass Name _: studmar extends studdet
To store the marks secured by the student in various subjects,
Method Name ; studmar()
Step I: Call the super class constructor using super()
Step 2: Initialize subl to 0
Step 3: Initialize sub2 to 0
Step 4: Initialize sub3 to 0
Method Name : void getdataQ,
Step 1: Get subject marks subl, sub2 and sub3 as input
Method Name : void displa
Step 1: Print the values of sub1, sub2 and sub3
Class Name _: studres extends studmar
To calculate and store the total marks, average and result of the student.
Method Name : studres()
Step 1: Call the super class constructor using super()
Step 2: Initialize total to 0
Step 3: Initialize avg to 0
Step 4: Initialize result to “Not calculated”
Method Name : void caleresultQ.
Step I: Calculate the total using the formula total=sub |+sub2+sub3
Step 2: Calculate the avg using the formula avg=total/3
Step 3: If the student has passed in all the 3 subjects and his avg>=75 then result” First class
with distinction”
Step 4: If the student has passed in all the 3 subjects and his a then result=" First,
class”
Step 5: Ifthe student has passed in all the 3 subjects and his avg>=50 then result=” Second
class”
Step 6: Ifthe student has passed in all the 3 subjects and his a then result=" Third
class”
Step 7: Ifthe student has failed in any of the 3 subjects then result="Fail””
Method Name : void display
Step 1: Print the values of total, avg and result
Class Name = studmain
To create objects of type “studres’ and to call appropriate methods of super classes and sub
class.
Method Name _: void main(String args{])
Step I: Create an object s of type ‘studres’
Step 2: Call the method getdata() using the object s
Step 3: Call the method calcresult() using the object s
Step 4: Call the method display() using the object s
Resul
‘The given program was implemented and output obtained was found to be correct.
7. Hierarchical Inheritance - Employee - Manager - Clerk — Labour
To create a super class employee dealing with employee details in general and to create
various sub classes such as manager, clerk and labour which deals with specific details of the
category of employee to reuse data and code of the super class using the concept of hierarchical
inheritance.
Algorithm:
Class Name _: employee
To store the details of employees such as eno, ename and age.
Method Name : employee()Step 1: Initialize eno to 0
Step 2: Initialize ename to empty string
Step 3: Initialize age to 0
Method Name : void getdataQ)
Step I: Get eno, ename and age as input
Method Name : void display
Step 1: Print the values of eno, ename and age
Class Name _: manager extends employee
To store the details such as qualification, basic pay, dearness allowance and house rent
allowance and to implement appropriate functionality.
Method Name : manager()
Step I: Call the super class constructor using super)
Step 2: Initialize qual to empty string
Step 3: Initialize bpay to 0
Step 4: Initialize da to 0
Step 5: Initialize hra to 0
Method Name : void getdata(
Step 1: Get qual, bpay, da and hra as input
Ly
Step I: Print the values of qual, bpay, da and ra
Class Name _: clerk extends employee
To store the details such as qualification, basic pay and dearness allowance and to implement
appropriate functionality.
Method Name = clerkQ
Step 1: Call the super class constructor using super)
Step 2: Initialize qual to empty string
Step 3: Initialize bpay to 0
Step 4: Initialize da to 0
Method Name : void getdataQ,
Step 1: Get qual, bpay and da as input
Method Name : void display
Step 1: Print the values of qual, bpay and da
Class Name : labour extends employee
To store the dailywages details and to implement appropriate functionality
Method Name_: labour()
Step I: Call the super class constructor using super)
Step 2: Initialize dailywages to 0
Method Name_: void getdata
Step 1: Get dailywages as input
Method Name : void displavQ
Step 1: Print the value of dailywages
Class Name : empmain
To create objects of type manager, clerk and labour and to call appropriate methods of super
class and sub classes.
Method Name _: void main(String args{]).
Step 1: Create an object m of type ‘manager’
Step 2: Create an object c of type “clerk”
Step 3: Create an object | of type ‘labour’
Step 4: Call the method getdata() using the object m
Step 5: Call the method display() using the object m
Step 6: Call the method getdata() using the object ¢
Step 7: Call the method display() using the object ¢
Step 8: Call the method getdata() using the object |
Step 9: Call the method display() using the object |Result:
The given program was implemented and output obtained was found to be correct.
Packages - Area of a Rectangle
rectangle class in package p1:
Aim:
To find the area ofa rectangle using class in a package
Algorithm:
Package : pt
Class Name : rectangle
To store the dimensions and to calculate the area of a rectangle.
Method Name : rectangle()
Step I: Tnilialize the values of length and breadth to 0
Method Name _: void getdata
Step I: Get length and breadth as input
Method Name_: void calcarea(
Step I: Calculate the area using the formula arealength*breadth
Method Name : void displavQ
Step 1: Print the values of length, breadth and area
Result:
‘The given program was implemented and output obtained was found to be correct.
rectpackmain class using rectangle class in package p1:
Ai
To find the area of a rectangle using a class within another package.
Algorithm:
Import : Package p1
Class Name _:_rectpackmain
To create objects of type ‘rectangle’ and to call appropriate methods.
Method Name : void main(String args{]).
Step 1: Create an object rl of type ‘rectangle’ available within the package ‘p1”
Step 2: Call the method getdata() using object r1
Step 3: Call the method calcarea() using object rl
Step 4: Call the method display() using object rl
Result:
The given program was implemented and output obtained was found to be correct.
9. Interfaces - Area of a circle
Aim:
To find the area of a citcle using interfaces and classes.
Algorithm:
Interface Name_: shape
To define the interface for various methods pertaining to objects of any shape.
Method Name : void getdimensions()
This method would be used to get the dimensions of the object as input.
Method Name : void calcarea()
This method would be used to calculate the area of the object using appropriate formula.
Method Name : void display
This method would be used to display the dimensions and area details of the object.
Class Name : circle implements shapeTo store the dimensions and to calculate the area of a circle by implementing the methods
listed in the interface ‘shape’.
Method Name : void getdimensions()
Step I: Get radius as input
Method Name : void calcarea()
Step I: Calculate the area using the formula area=3. 1429*(radius*radius)
Method Name : void display
Step 1 Print the values of radius and area
Class Name : circlemain
To create objects of type ‘cirele” and to call appropriate methods.
Method Name _: void main(String args{])
Step I: Create an objects c1 of type ‘circle’
Step 2: Call the method getdimensions() using the object cl
Step 3: Call the method calearea() using the object cl
Step 4: Call the method display() using the object cl
Result:
The given program was implemented and output obtained was found to be correct.
10. Exception Handling - Random number generation
Aim:
To generate 10 random numbers and to handle any exceptions that might occur.
Algorithm:
Class Name _: randomno
To generate 10 random numbers and to handle any exceptions that might occur.
Method Name_: void main(String args}
Step I: Tnitialize val, num and den to 0
Step 2: Create an object r of type ‘Random’
Step 3: Using a loop repeat steps fiom 4 to 9, 10 times, at end go to step 10
Step 4: Generate next random number using nextInt() method and store it in mum
Step 5: Generate next random number using nextint() method and store it in den
Step 6: Calculate the value of val using the formula val=12345/(num/den)
Step 7: If any exception occurs, set the value of val to 0 and print appropriate message
Step 8: Print the value of val
Step 9: Go to step 3
Step 10: End
Result:
The given program was implemented and output obtained was found to be correct.
11. Multithreading nerating odd and even numbers
To generate odd and even numbers using the concept of multithreading.
Algorithm:
Class Name : odd extends Thread
To generate odd numbers using a separate thread of execution.
Method Name : void runQ,
Step 1: Using a loop i-I ton step 2 perform steps 2 and 3, at end go to step 4
Step 2: Print the value of i
Step 3: Go to step 1
Step 4: End
Class Name _: even extends ThreadTo generate even mumbers using a separate thread of execution.
Method Name _: void run()
Step I: Using a loop i=2 to n step 2 perform steps 2 and 3, at end go to step 4
Step 2: Print the value of i
Step 3: Go to step 1
Step 4: End
Class Name : evenodd
To call appropriate methods to generate odd and even numbers.
Method Name : void main(String args{])
Step 1: Create an object o of type ‘odd’
Step 2: Create an object e of type ‘even’
Step 3: Call the method start() using the object ‘o"
Step 4: Call the method start() using the object “e”
Result:
‘The given program was implemented and output obtained was found to be correct.
12. Applet: 1 Manipulation
To create an applet in Java to manipulate foreground and background colors and to print
messages in the applet window.
Algorithm:
Class Name : appletcolor extends Applet
To manipulate the foreground and background colors of an applet window and to print
messages in the applet window.
Method Name : void init()
Step 1 Set the background color to ‘cyan’ using the method setBackground(Color.cyan)
Step 2: Set the foreground color to ‘red’ using the method setForeground (Color.red)
Step 3: Store “Inside init( ) —" in the variable msg
Method Name : void startQ)
Step 1: Store msg + “Inside start( ) ~" in the variable msg
Method Name : void paint(Graphics g)
Step 1: Store msg + “Inside paini( )--" in the variable msg
Step 2: Print the value of msg using the method drawString(msg, 10,30)
Result:
The given applet was implemented and output obtained was found to be correct.
13. Applets - Displaying in Status Bar
Aim:
To create an applet in Java to display messages in the status bar of the applet window,
Algorithm:
Class Name : appletstatus extends Applet
To display messages in the status bar of the applet window.
Method Name : void init()
Step I: Set the background color to ‘cyan’ using the method setBackground(Color.cyan)
Method Name : void paint(Graphics g)
Step 1: Print the message “This is in the applet window” in the applet window using the
method drawString("This is in the applet window". 10,30)
Step 2: Print the message “This is in the applet window” in the status bar using the
method showStatus("This is shown in the status windows")
Result:The given applet was implemented and output obtained was found to be correct.
14. Applets - Using Label and TextField
To add two numbers by getting input from the user dynamically in an applet in Java.
Algorithm:
Class Name _: appletuserinput extends Applet
To get input from the user dynamically in an applet window and to add the two numbers.
Aim:
Method Name = v
Step 1: Create an object labell of type “Label” using the constructor
Label("Enter number 1 : ")
Step 2: Create an object text! of type “TextField” using the constructor
TextField(10)
Step 3: Create an object label2 of type “Label” using the constructor
Label("Enter number 2 : ")
Step 4: Create an object text2 of type “TextField” using the constructor
TextField(10)
Step 5: Add the controls label, label2, text, text2 to the applet window using add() method
Step 6: Set the value of text] to 0 using setText("0")
Step 7: Set the value of text2 to 0 using setText("0")
Method Name : void paint(Graphies
Step 1: Initialize x, y and z to 0
Step 2: Get the input given by the user in text using getText() and store it in x
Get the input given by the user in text2 using getText() and store it in y
Calculate the value of z using the formula z=x'y
Convert the numeric value in z to type string and store it in s
Print the output using the method
drawString("The sum of the given two numbers is :" + s,10,75)
Method Name : boolean action(Event event, Object obj)
Step i: Call the method repaint()
Step 2: Return true
Result:
‘The given applet was implemented and output obtained was found to be correct.