Oops Lab Manual - II Yr
Oops Lab Manual - II Yr
OBJECTIVES:
LIST OF EXPERIMENTS:
1. Solve problems by using sequential search, binary search, and Quadratic sorting
algorithms(Selection, Insertion)
2. Develop Stack and Queue data structures using classes and objects.
3. Develop a java application with Employee class with Emp_name, Emp_id, Address,
Mail_id, Mobile_no as members. Inherit the classes, Programmer, Assistant Professor,
Associate Professor and Professor from employee class. Add Basic Pay (BP) as the member
of all the inherited classes with 97% of BP as DA, 10 % of BP as HRA, 12% of BP as PF,
0.1% of BP for staff club fund. Generate pay slips for the employees with their gross and
net salary.
4. Write a Java Program to create an abstract class named Shape that contains two integers
and an empty method named print Area(). Provide three classes named Rectangle, Triangle
and Circle such that each one of the classes extends the class Shape. Each one of the classes
contains only the method print Area () that prints the area of the givenshape.
5. Solve the above problem using an Interface.
7. Write a java program that implements a multi-threaded application that has three threads. First
thread generates a random integer every 1 second and if the value is even, second thread computes
the square of the number and prints. If the value is odd, the third thread will print the value of cube
of thenumber
8. Write a Java program to perform file operations.
9. Develop applications to demonstrate the features of generics classes.
TOTAL: 60 PERIODS
OUTCOMES:
Upon completion of the course, the students will be able to
• Develop and implement Java programs for simple applications that make use of
classes, packages andinterfaces.
• Develop and implement Java programs with arraylist, exception handling and
multithreading.
• Design applications using file processing, generic programming and eventhandling.
Ex.
No. Name of the Exercise Page No.
8 File Handling 37 – 39
9 Multithreading Implementation 40 – 41
12 Mini Project 57
Algorithm
Linear search
• Start
• If the size of the array is zero then, return -1, representing that the element is not found. This can
also be treated as the base condition of a recursion call.
LABORATORY ACTIVITY MANUAL
• Otherwise, check if the element at the current index in the array is equal to the key or not i.e,
arr[size – 1] == key
o If equal, then return the index of the found key.
• End
• Start
• Begin with the mid element of the whole array as a search key.
• If the value of the search key is equal to the item then return an index of the search key.
• Or if the value of the search key is less than the item in the middle of the interval, narrow the
interval to the lower half.
• Otherwise, narrow it to the upper half.
• Repeatedly check from the second point until the value is found or the interval is empty.
• End
Selection sort:
• Start
• Start
Push Operation
• Start
Pop operation
• Start
Enqueue Operation
• Start
Dequeue Operation
• Start
Algorithm
1)Start
2)Take the input from the user as employee name,id and basic salary
3)Calculate the the above parameters DA,HRA,GS,income tax and net salary
4)Display the output
LABORATORY ACTIVITY MANUAL
EX. NO:1
DATE:
AIM:
ALGORITHM:
Program :
OUTPUT:
RESULT:
VIVA QUESTIONS:
1. Explain how to create instance of a class by giving example
2. What are Access Specifiers
3. Can a top level class be private or protected?
4. Which access Modifiers are used in this program?
EX NO :2
DATE:
AIM:
To develop a java application to implement
ALGORITHM:
1. Start
2. Stop
Program :
RESULT
8
AIM:
To develop a java application to generate pay slip for different category of employees using the concept
of inheritance
.ALGORITM:
1. Start
2. Create the class Employee with name, Empid, address, mailid, mobileno as datamembers.
3. Inherit the classes Programmer, Asstprofessor, Associateprofessor and Professor from
employee class.
4. Add Basic Pay (BP) as the member of all the inheritedclasses.
5. Calculate DA as 97% of BP, HRA as 10% of BP, PF as 12% of BP, Staff club fund as 0.1% ofBP.
6. Calculate gross salary and netsalary.
7. Generate payslip for all categories ofemployees.
8. Create the objects for the inherited classes and invoke the necessary methods to display thePayslip
9. Stop
Program :
import java.util.*;
class Employee
{
int empid;
long mobile;
9
OUTPUT:
RESULT
Thus the Java application to generate pay slip for different category of employees was
implemented using inheritance and the program was executed successfully.
VIVA QUESTIONS:
1.What is Inheritance?
2.What are the types of Inheritance?
3.How to implement multiple inheritance in java
4.how to access two parent class in a child class?
handling.ALGORITHM
1. Start
2. Create the interface Stackoperation with method declarations for push andpop.
3. Create the class Astack which implements the interface and provides implementation for the
methods push and pop. Also define the method for displaying the values stored in the stack.
Handle the stack overflow and stack underflowcondition.
4. Create the class teststack. Get the choice from the user for the operation to be performed
and also handle the exception that occur while performing the stackoperation.
5. Create the object and invoke the method for push, pop, display based on the input from theuser.
6. Stop.
Program :
import java.io.*;
interface Stackoperation
{
public void push(int i);
public void pop();
}
class Astack implements Stackoperation
{
int stack[]=new int[5];
int top=-1;
int i;
public void push(int item)
{
if(top>=4)
{
System.out.println("Overflow");
}
else
{
top=top+1; stack[top]=item;
System.out.print("Element pushed: "+stack[top]);
}
}
public void pop()
{
if(top<0)
System.out.println("Underflow");
else
{
System.out.print("Element popped: "+stack[top]);
top=top-1;
15
OUTPUT:
17
RESULT:
Thus the java application for ADT stack operation has been implemented and executed successfully
18
AIM
To write a Java program to calculate the area of rectangle, circle and triangle using the
concept of abstract class.
ALGORITHM:
1. Start
2. Createanabstractclassnamedshapethatcontainstwointegersandanemptymethodnamed
printarea().
3. Provide three classes named rectangle, triangle and circle such that each one of the classes
extends the classShape.
4. Eachoftheinheritedclassfromshapeclassshouldprovidetheimplementationforthemethod
printarea().
5. Get the input and calculate the area of rectangle, circle andtriangle.
6. In the shapeclass, create the objects for the three inherited classes and invoke the methods
and display the area values of the differentshapes.
7. Stop.
Program :
import java.util.*;
abstract class shape
{
int a,b;
abstract public void printarea();
}
class rectangle extends shape
{
public int area_rect; public void printarea()
{
Scanner s=new Scanner(System.in);
System.out.println("Enter the length and breadth of rectangle"); a=s.nextInt();
b=s.nextInt(); area_rect=a*b;
System.out.println("Length of rectangle: "+a +"breadth of rectangle: "+b); System.out.println("The area
of rectangle is:"+area_rect);
}
}
class triangle extends shape
{
double area_tri; public void printarea()
{
Scanner s=new Scanner(System.in); System.out.println("Enter the base and height of triangle:");
a=s.nextInt();
19
OUTPUT:
20
RESULT
Thus the Java program for calculate the area of rectangle, circle and triangle was implemented and
executed successfully.
VIVA QUESTIONS
1.What is difference between abstract class and interface
2.Can a abstract class be defined without any abstract method?
3.Abstract is a specifier or modifier
21
AIM
To write a Java program to implement user defined exception handling.
ALGORITHM:
1. Start
2. Create a class NegativeAmtException which extends Exceptionclass.
3. Create a constructor which receives the string asargument.
4. Get the Amount as input from theuser.
5. If the amount is negative, the exception will begenerated.
6. Using the exception handling mechanism , the thrown exception is handled by
the catch construct.
7. After the exception is handled , the string “invalid amount “ will bedisplayed.
8. If the amount is greater than 0, the message “Amount Deposited “ will bedisplayed
9. Stop.
Program :
import java.util.*;
class NegativeAmtException extends Exception
{
String msg; NegativeAmtException(String msg)
{
this.msg=msg;
}
public String toString()
{
return msg;
}}
public class userdefined
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in); System.out.print("Enter Amount:"); int a=s.nextInt();
try
{
if(a<0)
{
throw new NegativeAmtException("Invalid Amount");
}
System.out.println("Amount Deposited");
}
catch(NegativeAmtException e)
{
22
Output
Program 2:
class MyException extends Exception
{
String str1; MyException(String str2)
{
str1=str2;
}
public String toString()
{
return ("MyException Occurred: "+str1) ;
}
}
class example
{
public static void main(String args[])
{
try
{
System.out.println("Starting of try block");
throw new MyException("This is My error Message");
}
catch(MyException exp)
{
System.out.println("Catch Block") ; System.out.println(exp) ;
}}}
23
RESULT
Thus the Java program to implement user defined exception handling has been implemented and executed
successfully
VIVA QUESTION
1.Define Exception and Error
2.Difference between throw and throws
3.mention some Build in exception
4.how to declare User defined Exception
EX NO:8 FILEHANDLING
DATE :
AIM
To write a java program that reads a file name from the user, displays information about
whether the file exists, whether the file is readable, or writable, the type of file and the
length of the file in bytes.
ALGORITHM:
1. Start
2. Create a class filedemo. Get the file name from theuser.
3. Use the file functions and display the information about thefile.
4. getName() displays the name of thefile.
5. getPath() diplays the path name of thefile.
24
Result:
Thus the java program to display file information has been implemented and executed successfully.
VIVA QUESTIONS
1.What is i/o stream
2.Types of file I/O stream
26
EX NO : 7 MULTITHREADING IMPLEMENTATION
DATE:
AIM
To write a java program that implements a multi-threaded application.
ALGORITHM:
1. Start
2. Create a class even which implements first thread that computes the square of the number.
3. run() method implements the code to be executed when thread getsexecuted.
4. Create a class odd which implements second thread that computes the cube of thenumber.
5. Create a third thread that generates random number. If the random number is even, it
displays the square of the number. If the random number generated is odd, it displays
the cube of the givennumber.
6. The Multithreading is performed and the task switched between multiplethreads.
7. The sleep () method makes the thread to suspend for the specifiedtime.
8. Stop.
Program :
import java.util.*;
class even implements Runnable
{
public int x; public even(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is EVEN and Square of " + x + " is: " + x * x);
}
}
class odd implements Runnable
{
public int x; public odd(int x)
{
this.x = x;
}
public void run()
{
System.out.println("New Thread "+ x +" is ODD and Cube of " + x + " is: " + x * x * x);
}
}
class A extends Thread
27
Output:
28
RESULT:
Thus the java program for multithreaded application has been implemented and executed successfully.
VIVA QUESTION
1.What is multithreading and what is the class in which these methods are defined?
29
VIVA QUESTION
1.How to write parameterized class in java using Generics
2.Can you pass list<string> to a method with accepts List<Object>
3.Can we use generics with Array?
31
addSub.addActionListener(this);
dot = new JButton(".");
buttonpanel.add(dot);
dot.addActionListener(this);
eq = new JButton("=");
buttonpanel.add(eq);
eq.addActionListener(this);
rec = new JButton("1/x");
buttonpanel.add(rec);
rec.addActionListener(this);
sqrt = new JButton("Sqrt");
buttonpanel.add(sqrt);
sqrt.addActionListener(this);
log = new JButton("log");
buttonpanel.add(log);
log.addActionListener(this);
sin = new JButton("SIN");
buttonpanel.add(sin);
sin.addActionListener(this);
cos = new JButton("COS");
buttonpanel.add(cos);
cos.addActionListener(this);
tan = new JButton("TAN");
34
OUTPUT:
45
RESULT
Thus the Java programs for scientific calculator has been implemented and executed successfully
VIVA QUESTION
1.Which container use a Border Layout as their default Layout?
2.Which Listener are implemented for JButton?
3.what is awt and Swing?
4.What is Grid Layout