OOPS USING JAVA LAB (2023-24)
OOPS USING JAVA LAB (2023-24)
OOPS USING JAVA LAB (2023-24)
LAB MANUAL
FOR
OOPS USING JAVA 2023-24
SUBJECT CODE: 22SCS033
PREPARED BY:
Mrs. SWATHI R , Assistant Professor
Name:_________________________________
SECTION/ Branch:______________________
Semester:_______________________________
USN:____________________________________
SRINIVAS UNIVERSITY
INSTITUTE OF ENGINEERING AND TECHNOLOGY
MUKKA, MANGALURU
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING
List of Experiments:
Sr.No Experiment Details
1 Write a Java program that display the roots of a quadratic equation ax2+bx=0.
Calculate thediscriminate D and basing on value of D, describe the nature of root.
5 Design a superclass 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.
6 Write a Java class called Customer to store their name and date_of_birth. The
date_of_birth format should be dd/mm/yyyy. Write methods to read customer data as
<name, dd/mm/yyyy> and display as <name, dd, mm, yyyy> using StringTokenizer
class considering the delimiter character as “/”.
7 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.
8 Write a java program that displays the number of characters, lines and words in a
text file.
9 Write a java program that reads a file and displays the file on the screen with line
number b.
10 Write a Java program that works as a simple calculator. Use a grid layout to arrange
buttons for the digits and for the +, -,*, % operations. Add a text field to display the
result. Handle any possible exceptions like divide by zero
COURSE OBJECTIVES:
Design and implement various algorithms in JAVA.
Employ various design strategies for problem solving constructs
COURSE OUTCOMES:
Apply the knowledge of mathematics, science engineering fundamentals, and an
engineering Specialization to the solution of complex engineering problems.
Understand the impact of the professional engineering solutions in societal and
environmental Contexts, and demonstrate the knowledge of, and need for sustainable
development
SCHEME OF EXAMINATION:
1. All laboratory experiments are to be included for practical examination.
2. Students are allowed to pick one experiment from the lot.
3. Strictly follow the instructions as printed on the cover page of answer script
4. Marks distribution: Procedure + Conduction + Viva:12 + 28 +10 = 50
5. Change of experiment is allowed only once and marks allotted to the procedure part to
be made zero
LABORATORY GUIDELINES
1. Equipment in the lab for the use of student community. Students need to maintain a
proper decorum in the computer lab. Students must use the equipment with care. Any
damage is caused is punishable.
3. Students are supposed to occupy the systems allotted to them and are not supposed to
talk or make noise in the lab.
4. Students are required to carry their observation book and lab records with completed
exercises while entering the lab.
Do’s
1. Come with completed observation and record
3. Write in time, out time and system details in the login register.
4. Remove your shoes or wear foot socks before you enter the lab.
7. Read and understand how to carry out an activity thoroughly before coming to the
laboratory.
9. Be on time
Don’ts
1. Do not eat or drink in the laboratory.
4. Do not disturb your neighboring students. They may be busy in completing tasks.
1. Write a Java program that display the roots of a quadratic equation ax2+bx=0.
Calculate the discriminate D and basing on value of D, describe the nature of root.
// value a, b, and c
double determinant = b * b - 4 * a * c;
if (determinant > 0) {
else if (determinant == 0) {
// determinant is equal to 0
// so -b + 0 == -b
else
Output:
import java.util.Scanner;
class Main {
char operator;
operator = input.next().charAt(0);
number1 = input.nextDouble();
number2 = input.nextDouble();
switch (operator) {
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
System.out.println("Invalid operator!");
break;
Output:
Choose an operator: +, -, *, or /
import java.util.Scanner;
int n=a.length,i,j,p,temp;
if(a[j+1]<a[j])
{temp=a[j+1];
a[j+1]=a[j];
a[j]=temp;}
System.out.print(a[i]+" ");
int n, res,i;
n = s.nextInt();
a[i] = s.nextInt();
printarray(a);
Sort(a);
printarray(a);
Output:
Enter 5 elements
elements in array
5 4 3 2 1
1 2 3 4 5
(i) USN
(ii) Name
(iii) Branch
(iv) Phone
Write a Java program to create n Student objects and print the USN, Name, Branch,
and Phone of these
objects with suitable headings.
String USN;
String Name;
String branch;
int phone;
USN=reg;
Name=name;
branch=brnch;
phone=ph;
void displayRecord()
int n=sc.nextInt();
for(int i=0;i<n;i++)
s[i]=new student();
for(int j=0;j<n;j++)
String USN=sc.next();
String Name=sc.next();
String branch=sc.next();
int phone=sc.nextInt();
s[j].insertRecord(USN,Name,branch,phone);
s[m].displayRecord();
monika
cse
93411
12
gowda
cse 9785
import java.lang.*;
class add
{
void display(int a,int b)
{
int c=a+b;
System.out.println("The sum of " + a + " & " + b + " is " + c);
}
void display(double a,double b)
{ double c=a+b;
System.out.println("The sum of " + a + " & " + b + " is " + c);
}
}
class add_demo
{
public static void main(String arg[])
{
add obj=new add();
obj.display(10,20);
obj.display(10.2,20.2);
}
}
Output:
The sum of 10 & 20 is 30
The sum of 10.2 & 20.2 is 30.4
double l=12.5,b=2.5;
double area()
return l*b;
double b=4.2,h=6.5;
double area()
return 0.5*b*h;
double s=6.5;
double area()
return 4*s;
class shapedemo
Output:
class Staff {
int staffid,
String phone,
int salary;
String name;
Staff(int id , int no, int sal, String na){
staffid=id;
phone=no;
salary=sal;
name=na;
}
void display(){
System.out.println("-------------------------------------");
System.out.println("Staff ID:"+ " "+ staffid);
System.out.println("Staff Phone number:" + " "+ phone);
System.out.println("Staff Salary:" +" "+ salary);
System.out.println("Staff Name:" +" "+ name);
}
}
class Teaching extends Staff {
String domain;
int no_of_publications;
public Teaching(int id, int no, int sal, String na,String d,int nop){
super(id,no,sal,na);
domain=d;
no_of_publications=nop;
}
void Tdisplay(){
System.out.println("-------------------------------------");
System.out.println("Teaching Staff Details");
super.display();
System.out.println("Domain :" +" "+domain);
System.out.println("No_of_publications:"+" "+no_of_publications);
}
}
class Technical extends Staff{
String skills;
public Technical(int id , int no, int sal, String na,String sk){
super(id,no,sal,na);
skills=sk;
}
void Tedisplay(){
System.out.println("-------------------------------------");
}
void Cdisplay(){
System.out.println("-------------------------------------");
System.out.println("Contract Staff Details");
super.display();
System.out.println("ContractPeriod:" + " "+period + "years");
}
}
public class Multilevel{
public static void main(String args[]){
Teaching t1=new Teaching(11,998765434,31000,"Anil","CSE",10);
Teaching t2=new Teaching(12,996655546,30000,"Anu","ISE",9);
Teaching t3=new Teaching(13,999933442,32000,"Anusha","EEE",8);
t1.Tdisplay();
t2.Tdisplay();
t3.Tdisplay();
Technical te1=new Technical(21,994433221,22000,"Kumar","C");
Technical te2=new Technical(22,998877665,28000,"Krisna","Java");
Technical te3=new Technical(23,991654321,33000,"Kiran","Java");
te1.Tedisplay();
te2.Tedisplay();
te3.Tedisplay();”
Contract ct1=new Contract(31,”998765434”,35000,"Anil",3);
Contract ct2=new Contract(32,912345678,39000,"Meghana",2);
Contract ct3=new Contract(33,992233445,30000,"Uma",4);
ct1.Cdisplay();
ct2.Cdisplay();
ct3.Cdisplay();
}
}
Output:
Output:
-------------------------------------
-------------------------------------
Technical Staff Details
-------------------------------------
Staff ID: 21
Staff Phone number: 994433221
Staff Salary: 22000
Staff Name: Kumar
Skills : C
-------------------------------------
Technical Staff Details
-------------------------------------
Staff ID: 22
Staff Phone number: 998877665
Staff Salary: 28000
Staff Name: Krisna
Skills : Java
-------------------------------------
Technical Staff Details
-------------------------------------
Staff ID: 23
Staff Phone number: 991654321
import java.util.Scanner;
import java.util.StringTokenizer;
class customer
{
String name;
String date;
public void read()
{
Scanner input =new Scanner(System.in);
name=input.next();
date=input.next();
}
public void display()
{
System.out.print(name+",");
String delims="/";
StringTokenizer st=new StringTokenizer(date,delims);
while(st.hasMoreElements()){
System.out.print(st.nextElement()+",");
}
System.out.println();
}
public static void main(String[] args)
{
customer[] cus=new customer[30];
Scanner sc =new Scanner(System.in);
for(int i=0;i<n;i++)
{
cus[i]=new customer();
cus[i].read();
}
for(int i=0;i<n;i++)
cus[i].display();
}
}
rama,12,2,2018,
laxman,11,4,2018,
import java.util.Random;
int x;
Square(int n)
x = n;
int sqr = x * x;
int x;
Cube(int n)
x = n;
int cub = x * x * x;
int r = random.nextInt(100);
s.start();
c.start();
try {
Thread.sleep(1000);
System.out.println(ex);
n.start();
Output:
Square of 82 = 6724
Cube of 82 = 551368
Square of 34 = 1156
Cube of 34 = 39304
Square of 17 = 289
Square of 84 = 7056
Cube of 84 = 592704
Cube of 34 = 39304
Square of 34 = 1156
Square of 51 = 2601
Cube of 51 = 132651
Square of 38 = 1444
Cube of 38 = 54872
Square of 94 = 8836
Cube of 94 = 830584
Square of 65 = 4225
Square of 75 = 5625
Cube of 75 = 421875
import java.io.*;
class FileDemo
{
public static void main(String args[])
{
try
int lines=0,chars=0,words=0;
int code=0;
{
code = fis.read();
chars++;
if(code==32)//
words++;
if(code==13)
{
lines++;
words++;
}
}
}
catch(FileNotFoundException e)
{
System.out.println("Cannot find the specified file...");
}
catch(IOException i)
}
}
Output:
No.of characters = 16
No.of words = 5
No.of lines = 3
import java.io.*;
import java.util.*;
class ReadFile{
int i;
boolean wrong=true;
while(wrong)
while((i=linereader.read())!=-1)
System.out.println("Line #:"+(linereader.getLineNumber()+1));
System.out.println((char)i+linereader.readLine());
wrong=false;
catch(FileNotFoundException e){
file_name = input.nextLine();
catch(IOException e){
Output