0% found this document useful (0 votes)
7 views42 pages

Java Lab Manual

The document outlines the syllabus and laboratory objectives for the Object Oriented Programming with Java Laboratory course at Acharya Institute of Technology, affiliated with Visvesvaraya Technological University. It includes details on course structure, assessment methods, and a list of programming experiments designed to teach Java programming concepts and object-oriented principles. The document also specifies evaluation criteria for Continuous Internal Evaluation (CIE) and Semester End Evaluation (SEE).

Uploaded by

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

Java Lab Manual

The document outlines the syllabus and laboratory objectives for the Object Oriented Programming with Java Laboratory course at Acharya Institute of Technology, affiliated with Visvesvaraya Technological University. It includes details on course structure, assessment methods, and a list of programming experiments designed to teach Java programming concepts and object-oriented principles. The document also specifies evaluation criteria for Continuous Internal Evaluation (CIE) and Semester End Evaluation (SEE).

Uploaded by

samarthv.21.becs
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

VISVESVARAYA TECHNOLOGICAL UNIVERSITY

BELAGAVI

OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY

(21CSL35)
(As per Visvesvaraya Technological University Syllabus)

Compiled By:

Prof. Mary M Dsouza Prof. Mithuna H R


Assistant Professor, Dept. of ISE Assistant Professor, Dept. of ISE

DEPARTMENT OF INFORMATION SCIENCE AND ENGINEERING


ACHARYA INSTITUTE OF TECHNOLOGY
Affiliated to VTU, Belagavi, Approved by AICTE, New Delhi and Govt. of Karnataka),
Acharya Dr. Sarvepalli Radhakrishnan Road, Bangalore-560107.
Ph : 91-080-28396011, 23723466, 28376431
URL: [Link]

2022-23
Disclaimer
The information contained in this document is the proprietary and exclusive property of Acharya
Institutes except as otherwise indicated. No part of this document, in whole or in part, may be reproduced,
stored, transmitted, or used for course material development purposes without the prior written
permission of Acharya Institutes.

The information contained in this document is subject to change without notice. The information in this
document is provided for informational purposes only.

Trademark

Edition: 2022-23

Document Owner
The primary contact for questions regarding this document is:

Author(s): 1. Prof. Mary M Dsouza


2. Prof. Mithuna H R

Department: Information Science & Engineering


Contact Email(s): Mary2462@[Link]
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

Department of Information Science and Engineering


Acharya Institute of Technology
Acharya [Link] Radhakrishnan Road Bangalore-560107
OBJECT ORIENTED PROGRAMMING WITH JAVA LABORATORY
[As per National Education Policy (NEP) scheme - Effective from the academic year 2021-22]

Semester : III I.A. Marks : 50


Course Code : 21CSL35 Exam Marks : 50
Total Hours / Week (L : T : P) : 0:0:2 Exam Hours : 03

Laboratory Objectives: This laboratory (21CSL35) will enable students to

 Learn and acquire the art Java Programming


 Configure Eclipse to run the applications
 Understand and implement Object Oriented concepts in JAVA
 Create GUI using swings

Descriptions (if any):


 Installation procedure of the Java software must be demonstratedcarried out in groups.
 Students should use the latest version of Eclipse to execute these programs.

© Copyright Acharya Institutes Page 1


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

List of Experiments

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 [Link] 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
javaenvironment.
 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
theLaboratory
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 [Link]
Name
2 Branc
h
Phone
Write a Java program to create n Student objects and print the USN, Name, Branch, and Phoneof
these objects with suitable headings.
Aim: Discuss the various Decision-making statements, loop constructs in java

Program:
3
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

Design a super class called Staff with details as StaffId, Name, Phone, Salary. Extend this class by
4
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 Constructoroverloading.
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 vice versa),time
converter (hours to minutes, seconds and vice versa) using packages.
Aim: Introduction to abstract classes, abstract methods, and Interface in java

7 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
© Copyright Acharya Institutes
biodata(). Page 2
Aim: Demonstrate creation of threads using Thread class and Runnable interface, multi-
threadedprogramming.
8
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
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.
Aim: Introduce java Collections.

9 Program: Write a program to perform string operations using ArrayList. Write functions for
thefollowing a. Append - add at end b. Insert – add at particular index c. Search d. List all
string 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, when b is
notzero. 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
thefile 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
calculatorusing Swings.
PART B – Practical Based Learning
A problem statement for each batch is to be generated in consultation with the co-examiner and
01 student should develop an algorithm, program and execute the program for the
givenproblem with appropriate outputs.

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 structured
programmingandbecome familiar with the fundamental concepts in OOP.
CO 3. Demonstrate the ability to design and develop java programs, analyze, and interpret object-
orienteddata and document results.
CO 4. Apply the concepts of multiprogramming, exception/event handling, abstraction to
developrobustprograms.
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 shallbe 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.
 Each experiment to be evaluated for conduction with observation sheet and record write-up.
Rubrics for the evaluation of the journal/write-up for hardware/software experiments designed by the
faculty who is handling the laboratory session and is made known to students at the beginning of the
practical session.
 Record should contain all the specified experiments in the syllabus and each experiment write-up
will be evaluated for 10 marks.
 Total marks scored by the students are scaled downed to 30 marks (60% of maximum marks).
 Weightage to be given for neatness and submission of record/write-up on time.
 Department shall conduct 02 tests for 100 marks, the first test shall be conducted after the 8th
weekof the semester and the second test shall be conducted after the 14th week of the semester.
 In each test, test write-up, conduction of experiment, acceptable result, and procedural
© Copyright
knowledge Acharya Institutes
will carry a weightage of 60% and the rest 40% for viva-voce. Page 3
 The suitable rubrics can be designed to evaluate each student’s performance and learning
ability. Rubrics suggested in Annexure-II of Regulation book
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore
 The average of 02 tests is scaled down to 20 marks (40% of the maximum marks).
The Sum of scaled-down marks scored in the report write-up/journal and average marks of two tests is the
total CIE marks scored by the student.

Semester End Evaluation (SEE):


 SEE marks for the practical course is 50 Marks.
 SEE shall be conducted jointly by the two examiners of the same institute, examiners are
appointed by the University
 All laboratory experiments are to be included for practical examination.
 (Rubrics) Breakup of marks and the instructions printed on the cover page of the answer
scriptto be strictly adhered to by the examiners. OR based on the course requirement
evaluation rubrics shall be decided jointly by examiners.
 Students can pick one question (experiment) from the questions lot prepared by the internal
/external examiners jointly.
 Evaluation of test write-up/ conduction procedure and result/viva will be conducted jointly by
examiners.
 General rubrics suggested for SEE are mentioned here, writeup-20%, Conduction procedure
and result in -60%, Viva-voce 20% of maximum marks. SEE for practical shall be evaluated
for 100 marks and scored marks shall be scaled down to 50 marks (however, based on course
type, rubrics shall be decided by the examiners)
 Students can pick one experiment from the questions lot of PART A with equal choice to all the
students in a batch. For PART B examiners should frame a question for each batch, student
should develop an algorithm, program, execute and demonstrate the results with appropriate
output for the given problem.
 Weightage of marks for PART A is 80% and for PART B is 20%. General rubrics suggested to
be followed for part A and part B.
 Change of experiment is allowed only once and Marks allotted to the procedure part to
bemade zero (Not allowed for Part B).
 The duration of SEE is 03 hours
 Rubrics suggested in Annexure-II of Regulation book
Suggested Learning Resources:
1. E Balagurusamy, Programming with Java, Graw Hill, 6th Edition, 2019.
2. Herbert Schildt, C: Java the Complete Reference, McGraw Hill, 11th Edition, 2020

© Copyright Acharya Institutes Page 4


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :1
1. Write a java program that prints all real solutions to the quadratic equation ax2+bx+c=0. Read in a,
b, c and use the quadratic formula.

Aim: Introduce the java fundamentals, data types, operators in java

PROGRAM :
import [Link];
public class Quadratic
{
public static void main(String[] args)
{

int a, b, c; // coefficients
double root1, root2;
[Link]("Enter the coefficients");
Scanner in=new Scanner([Link]);
a = [Link]();
b = [Link]();
c = [Link]();

// calculate the determinant (b2 - 4ac)


double d = b * b - 4 * a * c;
[Link]("Determinant="+d);
if (d > 0) // check if determinant is greater than 0
{

// two real and distinct roots


root1 = (-b + [Link](d)) / (2 * a);
root2 = (-b - [Link](d)) / (2 * a);
[Link]("The roots are real and distinct");
[Link]("root1 = %.2f and root2 = %.2f", root1, root2);
}

else if (d == 0) // check if determinant is equal to 0


{

// two real and equal roots


// determinant is equal to 0
// so -b + 0 == -b
root1 = root2 = -b / (2 * a);
[Link]("The roots are real and equal");
[Link]("root1 = root2 = %.2f;", root1);
}

© Copyright Acharya Institutes Page 5


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

else // if determinant is less than zero


{

// roots are complex number and distinct


double real = -b / (2 * a);
double imaginary = [Link](-d) / (2 * a);
[Link]("The roots are imaginary");
[Link]("root1 = %.2f+%.2fi", real, imaginary);
[Link]("\nroot2 = %.2f-%.2fi", real, imaginary);
}
}
}

OUT PUT:

© Copyright Acharya Institutes Page 6


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :2

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

Aim: Demonstrating creation of java classes, objects, constructors, declaration andinitialization of


variables.

PROGRAM :
import [Link].*;
public class Student
{
String usn,name,branch;
long phone;

void insertStudent(String reg,String nm, String br,long ph)


{
usn=reg;
name=nm;
branch=br;
phone=ph;
}

void displayStudent()
{
[Link]("**********************");
[Link]("USN= "+usn);
[Link]("NAME= "+name);
[Link]("BRANCH= "+branch);
[Link]("PHONE NUMBER= "+phone);
[Link]("**********************");
}

public static void main(String args[])


{
Student st[]=new Student [100];
Scanner ip=new Scanner([Link]);
[Link]("Enter the number of students");
int n=[Link]();

© Copyright Acharya Institutes Page 7


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

for(int i=0;i<n;i++)
st[i]=new Student();
for(int j=0;j<n;j++)
{
[Link]("Enter the Usn,Name,Branch,Phone Number");
String usn=[Link]();
String name=[Link]();
String branch=[Link]();
long phone=[Link]();
st[j].insertStudent(usn,name,branch,phone);

}
for( int m=0;m<n;m++)
{
[Link]("Student %d details are\n",m+1);
st[m].displayStudent();
}

}
}

OUTPUT:

© Copyright Acharya Institutes Page 8


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :3

3. A. Write a program to check prime number

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

PROGRAM :3A
import [Link];
class Prime
{
public static void main(String args[])
{
int i,n,flag=0;
[Link]("Enter the number");
Scanner inp=new Scanner([Link]);
n=[Link]();
for(i=2;i<n;i++)
{
if(n%i==0)
{
flag=1;
break;
}
}
if(flag==1)
[Link]("The given number is Not a Prime");
else
[Link]("The given number is Prime");
}
}

OUTPUT:

© Copyright Acharya Institutes Page 9


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :3B

3. [Link] a program for Arithmetic calculator using switch case menu

import [Link].*;
class Switch
{
public static void main(String[] args)
{
Scanner inp = new Scanner([Link]);
[Link]("Enter the Operator (+,-,*,/) : ");
char operator = [Link]().charAt(0);
[Link]("Enter the First Operand : ");
double first = [Link]();
[Link]("Enter the Second Operand : ");
double second = [Link]();
double result = 0;
switch(operator)
{
case '+':
result = first + second;
[Link]("The Result is : "+first+" "+operator+" "+second+" = "+result);
break;
case '-':
result = first - second;
[Link]("The Result is : \n "+first+" "+operator+" "+second+" = "+result);
break;
case '*':
result = first * second;
[Link]("The Result is : "+first+" "+operator+" "+second+" = "+result);
break;
case '/':
result = first / second;
[Link]("The Result is : \n "+first+" "+operator+" "+second+" = "+result);
break;
default :
[Link]("Invalid Operator");
break;
}
}
}

© Copyright Acharya Institutes Page 10


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

OUTPUT:

© Copyright Acharya Institutes Page 11


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :4

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

Aim: Demonstrate the core object-oriented concept of Inheritance, polymorphism

PROGRAM:

import [Link];
class Staff
{
String staffId;
String name;
long phone;
float salary;
public void accept()
{
Scanner scanner = new Scanner([Link]);
[Link]("Enter Staff Id: ");
staffId = [Link]();
[Link]("Enter Name: ");
name = [Link]();
[Link]("Enter Phone: ");
phone = [Link]();
[Link]("Enter Salary: ");
salary = [Link]();

}
public void display()
{
[Link]("Staff Id: " + staffId);
[Link]("Name: " + name);
[Link]("Phone: " + phone);
[Link]("Salary: " + salary);
}
}
class Teaching extends Staff
{
String domain;
int n;
public void accept()
{
[Link]();
Scanner scanner = new Scanner([Link]);
© Copyright Acharya Institutes Page 12
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

[Link]("Enter Domain: ");


domain = [Link]();
[Link]("Enter Number of Publications: ");
n = [Link]();
[Link]("\n");
}
public void display()
{
[Link]();
[Link]("Domain: " + domain);
[Link]("Publications:"+n);
[Link]("\n");
}
}
class Technical extends Staff
{
String skill;
public void accept()
{
[Link]();
Scanner scanner = new Scanner([Link]);
[Link]("Enter technical Skills: ");
skill = [Link]();
[Link]("\n");
}
public void display()
{
[Link]();
[Link]("Technical Skills: " + skill);
[Link]("\n");
}

}
class Contract extends Staff
{
int period;
public void accept()
{
[Link]();
Scanner scanner = new Scanner([Link]);
[Link]("Enter Period: ");
period = [Link]();
[Link]("\n");
}

© Copyright Acharya Institutes Page 13


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

public void display()


{
[Link]();
[Link]("Contract Period: " + period);
}
}
class Four
{
public static void main(String[] args)
{
Teaching teaching = new Teaching();
[Link]("Enter the details of Teaching Staff");
[Link]();

Technical technical = new Technical();


[Link]("Enter the details of Technical Staff");
[Link]();

Contract contract = new Contract();


[Link]("Enter the details of Contract Staff");
[Link]();

[Link]("The details of Teaching Staff");


[Link]();
[Link]("The details of Technical Staff");
[Link]();
[Link]("The details of Contract Staff");
[Link]();
}
}

© Copyright Acharya Institutes Page 14


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

OUTPUT :

© Copyright Acharya Institutes Page 15


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :5

5. Write a java program demonstrating Method overloading and Constructor overloading.

Aim: Introduce concepts of method overloading, constructor overloading.

PROGRAM:5A

Demonstrating Method overloading


class MOverloading
{
//adding two integer numbers
int add(int a, int b)
{
int sum = a+b;
return sum;
}
//adding three integer numbers
int add(int a, int b, int c)
{
int sum = a+b+c;
return sum;
}
float add(float a, float b)
{
float sum = a+b;
return sum;
}
}
class MOverload
{
public static void main(String args[])
{
MOverloading obj = new MOverloading();
int s1=[Link](10, 20);
int s2=[Link](10, 20, 30);
float s3=[Link](2.2f,2.2f);
[Link]("Method Overload Sum1="+s1);
[Link]("Method Overload Sum2="+s2);
[Link]("Method Overload Sum3="+s3);
}
}

© Copyright Acharya Institutes Page 16


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

OUTPUT:

© Copyright Acharya Institutes Page 17


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM:5B

Constructor Overloading
public class Constructor
{
int id;
String name;
Constructor()
{
[Link]("This is Default constructor");
[Link]("Student Id : "+id + "\nStudent Name : "+name);
}
Constructor(int i, String n)
{
[Link]("This is Parameterized Constructor:");
id = i;
name = n;
[Link]("Student Id : "+id + "\nStudent Name : "+name);
}
public static void main(String[] args)
{
Constructor s = new Constructor();
Constructor student = new Constructor(10, "David");
}
}

OUTPUT:

© Copyright Acharya Institutes Page 18


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :6

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

Aim: Introduce the concept of Abstraction, packages.

PROGRAM:

[Link]

package cc;
import [Link].*;
public class CurrencyC
{
double inr,usd;
double euro,yen;
Scanner in=new Scanner([Link]);
public void dollartorupee()
{
[Link]("Enter dollars to nvert into Rupees:");
usd=[Link]();
inr=usd*81.83;
[Link]("Dollar ="+usd+" equal to INR="+inr);
[Link]("\n");
}
public void rupeetodollar()
{
[Link]("Enter Rupee to convert into Dollars:");
inr=[Link]();
usd=inr/81.83;
[Link]("Rupee ="+inr+"equal to Dollars="+usd);
}
public void eurotorupee()
{
[Link]("Enter Euro to convert into Rupees:");
euro=[Link]();
inr=euro*79.06;
[Link]("Euro ="+euro+" equal to INR="+inr);
[Link]("\n");
}

© Copyright Acharya Institutes Page 19


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

public void rupeetoeuro()


{
[Link]("Enter Rupees to convert into Euro:");
inr=[Link]();
euro=(inr/79.06);
[Link]("Rupee ="+inr +"equal to Euro="+euro);
[Link]("\n");
}
public void yentoruppe()
{
[Link]("Enter Yen to convert into Rupees:");
yen=[Link]();
inr=yen*0.57;
[Link]("Yen ="+yen+" equal to INR="+inr);
[Link]("\n");
}
public void ruppetoyen()
{
[Link]("Enter Rupees to convert into Yen:");
inr=[Link]();
yen=(inr/0.57);
[Link]("INR="+inr +"equal to YEN"+yen);
[Link]("\n");
}
}

© Copyright Acharya Institutes Page 20


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

[Link]
package dc;
import [Link].*;
public class DistanceC
{
double km,m,miles;
Scanner in=new Scanner([Link]);
public void mtokm()
{
[Link]("Enter the distance in meter");
m=[Link]();
km=(m/1000);
[Link](m+"m" +" is equal to "+km+"km");
[Link]("\n");
}
public void kmtom()
{
[Link]("Enter the distance in Kilometer");
km=[Link]();
m=km*1000;
[Link](km+"km" +" is equal to "+m+"m");
[Link]("\n");
}
public void milestokm()
{
[Link]("Enter the distance in miles");
miles=[Link]();
km=(miles*1.60934);
[Link](miles+"miles" +" is equal to "+km+"km");
[Link]("\n");
}

© Copyright Acharya Institutes Page 21


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

public void kmtomiles()


{
[Link]("Enter the distance in km");
km=[Link]();
miles=(km*0.621371);
[Link](km+"km" +" is equal to "+miles+"miles");
}
}

[Link]
package tc;
import [Link].*;
public class TimeC
{
int hours,seconds,minutes;
Scanner in = new Scanner([Link]);
public void hourstominutes()
{
[Link]("Enter the no of Hours to convert into minutes");
hours=[Link]();
minutes=(hours*60);
[Link]("Minutes: " + minutes);
}
public void minutestohours()
{
[Link]("Enter the no of Minutes to convert into Hours");
minutes=[Link]();
hours=minutes/60;
[Link]("Hours: " + hours);
}
public void hourstoseconds()
{
[Link]("Enter the no of Hours to convert into Seconds");
hours=[Link]();
seconds=(hours*3600);
[Link]("Seconds: " + seconds);
}
public void secondstohours()
{
[Link]("Enter the no of Seconds to convert into Hours");
seconds=[Link]();
hours=seconds/3600;
[Link](seconds+"seconds"+ " is equal to "+hours+"hour");
}
}

© Copyright Acharya Institutes Page 22


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

Main Class
import cc.*;
import dc.*;
import tc.*;
public class Main
{
public static void main(String args[])
{
CurrencyC obj=new CurrencyC();
DistanceC obj1=new DistanceC();
TimeC obj2=new TimeC();

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
[Link]();

[Link]();
[Link]();
}
}

© Copyright Acharya Institutes Page 23


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

OUTPUT:
Enter dollars to convert into Rupees:1
Dollar =1.0 equal to INR=81.83

Enter Rupee to convert into Dollars: 80


Rupee =80.0equal to Dollars=0.977636563607479

Enter the distance in meter :1000


1000.0m is equal to 1.0km

Enter the distance in Kilometre :1


1.0km is equal to 1000.0m

Enter the no of Hours to convert into minutes: 1


Minutes: 60

Enter the no of Minutes to convert into Hours: 60


Hours: 1

© Copyright Acharya Institutes Page 24


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :7

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

Aim: Introduction to abstract classes, abstract methods, and Interface in java

PROGRAM:

interface Resume
{
void biodata();
}
class Teacher implements Resume
{
String name,qualification,achievements;
float experience;
public void biodata()
{
name="Imran Ulla Khan";
qualification="[Link]";
achievements="Q1 publication";
experience=14.8f;
[Link]("Teacher Resume");
[Link]("Name : " +name);
[Link]("Qualification : "+qualification);
[Link]("Achievements : "+achievements);
[Link]("Experience : "+experience);
}
}
class Student implements Resume
{
String name,discipline;
float result;
public void biodata()
{
name="Rahul Sharma";
result=9.8f;
discipline="Computer Science and Engineering";
[Link]("");
[Link]("Student Resume");
[Link]("Name : " +name);
[Link]("Result : "+result+" cgpa");
[Link]("Discipline : "+discipline);
}
}
© Copyright Acharya Institutes Page 25
public class InterfaceP
{
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

public static void main(String[] args)


{
Teacher obj1=new Teacher();
[Link]();

Student obj2=new Student();


[Link]();
}
}

OUTPUT:

© Copyright Acharya Institutes Page 26


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :8

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

Aim: Demonstrate creation of threads using Thread class and Runnable interface, multi-
threaded programming.

PROGRAM :

import [Link];
class Square extends Thread
{
int x;
Square(int n)
{
x = n;
}
public void run()
{
int sqr = x * x;
[Link]("Square of " + x + " = " + sqr );
}
}

class Cube extends Thread


{
int x;
Cube(int n)
{
x = n;
}
public void run()
{
int cub = x * x * x;
[Link]("Cube of " + x + " = " + cub );
}
}

© Copyright Acharya Institutes Page 27


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

class Rnumber extends Thread


{
public void run()
{
Random random = new Random();
for(int i =0; i<5; i++)
{
int randomInteger = [Link](10);
[Link]("Random Integer generated : " + randomInteger);
Square s = new Square(randomInteger);
[Link]();
Cube c = new Cube(randomInteger);
[Link]();
try
{
[Link](1000);
}
catch (InterruptedException ex)
{
[Link](ex);
}
}
}
}

public class ThreadP


{
public static void main(String[] args)
{
Rnumber n = new Rnumber();
[Link]();

}
}

© Copyright Acharya Institutes Page 28


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

OUTPUT:

© Copyright Acharya Institutes Page 29


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :9

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

Aim: Introduce java Collections.

import [Link].*;
public class ArrayL
{
ArrayList<String> list=new ArrayList<String>(); //Creating arraylist
public void arraydisplay()
{
[Link]("CSE");//Adding object in arraylist
[Link]("ISE");
[Link]("ME");
[Link]("ArrayList element are");
[Link](list);
[Link]("");
}
public void appendatend()
{
[Link]("Enter the element to append at end");
Scanner scob1=new Scanner([Link]);
String ele=[Link]();
[Link](ele);
[Link](list);
[Link]("");
}
public void insertatpos()
{
[Link]("Enter the position and element to insert");
Scanner scob1=new Scanner([Link]);
int posind=[Link]();
String ele=[Link]();
[Link](posind,ele);
[Link](list);
[Link]("");
}

© Copyright Acharya Institutes Page 30


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

public void searchele()


{
[Link]("Enter the Array element to search");
Scanner scobj=new Scanner([Link]);
String arele=[Link]();
int in=[Link](arele);
if(in==-1)
{
[Link]("Element not found");
}

© Copyright Acharya Institutes Page 31


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

else
{
[Link]("Element found at "+in);
}
}
void print()
{
Scanner nip=new Scanner([Link]);
[Link]("Enter the starting charecter to print strings");
char inputc=[Link]().charAt(0);
String strc=[Link](inputc);
[Link]("String starting with character "+strc);
for(int i=0;i<[Link]();i++)
{
if([Link](i).startsWith(strc))
{
[Link]([Link](i));
}
}
}
public static void main(String args[])
{
ArrayL obj=new ArrayL();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:

© Copyright Acharya Institutes Page 32


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :33

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

Aim: Exception handling in java, introduction to throwable class, throw, throws, finally

import [Link].*;
public class TryP
{
int c;
void div(int a,int b)
{
try
{
c=a/b;
[Link]("Result="+c);
}
catch(ArithmeticException e)
{
[Link]("Cannot divide by zero");
}
}
public static void main(String args[])
{
TryP obj=new TryP();
Scanner in=new Scanner([Link]);
[Link]("Enter the values of a and b");
int no1=[Link]();
int no2=[Link]();
[Link](no1,no2);
}
}

OUTPUT:

© Copyright Acharya Institutes Page 33


Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

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

Aim: Introduce File operations in java.

import [Link];
import [Link];
class FileP
{
public static void main(String args[ ])
{
Scanner obj=new Scanner([Link]);
String fname=[Link]();
File f1 = new File(fname);
[Link]("File Name: " + [Link]());
[Link](false);
[Link]([Link]() ? "File exists" : "File does not exist");
[Link]([Link]() ? "File is writeable" : "File is not writeable");
[Link]([Link]() ? "File is readable" : "File is not readable");
String fileName = [Link]();
int index = [Link]('.');
if(index > 0)
{
String type = [Link](index + 1);
[Link]("File type is " + type);
}
else
{
[Link]("File doesn't have type");
}
[Link]("File size: " + [Link]() + " Bytes");
}
}

OUTPUT:
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

PROGRAM :12
12. A. Develop an applet that displays a simple message in center of the screen.

Aim: Introduce java Applet, awt, swings.

PROGRAM:
import [Link];
import [Link];
/*
<applet code="[Link]" width="300" height="300">
</applet>
*/
public class AppletP extends Applet
{
public void paint(Graphics g)
{
[Link]("Welcome to applet",100,150);
}
}

OUTPUT:
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

12. B. Develop a simple calculator using Swings.

import [Link].*;
import [Link].*;
import [Link];
import [Link];

class Calculator implements ActionListener


{
JFrame frame; //Creating object of JFrame class
JTextField t;
JButton b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,bdot,badd,bmul,bsub,bdiv,beq,bclr;

static double a=0,b=0,res=0;


static int op=0;

public void Display()


{
frame=new JFrame();
[Link]("IUK Calculator"); //Title of the JFrame
[Link](225,300); //Calculator Size
[Link](null); //Setting Layout
[Link]([Link]); //Setting Background Color
[Link](true); //window resizing
[Link](true); //Setting window's visibility
[Link](JFrame.EXIT_ON_CLOSE);//Setting default close
operation

t=new JTextField();
[Link](30,10,165,35);

b0=new JButton("0");
[Link](30,50,45,40);
b1=new JButton("1");
[Link](70,50,45,40);
b2=new JButton("2");
[Link](110,50,45,40);
b3=new JButton("3");
[Link](150,50,45,40);

b4=new JButton("4");
[Link](30,90,45,40);
b5=new JButton("5");
[Link](70,90,45,40);
b6=new JButton("6");
[Link](110,90,45,40);
b7=new JButton("7");
[Link](150,90,45,40);

b8=new JButton("8");
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

[Link](30,130,45,40);
b9=new JButton("9");
[Link](70,130,45,40);
bdot=new JButton(".");
[Link](110,130,45,40);
badd=new JButton("+");
[Link](150,130,45,40);

bsub=new JButton("-");
[Link](30,170,45,40);
bmul=new JButton("*");
[Link](70,170,45,40);
bdiv=new JButton("/");
[Link](110,170,45,40);
beq=new JButton("=");
[Link](150,170,45,40);

bclr=new JButton("CLR");
[Link](30,210,165,40);

[Link](t);
[Link](b0);
[Link](b1);
[Link](b2);
[Link](b3);
[Link](b4);
[Link](b5);
[Link](b6);
[Link](b7);
[Link](b8);
[Link](b9);
[Link](bdot);
[Link](badd);
[Link](bsub);
[Link](bmul);
[Link](bdiv);
[Link](beq);
[Link](bclr);

[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

[Link](this);
[Link](this);
[Link](this);
[Link](this);
[Link](this);

}
public void actionPerformed(ActionEvent e)
{
if([Link]()==bclr)
{
[Link]("");
}
if([Link]()==b0)
{
[Link]([Link]().concat("0"));
}
if([Link]()==b1)
{
[Link]([Link]().concat("1"));
}
if([Link]()==b2)
{
[Link]([Link]().concat("2"));
}
if([Link]()==b3)
{
[Link]([Link]().concat("3"));
}
if([Link]()==b4)
{
[Link]([Link]().concat("4"));
}
if([Link]()==b5)
{
[Link]([Link]().concat("5"));
}
if([Link]()==b6)
{
[Link]([Link]().concat("6"));
}
if([Link]()==b7)
{
[Link]([Link]().concat("7"));
}
if([Link]()==b8)
{
[Link]([Link]().concat("8"));
}
if([Link]()==b9)
{
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

[Link]([Link]().concat("9"));
}
if([Link]()==bdot)
{
[Link]([Link]().concat("."));
}
if([Link]()==badd)
{
a=[Link]([Link]());
op=1;
[Link]("");
}
if([Link]()==bsub)
{
a=[Link]([Link]());
op=2;
[Link]("");
}
if([Link]()==bmul)
{
a=[Link]([Link]());
op=3;
[Link]("");
}
if([Link]()==bdiv)
{
a=[Link]([Link]());
op=4;
[Link]("");
}
if([Link]()==beq)
{
b=[Link]([Link]());
switch(op)
{
case 1:res=a+b;
break;
case 2:res=a-b;
break;
case 3:res=a*b;
break;
case 4:res=a/b;
break;
}
[Link](""+res);
}
}
}

public class CalculatorP


{
Object Oriented Programming with java Laboratory Dept. of ISE, AIT-Bangalore

public static void main(String[] args)


{
Calculator obj=new
Calculator();[Link]();
}
}

OUTPUT:

You might also like