0% found this document useful (0 votes)
7 views

OOP Lab Final Record

The document provides information about a laboratory course on object oriented programming. It includes the name, registration number, branch and year of the student. It also includes the vision, mission and objectives of the department and course. The document certifies that the work done by the student in the laboratory meets the requirements.

Uploaded by

dcstp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

OOP Lab Final Record

The document provides information about a laboratory course on object oriented programming. It includes the name, registration number, branch and year of the student. It also includes the vision, mission and objectives of the department and course. The document certifies that the work done by the student in the laboratory meets the requirements.

Uploaded by

dcstp
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

An Autonomous Institution

191CS32B-OBJECT ORIENTED PROGRAMMING LABORATORY

NAME :

REGISTER NO :

ROLL NO : VM-

BRANCH : B.Tech- ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

YEAR : II

SEMESTER : III

Department Of Artificial Intelligence and Data Science

Vision
To emerge as centre for academic excellence in the field of Department Of Artificial Intelligence And Data Science

by exposure to research and industry practices.

Mission
M1 - To provide good teaching and learning environment with conducive research atmosphere in the field of Computer
Science and Engineering.
M2 - To propagate lifelong learning.
M3 - To impart the right proportion of knowledge, attitudes and ethics in students to enable them take up positions of
responsibility in the society and make significant contributions.
An Autonomous Institution

CERTIFICATE

Name ……………………………………………………………………………………………………………………...
Year:…………………….Semester………………..Branch: B.Tech- Artificial Intelligence and Data Science

University Register No:………………………………………. College Roll No:VM-………………………… Certified that this is the

bonafide record of work done by the above student in the 191CS32B-OBJECT ORIENTED PROGRAMMING LABORATORY during

the academic year 20 -20.

Signature of Head of the Department Signature of the Course In Charge

Submitted for the University practical Examination held on…………………………………..at VETTECH


MULTITECH Dr.RANGARAJAN Dr.SAKUNTHALA ENGINEERING COLLEGE, No42, AVADI-
VELTECH ROAD, AVADI, CHENNAI-600062

Signature of Examiners

Internal Examiner External Examiner


DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE

PROGRAMME EDUCATIONAL OBJECTIVES (PEOs)

PEOs PROGRAMME EDUCATIONAL OBJECTIVES


Ability to identify, formulate and analyze complex Computer Science and
Engineering problems in the areas of hardware, software, theoretical Computer
PEO1
Science and applications to reach significant conclusions by applying Mathematics,
Natural sciences, Computer Science and Engineering principles.
Apply knowledge of mathematics, natural science, engineering fundamentals and
system fundamentals, software development, networking & communication, and
PEO2 information security to the solution of complex engineering problems in computer
science and engineering to get benefits in their professional career or higher education
and research or technological entrepreneur.
Design solutions for complex computer science and engineering problems using state
of the art tools and techniques, components or processes that meet specified needs
PEO3
with appropriate consideration for public health and safety, cultural, societal, and
environmental considerations.

PROGRAMME SPECIFIC OUTCOMES (PSOs)

PSO’s PROGRAMME SPECIFIC OUTCOMES

An ability to apply, design and development of application oriented software systems


PSO1
and to test and document in accordance with Computer Science and Engineering.

The design techniques, analysis and the building, testing, operation and maintenance
PSO2 of networks, databases, security and computer systems (both hardware and
software).

An ability to identify, formulate and solve hardware and software problems using
PSO3
sound computer engineering principles.
GRADUATE ATTIRIBUTES (GAs)
GA’s
Engineering Knowledge: Apply knowledge of mathematics,
GA1 science, engineering fundamentals and an Engineering Specialization to the solution of complex
engineering problems.

Problem Analysis: Identify, formulate, review research literature and analyze complex
GA2 engineering problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.

Design / Development of solutions: Design solutions for complex engineering problems and design
GA3 system components or processes that meet specified needs with appropriate consideration for public
health and safety, cultural, societal, and environmental considerations.

Conduct Investigations of Complex Problems: Use research-based knowledge and research


GA4 methods including design of experiments, analysis and interpretation of data, and synthesis of the
information to provide valid conclusions.

Modern tool usage: Create, select, and apply appropriate techniques, resources, and modern
GA5 engineering and IT tools including prediction and modeling to complex engineering activities with
an understanding of the limitations.
The Engineer and Society: Apply reasoning informed by the contextual knowledge to assess
GA6 societal, health, safety, legal and cultural issues and the consequent responsibilities relevant to the
professional engineering practice.

Environment and sustainability: Understand the impact of the professional engineering solutions
GA7 in societal and environmental contexts, and demonstrate the knowledge of, and need for
sustainable development.

Ethics: Apply ethical principles and commit to professional ethics and responsibilities and norms of
GA8 the engineering practice.

Individual and team work: Function effectively as an individual, and as a member or leader in
GA9 diverse teams, and in multidisciplinary settings.

Communication: Communicate effectively on complex engineering activities with the engineering


GA10 community and with society at large, such as, being able to comprehend and write effective reports
and design documentation, make effective presentations, and give and receive clear instructions

Project Management and Finance: Demonstrate knowledge and understanding of the engineering
GA11 and management principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.

Life-long learning: Recognize the need for, and have the preparation and ability to engage in
GA12 independent and life-long learning in the broadest context of technological change.
COURSE OBJECTIVES

The student should be made to

➢ To build software development skills using java programming for real-world applications.
➢ To understand and apply the concepts of classes, packages, interfaces, arraylist, exception handling and file
processing.
➢ To develop applications using generic programming and event handling.

At the end of the course, the student should be able to

Course
Outcome
CO Statements

CO1 Configure various Java programs for simple applications that make use of classes,
packages and interfaces.

CO2 Design and deploy Java programs with array list, exception handling.

CO3 Learn how to design applications using file processing, generic programming and
event handling.
CO4 Develop and implement Java programs with and multithreading.
Implement the concepts of classes, packages, interfaces, array list, exception
CO5
handling and file processing.

Mapping CO's with GA 's & PSO’S

Course
GA10

GA11

GA12
GA 1
GA 2
GA 3
GA 4
GA 5

GA 6

GA 7

GA 8

GA 9

PSO1

PSO2

PSO3
Outcome

CO1 3 2 2 1 1 1 - - - - - 2 3 2 2

CO2 3 2 2 1 1 1 - - - - - 1 3 2 2

CO3 3 2 2 1 1 1 1 - 1 - - 1 3 3 2

CO4 3 2 2 2 1 1 1 - - - - 2 3 2 2

CO5 3 2 1 1 1 1 - - 1 - - 2 2 2 2

CO 3 2 2 1 1 1 1 - 1 - - 2 2 2 2
1 – Low 2 – Medium 3 – High
Course Code: 191CS323
Course: OBJECT ORIENTED PROGRAMMING LABORATORY

COURSE PLAN

Ex. No Name of the Exercise CO Page No.

CO1,CO3
1 PROGRAM TO GENERATE ELECTRICITY BILL

PROGRAM TO IMPLEMENT CURRENCY CONVERTER,


2 DISTANCE CONVERTER AND TIME CONVERTER CO1
USING PACKAGES
PROGRAM TO GENERATE PAYSLIP USING
3 CO3
INHERITANCE
4 PROGRAM FOR STACK ADT USING INTERFACE CO3

5
PROGRAM TO PERFORM STRING OPERATIONS CO3,CO4
USING ARRAYLIST
PROGRAM TO CALCULATE AREA USING ABSTRACT
6 CO4
CLASS
PROGRAM TO IMPLEMENT USER DEFINED EXCEPTION
7 CO1,CO4
HANDLING
8 PROGRAM FOR DISPLAYING FILE INFORMATION CO5

CONTENT BEYOND SYLLABUS

9 PROGRAM TO IMPLEMENT MULTITHREADED CO5


APPLICATION.

PROGRAM TO FIND THE MAXIMUM VALUE FROM CO3


THE GIVEN TYPE OF ELEMENTS USING GENERICS
10

11 PROGRAM TO DESIGN A CALCULATOR USING EVENT CO3


DRIVEN PROGRAMMING PARADIGM OF JAVA
EX.NO: 1
PROGRAM TO GENERATE ELECTRICITY BILL
DATE:

AIM:

To develop a Java application to generate Electricity bill.

PROCEDURE :

1. Create a class with the following members Consumer no., consumer name, previous month reading, current month
reading, type of EB connection (i.e domestic or commercial)

2. Compute the bill amount using the following tariff. If the type of the 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 If the type of the EB connection is commercial, calculate the amount to 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

3. Create the object for the created class .Invoke the methods to get the input from the consumer and display the
consumer information with the generated electricity bill.

Coding:

import java.util.*;

class ebill { public static void main (String args[])

customerdata ob = new customerdata();

ob.getdata(); ob.calc(); ob.display();

}}

class customerdata { Scanner in = new Scanner(System.in);

Scanner ins = new Scanner(System.in);

String cname,type; int bn;

double current,previous,tbill,units;

void getdata() { System.out.print ("\n\t Enter consumer number ");

bn = in.nextInt();

System.out.print ("\n\t Enter Type of connection (D for Domestic or C for Commercial) ");

type = ins.nextLine();

System.out.print ("\n\t Enter consumer name ");

cname = ins.nextLine();

System.out.print ("\n\t Enter previous month reading ");

previous= in.nextDouble(); System.out.print ("\n\t Enter current month reading ");

current= in.nextDouble();
}

void calc()

units=current-previous;

if(type.equals("D"))

if (units<=100) tbill=1 * units;

else if (units>100 && units<=200) tbill=2.50*units;

else if(units>200 && units<=500) tbill= 4*units;

else tbill= 6*units;

else { if (units<=100) tbill= 2 * units;

else if(units>100 && units<=200) tbill=4.50*units;

else if(units>200 && units<=500) tbill= 6*units;

else 4 tbill= 7*units;

}}

void display()

System.out.println("\n\t Consumer number = "+bn);

System.out.println ("\n\t Consumer name = "+cname);

if(type.equals("D")) System.out.println ("\n\t type of connection = DOMESTIC ");

else System.out.println ("\n\t type of connection = COMMERCIAL ");

System.out.println ("\n\t Current Month Reading = "+current);

System.out.println ("\n\t Previous Month Reading = "+previous);

System.out.println ("\n\t Total units = "+units);

System.out.println ("\n\t Total bill = RS "+tbill);

}}

OUTPUT:
RESULT :

Thus the java program to generate electricity bill was implemented and executed successfully.
EX.NO: 2 PROGRAM TO IMPLEMENT CURRENCY CONVERTER, DISTANCE
DATE: CONVERTER AND TIME CONVERTER USING PACKAGES

AIM :

To develop a java application to implement currency converter ,distance converter and time converter using the
concept of packages .

PROCEDURE:

1. Create a Package currencyconversion and place the class currency under the package

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

3. Create the package distanceconverion and create the class distance within the package

4. Create the methods to convert from meter to km, km to meter, miles to km,km to miles

5. Create the package timeconversion and create the class timer .Create the methods to convert from hours to minutes
,hours to seconds , minutes to hours and seconds to hours

6. Create a class and import the packages currencyconversion,distanceconversion and time conversion.Create the
objects for the class currency,distance and timer.

7. Get the choice from the user and invoke the methods to perform the corresponding conversion and display the
value.

CODING:

currencyconversion.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:");

euro=in.nextInt();

inr=euro*79.50;

System.out.println("Euro ="+euro +"equal to INR="+inr);

public void rupeetoeuro()

System.out.println("Enter Rupees to convert into Euro:");

inr=in.nextInt();

euro=(inr/79.50);

System.out.println("Rupee ="+inr +"equal to Euro="+euro);

public void yentorupee()

System.out.println("Enter yen to convert into Rupees:");

yen=in.nextInt();

inr=yen*0.61;

System.out.println("YEN="+yen +"equal to INR="+inr);

public void rupeetoyen()

System.out.println("Enter Rupees to convert into Yen:");

inr=in.nextInt();

yen=(inr/0.61);

System.out.println("INR="+inr +"equal to YEN"+yen);

}}

distance.java package distanceconversion;

import java.util.*;

public class distance { double km,m,miles;

Scanner sc = new Scanner(System.in);


public void kmtom()

System.out.print("Enter in km ");

km=sc.nextDouble();

m=(km*1000);

System.out.println(km+"km" +"equal to"+m+"metres");

public void mtokm()

System.out.print("Enter in meter ");

m=sc.nextDouble();

km=(m/1000);

System.out.println(m+"m" +"equal to"+km+"kilometres");

public void milestokm()

System.out.print("Enter in miles");

miles=sc.nextDouble();

km=(miles*1.60934);

System.out.println(miles+"miles" +"equal to"+km+"kilometres");

10 }

public void kmtomiles()

{ System.out.print("Enter in km");

km=sc.nextDouble();

miles=(km*0.621371);

System.out.println(km+"km" +"equal to"+miles+"miles");

}}

timer.java package timeconversion;

import java.util.*;

public class timer { int hours,seconds,minutes;

int input;

Scanner sc = new Scanner(System.in);


public void secondstohours()

System.out.print("Enter the number of seconds: ");

input = sc.nextInt();

hours = input / 3600;

minutes = (input % 3600) / 60; seconds = (input % 3600) % 60;

System.out.println("Hours: " + hours);

System.out.println("Minutes: " + minutes);

System.out.println("Seconds: " + seconds); 11 }

public void minutestohours()

{ System.out.print("Enter the number of minutes: ");

minutes=sc.nextInt();

hours=minutes/60; minutes=minutes%60;

System.out.println("Hours: " + hours);

System.out.println("Minutes: " + minutes); }

public void hourstominutes()

System.out.println("enter the no of hours");

hours=sc.nextInt(); minutes=(hours*60);

System.out.println("Minutes: " + minutes);

public void hourstoseconds()

System.out.println("enter the no of hours");

hours=sc.nextInt(); seconds=(hours*3600);

System.out.println("Minutes: " + seconds);

}}

12 converter.java

import java.util.*; import java.io.*;

import currencyconversion.*;

import distanceconversion.*;

import timeconversion.*; 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 ");

System.out.println("10.kilometer to miles");

13 System.out.println("11.Hours to Minutes");

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

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

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

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

choice=s.nextInt(); switch(choice)

case 1: { c.dollartorupee();

break;

case 2:

c.rupeetodollar();

break; }

case 3:

{ c.eurotorupee();

break;
}

case 4:

c.rupeetoeuro();

break;

14

case 5:

{c.yentorupee(); break;}

case 6 : { c.rupeetoyen();

break; }

case 7 :

{ d.mtokm();

break; }

case 8 :

{ d.kmtom();

break; }

case 9 :

{ d.milestokm();

break; }

case 10 : { d.kmtomiles();

15 break; }

case 11 : { t.hourstominutes();

break; }

case 12 :

{ t.hourstoseconds();

break; }

case 13 : { t.secondstohours();

break; }

case 14 :

{ t.minutestohours();

break; }
}

System.out.println("Enter 0 to quit and 1 to continue ");

ch=s.nextInt(); }while(ch==1);

}
RESULT :
Thus the java application to implement currency converter ,distance converter and time converter
was implemented and executed successfully.
EX.NO: 3
PROGRAM TO GENERATE PAYSLIP USING INHERITANCE
DATE:

AIM:

To develop a java application to generate pay slip for different category of employees using the concept of
inheritance.

PROCEDURE:

1. Create the class employee with name, Empid, address, mailid, mobileno as members.

2. Inherit the classes programmer, asstprofessor,associateprofessor and professor from employee class.

3. Add Basic Pay (BP) as the member of all the inherited classes.

4. Calculate DA as 97% of BP, HRA as 10% of BP, PF as 12% of BP, Staff club fund as 0.1% of BP.

5. Calculate gross salary and net salary.

6. Generate payslip for all categories of employees.

7.Create the objects for the inherited classes and invoke the necessary methods to display the Payslip.

CODING:

import java.util.*;

class employee { int empid;

long mobile;

String name, address, mailid;

Scanner get = new Scanner(System.in);

void getdata()

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

name = get.nextLine();

System.out.println("Enter Mail id");

mailid = get.nextLine();

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

address = get.nextLine();

System.out.println("Enter employee id ");

empid = get.nextInt();

System.out.println("Enter Mobile Number");

mobile = get.nextLong();
}

void display()

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

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

System.out.println("Mail id : "+mailid);

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

20 System.out.println("Mobile Number: "+mobile);

class programmer extends employee

double salary,bp,da,hra,pf,club,net,gross;

void getprogrammer()

System.out.println("Enter basic pay");

bp = get.nextDouble();

void calculateprog()

da=(0.97*bp);

hra=(0.10*bp);

pf=(0.12*bp);

club=(0.1*bp);

gross=(bp+da+hra);

net=(gross-pf-club);

System.out.println("************************************************");

System.out.println("PAY SLIP FOR PROGRAMMER");


System.out.println("************************************************");

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

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

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

System.out.println("CLUB:Rs"+club);

21 System.out.println("GROSS PAY:Rs"+gross);

System.out.println("NET PAY:Rs"+net);

}}

class asstprofessor extends employee

double salary,bp,da,hra,pf,club,net,gross;

void getasst()

System.out.println("Enter basic pay");

bp = get.nextDouble();

} void calculateasst()

da=(0.97*bp);

hra=(0.10*bp);

pf=(0.12*bp);

club=(0.1*bp);

gross=(bp+da+hra);

net=(gross-pf-club);

System.out.println("************************************************");

System.out.println("PAY SLIP FOR ASSISTANT PROFESSOR");


System.out.println("************************************************");

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

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

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

System.out.println("PF:Rs"+pf);

22 System.out.println("CLUB:Rs"+club);

System.out.println("GROSS PAY:Rs"+gross);

System.out.println("NET PAY:Rs"+net);

}}

class associateprofessor extends employee


{

double salary,bp,da,hra,pf,club,net,gross;

void getassociate()

System.out.println("Enter basic pay");

bp = get.nextDouble();

} void calculateassociate()

da=(0.97*bp);

hra=(0.10*bp);

pf=(0.12*bp);

club=(0.1*bp);

gross=(bp+da+hra);

net=(gross-pf-club);

System.out.println("************************************************");

System.out.println("PAY SLIP FOR ASSOCIATE PROFESSOR");


System.out.println("************************************************");

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

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

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

System.out.println("PF:Rs"+pf);

23 System.out.println("CLUB:Rs"+club);

System.out.println("GROSS PAY:Rs"+gross);

System.out.println("NET PAY:Rs"+net);

}}

class professor extends employee

double salary,bp,da,hra,pf,club,net,gross;

void getprofessor()

System.out.println("Enter basic pay");

bp = get.nextDouble();
}

void calculateprofessor()

da=(0.97*bp);

hra=(0.10*bp);

pf=(0.12*bp);

club=(0.1*bp);

gross=(bp+da+hra);

net=(gross-pf-club);

System.out.println("************************************************");

System.out.println("PAY SLIP FOR PROFESSOR");


System.out.println("************************************************");

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

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

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

24 System.out.println("PF:Rs"+pf);

System.out.println("CLUB:Rs"+club);

System.out.println("GROSS PAY:Rs"+gross);

System.out.println("NET PAY:Rs"+net);

}}

class salary

public static void main(String args[])

int choice,cont;

do

System.out.println("PAYROLL");

System.out.println(" 1.PROGRAMMER \t 2.ASSISTANT PROFESSOR \t 3.ASSOCIATE PROFESSOR \t


4.PROFESSOR "); Scanner c = new Scanner(System.in);

choice=c.nextInt(); switch(choice)

case 1:
{

programmer p=new programmer();

p.getdata(); p.getprogrammer();

p.display(); p.calculateprog();

break;

25

case 2: { asstprofessor asst=new asstprofessor();

asst.getdata();

asst.getasst();

asst.display();

asst.calculateasst();

break;

case 3:

associateprofessor asso=new associateprofessor();

asso.getdata();

asso.getassociate();

asso.display();

asso.calculateassociate();

break;

case 4:

professor prof=new professor();

prof.getdata();

prof.getprofessor();

prof.display();

prof.calculateprofessor();

break;

}
}

System.out.println("Do u want to continue 0 to quit and 1 to continue ");

cont=c.nextInt();

while(cont==1);

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.
EX.NO: 4
PROGRAM FOR STACK ADT USING INTERFACE
DATE:

AIM:

To design a java application to implement array implementation of stack using the concept of interface and exception
handling.

PROCEDURE :

1. Create the interface stackoperation with method declarations for push and pop.

2. 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
underflow condition .

3. 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 stack operation.

4. Create the object and invoke the method for push,pop,display based on the input from the user.

CODING:

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.println("item pushed"+stack[top]);

public void pop()

30 if(top<=top;i++)

System.out.println("element:"+stack[i]);

class teststack

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

int ch,c;

int i;

Astack s=new Astack();

DataInputStream in=new DataInputStream(System.in);

31 do

try { System.out.println("ARRAY STACK");

System.out.println("1.push 2.pop 3.display 4.exit");

System.out.println("enter ur choice:"); ch=Integer.parseInt(in.readLine()); switch(ch) { case 1:


System.out.println("enter the value to push:");

i=Integer.parseInt(in.readLine());

s.push(i);

break;

case 2:

s.pop();

break;

case 3:

System.out.println("the elements are:");


s.display();

break;

case 4:

break;

catch(IOException e)

System.out.println("io error");

System.out.println("Do u want to continue 0 to quit and 1 to continue ");

c=Integer.parseInt(in.readLine());

while(c==1);

}
OUTPUT:

RESULT:

Thus the java application for stack operations has been implemented and executed successfully.
EX.NO: 5
PROGRAM TO PERFORM STRING OPERATIONS USING ARRAYLIST
DATE:

AIM:

To write a java program to perform string operations using ArrayList for the following functions a. Append - add at
end b. Insert – add at particular index c. Search d. List all string starts with given letter.

PROCEDURE:

1.Create the class arraylistexample. Create the object for the arraylist class.

2.Display the options to the user for performing string handling .

3.Use the function add() to append the string at the end and to insert the string at the particular index.

4. The function sort () is used to sort the elements in the array list.

5. The function indexof() is used to search whether the element is in the array list or not.

6.The function startsWith () is used to find whether the element starts with the specified character.

7.The function remove() is used to remove the element from the arraylist.

8.The function size() is used to determine the number of elements in the array list

CODING:

import java.util.*;

import java.io.*;

public class arraylistexample

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

ArrayList obj = new ArrayList();

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

str=in.readLine();

obj.add(str);

break;

case 2:

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

str=in.readLine();

System.out.println("Specify the index/position to insert");

i=Integer.parseInt(in.readLine());

obj.add(i-1,str);

System.out.println("The array list has following elements:"+obj);

break;

case 3:

System.out.println("Enter the string to search ");

str=in.readLine();

j=obj.indexOf(str);

if(j==-1)

System.out.println("Element not found");

else

System.out.println("Index of:"+str+"is"+j);

break;

}
case 4:

System.out.println("Enter the character to List string that starts with specified character");

str=in.readLine();

}}

OUTPUT:

RESULT:

Thus the java program to perform string operations using Array List has been implemented and executed
successfully.
EX.NO: 6
PROGRAM TO CALCULATE AREA USING ABSTRACT CLASS
DATE:

AIM:

To write a java program to calculate the area of rectangle,circle and triangle using the concept of abstract class.

PROCEDURE:

1. Create an abstract class named shape that contains two integers and an empty method named printarea().

2. Provide three classes named rectangle, triangle and circle such that each one of the classes extends the class Shape.

3.Each of the inherited class from shape class should provide the implementation for the method printarea().

4.Get the input and calculate the area of rectangle,circle and triangle .

5. In the shapeclass , create the objects for the three inherited classes and invoke the methods and display the area
values of the different shapes.

CODING:

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

b=s.nextInt();

System.out.println("Base of triangle "+a +"height of triangle "+b);

area_tri=(0.5*a*b);

System.out.println("The area of triangle is:"+area_tri);

class circle extends shape

double area_circle; public void printarea()

Scanner s=new Scanner(System.in);

System.out.println("enter the radius of circle");

a=s.nextInt();

area_circle=(3.14*a*a);

System.out.println("Radius of circle"+a);

System.out.println("The area of circle is:"+area_circle);

public class shapeclass

public static void main(String[] args)

rectangle r=new rectangle();

r.printarea();

triangle t=new triangle();

t.printarea();
circle r1=new circle();

r1.printarea();

OUTPUT:

RESULT:

Thus a java program for calculate the area of rectangle,circle and triangle was implemented and executed
successfully.
EX.NO: 7
PROGRAM TO IMPLEMENT USER DEFINED EXCEPTION HANDLING
DATE:

AIM:

To write a java program to implement user defined exception handling.

PROCEDURE:

1.Create a class which extends Exception class.

2.Create a constructor which receives the string as argument.

3.Get the Amount as input from the user.

4.If the amount is negative , the exception will be generated.

5.Using the exception handling mechanism , the thrown exception is handled by the catch construct.

6.After the exception is handled , the string “invalid amount “ will be displayed.

7.If the amount is greater than 0 , the message “Amount Deposited “ will be displayed.

(a)CODING:

import java.util.Scanner;

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

OUTPUT:

(b)CODING:

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

OUTPUT:

RESULT:

Thus a java program to implement user defined exception handling has been implemented and executed
successfully.
EX.NO: 8
PROGRAM FOR DISPLAYING FILE INFORMATION
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.

PROCEDURE:

1.Create a class filedemo. Get the file name from the user .

2.Use the file functions and display the information about the file.

3.getName() displays the name of the file.

4.getPath() diplays the path name of the file.

5.getParent () -This method returns the pathname string of this abstract pathname’s parent, or null if this pathname
does not name a parent directory.

6.exists() – Checks whether the file exists or not.

7. canRead()-This method is basically a check if the file can be read.

8. canWrite()-verifies whether the application can write to the file.

9. isDirectory() – displays whether it is a directory or not.

10. isFile() – displays whether it is a file or not.

11. lastmodified() – displays the last modified information.

12.length()- displays the size of the file.

13. delete() – deletes the file

14.Invoke the predefined functions abd display the iformation about the file.

CODING:

import java.io.*;

import java.util.*;

class filedemo { public static void main(String args[])

String filename;

Scanner s=new Scanner(System.in);

System.out.println("Enter the file name ");

filename=s.nextLine(); File f1=new File(filename);

System.out.println("*****************");

System.out.println("FILE INFORMATION");
System.out.println("*****************");

System.out.println("NAME OF THE FILE "+f1.getName());

System.out.println("PATH OF THE FILE "+f1.getPath());

System.out.println("PARENT"+f1.getParent());

if(f1.exists())

System.out.println("THE FILE EXISTS ");

else

System.out.println("THE FILE DOES NOT ExISTS ");

if(f1.canRead())

System.out.println("THE FILE CAN BE READ ");

else System.out.println("THE FILE CANNOT BE READ ");

if(f1.canWrite())

53 System.out.println("WRITE OPERATION IS PERMITTED");

else

System.out.println("WRITE OPERATION IS NOT PERMITTED");

if(f1.isDirectory())

System.out.println("IT IS A DIRECTORY ");

Else

System.out.println("NOT A DIRECTORY");

if(f1.isFile()) System.out.println("IT IS A FILE ");

else System.out.println("NOT A FILE");

System.out.println("File last modified "+ f1.lastModified());

System.out.println("LENGTH OF THE FILE "+f1.length());

System.out.println("FILE DELETED "+f1.delete()); } }


OUTPUT:

RESULT:

Thus a java program to display file information has been implemented and executed successfully
EX.NO: 9
PROGRAM TO IMPLEMENT MULTITHREADED APPLICATION
DATE:

AIM:

To write a java program that implements a multi-threaded application .

PROCEDURE:

1.Create a class even which implements first thread that computes .the square of the number .

2. run() method implements the code to be executed when thread gets executed.

3.Create a class odd which implements second thread that computes the cube of the number.

4.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 given number .

5.The Multithreading is performed and the task switched between multiple threads.

6.The sleep () method makes the thread to suspend for the specified time.

CODING:

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 57

public void run()

int num = 0;

Random r = new Random();

try

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

num = r.nextInt(100);

System.out.println("Main Thread and Generated Number is " + num);

if (num % 2 == 0)

Thread t1 = new Thread(new even(num));

t1.start();

Else

Thread t2 = new Thread(new odd(num));

t2.start(); } Thread.sleep(1000);

System.out.println(" ");

catch (Exception ex)

System.out.println(ex.getMessage());

}
}

public class multithreadprog

public static void main(String[] args)

A a = new A(); a.start();

RESULT:

Thus a java program for multi-threaded application has been implemented and executed successfully
EX.NO: 10 PROGRAM TO FIND THE MAXIMUM VALUE FROM THE GIVEN TYPE OF
DATE: ELEMENTS USING GENERICS

AIM:

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

PROCEDURE:

1.Create a class Myclass to implement generic class and generic methods.

2.Get the set of the values belonging to specific data type.

3.Create the objects of the class to hold integer, character and double values.

4.Create the method to compare the values and find the maximum value stored in the array.

5.Invoke the method with integer, character or double values . The output will be displayed based on the data type
passed to the method.

CODING:

class MyClass>

T[] vals;

MyClass(T[] o) { vals = o;

public T min()

T v = vals[0];

for(int i=1; i < vals.length; i++)

if(vals[i].compareTo(v) < 0)

v = vals[i]; return v;

public T max()

T v = vals[0];

for(int i=1; i < vals.length;i++)

if(vals[i].compareTo(v) > 0) v = vals[i];

return v;

}
class gendemo

62 public static void main(String args[])

int i;

Integer inums[]={10,2,5,4,6,1};

Character chs[]={'v','p','s','a','n','h'};

Double d[]={20.2,45.4,71.6,88.3,54.6,10.4};

MyClass iob = new MyClass(inums);

MyClass cob = new MyClass(chs);

MyClassdob = new MyClass(d);

System.out.println("Max value in inums: " + iob.max());

System.out.println("Min value in inums: " + iob.min());

System.out.println("Max value in chs: " + cob.max());

System.out.println("Min value in chs: " + cob.min());

System.out.println("Max value in chs: " + dob.max());

System.out.println("Min value in chs: " + dob.min());

}}

RESULT :

Thus a java program to find the maximum value from the given type of elements has been implemented
using generics and executed successfully
EX.NO: 11 PROGRAM TO DESIGN A CALCULATOR USING EVENT DRIVEN
DATE: PROGRAMMING PARADIGM OF JAVA

AIM:

To design a calculator using event driven programming paradigm of Java with the following options a) Decimal
Manipulations b) Scientific Manipulations.

PROCEDURE:

1. import the swing packages and awt packages.

2. Create the class scientific calculator that implements action listener.

3. Create the container and add controls for digits , scientific calculations and decimal Manipulations.

4. The different layouts can be used to lay the controls.

5.When the user presses the control , the event is generated and handled .

6. The corresponding decimal , numeric and scientific calculations are performed.

CODING:

import java.awt.*;

import javax.swing.*;

import java.awt.event.*;

import javax.swing.event.*;

public class ScientificCalculator extends JFrame implements ActionListener

JTextField tfield;

double temp, temp1, result, a;

static double m1, m2;

int k = 1, x = 0, y = 0, z = 0;

char ch;

JButton b1, b2, b3, b4, b5, b6, b7, b8, b9, zero, clr, pow2, pow3, exp, fac, plus, min, div, log, rec, mul, eq, addSub,
dot, mr, mc, mp, mm, sqrt, sin, cos, tan;

Container cont;

JPanel textPanel, buttonpanel; ScientificCalculator() { cont = getContentPane(); cont.setLayout(new


BorderLayout());

JPanel textpanel = new JPanel();

tfield = new JTextField(25);

tfield.setHorizontalAlignment(SwingConstants.RIGHT);

tfield.addKeyListener(new KeyAdapter()
{

public void keyTyped(KeyEvent keyevent)

66 char c = keyevent.getKeyChar();

if (c >= '0' && c <= '9')

else { keyevent.consume(); } } });

textpanel.add(tfield);

buttonpanel = new JPanel();

buttonpanel.setLayout(new GridLayout(8, 4, 2, 2));

boolean t = true; mr = new JButton("MR");

buttonpanel.add(mr);

mr.addActionListener(this);

mc = new JButton("MC");

buttonpanel.add(mc);

mc.addActionListener(this);

mp = new JButton("M+");

buttonpanel.add(mp);

mp.addActionListener(this);

mm = new JButton("M-");

buttonpanel.add(mm);

mm.addActionListener(this);

b1 = new JButton("1");

buttonpanel.add(b1);

67 b1.addActionListener(this);

b2 = new JButton("2");

buttonpanel.add(b2);

b2.addActionListener(this);

b3 = new JButton("3");

buttonpanel.add(b3);

b3.addActionListener(this);
b4 = new JButton("4");

buttonpanel.add(b4);

b4.addActionListener(this);

b5 = new JButton("5");

buttonpanel.add(b5);

b5.addActionListener(this);

b6 = new JButton("6");

buttonpanel.add(b6);

b6.addActionListener(this);

b7 = new JButton("7");

buttonpanel.add(b7);

b7.addActionListener(this);

b8 = new JButton("8");

buttonpanel.add(b8);

b8.addActionListener(this);

b9 = new JButton("9");

buttonpanel.add(b9);

b9.addActionListener(this);

zero = new JButton("0");

buttonpanel.add(zero);

68 zero.addActionListener(this);

plus = new JButton("+");

buttonpanel.add(plus);

plus.addActionListener(this);

min = new JButton(" -");

buttonpanel.add(min);

min.addActionListener(this);

mul = new JButton("*");

buttonpanel.add(mul);

mul.addActionListener(this);

div = new JButton("/");

div.addActionListener(this);
buttonpanel.add(div);

addSub = new JButton("+/ -");

buttonpanel.add(addSub);

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

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

buttonpanel.add(tan);

tan.addActionListener(this);

pow2 = new JButton("x^2");

buttonpanel.add(pow2);

pow2.addActionListener(this);

pow3 = new JButton("x^3");


buttonpanel.add(pow3);

pow3.addActionListener(this);

exp = new JButton("Exp");

exp.addActionListener(this);

buttonpanel.add(exp);

fac = new JButton("n!");

fac.addActionListener(this);

buttonpanel.add(fac);

clr = new JButton("AC");

buttonpanel.add(clr);

70 clr.addActionListener(this);

cont.add("Center", buttonpanel);

cont.add("North", textpanel);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

} public void actionPerformed(ActionEvent e)

String s = e.getActionCommand();

if (s.equals("1"))

{ if (z == 0) {

tfield.setText(tfield.getText() + "1");

Else

tfield.setText("");

tfield.setText(tfield.getText() + "1"); z = 0;

if (s.equals("2"))

{ if (z == 0)

tfield.setText(tfield.getText() + "2");

}
else

71 tfield.setText("");

tfield.setText(tfield.getText() + "2"); z = 0;

if (s.equals("3")) { if (z == 0)

tfield.setText(tfield.getText() + "3");

Else

{ tfield.setText("");

tfield.setText(tfield.getText() + "3");

z = 0; }}

if (s.equals("4"))

{ if (z == 0)

{ tfield.setText(tfield.getText() + "4"); }

else

{ tfield.setText("");

tfield.setText(tfield.getText() + "4"); z = 0; }}

72 if (s.equals("5"))

{ if (z == 0)

tfield.setText(tfield.getText() + "5");

else

tfield.setText("");

tfield.setText(tfield.getText() + "5");

z = 0; }}

if (s.equals("6"))

{
if (z == 0)

tfield.setText(tfield.getText() + "6");

Else

{ tfield.setText("");

tfield.setText(tfield.getText() + "6"); z = 0; }}

if (s.equals("7"))

if (z == 0)

tfield.setText(tfield.getText() + "7");

else 73 { tfield.setText("");

tfield.setText(tfield.getText() + "7");

z = 0;

}}

if (s.equals("8"))

{ if (z == 0)

tfield.setText(tfield.getText() + "8");

Else

tfield.setText("");

tfield.setText(tfield.getText() + "8");

z = 0; }} if (s.equals("9"))

{ if (z == 0)

{ tfield.setText(tfield.getText() + "9");

else { tfield.setText("");

tfield.setText(tfield.getText() + "9");
z = 0; } 74 } if (s.equals("0"))

{ if (z == 0)

tfield.setText(tfield.getText() + "0");

else

tfield.setText("");

tfield.setText(tfield.getText() + "0");

z = 0;

}}

if (s.equals("AC")) { tfield.setText("");

x = 0; y = 0; z = 0;

if (s.equals("log"))

if (tfield.getText().equals(""))

tfield.setText("");

else {

a = Math.log(Double.parseDouble(tfield.getText()));

75 tfield.setText("");

tfield.setText(tfield.getText() + a);

}}

if (s.equals("1/x")) { if (tfield.getText().equals(""))

tfield.setText("");

Else

a = 1 / Double.parseDouble(tfield.getText());
tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("Exp"))

if (tfield.getText().equals(""))

tfield.setText("");

else

a = Math.exp(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("x^2"))

76 if (tfield.getText().equals(""))

tfield.setText("");

} else

a = Math.pow(Double.parseDouble(tfield.getText()), 2); tfield.setText(""); tfield.setText(tfield.getText() + a); } }

if (s.equals("x^3"))

if (tfield.getText().equals(""))

tfield.setText("");

else
{

a = Math.pow(Double.parseDouble(tfield.getText()), 3);

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("+/-")) { if (x == 0)

tfield.setText("-" + tfield.getText()); x = 1;

else 77 { tfield.setText(tfield.getText());

if (s.equals(".")) { if (y == 0)

tfield.setText(tfield.getText() + ".");

y = 1;

else { tfield.setText(tfield.getText());

if (s.equals("+"))

if (tfield.getText().equals(""))

tfield.setText(""); temp = 0; ch = '+';

Else

temp = Double.parseDouble(tfield.getText());

tfield.setText("");

ch = '+';
78 y = 0;

x = 0;

tfield.requestFocus();

if (s.equals("-"))

if (tfield.getText().equals(""))

tfield.setText("");

temp = 0; ch = '-';

else

x = 0; y = 0; temp = Double.parseDouble(tfield.getText());

tfield.setText("");

ch = '-';

tfield.requestFocus();

if (s.equals("/"))

if (tfield.getText().equals(""))

tfield.setText("");

79 temp = 1;

ch = '/';

Else

x = 0; y = 0; temp = Double.parseDouble(tfield.getText());

ch = '/'; tfield.setText("");
}

tfield.requestFocus();

if (s.equals("*"))

if (tfield.getText().equals(""))

tfield.setText("");

temp = 1; ch = '*';

} else

{ x = 0; y = 0;

temp = Double.parseDouble(tfield.getText());

ch = '*'; tfield.setText("");

80 }

tfield.requestFocus();

if (s.equals("MC"))

m1 = 0; tfield.setText(""); }

if (s.equals("MR")) { tfield.setText("");

tfield.setText(tfield.getText() + m1);

} if (s.equals("M+"))

{ if (k == 1) { m1 = Double.parseDouble(tfield.getText()); k++;

else

{ m1 += Double.parseDouble(tfield.getText()); tfield.setText("" + m1);

if (s.equals("M-")) { 81 if (k == 1) { m1 = Double.parseDouble(tfield.getText()); k++; }

else

m1 -= Double.parseDouble(tfield.getText());
tfield.setText("" + m1); } }

if (s.equals("Sqrt")) {

if (tfield.getText().equals(""))

tfield.setText("");

else

a = Math.sqrt(Double.parseDouble(tfield.getText()));

tfield.setText("");

field.setText(tfield.getText() + a);

}}

if (s.equals("SIN"))

if (tfield.getText().equals(""))

82 tfield.setText("");

else

a = Math.sin(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

if (s.equals("COS")) { if (tfield.getText().equals(""))

{ tfield.setText(""); }

else { a = Math.cos(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

}}

if (s.equals("TAN"))
{ if (tfield.getText().equals(""))

{ tfield.setText(""); }

else 83

a = Math.tan(Double.parseDouble(tfield.getText()));

tfield.setText("");

tfield.setText(tfield.getText() + a);

}}

if (s.equals("="))

{ if (tfield.getText().equals(""))

tfield.setText(""); }

else

temp1 = Double.parseDouble(tfield.getText());

switch (ch)

case '+': result = temp + temp1;

break;

case '-':

result = temp - temp1;

break;

case '/':

result = temp / temp1;

break;

case '*':

84 result = temp * temp1;

break;

tfield.setText("");

tfield.setText(tfield.getText() + result); z = 1;

}
}

if (s.equals("n!"))

if (tfield.getText().equals(""))

tfield.setText("");

Else

a = fact(Double.parseDouble(tfield.getText())); tfield.setText("");

tfield.setText(tfield.getText() + a);

tfield.requestFocus();

double fact(double x)

int er = 0; if (x < 0) 85

{ er = 20;

return 0;

double i, s = 1;

for (i = 2; i <= x; i += 1.0) s *= i; return s;

public static void main(String args[])

try

UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");

catch (Exception e)

{}
ScientificCalculator f = new ScientificCalculator();

f.setTitle("ScientificCalculator");

f.pack();

f.setVisible(true);

}}

RESULT:

Thus the java programs for scientific calculator has been implemented and executed successfully

You might also like