0% found this document useful (0 votes)
15 views133 pages

Divina Oops Record

The document is a laboratory record for Divina Godvia Violet R, a student at Francis Xavier Engineering College, detailing her work in the Object Oriented Programming Systems Laboratory during the 2021-2022 academic year. It includes a bonafide certificate, an index of experiments, and specific Java programming projects such as electricity bill generation, student performance analysis, and various converters. Each experiment outlines objectives, procedures, and sample code, demonstrating the student's practical application of programming concepts.

Uploaded by

19tuec115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views133 pages

Divina Oops Record

The document is a laboratory record for Divina Godvia Violet R, a student at Francis Xavier Engineering College, detailing her work in the Object Oriented Programming Systems Laboratory during the 2021-2022 academic year. It includes a bonafide certificate, an index of experiments, and specific Java programming projects such as electricity bill generation, student performance analysis, and various converters. Each experiment outlines objectives, procedures, and sample code, demonstrating the student's practical application of programming concepts.

Uploaded by

19tuec115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 133

FRANCIS XAVIER ENGINEERING COLLEGE

(AUTONOMOUS), TIRUNELVELI

DEPARTMENT OF COMPUTER SCIENCE AND


ENGINEERING

LABORATORY RECORD

2021 – 2022 / ODD SEMESTER

REGISTER NUMBER : 2012022

NAME OF THE STUDENT : DIVINA GODVIA VIOLET R

BRANCH : COMPUTER SCIENCE AND

ENGINEERING

BATCH : 2020 – 2024 BATCH

YEAR / SEMESTER : II YEAR / III SEMESTER

1
FRANCIS XAVIER ENGINEERING COLLEGE
(AUTONOMOUS) , TIRUNELVELI-627003

ROLL NO: 20CSR022

BONAFIDE CERTIFICATE

Certified that this is a bonafide record of work done by Selvan / Selvi


DIVINA GODVIA VIOLET R with Register No. 2012022 of III semester in B.E-
Computer Science and Engineering branch of this institution in the 19CS3612
OBJECT ORIENTED PROGRAMMING SYSTEMS LABORATORY during 2021-
2022.

Staff-in charge H.O.D

Submitted for the University Practical Examination held


on………………………..

Internal Examiner External Examiner

2
INDEX
PAGE
EX.NO DATE NAME OF THE EXPERIMENT MARKS SIGN
NO
1(a) 12/08/2021 4
Electricity Bill Generation

1(b) 12/08/2021 11
Students Performance Analysis

2(a) 19/08/2021 15
Currency, Distance and Time Converter

2(b) 26/08/2021 28
Arithmetic Calculator

3 26/08/2021 32
Generating Pay Roll Slip For Employees

4 01/09/2021 42
Stack ADT Using Java Interface

5 06/09/2021 50
Perform String Operations Using ArrayList

6 08/09/2021 56
Abstract Class

7 13/09/2021 63
User Defined Exception Handling

8 13/09/2021 67
File Program

9 15/09/2021 73
Multi-Thread Program

10 15/09/2021 81
Generic Function

11 30/09/2021 85
Calculator

12 10/11/2021
Mini Project Using Java

3
Ex. No: 1 ELECTRICITY BILL GENERATION

Date:12/08/2021

OBJECTIVE:

To develop a java application to generate Electricity bill.

PROCEDURE:

STEP-1: Start the program.

STEP-2: Create the consumer class with following members: Consumer


no, Consumer name, previous month reading, current

month reading ,type of EB connection(domestic or commercial).

STEP-3: If the type of EB Connection is domestic, calculate the amount to

be paid as follows:
First 100 units –Rs
1 per unit
101-200 units –Rs
2.50 per unit
201-500 units –Rs
4 per unit

>501 units -Rs 6


per unit.

STEP-4: If the type of EB connection is commercial, calculate the amount

be paid as follows:
First 100 units –Rs
2 per unit

101-200 units –Rs


4.50 per unit

201-500 units –Rs


6 per unit

>501 units -Rs 7


per unit.
STEP-5: Print the amount.

STEP-6: Stop the program. 4


4
PROGRAM:

import java.io.*;

import java.util.*;

class Eb

Scanner s=new Scanner(System.in);

public void cust()

double monthread;

System.out.println("Enter the customer number:");

String customernumber=s.nextLine();

System.out.println("Enter the customer name:");

String customername=s.nextLine();

System.out.println("Enter the previous month reading");

double prevmonthreading=s.nextDouble();

System.out.println("Enter the current month reading");

double curmonthreading=s.nextDouble();

monthread=curmonthreading-prevmonthreading;

System.out.println("Enter the type of EB connection(1 for domestic and

2 for commercial)");

int choice=s.nextInt();

switch(choice)

case 1:
5
{

if(monthread<=100)

System.out.println("Rs.1 per unit");


System.out.println("The Amount to be paid is: " + (1*monthread));

else if(monthread>=101 && monthread<=200)

System.out.println("Rs. 2.5 per unit");

System.out.println("The Amount to be paid is:" +

(2.50*monthread));

else if(monthread>=201 && monthread<=500)

System.out.println("Rs. 4 per unit");

System.out.println("The Amount to be paid is: " +

(4*monthread));

else if(monthread>=501)

System.out.println("Rs. 6 per unit");

System.out.println("The Amount to be paid is:" +

(6*monthread));

6
else

System.out.println("No more conditions");

break;

case 2:

if(monthread<=100)

System.out.println("Rs. 2 per unit");

System.out.println("The Amount to be paid is: "+ (2*monthread));

else if(monthread>=101 && monthread<=200)

System.out.println("Rs. 4.50 per unit");

System.out.println("The Amount to be paid is:" +

(4.50*monthread));

else if(monthread>=201 && monthread<=500)

System.out.println("Rs. 6 per unit");

System.out.println("The Amount to be paid is:" + (6*monthread));

7
else if(monthread>=501)

System.out.println("Rs. 7 per unit");

System.out.println("The Amount to be paid is: " +(7*monthread));

else

System.out.println("No more conditions");

break;

public static void main(String args[])

Eb E=new Eb();

E.cust();

8
OUTPUT:

9
CONCLUSION:

Thus the JAVA application to calculate the EB bill amount

has been created and implemented successfully.

10
Ex. No: 1(b) STUDENTS PERFORMANCE ANALYSIS

Date:12/08/2021

OBJECTIVE:

To develop a java application to analyse the performance of the students.

PROCEDURE:

STEP-1: Start the program.

STEP-2: Create the student class with following members: rno,


name,m1,m2,m3, total and per.

STEP-3: Based on the percentage per, the class will be displayed as follows:
>75 percentage –
Distinction
60-75 percentage
–First class
<60 percentage
–Second class
STEP-4: Display all the details.

STEP-5: Stop the program

11
PROGRAM:
import java.io.*;

import java.util.*;

class student1

Scanner s=new Scanner(System.in);

public void stud()

int rno,m1,m2,m3,total;
String name;
float per;

System.out.println("Enter the student roll number,name:");


rno=s.nextInt();
name=s.nextLine();
System.out.println("Enter the 3subjects marks");
m1=s.nextInt();
m2=s.nextInt();
m3=s.nextInt();
total=m1+m2+m3;
System.out.println("Roll No.:"+rno);
System.out.println("Name:"+name);
System.out.println("Marks:"+m1+"\t"+m2+"\t"+m3);
System.out.println("Total ="+total);
per=total/3;
if(per>=75)

System.out.println("Class: Distinction");

}
else if(per>=60 && per<=74)

12
{

System.out.println("Class:First Class");

else if(per<60)

System.out.println("Class:Second Class");

}
else
{
System.out.println();
}

public static void main(String args[])

student1 s=new student1();

s.stud();

13
OUTPUT:

RESULT:

Thus the Java program to analyze the performance of the students has
been created and executed successfully.

14
Ex. No: 2a CURRENCY, DISTANCE AND TIME CONVERTER

Date:19/08/2021

OBJECTIVE:

To develop a java application to implement currency converter, distance


converter and time converters using packages.

PROCEDURE:

STEP-1: Start the program.

STEP-2: Create a package named Myownpackage.

STEP-3: Create a user defined function named MConverter()


STEP-4: Use switch cases to perform Dollar to INR ,EURO to INR, Yen to INR
conversions and viceversa.

STEP-5: Create a user defined function named DConverter()


STEP-6: Use switch cases to perform Meter to KM, Miles to KM conversions
and vice versa.

STEP-7: Create a user defined function named TConverter()


STEP-8: Use switch cases to perform Hours to minutes , Hours to
seconds conversions and vice versa.

STEP-9: Create a new java program and import the package Myownpackage.
STEP-10: Use switch cases to call the functions which we have used in the
previous program

STEP-11: Stop the program

15
PROGRAM-1:

package Myownpackage;

import java.io.*;

import java.util.*;

public class A

Scanner s=new Scanner(System.in);

public void MConverter()

int choice;

System.out.println("The Choices are ");

System.out.println("1.Dollar to INR");

System.out.println("2.INR to Dollar");

System.out.println("3.YEN to INR");

System.out.println("4. INR to YEN");

System.out.println("5.Euro to INR");

System.out.println("6.INR to Euro");

System.out.println("Enter your choice");

choice=s.nextInt();

switch(choice)

case 1:

double dollar;

16
System.out.println("Enter the Dollar Amount to be converted to INR:");
dollar=s.nextDouble();

System.out.println("The INR amount is " +


(dollar*60)); break;

case 2:

double Inr;

System.out.println("Enter the INR Amount to be converted to


Dollar:"); Inr=s.nextDouble();

System.out.println("The Dollar Amount is " +(Inr/60));


break;

case 3:

double Yen;

System.out.println("Enter the YEN Amount to be converted to


INR:"); Yen=s.nextDouble();

System.out.println("The INR Amount is "

+(Yen*40.0)); break;

case 4:

double Inr;
17
System.out.println("Enter the INR Amount to be converted to
YEN:"); Inr=s.nextDouble();

System.out.println("The YEN Amount is " +(Inr/40.0));


break;

case 5:

double Euro;

System.out.println("Enter the Euro Amount to be converted


to INR:"); Euro=s.nextDouble();

System.out.println("The INR Amount is " +


(Euro*80)); break;

case 6:

double Inr;

System.out.println("Enter the INR Amount to be converted to


Euro:"); Inr=s.nextDouble();

System.out.println("The Euro Amount is " +


(Inr/80.0)); break;

public void DConverter()


18
{

int choice;

System.out.println("The Choices are ");

System.out.println("1.Metres to Kilometer");

System.out.println("2.Kilometer to Metres");

System.out.println("3.Miles to Kilometer");

System.out.println("4. Kilometer to Miles");

System.out.println("Enter your choice");

choice=s.nextInt();

switch(choice)

{case 1:

double metre;

System.out.println("Enter the Metres to be converted to

Kilometres:");

metre=s.nextDouble();

System.out.println("The Kilometres is " +

(metre*0.001));

break;

case 2:

double km;

System.out.println("Enter the Kilometers to be converted to Metres:");


km=s.nextDouble();
19
System.out.println("The Metres is " +

(km/0.001)); break;

case 3:

double miles;

System.out.println("Enter the Miles to be converted to

Kilometres:"); miles=s.nextDouble();

System.out.println("The Kilometres is " + (miles*1.609344));

break;

case 4:

double km;

System.out.println("Enter the Kilometres to be converted to

Miles:"); km=s.nextDouble();

System.out.println("The Miles is " +

(km/1.609344)); break;

public void TConverter()

int choice;
20
System.out.println("The Choices are ");

System.out.println("1.Hours to Minutes");

System.out.println("2.Minutes to Hours");

System.out.println("3.Seconds to Hours");

System.out.println("4. Hours to Seconds");

System.out.println("Enter your choice");

choice=s.nextInt();

switch(choice)

case 1:

int hrs;

System.out.println("Enter the Hours to be converted to

Minutes:"); hrs=s.nextInt(); System.out.println("The

Minutes is " + (hrs*60)); break;

case 2:

int mnts;

System.out.println("Enter the Minutes to be converted to

Hours:"); mnts=s.nextInt();

System.out.println("The Hours is " +

(mnts/60)); break;

21
}

case 3:

int seconds;

System.out.println("Enter the Seconds to be converted to Hours:");


seconds=s.nextInt();

System.out.println("The Hours is " +


(seconds/3600)); break;

case 4:

int hrs;

System.out.println("Enter the Hours to be converted to Seconds:");


hrs=s.nextInt();

System.out.println("The Seconds is " + (hrs*3600));


break;

22
PROGRAM-2:

import Myownpackage.A;

import java.io.*;

import java.util.*;

class Next

public static void main(String args[])

Scanner s=new Scanner(System.in);

int ch;

A ob=new A();

do

System.out.println("The Choices are");


System.out.println("1.Money Converter");

System.out.println("2.Distance Converter");

System.out.println("3.Time Converter");

System.out.println("Enter your choice");

ch=s.nextInt();

switch(ch)

case 1:

ob.MConverter();

break;

23
case 2:

ob.DConverter();

break;

case 3:

ob.TConverter();

break;

}while(ch<=3);

24
24
OUTPUT-1:

25
25
26
26
CONCLUSION:
Thus a Java application has been created to perform Currency,
Distance and Time converters using packages and implemented successfully.

27
Ex. No: 2(b) ARITHMETIC CALCULATOR

Date:26/08/2021

OBJECTIVE:

To develop a java application to implement arithmetic calculator to perform addition,


subtraction, multiplication and division using packages.

PROCEDURE:

STEP-1: Start the program.

STEP-2: Create a package named calculator.

STEP-3: Create a user defined class Calculator with a member function named calc()

STEP-4: Use switch cases to perform arithmetic operations such as addition, subtraction,
multiplication and division .

STEP-5: Create a new java program compute.java and import the package calculator.

STEP-6: From compute.java, call the method calc().

STEP-7: Stop the program

28
PACKAGE:
package calculator;
import java.util.Scanner;

public class Calculator {

public void calc() {

double num1, num2;


Scanner scanner = new Scanner(System.in);
System.out.print("Enter first number:");

num1 = scanner.nextDouble();
System.out.print("Enter second number:");
num2 = scanner.nextDouble();

System.out.print("Enter an operator (+, -, *, /): ");


char operator = scanner.next().charAt(0);

scanner.close();
double output;

switch(operator)
{
case '+':
output = num1 + num2;
break;

case '-':
output = num1 - num2;
break;

case '*':
output = num1 * num2;
break;

case '/':
output = num1 / num2;
break;

default:
System.out.printf("You have entered wrong operator");
return;
}

System.out.println(num1+" "+operator+" "+num2+": "+output);


}
}
29
IMPORTING PACKAGE:
import calculator.Calculator;
public class compute
{
public static void main(String arg[])
{
Calculator c=new Calculator();
c.calc();
}
}

30
OUTPUT:

RESULT:

Thus a java application to implement arithmetic calculator to perform addition, subtraction,


multiplication and division using packages was done successfully.

31
Ex.No:3 GENERATING PAY ROLL SLIP FOR EMPLOYEES

Date:26/08/2021

OBJECTIVE:

To 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.

PROCEDURE:

STEP-1: Decalre Class Employee and Define the Method void display()
STEP-2: Declare class Programmer and extends Employee. Define
Programmer () constructor and calculate bp, hra, da, pf and salary.
STEP-3: Declare class AssistProf and extends Employee. Define AssistProf
() constructor and calculate bp, hra, da, pf and salary.
STEP-4: Declare class AssoProf and extends Employee. Define AssoProf ()
constructor and calculate bp, hra, da, pf and salary.
STEP-5: Declare class Professor and extends Employee. Define Professor ()
constructor and calculate bp, hra, da, pf and salary.
STEP-6: Declare main class as Employee and create instances of those
classes and call display() method and print the details.

32
PROGRAM:
import java.io.*;

import java.util.Scanner;

class Employee
{

int Emp_id,Mobile_no;

String Emp_name,address,Mail_id;

Scanner get=new Scanner(System.in);

public Employee()
{

System.out.println("Enter name of the Employee:");

Emp_name=get.nextLine();

System.out.println("Enter Mail id of the Employee:");

Mail_id=get.nextLine();

System.out.println("Enter address of the Employee:");

address=get.nextLine();

System.out.println("Enter Emp id:");

Emp_id=get.nextInt();

System.out.println("Enter Mobile No:");

Mobile_no=get.nextInt();

void display()

{
System.out.println("Employee id:"+Emp_id);

System.out.println("Employee name:"+Emp_name);

System.out.println("Employee Mobile Number"+Mobile_no);

33
System.out.println("Employee mail:"+Mail_id);

System.out.println("Employee address:"+address);

class Programmer extends Employee


{

float bp,salary,hra,da,swf,pf;

public Programmer()
{

System.out.println("Enter Bas Pay:");

bp=get.nextFloat();

hra=bp*10/100;

da=bp*97/100;

pf=bp*12/100;

swf=bp*1/10000;

salary=bp+hra+da-pf-swf;

void display()

System.out.println("Programmer Details:");

super.display();

System.out.println("Basic Pay:"+bp);

System.out.println("HRA:"+hra);

System.out.println("DA:"+da);
34
System.out.println("Staff welare amount:"+swf);
System.out.println("P F:"+pf);
System.out.println("Salary:"+salary);

class AssistProf extends Employee

float bp,salary,hra,da,swf,pf;

public AssistProf()
{

System.out.println("Enter Bas Pay:");

bp=get.nextFloat();

hra=bp*10/100;

da=bp*97/100;

pf=bp*12/100;

swf=bp *1/10000;

salary=bp+hra+da-pf-swf;

void display()

System.out.println("Assstant Profesor Details:");

super.display();

System.out.println("Basic Pay:"+bp);

35
System.out.println("HRA:"+hra);

System.out.println("DA:"+da);

System.out.println("Staff welare amount:"+swf);

System.out.println("P F:"+pf);

System.out.println("Salary:"+salary);

class AssoProf extends Employee

float bp,salary,hra,da,swf,pf;

public AssoProf() {

System.out.println("Enter Bas Pay:");

bp=get.nextFloat();

hra=bp*10/100;

da=bp*97/100;

pf=bp*12/100;

swf=bp *1/10000;

salary=bp+hra+da-pf-swf;

void display()

System.out.println("Associate Professor
Details:");
super.display();
36
System.out.println("Basic Pay:"+bp);

System.out.println("HRA:"+hra);

System.out.println("DA:"+da);

System.out.println("Staff welare amount:"+swf);

System.out.println("P F:"+pf);

System.out.println("Salary:"+salary);

}
}

class Professor extends Employee

float bp,salary,hra,da,swf,pf;

public Professor()
{
System.out.println("Enter Bas Pay:");

bp=get.nextFloat();

hra=bp*10/100;

da=bp*97/100;

pf=bp*12/100;

swf=bp *1/10000;

salary=bp+hra+da-pf-swf;
}

void display()

37
System.out.println("Professor Details:");

super.display();

System.out.println("Basic Pay:"+bp);

System.out.println("HRA:"+hra);

System.out.println("DA:"+da);

System.out.println("Staff welare amount:"+swf);

System.out.println("P F:"+pf);

System.out.println("Salary:"+salary);

}
}
public class Employees
{
public static void main(String[] args)
{
System.out.println("Enter Employee Details:");
Employee e=new Employee();
e.display();

Programmer obj1=new Programmer();

obj1.display();

AssistProf obj2=new AssistProf();

obj2.display();

AssoProf obj3=new AssoProf();

obj3.display();

Professor obj4=new Professor();

obj4.display();

} 38
OUTPUT-1:

39
40
CONCLUSION:
Thus the Java program has been created to generate pay slips and
it has been implemented successfully.

41
Ex.No : 4 STACK ADT USING JAVA INTERFACE

Date:01/09/2021

OBJECTIVE:
To design a java interface for ADT stack. Implement this interface using
array. Provide necessary exception handling in both the implementation.

PROCEDURE:

STEP-1: Implement Mystack interface.


STEP-2: Create class Stack and extend Mystack. Declare and define pop()
and push() method.

STEP-3: Use Stack exceptions Stack Underflow and stack overflow.


STEP-4: Create main class as Stack_ADT and Create instances to access the
Stack Class

STEP-5: Perform Push and Pop operation.

42
PROGRAM:

import java.io.*;

interface Mystack

public void pop();

public void push();

public void display();

class Stack_array implements Mystack

final static int n=5;

int stack[]=new int[n];

int top=-1;

public void push()

try

BufferedReader br=new BufferedReader(new InputStreamReader(System.in));

if(top==(n-1))

System.out.println(" Stack Overflow");

return;

43
else

System.out.println("Enter the element");


int ele=Integer.parseInt(br.readLine());
stack[++top]=ele;

catch(IOException e)

System.out.println("e");

public void pop()

if(top<0)

System.out.println("Stack underflow");

return;

else

int popper=stack[top];

top--;
System.out.println("Popped element:" +popper);

44
}

public void display()

if(top<0)

System.out.println("Stack is empty");

return;

else

String str=" ";

for(int i=0; i<=top; i++)

str=str+" "+stack[i]+" <--";

System.out.println("Elements are:"+str);

class Stack_ADT

public static void main(String arg[])throws IOException

BufferedReader br=new BufferedReader(new InputStreamReader

45
(System.in));

System.out.println("Implementation of Stack using

Array"); Stack_array stk=new Stack_array(); int ch=0;

do

System.out.println("1.Push 2.Pop 3.Display

4.Exit"); System.out.println("Enter your choice:");

ch=Integer.parseInt(br.readLine()); switch(ch)

case 1:

stk.push();

break;

case 2:

stk.pop();

break;

case 3:

stk.display();

break;

case 4:

System.exit(0);

46
while(ch<5);

47
OUTPUT:

48
CONCLUSION:
Thus the Java program has been created to design a java
interface for STACK ADT and it has been implemented successfully.

49
Ex.No:05 PERFORM STRING OPERATIONS USING ARRAYLIST

Date:06/09/2021

OBJECTIVE:
To write a program to perform append , insert, search and list all
strings with given letter operations using ArrayList.

PROCEDURE:

STEP-1: Append the given string to the arraylist using add() method.

STEP-2: Insert the element at the particular index. Firstly get the index and
string and then use add(index,strobj) method to add the element at particular
index.

STEP-3: Get the element and find whether the element is present or not
using listobj.contains() method. It return either true or false.
STEP-4: Get a letter from input and use obj.startsWith(strobj) method find the
element.

STEP-5: Print all elements.

50
PROGRAM:

import java.io.*;

import java.util.*;

public class Array_List {

public static void main(String[] args)


{

Scanner get=new Scanner(System.in);


String x;

ArrayList<String> al=new ArrayList<>(10);

int choice;

int k=1;

while(k==1)

{
System.out.println("1.append an element-at end 2.Insert-at particular Index
\n 3.Search \n 4.List all string starts with given Letter");

System.out.println("5.Display \n 6.Exit");
choice=get.nextInt();
switch(choice)

case 1 : {

System.out.println("Enter an element");

x=get.next();

al.add(x);

break;

51
case 2 :{

System.out.println("Enter index:");

int y=get.nextInt();

System.out.println("Enter an element");

x=get.nextLine();

al.add(y,x);

break;

case 3:{

System.out.println("Enter an element to be searched");

String el=get.next();

if(al.contains(el))

System.out.println("yes,the element is in the arraylist");

else{

System.out.println("No such element is present");

break;

case 4:{

System.out.println("Enter the starting letter");

52
String letter=get.next();
for(String i:al)
{
if(i.startsWith(letter))
{
System.out.println(i);
}
}
break;
}
case 5:{

if(al.isEmpty())

System.out.println("Empty List");

for(String i:al)

System.out.println(i);

break;

case 6 :{

k=2;

break;

default:{

System.out.println("try 1-6");
}

}
}
} 53
OUTPUT:

54
CONCLUSION:
Thus the Java program has been created to perform string
operations using ArrayList and it has been implemented successfully.

55
Ex.No: 06 ABSTRACT CLASS

Date:08/09/2021

OBJECTIVE:

To write a Java program to create an abstract class named Shape that


contains two integers and an empty method named print Area() and to print the
area of the given shape.

PROCEDURE:

STEP-1: Start the program.


STEP-2: Create an abstract class named Shape and declare two integers and an
empty method named print_area().
STEP-3: Create three classes named Rectangle, Triangle and Circle such
that each one of the classes extends the class Shape.
STEP-4: Define print_area() method in each of the classes that prints the area of
the given shapes.
STEP-5: In the main() function, use switch cases to perform the area of
the necessary shapes.

STEP-6: Stop the program.

56
PROGRAM:

import java.io.*;

import java.util.*;

abstract class Shape

int x,y;

Scanner s=new Scanner(System.in);

abstract void print_area();

class Rectangle extends Shape

void print_area()

int area;

System.out.println("Enter the length: ");

x=s.nextInt();

System.out.println("Enter the breadth: ");

y=s.nextInt();

area=x*y;

System.out.println("The Area of the Rectangle is "+area);

class Triangle extends Shape

57
void print_area()

double area;

System.out.println("Enter the length: ");

x=s.nextInt();

System.out.println("Enter the breadth: ");

y=s.nextInt();

area=(double)((x*y)/2);

System.out.println("The Area of the Triangle is "+area);

class Circle extends Shape

void print_area()

double area;

System.out.println("Enter the radius: ");

x=s.nextInt();

area=3.14*x*x;

System.out.println("The Area of the circle is "+area);

class Main

58
public static void main(String args[])

int choice;

Rectangle r=new Rectangle();

Triangle t=new Triangle();

Circle c=new Circle();

do

System.out.println("Press 1 to find the area of the Rectangle");

System.out.println("Press 2 to find the area of the Triangle");

System.out.println("Press 3 to find the area of the Circle");

Scanner sc=new Scanner(System.in);

choice=sc.nextInt();

switch(choice)

case 1:

r.print_area();

break;

case 2:

t.print_area();

break;

59
}

case 3:

c.print_area();

break;

}while(choice<4);

60
OUTPUT:

61
CONCLUSION:

Thus a Java program has been created to create a abstract


class and thereby to calulcate the area of the shapes and it has been implemented
successfully.

62
Ex.No:07 USER DEFINED EXCEPTION HANDLING

Date:13/09/2021

OBJECTIVE:
To write a Java program to implement user defined exception handling

PROCEDURE:

STEP-1: Start the program.

STEP-2: Create a new class by extending the Exception class

STEP-3: Declare a string in the class.

STEP-4: Use a constructor to pass the string.

STEP-5: Define a function to throw the exception

STEP-6: In the main() function, use try block to perform necessary operations

STEP-7: Use catch block to catch the exception.

STEP-8: Stop the program.

63
PROGRAM:

import java.io.*;

class MyException extends Exception{

String str1;

MyException(String str2) {

str1=str2;

public String toString(){

return ("MyException Occurred: "+str1) ;

class Example1{

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) ;

64
OUTPUT:

65
CONCLUSION:
Thus the Java program has been created to handle the
user defined exception and it has been implemented successfully.

66
Ex.No:08 FILE PROGRAM

Date:13/09/2021

OBJECTIVE:

To write a java program that reads a file name from the user and displays
information about whether the file is readable, or writable, the type of file and
the length of the file in bytes.

PROCEDURE:

STEP-1:Read the file name by using the BufferedReader of the


Character stream Classes.

STEP-2: Assign the file name to an object.

STEP-3: Check whether the file exists by using exists() method

STEP-4: If it exists, the size of the file is given by length() method


STEP-5: We can find its type by the last characters of the file name which is the
extension by using the endsWith() method.

67
PROGRAM:

import java.io.*;

public class FileExample

public static void main(String args[]) throws


IOException {

BufferedReader br = new BufferedReader(new

InputStreamReader(System.in));

System.out.println("\nEnter a file name:");


String fName=br.readLine(); File f =new
File(fName);

String result = f.exists()?"Exists": "does not exist.";

System.out.println("\nThe given file "+result);

if(f.exists())

{
System.out.println("The file length is "+f.length()+"in bytes");
if(fName.endsWith(".jpg")||fName.endsWith(".gif")||fName

.endsWith(".png"))

System.out.println("The given file is an Image file");

else if(fName.endsWith(".exe")){

System.out.println("The given file is an executable file");

else if(fName.endsWith(".txt"))

{ 68
System.out.println("The given file is a text file");

69
else if(fName.endsWith(".java"))

System.out.println("The given file is a java file");

else

System.out.println("The file type is unknown");

70
OUTPUT:

71
CONCLUSION:
Thus the program the reads the file name and displays the
information about the file is executed and verified successfully.

72
Ex.No:09 MULTI-THREAD PROGRAM

Date:15/09/2021

OBJECTIVE:

To write a java program that implements a multi-thread


application that has three threads in which the first thread generates a random
integer every 1 sec and if the value is even, the second thread computes the
square of the number and prints it. Else the third thread will print the value of
the cube

PROCEDURE:

STEP-1: Create a 3 thread that implements Runnable method.

STEP-2: One for generating random integer.


STEP-3: One for computing and printing the square of the number if the integer
is even.
STEP-4: And the other for computing and printing the cube of the number if
the integer is odd.
STEP-5: To delay one second between each generation the Tread.sleep(1000)
is used.

STEP-6: In order to execute the thread start() method is used.

STEP-7: After all the integers are generated the execution of the threads stops.

73
PROGRAM:

class Random implements Runnable

double num;

public void run()

try

SquareThread sqt = new SquareThread();

Thread squareThread = new Thread(sqt);

CubeThread cbt = new CubeThread();

Thread cubeThread = new Thread(cbt);

squareThread.start();

cubeThread.start();

for(int i=0;i<5;i++)

System.out.println("t1-"+i);

if(i%2==0)

sqt.setNum(new Double(i));

else

cbt.setNum(new Double(i));

74
}

Thread.sleep(1000);

catch(InterruptedException e)

e.printStackTrace();

class SquareThread implements Runnable

Double num;

public void run()

try

int i=0;

do

i++;

if(num!=null&&num%2==0)

System.out.println("t2-->Square of"+num+ " =

"+(num*num));
75
num=null;

Thread.sleep(1000);

}while(i<=5);

catch(Exception e)

e.printStackTrace();

public Double getNum()

return num;

public void setNum(Double num)

this.num=num;

class CubeThread implements Runnable

Double num;

public void run()

76
try

int i=0;

do

i++;

if(num!=null&&num%2!=0)

{
System.out.println("t3-->Cube of
"+num+"="+(num*num*num));

num=null;

Thread.sleep(1000);

}while(i<=5);

catch(Exception e)

e.printStackTrace();

public Double getNum()

return num;

public void setNum(Double num)

77
{

this.num=num;

public class randomthread

public static void main(String args[]) throws InterruptedException

Thread randomThread =new Thread(new

Random()); randomThread.start();

78
OUTPUT:

79
CONCLUSION:
Thus, the required program that generates integers and prints
its square or cube based on that integer, is executed and verified successfully.

80
Ex. No: 10 GENERIC FUNCTION

Date:15/09/2021

OBJECTIVE:

To write a java program to find the maximum value from the given
type of elements using a generic function.

PROCEDURE:

STEP-1: Start the program

STEP-2: Define the array with the elements .

STEP-3: Sets the first value in the array as the current maximum

STEP-4: Find the maximum value by comparing each elements of the array

STEP-5: Display the maximum value

STEP-6: Stop the program.

81
PROGRAM:

import java.io.*;

public class Maximum

public static void main(String args[])

{Integer[] list=new Integer[10];

for(int i=0;i<list.length;i++)

list[i]=i;

}
System.out.println("Max="+max(list));

public static <E extends Comparable<E>> E max(E[]list)

}
return max;
}
max=list[0];
for(inti=1;i<list.length;i++)
{

E element=list[i];
if(element.compareTo(max)>0)
{
max=element;
}
}
return max;

82
OUTPUT:

83
CONCLUSION:
Thus the implementation of generic function is achieved for finding the
maximum value from the given type of elements.

84
Ex. No.: 11 CALCULATOR

Date:30/09/2021

OBJECTIVE:
To design a calculator using event-driven programming paradigm
of Java for Decimal manipulations and Scientific manipulations.

PROCEDURE:

STEP-1: Start the program

STEP-2: Using the swing components design the buttons of the calculator
STEP-3: Use key events and key listener to listen the events of the calculator.

STEP-4: Do the necessary manipulations.

STEP-5: Stop the program.

85
PROGRAM:

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

class Calculator extends JFrame

{
private final Font BIGGER_FONT=new Font("monspaced",Font.PLAIN,20);

private JTextFieldtextfield;

private boolean number=true;

private String equalOp="=";

private CalculatorOp op=new

CalculatorOp(); public Calculator()

textfield=new JTextField("",12);

textfield.setHorizontalAlignment(JTextField.RIGHT);

textfield.setFont(BIGGER_FONT);

ActionListenernumberListener=new

NumberListener(); String buttonOrder="1234567890";

JPanelbuttonPanel=new JPanel();

buttonPanel.setLayout(new GridLayout(4,4,4,4));

for(inti=0;i<buttonOrder.length();i++) {

String Key=buttonOrder.substring(i,i+1);

86
if(Key.equals(""))

buttonPanel.add(new JLabel(""));

else

JButton button=new JButton(Key);

button.addActionListener(numberListener);

button.setFont(BIGGER_FONT);

buttonPanel.add(button); }

ActionListeneroperatorListener=new

OperatorListener(); JPanel panel=new JPanel();

panel.setLayout(new GridLayout(4,4,4,4));

String[]opOrder={"+","-","*","/","=","C","sin","cos","log"};

for(inti=0;i<opOrder.length;i++)

JButton button=new JButton(opOrder[i]);

button.addActionListener(operatorListener);

button.setFont(BIGGER_FONT);

panel.add(button);

JPanel pan=new JPanel();

87
pan.setLayout(new BorderLayout(4,4));

pan.add(textfield,BorderLayout.NORTH);

pan.add(buttonPanel,BorderLayout.CENTER);

pan.add(panel,BorderLayout.EAST);

this.setContentPane(pan); this.pack();

this.setTitle("Calculator");

this.setResizable(false);

private void action()

number=true;

textfield.setText("");

equalOp="=";

op.setTotal("");

class OperatorListener implements ActionListener

public void actionPerformed(ActionEvent e)

String displayText=textfield.getText();

if(e.getActionCommand().equals("sin"))

textfield.setText(""+Math.sin(Double.valueOf(displayText).doubleValue()));

88
}

else

if(e.getActionCommand().equals("cos"))

textfield.setText(""+Math.cos(Double.valueOf(displayText).doubleValue()));

else

if(e.getActionCommand().equals("log"))

textfield.setText(""+Math.log(Double.valueOf(displayText).doubleValue()));

else if(e.getActionCommand().equals("c"))

textfield.setText("");

else

if(number)

action();

textfield.setText("");

else

89
number=true;

if(equalOp.equals("="))

op.setTotal(displayText);

}else

if(equalOp.equals("+"))

op.add(displayText);

else if(equalOp.equals("-"))

op.subtract(displayText);

else if(equalOp.equals("*"))

op.multiply(displayText);

else if(equalOp.equals("/"))

op.divide(displayText);

textfield.setText(""+op.getTotalString());

equalOp=e.getActionCommand();

90
}

class NumberListener implements ActionListener

public void actionPerformed(ActionEvent event)

String digit=event.getActionCommand();

if(number)

textfield.setText(digit);

number=false;

else

textfield.setText(textfield.getText()+digit);

public class CalculatorOp

private int total;

public CalculatorOp()

91
total=0;

public String getTotalString()

return""+total;

public void setTotal(String n)

total=convertToNumber(n);

public void add(String n)

total+=convertToNumber(n);

public void subtract(String n)

total-=convertToNumber(n);

public void multiply(String n)

total*=convertToNumber(n);

public void divide(String n)

92
total/=convertToNumber(n);

private intconvertToNumber(String n)

return Integer.parseInt(n);

class MAIN

public static void main(String args[])

JFrame frame=new Calculator();

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.setVisible(true);

93
OUTPUT:

94
CONCLUSION:
Thus the implementation of generic function is achieved for
finding the maximum value from the given type of elements.

95
Ex .No:12 MINI PROJECT USING JAVA

Date:

OBJECTIVE:

To create a mini project using java programming language

PROCEDURE:

STEP-1: The doMenu() function is used to display the menu of the reservation.

STEP-2:The following methods are used based on the choice selected


by the user.

STEP-3:To book tickets the doBook() function is used.


STEP-4:It gets the information about the tickets needs and checks with
the maximum tickets available.
STEP-5:If the maximum number of tickets is not filled then the tickets
are booked and displays the amount to be paid.
STEP-6:The doCancel() function is used cancel the ticket by checking the
passenger number.
STEP-7:The doDispList() function is used to display the information of the
passengers whose tickets were booked
STEP-8:The doSearch() function is used to display the details a passenger
by getting the number
STEP-9: The doDispUnbooked() function is used to number of
tickets remaining in all the class in the train
STEP-10: In main function of the program .object to the class RailwayBooking is
created by using that object domenu() is called to run the program.
PROGRAM:

import java.io.*;

//**************************************

// Name: RAILWAY RESERVATION

// Description:YOU CAN BOOK,CANCEL,VIEW TICKETS


HISTORy

//

//

// Inputs:None

//

// Returns:None

//

//Assumes:FIRSTLY DOWNLOAD JDK FILE AND BLUEJ TO USE


THE APPLICATION

//

//Side Effects:NO ``

//**************************************

/* PROJECT ON:railway reservation


DESCRIPTION : To Book / Cancel ticket, Display reservation
chart, Search passenger number, And exit.

*/

public class RailwayBooking

BufferedReaderbr = new BufferedReader(new


InputStreamReader(System.in));
//System.out.print("Please enter a

word"); int pno[]=new int[275];

String name[]=new String[275];

String phno[]=new String[275];

int age[]=new int[275];

int cl[]=new int[275];

int pcount=0;
int pnum=1;

int max1=75;

int max2=125;

int max3=175;

public void doMenu() throws Exception

int cho;

do

doHeading();

System.out.println("1.Book ticket");

System.out.println("2.Cancel ticket");

System.out.println("3.Search passenger");

System.out.println("4.Reservation chart");
System.out.println("5.Displayunbooked tickets");

System.out.println("6.Exit");
System.out.println("Please enter your choice");
cho=Integer.parseInt(br.readLine());

switch(cho)

case 1:doBook();

break;

case 2:doCancel();

break;

case 3:doSearch();

break;

case 4:doDispList();
break;

case 5:doDispUnbooked();

break;

case 6:doExit();

break;

default :System.out.println("Invalid choice");

}while(cho!=6);

private void doHeading()throws Exception


{

System.out.println("###########################################
##############");
System.out.println("*********Railway Reservation ForNellai
Express***********");

System.out.println("###########################################

##############");

private void doBook()throws Exception

System.out.println("Please enter the class of

ticket"); System.out.println("1. AC\t 2. First\t 3.

Sleeper\t"); int c=Integer.parseInt(br.readLine());

System.out.println("Please enter no. of tickets"); int

t=Integer.parseInt(br.readLine());

intticketAvailable=0;

if(c==1 && max1>=t)

ticketAvailable=1;

if(c==2 && max2>=t)

ticketAvailable=1;

if(c==3 && max3>=t)

{
ticketAvailable=1;

if(ticketAvailable==1)

for(inti=0;i<t;i++)

pno[pcount]=pnum;

System.out.println("Please enter your name");

name[pcount]=br.readLine();
System.out.println("Please enter your age");
age[pcount]=Integer.parseInt(br.readLine());
cl[pcount]=c;

System.out.println("Please enter your phno");


phno[pcount]=br.readLine(); pcount++;
pnum++;

System.out.println("Ticket successfully

booked"); }//for

if(c==1)

max1-=t;

System.out.println("Please pay Rs."+t*1500);

if(c==2)

max2-=t;

System.out.println("Please pay Rs."+t*1200);

if(c==3)

max3-=t;

System.out.println("Please pay Rs."+t*1000);

private void doCancel()throws Exception

{
intt_pno[]=new int[275];

String t_name[]=new String[275];

String t_phno[]=new String[275];

intt_age[]=new int[275];

intt_cl[]=new int[275];

intt_pcount=0;

intpassengerFound=0;
System.out.println("Please enter your passenger no.");

int p=Integer.parseInt(br.readLine());
for(inti=0;i<pcount;i++)

if(pno[i]!=p)

{//transfer

t_pno[t_pcount]=pno[i];

t_name[t_pcount]=name[i];

t_phno[t_pcount]=phno[i];

t_age[t_pcount]=age[i];

t_cl[t_pcount]=cl[i];

t_pcount++;

else

passengerFound=1;

if(cl[i]==1)

max1++;
System.out.println("Please collect refund of Rs."+1800);

if(cl[i]==2)

max2++;
System.out.println("Please collect refund of Rs."+1500);

if(cl[i]==3)

{
max3++;
System.out.println("Please collect refund of
Rs."+1000); }//else

}//if

if(passengerFound==1)

pno=t_pno;

name=t_name;

age=t_age;

cl=t_cl;
phno=t_phno;

pcount=t_pcount;

System.out.println("ticket successfully cancelled");

}//method

private void doDispList()throws Exception

System.out.println("Passenger list in AC class");


System.out.println("pno \t name \t\t age \t phno");
for(inti=0;i<pcount;i++)

if(cl[i]==1)

System.out.println(pno[i]+"\t"+name[i]+"\t\t"+age[i]+"\t"+phno[i]);

System.out.println("Passenger list in First class");


System.out.println("pno \t name \t\t age \t phno");
for(inti=0;i<pcount;i++)

if(cl[i]==2)

System.out.println(pno[i]+"\t"+name[i]+"\t\t"+age[i]+"\t"+phno[i]);

System.out.println("Passenger list in Sleeper


class"); System.out.println("pno \t name \t\t age \t
phno"); for(inti=0;i<pcount;i++)
{

if(cl[i]==3)

System.out.println(pno[i]+"\t"+name[i]+"\t\t"+age[i]+"\t"+phno[i]);

private void doSearch()throws Exception

{
intpassengerFound=0;

System.out.println("Please enter passenger no. to search");


int p=Integer.parseInt(br.readLine());
for(inti=0;i<pcount;i++)

if(pno[i]==p)

System.out.println("Detail found");

passengerFound=1;

System.out.println("passenger no.="+pno[i]);

System.out.println("name="+name[i]);

System.out.println("class="+cl[i]);

System.out.println("phno="+phno[i]);

System.out.println("age="+age[i]);

}//for

if(passengerFound==0)

System.out.println("No such passenger");

}//method

private void doDispUnbooked()throws Exception

System.out.println("No. of booked tickets status");

System.out.println("AC class"+max1);

System.out.println("First class"+max2);

System.out.println("Sleeper class"+max3);

private void doExit()


{

System.exit(0);

public static void main(String[] args) throws Exception {


RailwayBookingrb=new RailwayBooking(); rb.doMenu();

}
OUTPUT:
CONCLUSION:
Thus the mini project (Railway reservation) using
java programming language is executed and verified successfully.

You might also like