Professional Documents
Culture Documents
21CSL35-OOPS LAB - Lab Manual 21CSL35-OOPS LAB - Lab Manual
21CSL35-OOPS LAB - Lab Manual 21CSL35-OOPS LAB - Lab Manual
LAB MANUAL
Semester-III
Course Objectives
CLO 1. Demonstrate the use of Eclipse/Net beans IDE to create Java Applications.
CLO 2. Using java programming to develop programs for solving real-world problems.
CLO 3. Reinforce the understanding of basic object-oriented programming concepts.
Prerequisites
Students should be familiarized about java installation and setting the java environment.
Usage of IDEs like Eclipse/Net beans should be introduced.
CORRELATION
PROGRAM OUTCOMES (PO), PROGRAM SPECIFIC OUTCOMES (PSO)
LEVELS
Moderate/
PO3 Design/development of solutions PO9 Individual and teamwork 2
Medium
PSO1 Design and develop applications using different stacks of web and programming technologies.
PSO2 Design and develop secure, parallel, distributed, networked, and digital systems.
PSO3 Apply software engineering methods to design, develop, test and manage software systems.
PSO4 Design and develop intelligent applications for business and industry.
Syllabus
Part-A-
List of problems for which student should develop program and execute in the
Laboratory
1. Write a java program that prints all real solutions to the quadratic equation. ax2+bx+c=0. Read in a, b, c and
use the quadratic formula.
2. Create a Java class called Student with the following details as variables within it.
USN, Name, Branch, Phone.
Write a Java program to create n Student objects and print the USN, Name, Branch, and Phone
of these objects with suitable headings.
3. i) Write a program to check prime number
ii) Write a program for Arithmetic calculator using switch case menu
4. Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend this class by writing three
subclasses namely Teaching (domain, publications), Technical (skills), and Contract (period). Write a Java
program to read and display at least 3 staff objects of all three categories.
5. Write a java program demonstrating Method overloading and Constructor overloading.
6. Develop a java application to implement currency converter (Dollar to INR, EURO to INR, Yen to INR and
vice versa), distance converter (meter to KM, miles to KM and vice versa), time converter (hours to minutes,
seconds and vice versa) using packages.
7. Write a program to generate the resume. Create 2 Java classes Teacher (data: personal information,
qualification, experience, achievements) and Student (data: personal information, result, discipline) which
implements the java interface Resume with the method biodata().
8. Write a Java program that implements a multi-thread application that has three threads. First thread generates a
random integer for every 1 second; second thread computes the square of the number and prints; third thread
will print the value of cube of the number.
9. Write a program to perform string operations using ArrayList. Write functions for the following a. Append -
add at end b. Insert – add at particular index c. Search d. List all string starts with given letter.
10. Write a Java program to read two integers a and b. Compute a/b and print, when b is not zero. Raise an
exception when b is equal to zero.
11. 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.
A problem statement for each batch is to be generated in consultation with the co-examiner and student should
develop an algorithm, program and execute the program for the given problem with appropriate outputs.
Content Beyond Syllabus
Prog # Title
13
14
Program-1: Write a java program that prints all real solutions to the quadratic equation. ax2+bx+c=0. Read in a, b,
c and use the quadratic formula.
Aim: Introduce the java fundamentals, data types, operators in java.
Problem Statement: Design and develop a flowchart or an algorithm that accepts 3 coefficients of a quadratic
equation as input and compute all possible roots. Implement a C program for the developed flowchart and execute
to find the possible roots for the given set of coefficients and print messages accordingly.
Algorithm:
1. Start.
Flowchart:
Program Code:
import java.util.*;
public class Main {
public static void main(String[] args) {
double a,b,c,determinant;
double root1, root2;
System.out.println("Enter the coefficients a,b,c");
Scanner sc = new Scanner(System.in);
a=sc.nextInt();
if (a==0)
{
System.out.println("a can not be zero");
}
else
{
b=sc.nextInt();
c=sc.nextInt();
determinant = b * b - 4 * a * c;
// check if determinant is greater than 0
if (determinant > 0) {
// two real and distinct roots
root1 = (-b + Math.sqrt(determinant)) / (2 * a);
root2 = (-b - Math.sqrt(determinant)) / (2 * a);
System.out.format("root1 = %.2f and root2 = %.2f", root1, root2);
}
// check if determinant is equal to 0
else if (determinant == 0) {
// two real and equal roots
Department of ComputerScience and Engineering, CMRIT, Bangalore.
Program-2: Create a Java class called Student with the following details as variables within it.
USN, Name, Branch, Phone.
Write a Java program to create n Student objects and print the USN, Name, Branch, and Phone of these objects
with suitable headings.
Aim: Demonstrating creation of java classes, objects, constructors, declaration, and initialization of variables.
Problem Statement: Design and develop a flowchart or an algorithm that create n number of objects and input the
following parameters such as USN, Name, Branch, Phone. Create a constructor to initialize class variables with
default values and develop corresponding methods for reading and displaying the variables declared in the class.
Algorithm:
1. Start
2. Declare the class and variables (USN, Name, Branch, Phone).
3. Create no-argument constructor to initialize class variables with default values.
4. Read n value from the user and create n objects.
5. Read values for class variables using object.
6. Implement read() method to read the values from calling method.
7. Implement display() method to display the values of class members referenced by individual object.
8. Stop.
Flowchart:
Program Code:
import java.io.*;
class Student
{
String usn, name, branch;
long ph;
Student()
{
usn = name = branch = "no value";
ph = 0;
}
void read_data(String u, String n, String b, long p)
{
usn = u;
name = n;
branch = b;
ph =p;
}
void display()
{
System.out.println(usn + "\t" + name + "\t" + branch + "\t" + ph);
}
}
class Main
{
public static void main(String args[]) throws Exception
{
String u, n, b;
long p;
int no;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Department of ComputerScience and Engineering, CMRIT, Bangalore.
Flowchart: (a)
Flowchart: (b)
Program-4 Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend this class by
writing three subclasses namely Teaching (domain, publications), Technical (skills), and Contract (period). Write a
Java program to read and display at least 3 staff objects of all three categories.
Aim: Demonstration of how classes can be extended using inheritance and how methods will be invoked by
object.
Problem Statement: a) Create a Java program with a staff main class, which has properties like Staffid, Name,
Phone, and Salary. Three classes, including Teaching (domain, publications), Technical (skills), and Contract, are
extended from the primary class staff (period). Make an object for each class, call its methods, and show
information about the main class with any of the other three classes.
Algorithm:
1. Start
2. Create a class Staff with the variables StaffId, Name, Phone and Salary.
3. Extend the class Staff by class Teaching and add attributes (domain, publications).
4. Extend the class Staff by class Technical and add attributes (skills).
5. Extend the class Staff by class Contract and add attributes (period).
6. Create a new class and objects for the classes Teaching, Technical, and Contract.
7. Using the respective object, invoke the methods implemented in the parent and derived classes.
8. Finally, create a display() method to display user-supplied data.
9. Stop
Flowchart:
Program Code:
import java.util.Scanner;
class Staff {
private String staffId;
private String name;
private long phone;
private float salary;
public void accept() {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Staff Id: ");
staffId = scanner.next();
System.out.print("Enter Name: ");
name = scanner.next();
System.out.print("Enter Phone: ");
phone = scanner.nextLong();
System.out.print("Enter Salary: ");
salary = scanner.nextFloat();
}
public void display() {
System.out.println("Staff Id: " + staffId);
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Salary: " + salary);
}
}
class Teaching extends Staff {
private String domain;
private String[] publications;
Flowchart: (a)
Flowchart: (b)
Program-6: Develop a java application to implement currency converter (Dollar to INR, EURO to INR, Yen to
INR and vice versa), distance converter (meter to KM, miles to KM and vice versa), time converter (hours to
minutes, seconds and vice versa) using packages.
Aim: To develop a java application to implement currency converter, distance converter and time converter using
the concept of packages.
Problem Statement: Create a Java software that converts between currency, distance, and time. Make a package
containing three classes with method implementations, such as Currency, Distance, and Time. Design a primary
class, make objects for those classes, then call the appropriate methods to do conversions.
Algorithm:
1. Start
2. Create a Package currency conversion and place the class currency under the package.
3. Create the methods to perform currency conversion from dollar to rupee, rupee to dollar, euro to rupee,
rupee to euro, yen to rupee and rupee to yen.
4. Create the package distance conversion and create the class distance within the package.
5. Create the methods to convert from meter to km, km to meter, miles to km, km to miles.
6. Create the package time conversion and create the class timer. Create the methods to convert from hours to
minutes, hours to seconds, minutes to hours and seconds to hours.
7. Create a class and import the packages currency conversion, distance conversion and time conversion.
Create the objects for the class currency, distance and timer.
8. Get the choice from the user and invoke the methods to perform the corresponding conversion and display
the value.
9. Stop
Program Code:
//currency.java
package currencyconversion;
import java.util.*;
public class currency
{
double inr,usd;
double euro,yen;
Scanner in=new Scanner(System.in);
public void dollartorupee()
{
System.out.println("Enter dollars to convert into Rupees:");
usd=in.nextInt();
inr=usd*67;
System.out.println("Dollar ="+usd+"equal to INR="+inr);
}
public void rupeetodollar()
{
System.out.println("Enter Rupee to convert into Dollars:");
inr=in.nextInt();
usd=inr/67;
System.out.println("Rupee ="+inr+"equal to Dollars="+usd);
}
public void eurotorupee()
{
System.out.println("Enter euro to convert into Rupees:");
//converter.java
import currencyconversion.currency;
import distanceconversion.distance;
import timeconversion.timer;
import java.util.*;
import java.io.*;
class converter
{
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int choice,ch;
currency c=new currency();
distance d=new distance();
timer t=new timer();
do
{
System.out.println("1.dollar to rupee ");
System.out.println("2.rupee to dollar ");
System.out.println("3.Euro to rupee ");
System.out.println("4..rupee to Euro ");
System.out.println("5.Yen to rupee ");
System.out.println("6.Rupee to Yen ");
System.out.println("7.Meter to kilometer ");
System.out.println("8.kilometer to meter ");
System.out.println("9.Miles to kilometer ");
Department of ComputerScience and Engineering, CMRIT, Bangalore.
Program-7: Write a program to generate the resume. Create 2 Java classes Teacher (data: personal information,
qualification, experience, achievements) and Student (data: personal information, result, discipline) which
implements the java interface Resume with the method biodata().
Aim: Introduction to abstract classes, abstract methods, and interfaces in java.
Problem Statement: Create a Java application to display resume of student and faculty using the concepts of
classes and interfaces in java.
Algorithm:
1. Start
2. Create the Resume interface and include the abstract method (biodata()).
3. Create the Faculty class by including relevant class members’ personal information, qualifications,
experience, and achievement.
4. Create the Student class by including relevant class members’ personal information, result, and discipline..
5. Allow the Faculty class to implement the interface Resume, and this class to implement abstract method
biodata().
6. Allow the Student class to implement the interface Resume, and this class to implement abstract method
biodata().
7. Create a Main class in which you will create objects for classes such as Faculty and Student. And use class
methods to generate a resume.
8. Stop
Flowchart:
Program Code:
import java.util.Scanner;
interface Resume
{
public void biodata();
}
class Teacher implements Resume
{
String name,gen,dept,qual,achiv,mail,address,mob;
int exp,fac_id;
public void biodata()
{
Scanner s1=new Scanner(System.in);
System.out.print("Name:");
name=s1.next();
System.out.print("Gender:");
gen=s1.next();
System.out.print("Employee ID:");
fac_id=s1.nextInt();
System.out.print("Department:");
dept=s1.next();
System.out.print("Qualification:");
qual=s1.next();
System.out.print("Achievement:");
achiv=s1.next();
System.out.print("Experience:");
Program-8: Write a Java program that implements a multi-thread application that has three threads. First thread
generates a random integer for every 1 second; second thread computes the square of the number and prints; third
thread will print the value of cube of the number.
Aim: Demonstration creation of threads using Thread class and Runnable interface, multithreaded programming.
Problem Statement: Develop java application to generate random number and find out the square and cube of the
same random number using multithreaded programming.
Algorithm:
1. Start
2. Create a class called ‘Square’ and implement ‘Runnable’ interface. Use this class to receive a number from
a thread and to print square of that number in run method of it.
3. Create a class called ‘Cube’ and implement ‘Runnable’ interface. Use this class to receive a number from a
thread and to print cube of that number in run method of it.
4. Create a class called ‘Number’ that should extend ‘Thread’ class.
4.1 Inside the ‘run’ method of this class generate random number and pass this number to constructor
‘Square’.
4.2 Call ‘run’ method in ‘Square’ class using t1.start() method to find the square of random number.
4.3 Inside the ‘run’ method of this class generate random number and pass this number to constructor
‘Cube’.
4.4 Call ‘run’ method in ‘Cube’ class using t1.start() method to find the cube of random number.
5. Create main class and call the method of Number class to display the output.
6. Stop
Flowchart:
Program Code:
import java.util.*;
class Square implements Runnable
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}
class Cube implements Runnable
{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
Program-9: Write a program to perform string operations using ArrayList. Write functions for the following a.
Append - add at end b. Insert – add at particular index c. Search d. List all string starts with given letter.
Aim: Introduce java Collections.
Problem Statement: Develop a java program to create ArrayList and implement operations such as add element at
the end, insert at index, search, and list all strings start with given letter.
Algorithm :
1. Start
2. Create the class DemoArrayList and create the object for the class.
3. Display the options to the user for performing string handling.
4. Use the function add() to append the string at the end and to insert the string at the particular index.
5. The function sort() is used to sort the elements in the array list.
6. The function indexof() is used to search whether the element is in the array list or not.
7. The function startswith() is used to find whether the element starts with the specified character.
8. The function remove() is used to remove the element form the array list.
9. The function size() is used to determine the number of elements in the array list.
10. Stop
Flowchart:
Program Code:
import java.util.*;
import java.io.*;
public class DemoArrayList
{
public static void main(String args[])throws IOException
{
ArrayList<String> obj = new ArrayList<String>();
DataInputStream in=new DataInputStream(System.in);
int c,ch;
int i,j;
String str,str1;
do
{
System.out.println("STRING MANIPULATION");
System.out.println("******************************");
System.out.println(" 1. Append at end \t 2.Insert at particular index \t 3.Search \t");
System.out.println( "4.List string that starting with letter \t");
System.out.println("5.Size \t 6.Remove \t 7.Sort\t 8.Display\t" );
System.out.println("Enter the choice ");
c=Integer.parseInt(in.readLine());
switch(c)
{
case 1:
{
System.out.println("Enter the string ");
Department of ComputerScience and Engineering, CMRIT, Bangalore.
Program-10: Write a Java program to read two integers a and b. Compute a/b and print, when b is not zero. Raise
an exception when b is equal to zero.
Aim: Exception handling in java, introduction to throwable class, throw, throws, finally.
Problem Statement: Develop a simple java program to divide one number by another number and generate
exception if the second number is zero.
Algorithm:
1. Start
2. Create a class called ‘DemoException’.
3. Read two integers (a, b) from the user.
4. Use Exception handling mechanism to handle exception.
5. If b is equal to zero , then throw an exception using the keyword ‘throw’.
6. Otherwise return the result of a/b.
7. Stop.
Flowchart:
Program Code:
import java.util.Scanner;
public class DemoException {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int a,b,c;
System.out.print("Enter the Two Interger Values\nA :");
a=scan.nextInt();
System.out.print("B :");
b=scan.nextInt();
scan.close();
try {
if(b==0)
throw new ArithmeticException("Divide By Zero");
c=a/b;
System.out.println("\nThe Value of "+a+" / "+b+" is "+c);
}catch(ArithmeticException e)
{
e.printStackTrace();
}
}
}
Sample Output:
E:\Java Programs>java DemoException
Enter the Two Interger Values
A :4
B :4
Program-11: 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.
Flowchart:
Program Code:
import java.io.*;
import javax.swing.*;
class FileDemo
{
public static void main(String args[])
{
String filename = JOptionPane.showInputDialog("Enter filename: ");
File f = new File(filename);
System.out.println("File exists: "+f.exists());
System.out.println("File is readable: "+f.canRead());
System.out.println("File is writable: "+f.canWrite());
System.out.println("Is a directory: "+f.isDirectory());
System.out.println("length of the file: "+f.length()+" bytes");
try
{
char ch;
StringBuffer buff = new StringBuffer("");
FileInputStream fis = new FileInputStream(filename);
while(fis.available()!=0)
{
ch = (char)fis.read();
buff.append(ch);
}
System.out.println("\nContents of the file are: ");
System.out.println(buff);
Department of ComputerScience and Engineering, CMRIT, Bangalore.
Program-12: Develop an applet that displays a simple message in center of the screen. Develop a simple
calculator using Swings.
Flowchart: (Calculator)