0% found this document useful (0 votes)
168 views68 pages

Java Lab Manual1 PDF

This document outlines a laboratory manual for an Object Oriented Programming with Java course. It includes 12 programming assignments that cover Java fundamentals, OOP concepts like inheritance and polymorphism, collections, exceptions, file I/O, applets, and Swing. It aims to help students learn to design, develop, debug and document Java projects using an IDE. Assessment includes continuous internal evaluation based on assignments and a semester exam testing students' skills in applying OOP concepts to develop robust programs.

Uploaded by

Gangarathna Mani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
168 views68 pages

Java Lab Manual1 PDF

This document outlines a laboratory manual for an Object Oriented Programming with Java course. It includes 12 programming assignments that cover Java fundamentals, OOP concepts like inheritance and polymorphism, collections, exceptions, file I/O, applets, and Swing. It aims to help students learn to design, develop, debug and document Java projects using an IDE. Assessment includes continuous internal evaluation based on assignments and a semester exam testing students' skills in applying OOP concepts to develop robust programs.

Uploaded by

Gangarathna Mani
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 68

SEA COLLEGE OF ENGINEERING AND TECHNOLOGY

Ekta Nagar, Basavanapura(K.R.Puram),

Virgonagar Post, Bangalore-560049, INDIA

Email: seaeduinfo@seaedu.ac.in Website: www.seaedu.ac.in

DEPARTMENT OF CSE/ISE/IOT/AIML (3rd Semester)

Object Oriented Programming with JAVA Laboratory Manual

Course Code: 21CSL35

ACADEMIC YEAR 2022-23 [ODD]

NAME OF THE STUDENT : ………………………………………………………………………………

BRANCH : ………………………………………………………………………………

UNIVERSITY SEAT NO. : ………………………………………………………………………………

SEMESTER & SECTION : SEMESTER : SECTION :

BATCH :………………………………………………………………………………

SEACET Dept. of CSE/ISE/AIML/IOT


OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY
Course Code 21CSL35 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 0:0:2:0 SEE Marks 50
Total Hours of Pedagogy 24 Total Marks 100
Credits 1 Exam Hours 03
Course Objectives:

CLO 1. Demonstrate the use of Eclipse/Netbeans IDE to create Java Applications.


CLO 2. Using java programming to develop programs for solving real-world problems.
CLO 3.Reinforce the understanding of basic object-oriented programming concepts.
Note: two hours tutorial is suggested for each laboratory sessions.
Prerequisite
 Students should be familiarized about java installation and setting the java
environment.
 Usage of IDEs like Eclipse/Netbeans should be introduced.

Sl. No. PART A – List of problems for which student should develop program and execute in the
Laboratory
Aim: Introduce the java fundamentals, data types, operators in java
1
Program: Write a java program that prints all real solutions to the quadratic equation
ax2+bx+c=0. Read in a, b, c and use the quadratic formula.
Aim: Demonstrating creation of java classes, objects, constructors, declaration andinitialization
of variables.

Program: Create a Java class called Student with the following details as variables within it.USN
Name Branch
2
Phone
Write a Java program to create n Student objects and print the USN, Name, Branch, andPhone
of these objects with suitable headings.

Aim: Discuss the various Decision-making statements, loop constructs in java

3 Program:
A. Write a program to check prime number
B. Write a program for Arithmetic calculator using switch case menu
Aim: Demonstrate the core object-oriented concept of Inheritance, polymorphism

4 Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend thisclass
by writing three subclasses namely Teaching (domain, publications), Technical (skills), and
Contract (period). Write a Java program to read and display at least 3 staff
objects of all three categories.
Aim: Introduce concepts of method overloading, constructor overloading, overriding.
5
Program: Write a java program demonstrating Method overloading and Constructor
overloading.

SEACET Dept. of CSE/ISE/AIML/IOT


Aim: Introduce the concept of Abstraction, packages.

6 Program: Develop a java application to implement currency converter (Dollar to INR, EURO
to INR, Yen to INR and vice versa), distance converter (meter to KM, miles to KM and viceversa),
time converter (hours to minutes, seconds and vice versa) using packages.
7 Aim: Introduction to abstract classes, abstract methods, and Interface in java

Program: Write a program to generate the resume. Create 2 Java classes Teacher (data:
personal information, qualification, experience, achievements) and Student (data: personal
information, result, discipline) which implements the java interface Resume with the
method biodata().
Aim: Demonstrate creation of threads using Thread class and Runnable interface, multi-
threaded programming.
8 Program: Write a Java program that implements a multi-thread application that has three
threads. First thread generates a random integer for every 1 second; second thread
computes the square of the number and prints; third thread will print the value of cube ofthe
number.
Aim: Introduce java Collections.

9 Program: Write a program to perform string operations using ArrayList. Write functions for
the following a. Append - add at end b. Insert – add at particular index c. Search d. List allstring
starts with given letter.
Aim: Exception handling in java, introduction to throwable class, throw, throws, finally.
10
Program: Write a Java program to read two integers a and b. Compute a/b and print, whenb
is not zero. Raise an exception when b is equal to zero.
Aim: Introduce File operations in java.

11 Program:
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
Aim: Introduce java Applet, awt, swings.

12 Programs:
Develop an applet that displays a simple message in center of the screen.Develop a
simple calculator using Swings.
PART B – Practical Based Learning
A problem statement for each batch is to be generated in consultation with the co-examiner
01 and student should develop an algorithm, program and execute the program for the given
problem with appropriate outputs.

SEACET Dept. of CSE/ISE/AIML/IOT


Course Outcome (Course Skill Set)
At the end of the course the student will be able to:

CO 1. Use Eclipse/NetBeans IDE to design, develop, debug Java Projects.


CO 2. Analyze the necessity for Object Oriented Programming paradigm over structuredprogramming and
become familiar with the fundamental concepts in OOP.
CO 3. Demonstrate the ability to design and develop java programs, analyze, and interpret object-oriented
data and document results.
CO 4. Apply the concepts of multiprogramming, exception/event handling, abstraction to developrobust
programs.
CO 5. Develop user friendly applications using File I/O and GUI concepts.
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%.
The minimum passing mark for the CIE is 40% of the maximum marks (20 marks). A student shall be
deemed to have satisfied the academic requirements and earned the credits allotted to each course. The
student has to secure not less than 35% (18 Marks out of 50) in the semester-end examination (SEE).
Continuous Internal Evaluation (CIE):
CIE marks for the practical course is 50 Marks.
The split-up of CIE marks for record/ journal and test are in the ratio 60:40.

SEACET Dept. of CSE/ISE/AIML/IOT


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program -1
Write a java program that prints all real solutions to the quadratic equationax2+bx+c=0. Read in a, b, c and
use the quadratic formula.

…................................................................................................................
import java.util.Scanner;

class s1
{
public static void main (String args[])
{
System.out.println("Enter the cofficients a,b,c of quadratic equation ax2 + bx
+ c = 0 and where a not 0 ");
Scanner sc = new Scanner(System.in);
double a=sc.nextInt();
if (a==0)
{
System.out.println("a can not be zero");
}
else
{
double b=sc.nextInt();
double c=sc.nextInt();
double z=b*b-4*a*c;
EquationCheck ob=new EquationCheck();

if (z<0)
{
System.out.println("There are no real solutions");
}
else if(z==0)
{
System.out.println("The solutions are real and equal");
ob.check(a,b,c);
ob.display();
}
else
{
System.out.println("The solutions are real and distinct");
ob.check(a,b,c);
ob.display();
}
}
}
}

class EquationCheck
{
double a;
double b;

SEACET Dept. of CSE/ISE/AIML/IOT Page 1


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

double c;
double x1;
double x2;
void check(double a,double b,double c)
{
this.a=a;
this.b=b;
this.c=c;
double z=Math.pow( b*b-4*a*c , 0.5 );
x1=(-b-z)/(2*a);
x2=(-b+z)/(2*a);
}

void display()
{
System.out.println(x1);
System.out.println(x2);
}
}

::::::::::::::::::::::::::::::::::::::::::
Output:-

Enter the cofficients a,b,c of quadratic equation ax2 + bx + c = 0 and where a not 0

0 1 2
a can not be zero

…………………………………………………………………………………………………………………………………………………………………………………………………………………………….
Enter the coefficients a ,b ,c of quadratic equation ax2 + bx + c = 0 and where a not 0

1 4 4
The solutions are real and equal
-2.0
-2.0
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………

Enter the coefficients a, b, c of quadratic equation ax2 + bx + c = 0 and where a not 0


1 5 3
The solutions are real and distinct
-4.302775637731995
-0.6972243622680054
…………………………………………………………………………………………………………………………………………………………………………………………………………………………………

Enter the coefficients a, b, c of quadratic equation ax2 + bx + c = 0 and where a not 0

3 2 1
There are no real solution

SEACET Dept. of CSE/ISE/AIML/IOT Page 2


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program -2:-
Program: Create a Java class called Student with the following details as
variables within it. USN, Name ,Branch, Phone.
Write a Java program to create n Student objects and print the USN, Name,
Branch, and Phone of these objects with suitable headings .
…................................................................................................................................................................
import java.util.Scanner;

public class student {


String USN;
String Name;
String branch;
int phone;
void insertRecord(String reg,String name, String brnch,int ph) {
USN=reg;
Name=name;
branch=brnch;
phone=ph;
}

void displayRecord()
{
System.out.println(USN+" "+Name+" "+branch+" "+phone);
}

public static void main(String args[])


{
student s[]=new student [100];
Scanner sc=new Scanner(System.in);
System.out.println("enter the number of students");
int n=sc.nextInt();
for(int i=0;i<n;i++)
s[i]=new student();
for(int j=0;j<n;j++)
{ System.out.println("enter the usn,name,branch,phone");
String USN=sc.next();
String Name=sc.next();
String branch=sc.next();
int phone=sc.nextInt();
s[j].insertRecord(USN,Name,branch,phone);

for( int m=0;m<n;m++)


{
s[m].displayRecord();
}}}

SEACET Dept. of CSE/ISE/AIML/IOT Page 3


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Output:-
enter the number of students
3
enter the usn,name,branch,phone

101 RAVI CSE 12345


enter the usn,name,branch,phone

102 RAM IT 45678


enter the usn,name,branch,phone

103 JHON IOT 789006


101 RAVI CSE 12345
102 RAM IT 45678
103 JHON IOT 789006

:::::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 4


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 3:-
A. Write a program to check prime number
B. Write a program for Arithmetic calculator using switch case menu
….........................................................................................................
A. Write a program to check prime number:-
import java.util.Scanner;

public class CodesCracker


{
public static void main(String[] args)
{
int num, i, count=0;
Scanner s = new Scanner(System.in);

System.out.print("Enter a Number: ");


num = s.nextInt();

for(i=2; i<num; i++)


{
if(num%i == 0)
{
count++;
break;
}
}

if(count==0)
System.out.println("\nIt is a Prime Number.");
else
System.out.println("\nIt is not a Prime Number.");
}
}

OUTPUT:-
Enter a Number: 13

It is a Prime Number.
……………………………………………
Enter a Number: 29

It is a Prime Number.
.......................
Enter a Number: 100

SEACET Dept. of CSE/ISE/AIML/IOT Page 5


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

It is not a Prime Number

..............................
Enter a Number: 58

It is not a Prime Number.

::::::::::::::::::::::::::::::::::::::::::
B. Write a program for Arithmetic calculator using switch case menu
import java.util.Scanner;
public class ab39_CalculatorUsingSwitch {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter value of 1st number :");
int a = sc.nextInt();

System.out.println("Enter value of 2nd number :");


int b = sc.nextInt();

System.out.println("Select operation");
System.out.println("Addition-a: Subtraction-s: Multiplication-m: Division-d: ");
char ch = sc.next().charAt(0);
switch(ch) {
case 'a' :
System.out.println("Sum of the given two numbers: "+(a+b));
break;
case 's' :
System.out.println("Difference between the two numbers: "+(a-b));
break;
case 'm' :
System.out.println("Product of the two numbers: "+(a*b));
break;
case 'd' :
System.out.println("Result of the division: "+(a/b));
break;

SEACET Dept. of CSE/ISE/AIML/IOT Page 6


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

default :
System.out.println("Invalid grade");
}
}
}

………………………………………………………..
Output:-
Enter value of 1st number :
20
Enter value of 2nd number :
30
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
a
Sum of the given two numbers: 50
……………………………………………………………………………………………..
Enter value of 1st number ::
100
Enter value of 2nd number ::
20
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
s
Difference between the two numbers: 80
………………………………………………………………………………………………………
Enter value of 1st number ::
300
Enter value of 2nd number ::
2
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
m
Product of the two numbers: 600
…………………………………………………………………………………………………
Enter value of 1st number ::
400
Enter value of 2nd number ::
8
Select operation
Addition-a: Subtraction-s: Multiplication-m: Division-d:
d
Result of the division: 50

::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 7


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 4:-
Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend
this class by writing three subclasses namely Teaching (domain, publications),
Technical (skills), and Contract (period). Write a Java program to read and display at
least 3 staff objects of all three categories
class Staff
{
private int StaffId;
private String Name;
private String Phone;
private long Salary;

public Staff(int staffId,String name,String phone,long salary)


{
StaffId = staffId;
Name = name;
Phone = phone;
Salary = salary;
}
public void Display()
{
System.out.print("\t"+StaffId+"\t"+Name+"\t\t"+Phone+"\t\t"+Salary);
}
}

class Teaching extends Staff


{
private String Domain;
private int Publications;

public Teaching(int staffId, String name, String phone,long salary, String domain,
int publications)
{
super(staffId, name, phone, salary);
Domain = domain;
Publications = publications;
}
public void Display()
{
super.Display();
System.out.print("\t\t"+Domain+"\t\t"+Publications+"\t\t"+"--"+"\t"+"--");
}
}

class Technical extends Staff


{
private String Skills;
public Technical(int staffId, String name, String phone,long salary, String
skills)

SEACET Dept. of CSE/ISE/AIML/IOT Page 8


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

{
super(staffId, name, phone, salary);
Skills = skills;
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t"+Skills+"\t"+"--");
}
}

class Contract extends Staff


{
private int Period;
public Contract(int staffId, String name, String phone, long salary, int
period)
{
super(staffId, name, phone, salary);
this.Period = period;
}
public void Display()
{
super.Display();
System.out.print("\t\t--"+"\t\t"+"--"+"\t\t"+"--"+"\t"+Period);
}
}

public class lab2


{
public static void main(String[] args)
{
Staff staff[]=new Staff[3];
staff[0]=new Teaching(0001,"Narendra","271173",90000,"CSE",3);
staff[1]=new Technical(0002,"Aroha","271172",2000,"Server Admin");
staff[2]=new Contract(0003,"Rahul","271174",9000,3);
System.out.println("Staff
ID\tName\t\tPhone\t\tSalary\t\tDomain\tPublication\tSkills\t\tPeriod");
for(int i=0;i<3;i++)
{
staff[i].Display();
System.out.println();
}
}
}
:::::::::::::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 9


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Output:-

Staff ID Name Phone Salary Domain Publication Skills Period


1 Narendra 271173 90000 CSE 3 -- --
2 Aroha 271172 2000 -- -- Server Admin --
3 Rahul 271174 9000 -- -- -- 3

………………………………………………………………………………………………………………………………………………………..

SEACET Dept. of CSE/ISE/AIML/IOT Page 10


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 5:-
Write a java program demonstrating Method overloading and Constructor
overloading.

// Method overloading in Java

public class Sum {

// Overloaded sum(). This sum takes two int parameters


public int sum(int x, int y) { return (x + y); }

// Overloaded sum(). This sum takes three int parameters


public int sum(int x, int y, int z)
{
return (x + y + z);
}

// Overloaded sum(). This sum takes two double


// parameters
public double sum(double x, double y)
{
return (x + y);
}

// Driver code
public static void main(String args[])
{
Sum s = new Sum();
System.out.println(s.sum(10, 20));

SEACET Dept. of CSE/ISE/AIML/IOT Page 11


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

System.out.println(s.sum(10, 20, 30));


System.out.println(s.sum(10.5, 20.5));
}
}
……………………………………………………………………………………
Output:-
30
60
31.0

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
Program:-5(ii)
(Constructor Overloading):-
// Java program to illustrate
// Constructor Overloading
class Box
{
double width, height, depth;

// constructor used when all dimensions


// specified
Box(double w, double h, double d)
{
width = w;
height = h;
depth = d;
}

// constructor used when no dimensions


// specified
Box()

SEACET Dept. of CSE/ISE/AIML/IOT Page 12


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

{
width = height = depth = 0;
}

// constructor used when cube is created


Box(double len)
{
width = height = depth = len;
}

// compute and return volume


double volume()
{
return width * height * depth;
}
}

// Driver code
public class Test
{
public static void main(String args[])
{
// create boxes using the various
// constructors
Box mybox1 = new Box(10, 20, 15);
Box mybox2 = new Box();
Box mycube = new Box(7);

double vol;

// get volume of first box


SEACET Dept. of CSE/ISE/AIML/IOT Page 13
OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

vol = mybox1.volume();
System.out.println(" Volume of mybox1 is " + vol);

// get volume of second box


vol = mybox2.volume();
System.out.println(" Volume of mybox2 is " + vol);

// get volume of cube


vol = mycube.volume();
System.out.println(" Volume of mycube is " + vol);
}
}
………………………………………………………………………………….
Output:-
Volume of mybox1 is 3000.0
Volume of mybox2 is 0.0
Volume of mycube is 343.0

:::::::::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 14


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 6(Currency converter):-


Develop a java application to implement currency converter (Dollar to INR, EURO
to INR, Yen to INR and vice versa), distance converter (meter to KM, miles to KM and
vice versa), time converter (hours to minutes, seconds and vice versa) using packages.

currencycoversion.java:-
package project2;

import java.util.*;

public class currencyconversion

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

SEACET Dept. of CSE/ISE/AIML/IOT Page 15


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

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

SEACET Dept. of CSE/ISE/AIML/IOT Page 16


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

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

............................................................
distanceconversion.java:-

package project2;
//package distanceconversion;

import java.util.*;

public class distanceconversion

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

public void mtokm()

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


SEACET Dept. of CSE/ISE/AIML/IOT Page 17
OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

m=sc.nextDouble();

km=(m/1000);

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

public void milestokm()

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

miles=sc.nextDouble();

km=(miles*1.60934);

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

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

…............................................

timeconversion.java
package project2;

import java.util.*;

public class timeconversion

SEACET Dept. of CSE/ISE/AIML/IOT Page 18


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

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

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

SEACET Dept. of CSE/ISE/AIML/IOT Page 19


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

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

}}

............................................

converter.java:-
package project2;

import java.util.*;

import java.io.*;

class converter

public static void main(String args[])

Scanner s=new Scanner(System.in);

SEACET Dept. of CSE/ISE/AIML/IOT Page 20


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

int choice,ch;

currencyconversion c=new currencyconversion();

distanceconversion d=new distanceconversion();

timeconversion t=new timeconversion();

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

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

choice=s.nextInt();

switch(choice)

case 1:

SEACET Dept. of CSE/ISE/AIML/IOT Page 21


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

c.dollartorupee();

break;

case 2:

c.rupeetodollar();

break;

case 3:

c.eurotorupee();

break;

case 4:

c.rupeetoeuro();

break;

case 5:

{c.yentorupee();

break;}

case 6 :

c.rupeetoyen();

SEACET Dept. of CSE/ISE/AIML/IOT Page 22


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

break;

case 7 :

d.mtokm();

break;

case 8 :

d.kmtom();

break;

case 9 :

d.milestokm();

break;

case 10 :

d.kmtomiles();

break;

case 11 :

t.hourstominutes();

SEACET Dept. of CSE/ISE/AIML/IOT Page 23


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

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

…..............................

Output:-
1.dollar to rupee
2.rupee to dollar

SEACET Dept. of CSE/ISE/AIML/IOT Page 24


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…......................................

Enter your choice

Enter dollars to convert into Rupees:

Dollar =1.0equal to INR= 67.0

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

SEACET Dept. of CSE/ISE/AIML/IOT Page 25


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…......................................

Enter your choice

Enter Rupee to convert into Dollars:

100

Rupee =100.0equal to Dollars= 1.492537313432836

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

SEACET Dept. of CSE/ISE/AIML/IOT Page 26


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

….................................

Enter your choice

Enter dollars to convert into Rupees:

20

Dollar =20.0equal to INR= 1340.0

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

SEACET Dept. of CSE/ISE/AIML/IOT Page 27


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

….............................

Enter your choice

Enter euro to convert into Rupees:

Euro =1.0equal to INR= 79.5

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

SEACET Dept. of CSE/ISE/AIML/IOT Page 28


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

…..................................

Enter your choice

Enter Rupees to convert into Euro:

500

Rupee =500.0equal to Euro= 6.289308176100629

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

….................................

Enter your choice

SEACET Dept. of CSE/ISE/AIML/IOT Page 29


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Enter yen to convert into Rupees:

YEN=1.0equal to INR= 0.61

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

….................................

Enter your choice

Enter Rupees to convert into Yen:

100

INR=100.0equal to YEN 163.9344262295082

SEACET Dept. of CSE/ISE/AIML/IOT Page 30


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…...................................

Enter your choice

Enter in meter 1000

1000.0m equal to 1.0 kilometers

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

SEACET Dept. of CSE/ISE/AIML/IOT Page 31


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…...............................

Enter your choice

Enter in km 5

5.0 km equal to 5000.0 meter

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

SEACET Dept. of CSE/ISE/AIML/IOT Page 32


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…..............................

Enter your choice

Enter in miles1

1.0 miles equal to 1.60934 kilometers

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

SEACET Dept. of CSE/ISE/AIML/IOT Page 33


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…..............................

Enter your choice

10

Enter in km 100

100.0 km equal to 62.137100000000004 miles

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

SEACET Dept. of CSE/ISE/AIML/IOT Page 34


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

14.Minutes to Hours

…........................................

Enter your choice

11

enter the no of hours

Minutes: 60

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…........................................

Enter your choice

SEACET Dept. of CSE/ISE/AIML/IOT Page 35


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

12

enter the no of hours

Minutes: 10800

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

….......................................

Enter your choice

13

Enter the number of seconds: 600

Hours: 0

SEACET Dept. of CSE/ISE/AIML/IOT Page 36


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Minutes: 10

Seconds: 0

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

…...................................

Enter your choice

13

Enter the number of seconds: 60

Hours: 0

Minutes: 1

Seconds: 0

SEACET Dept. of CSE/ISE/AIML/IOT Page 37


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Enter 0 to quit and 1 to continue

1.dollar to rupee

2.rupee to dollar

3.Euro to rupee

4..rupee to Euro

5.Yen to rupee

6.Rupee to Yen

7.Meter to kilometer

8.kilometer to meter

9.Miles to kilometer

10.kilometer to miles

11.Hours to Minutes

12.Hours to Seconds

13.Seconds to Hours

14.Minutes to Hours

….................................

Enter ur choice

14

Enter the number of minutes: 600

Hours: 10

Minutes: 0

Enter 0 to quit and 1 to continue

:::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 38


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 7:-
Introduction to abstract classes, abstract methods, and Interface in java Program: Write a
program to generate the resume. Create 2 Java classes Teacher (data: personal information,
qualification, experience, achievements) and Student (data: personal information, result,
discipline) which implements the java interface Resume with the method biodata().

Resume.java

package lab;

public interface Resume

void biodata();

……………………………………………………………………………………………………………….

Student.java

package lab;

public class Student implements Resume

String personalinformation;

String result ;

String discipline;

Student(String personalinformation,String result,String discipline)

this.personalinformation=personalinformation;

this.result=result;

SEACET Dept. of CSE/ISE/AIML/IOT Page 39


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

this.discipline=discipline;

@Override

public void biodata() {

// TODO Auto-generated method stub

System.out.println("Name is :"+personalinformation);

System.out.println("Result is :"+result);

System.out.println("Discipline :"+discipline);

}}

…………………………………………………………………………………………………………………………..

Teacher.java

package lab;

public class Teacher implements Resume{

String personalinformation;

String qualification ;

String experience;

String achievement;

Teacher(String personalinformation,String qualification,String experience,String achievement)

this.personalinformation=personalinformation;

this.qualification=qualification;

SEACET Dept. of CSE/ISE/AIML/IOT Page 40


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

this.experience=experience;

this.achievement=achievement;

@Override

public void biodata() {

// TODO Auto-generated method stub

System.out.println("Name is :"+personalinformation);

System.out.println("Qualification is :"+qualification);

System.out.println("Experience is :"+experience);

System.out.println("Achievement:"+achievement);

}}

…………………………………………………………………………………………………………………………….

Program7.java

package lab;

public class Program7 {

public static void main(String[] args) {

// TODO Auto-generated method stub

Student s1=new Student("Arman","Distinction","CSE");

Student s2=new Student("Saburi","First Class","ISE");

Student s3=new Student("Radha","First Class","ISE");

Teacher t1=new Teacher("Sudha","M.Tech","3 years","Best teacher Award");

Teacher t2=new Teacher("Radhika","Phd","5 years","vtu gold medalist");

SEACET Dept. of CSE/ISE/AIML/IOT Page 41


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

System.out.println("Student Details are");

s1.biodata();

System.out.println();

s2.biodata();

System.out.println();

s3.biodata();

System.out.println();

System.out.println("Teacher Details are");

t1.biodata();

System.out.println();

t2.biodata();

…………………………………………………………………………………………………………………………………………

Output:-
Student Details are
Name is :Arman
Result is :Distinction
Discipline :CSE

Name is :Saburi
Result is :First Class
Discipline :ISE

Name is :Radha
Result is :First Class
Discipline :ISE

Teacher Details are

SEACET Dept. of CSE/ISE/AIML/IOT Page 42


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Name is :Sudha
Qualification is :M.Tech
Experience is :3 years
Achievement:Best teacher Award

Name is :Radhika
Qualification is :Phd
Experience is :5 years
Achievement:vtu gold medalist

SEACET Dept. of CSE/ISE/AIML/IOT Page 43


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 8:-
Program: Write a Java program that implements a multi-thread application that has
three threads. First thread generates a random integer for every 1 second; second
thread computes the square of the number and prints; third thread will print the value
of cube of the number.

import java.util.Random;

class Square extends Thread


{

int x; Square(int n)
{

x = n;
}

public void run()


{

int sqr = x * x;
System.out.println("Square of " + x + " = " + sqr );
}
}

class Cube extends Thread


{

int x; Cube(int n)
{

x = n;
}

public void run()


{

int cub = x * x * x;
System.out.println("Cube of " + x + " = " + cub );
}
}

class Number extends Thread


{

SEACET Dept. of CSE/ISE/AIML/IOT Page 44


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

public void run()


{

Random random = new Random();


for(int i =0; i<10; i++)
{

int randomInteger = random.nextInt(100);


System.out.println("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
s.start();
Cube c = new Cube(randomInteger);
c.start();
try {
Thread.sleep(1000);

} catch (InterruptedException ex)


{
System.out.println(ex);
}
}
}
}
public class Lab3b {
public static void main(String args[])
{

Number n = new Number(); n.start();


}
}

…...........................................................
Output:-
Random Integer generated : 99
Square of 99 = 9801
Cube of 99 = 970299
Random Integer generated : 23
Square of 23 = 529
Cube of 23 = 12167
Random Integer generated : 1
Square of 1 = 1
Cube of 1 = 1
Random Integer generated : 90
Square of 90 = 8100
Cube of 90 = 729000
Random Integer generated : 92
Square of 92 = 8464
Cube of 92 = 778688
Random Integer generated : 64
Square of 64 = 4096
Cube of 64 = 262144
SEACET Dept. of CSE/ISE/AIML/IOT Page 45
OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Random Integer generated : 0


Square of 0 = 0
Cube of 0 = 0
Random Integer generated : 87
Square of 87 = 7569
Cube of 87 = 658503
Random Integer generated : 1
Cube of 1 = 1
Square of 1 = 1
Random Integer generated : 96
Square of 96 = 9216
Cube of 96 = 884736

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 46


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Program 9:-
Program: Write a program to perform string operations using Array List. Write
functions for the following a. Append - add at end b. Insert – add at particular index
c. Search d. List all string starts with given letter.

package p1;
import java.util.*;

import java.io.*;

public class arraylistexample

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

ArrayList<String> obj = new ArrayList<String>();

DataInputStream in=new DataInputStream(System.in);

int c,ch;

int i,j;

String str,str1;

do

System.out.println("STRING MANIPULATION");

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

System.out.println(" 1. Append at end \t");

System.out.println("2.Insert at particular index");

System.out.println("\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());

SEACET Dept. of CSE/ISE/AIML/IOT Page 47


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

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

SEACET Dept. of CSE/ISE/AIML/IOT Page 48


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

break;

case 4:

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


character");

str=in.readLine();

for(i=0;i<(obj.size()-1);i++)

str1=obj.get(i);

if(str1.startsWith(str))

System.out.println(str1);

break;

case 5:

System.out.println("Size of the list "+obj.size());

break;

case 6:

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

str=in.readLine();

if(obj.remove(str))

SEACET Dept. of CSE/ISE/AIML/IOT Page 49


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

System.out.println("Element Removed"+str);

else

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

break;

case 7:

Collections.sort(obj);

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

break;

case 8:

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

break;

System.out.println("enter 0 to break and 1 to continue");

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

}while(ch==1);

…......................................................................

SEACET Dept. of CSE/ISE/AIML/IOT Page 50


OBJECT ORIENTED PROGRAMMING WITH JAVA LAB MANUAL

Output:-
STRING MANIPULATION
******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the string

apple

enter 0 to break and 1 to continue

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5.Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the string

grapes

Specify the index/position to insert

SEACET Dept. of CSE/ISE/AIML/IOT Page 51


Object Oriented Programming with JAVA Lab Manual (21CSL35)

The array list has following elements:[apple, grapes]

enter 0 to break and 1 to continue

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the string

guava

enter 0 to break and 1 to continue

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5.Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the string

banana

enter 0 to break and 1 to continue

SEACET Dept. of CSE/ISE/AIML/IOT 52


Object Oriented Programming with JAVA Lab Manual (21CSL35)

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the string to search

guava

Index of: guava is 2

enter 0 to break and 1 to continue

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the character to List string that starts with specified character

app

apple

enter 0 to break and 1 to continue

SEACET Dept. of CSE/ISE/AIML/IOT 53


1

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

Size of the list 4

enter 0 to break and 1 to continue

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

Enter the element to remove

banana

Element Removed banana

enter 0 to break and 1 to continue

STRING MANIPULATION

SEACET Dept. of CSE/ISE/AIML/IOT Page 54


******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

The array list has following elements:[apple, grapes, guava]

enter 0 to break and 1 to continue

STRING MANIPULATION

******************************

1. Append at end

2.Insert at particular index

3.Search

4. List string that starting with letter

5. Size 6.Remove 7.Sort 8.Display

Enter the choice

The array list has following elements:[apple, grapes, guava]

enter 0 to break and 1 to continue

SEACET Dept. of CSE/ISE/AIML/IOT Page 55


Program 10:-
Program: Write a Java program to read two integers a and b. Compute a/b and print,
when b is not zero. Raise an exception when b is equal to zero.

import java.util.Scanner;
class Division

public static void main(String[] args)

int a,b,result;

Scanner input =new Scanner(System.in);

System.out.println("Input two integers");

a=input.nextInt();

b=input.nextInt();

try

result=a/b;

System.out.println("Result="+result);

catch(ArithmeticException e)

System.out.println("Divisor is Zero");

System.out.println("exception caught: Divide by zero error"+e);

}}}

…...........................................................

SEACET Dept. of CSE/ISE/AIML/IOT Page 56


Output:-
Input two integers
120 4

Result=30

…................................

Input two integers

5 50

Result=0

….................................

Input two integers


30 5

Result=6

…..................................

Input two integers

25 0

Divisor is Zero

exception caught: Divide by zero errorjava.lang.ArithmeticException: / by zero

::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 57


Program 11:-
Write a java program that reads a file name from the user, displays information about
whether the file exists, whether the file is readable, or writable, the type of file and
the length of the file in bytes.
import java.io.*;
class filedemo
{
public static void p(String str)
{
System.out.println(str);
}
public static void analyze(String s) {
File f=new File(s);
if(f.exists())
{
p(f.getName()+" is a file");
p(f.canRead()?" is readable":" is not readable");
p(f.canWrite()?" is writable":" is not writable");
p("Filesize:"+f.length()+" bytes");
p("File last modified:"+f.lastModified());
}
if(f.isDirectory())
{
p(f.getName()+" is directory");
p("List of files");
String dir[]=f.list();
for(int i=0;i<dir.length;i++)
p(dir[i]);
}
}

}
public class FileDetails
{
public static void main(String rr[])throws IOException
{
filedemo fd=new filedemo();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter the file name:");
String s=br.readLine();
fd.analyze(s);
}
}
………………………………………………………………………………………………………………………………………………………………………………………………………………..

SEACET Dept. of CSE/ISE/AIML/IOT Page 58


Output:-(First create a file):-
Enter the file name:
AA
AA is a file
is readable
is writable
Filesize:19 bytes
File last modified:1667031945714
………………………………………………………………………………………………………………………………………………………………………………………………………………..
Enter the file name:
Welcome
Welcome is a file
is readable
is writable
Filesize:365 bytes
File last modified:1667022283981
::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

SEACET Dept. of CSE/ISE/AIML/IOT Page 59


Program12:-
Develop an applet that displays a simple message in center of the screen. Develop a
simple calculator using Swings.
//Develop an applet that displays a simple message in center of the screen.
import java.awt.*;
import java.applet.Applet;
/*<applet code=Welcome.class height=800 width=400>
</applet> */
public class Welcome extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.yellow);

g.setColor(Color.green);
g.drawString("HELLO",50,40);

g.setColor(Color.red);
g.drawString("Welcome to Applet",60,70);
}}
.............................................................
Output:-

::::::::::::::::::::::::::::::::::::::::::::::::::::
// Java program to create a simple calculator
// with basic +, -, /, * using java swing elements

SEACET Dept. of CSE/ISE/AIML/IOT Page 60


import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
class calculator1 extends JFrame implements ActionListener {
// create a frame
static JFrame f;

// create a textfield
static JTextField l;

// store operator and operands


String s0, s1, s2;

// default constructor
calculator1()
{
s0 = s1 = s2 = "";
}

// main function
public static void main(String args[])
{
// create a frame
f = new JFrame("calculator");

try {
// set look and feel
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}
catch (Exception e) {
System.err.println(e.getMessage());
}

// create a object of class


calculator1 c = new calculator1();

// create a textfield
l = new JTextField(16);

// set the textfield to non editable


l.setEditable(false);

// create number buttons and some operators


JButton b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, ba, bs, bd, bm, be, beq, beq1;

// create number buttons


b0 = new JButton("0");
b1 = new JButton("1");
b2 = new JButton("2");
b3 = new JButton("3");
b4 = new JButton("4");
b5 = new JButton("5");
b6 = new JButton("6");

SEACET Dept. of CSE/ISE/AIML/IOT Page 61


b7 = new JButton("7");
b8 = new JButton("8");
b9 = new JButton("9");

// equals button
beq1 = new JButton("=");

// create operator buttons


ba = new JButton("+");
bs = new JButton("-");
bd = new JButton("/");
bm = new JButton("*");
beq = new JButton("C");

// create . button
be = new JButton(".");

// create a panel
JPanel p = new JPanel();

// add action listeners


bm.addActionListener(c);
bd.addActionListener(c);
bs.addActionListener(c);
ba.addActionListener(c);
b9.addActionListener(c);
b8.addActionListener(c);
b7.addActionListener(c);
b6.addActionListener(c);
b5.addActionListener(c);
b4.addActionListener(c);
b3.addActionListener(c);
b2.addActionListener(c);
b1.addActionListener(c);
b0.addActionListener(c);
be.addActionListener(c);
beq.addActionListener(c);
beq1.addActionListener(c);

// add elements to panel


p.add(l);
p.add(ba);
p.add(b1);
p.add(b2);
p.add(b3);
p.add(bs);
p.add(b4);
p.add(b5);
p.add(b6);
p.add(bm);
p.add(b7);
p.add(b8);
p.add(b9);
p.add(bd);

SEACET Dept. of CSE/ISE/AIML/IOT Page 62


p.add(be);
p.add(b0);
p.add(beq);
p.add(beq1);

// set Background of panel


p.setBackground(Color.blue);

// add panel to frame


f.add(p);

f.setSize(200, 220);
f.show();
}
public void actionPerformed(ActionEvent e)
{
String s = e.getActionCommand();

// if the value is a number


if ((s.charAt(0) >= '0' && s.charAt(0) <= '9') || s.charAt(0) == '.') {
// if operand is present then add to second no
if (!s1.equals(""))
s2 = s2 + s;
else
s0 = s0 + s;

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == 'C') {
// clear the one letter
s0 = s1 = s2 = "";

// set the value of text


l.setText(s0 + s1 + s2);
}
else if (s.charAt(0) == '=') {

double te;

// store the value in 1st


if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// set the value of text


l.setText(s0 + s1 + s2 + "=" + te);

SEACET Dept. of CSE/ISE/AIML/IOT Page 63


// convert it to string
s0 = Double.toString(te);

s1 = s2 = "";
}
else {
// if there was no operand
if (s1.equals("") || s2.equals(""))
s1 = s;
// else evaluate
else {
double te;

// store the value in 1st


if (s1.equals("+"))
te = (Double.parseDouble(s0) + Double.parseDouble(s2));
else if (s1.equals("-"))
te = (Double.parseDouble(s0) - Double.parseDouble(s2));
else if (s1.equals("/"))
te = (Double.parseDouble(s0) / Double.parseDouble(s2));
else
te = (Double.parseDouble(s0) * Double.parseDouble(s2));

// convert it to string
s0 = Double.toString(te);

// place the operator


s1 = s;

// make the operand blank


s2 = "";
}

// set the value of text


l.setText(s0 + s1 + s2);
}}}
……………………………………………………………………………………………………………………………………
Output:-

SEACET Dept. of CSE/ISE/AIML/IOT Page 64

You might also like