OOPs with JAVA LAB MANUAL [21ECI62]
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
NAGARJUNA COLLEGE OF ENGINEERING &
TECHNOLOGY
(A unit of Nagarjuna Education Society)
Mudugurki (V), Venkatagirikote (P), Devanahalli (T), Bangalore(R) – 562164
Approved by AICTE/ NBA Accredited/ Affiliated to VTU/ NAAC A+ Accredited
OOPs with JAVA LAB MANUAL [21ECI62]
By,
Mr. MOHAN GOWDA G S
Assistant Professor
Department of Computer Science and Engineering
NAGARJUNA COLLEGE OF ENGINEERING & TECHNOLOGY
1. A) Develop a Java program for an advanced arithmetic calculator that takes two integer
operands and an operator from the user. The program should be capable of performing
addition, subtraction, multiplication, and division.
package p1;
import [Link];
public class lab1 {
public static void main(String[] args) {
int a,b;
String ch;
Scanner sc = new Scanner([Link]);
[Link]("Enter the operand 1:");
a=[Link]();
[Link]("Enter the operator:");
ch=[Link]();
[Link]("Enter the operand 2:");
b=[Link]();
switch(ch) {
case"+":
[Link]("the value is:");
[Link](a+b);
break;
case"-":
[Link]("the value is:");
[Link](a-b);
break;
case"*":
[Link]("the value is:");
[Link](a*b);
break;
case"/":
[Link]("the value is:");
[Link]((float)a/(float)b);
break;
case"%":
[Link](a%b);
break;
default:
[Link]("Invalid Operator!!! Enter the valid operator");
}
}
}
Output:
OUTPUT 1:- OUTPUT 2:- OUTPUT 3:-
Enter the operand 1: Enter the operand 1: Enter the operand 1:
6 6 6
Enter the operator: Enter the operator: Enter the operator:
+ - *
Enter the operand 2: Enter the operand 2: Enter the operand 2:
7 7 7
the value is: the value is: the value is:
13 -1 42
OUTPUT 4:- OUTPUT 5:- OUTPUT 6:-
Enter the operand 1: Enter the operand 1: Enter the operand 1:
6 6 4
Enter the operator: Enter the operator: Enter the operator:
/ % 4
Enter the operand 2: Enter the operand 2: Enter the operand 2:
7 7 7
the value is: the value is: Invalid Operator!!! Enter
0.85714287 6 the valid operator
1. B) Write a Java program to generate the first 'n' terms of the Fibonacci series
package p1;
import [Link];
public class Lab_Program_1b {
public static void main(String[] args)
{
int n, i, first, second, next;
[Link]("Enter the value of n");
Scanner sc = new Scanner([Link]);
n=[Link]();
first=0;
second=1;
[Link]("Fibonacci series are:\n");
[Link](first+"\t"+second);
for(i=2;i<=n-1;i++)
{
next=first+second;
[Link]("\t"+next);
first=second;
second=next;
}
}}
OUTPUT:-
Enter the value of n
9
Fibonacci series are:
0 1 1 2 3 5 8 13 21
2. A) Develop a Java program showcasing method overloading with a base class "Phone"
containing the dial() method, and two subclasses "CameraPhone" and "SmartPhone"
that inherit from the base class and enhance its features. The program should
demonstrate and print the results of these enhancements.
package p1;
class phone
{
void dial() {
[Link]("Calling friend using this number through a regular phone");
}
}
class camera_phone extends phone {
void dial(String n) {
[Link]("calling "+n+"using camera phone");
}
void take_photo() {
[Link]("Take photo using camera phone");
}
}
class smart_phone extends camera_phone{
void dial(String n , boolean b) {
if(b) {
[Link]("calling "+n+"through video call");
}
else {
[Link]("calling "+n+"through normal voice call");
}
}
void acces_internet() {
[Link]("Accessing internet for WWW");
}}
public class Lab_Program2a {
public static void main(String[] args) {
phone p=new phone();
[Link]();
camera_phone c=new camera_phone();
[Link]();
[Link]("Priya ");
c.take_photo();
smart_phone s=new smart_phone();
[Link]("Priya ",true);
s .acces_internet();
}
}
OUTPUT:-
Calling friend using this number through a regular phone
Calling friend using this number through a regular phone
calling Priya using camera phone
Take photo using camera phone
calling Priya through video call
Accessing internet for WWW
2.B) Develop a Java program illustrating constructor overloading for calculating the area of
a rectangle and a circle using appropriate constructors.
package p1;
import [Link].*;
class Shape_A_C{
Shape_A_C(int r){
[Link]("A circle is created");
[Link]("Area of circle which was created is
"+([Link]*r*r)+" cm2");
}
Shape_A_C(int l,int b){
[Link]("A rectangle is created");
[Link]("Area of rectangle which was created is
"+(l*b)+"cm2");
}
}
public class Lab_Program2b {
public static void main(String[] args) {
new Shape_A_C(4);
new Shape_A_C(3,4);
}
}
OUTPUT:-
A circle is created
Area of circle which was created is 50.26548245743669 cm2
A rectangle is created
Area of rectangle which was created is 12cm2
3. A) Create a Java program with a vehicle hierarchy, including Vehicle, Car, SportsCar,
and Truck classes. Implement methods for starting and stopping in the base class and
specialized methods for accelerating, adding turbo boost, and loading cargo in the
subclasses, with appropriate method overrides.
package p1;
class vehicles
{
void start()
{
[Link]("vehicle started");
}
void stop() {
[Link]("vehicle stopped");
}
}
class car extends vehicles
{
@Override
void start()
{
[Link]("car started");
}
@Override
void stop() {
[Link]("car stopped");
}
void accelerate() {
[Link]("accelerating in a car");
}
}
class sports_car extends vehicles{
@Override
void start() {
[Link]("sports car started");
}
@Override
void stop() {
[Link]("sports car stopped");
}
void turbo() {
[Link]("Sports turbo boosting in my sports car");
}
}
class truck extends vehicles{
@Override
void start() {
[Link]("truck started");
}
@Override
void stop() {
[Link]("truck stopped");
}
void load_cargo() {
[Link]("load cargo into the truck");
}
}
public class Lab_Program3a{
public static void main(String[] args) {
car c1=new car();
[Link]();
[Link]();
[Link]();
sports_car s1=new sports_car();
[Link]();
[Link]();
[Link]();
truck t1=new truck();
[Link]();
t1.load_cargo();
[Link]();
}
}
OUTPUT:
car started
accelerating in a car
car stopped
sports car started
Sports turbo boosting in my sports car
sports car stopped
truck started
load cargo into the truck
truck stopped
3.B) Create a Java program that models electronic devices (e.g., smartphones, laptops, and
tablets) using a common interface for power management. The program should allow users
to interact with the devices and control their power state.
package p1;
interface power_management{
void poweron();
void poweroff();
boolean ispoweron();
}
class electronic_devices implements power_management{
boolean state=false;
@Override
public void poweron() {
state=true;
[Link]("electronic device is turnes on");
}
@Override
public void poweroff() {
state=false;
[Link]("electronic device is turnes off");
}
@Override
public boolean ispoweron(){
return state;
}
}
class smart_phone1 extends electronic_devices{
public void poweron() {
state=true;
[Link]("smart phone is turned on");
}
@Override
public void poweroff() {
state=false;
[Link]("smart phone is turned off");
}
void
dial() {
if(ispoweron())
[Link]("call my friend using smart phone");
else
[Link]("hey idiot,first turn on the phone");
}
}
class laptop extends electronic_devices{
public void poweron() {
state=true;
[Link]("laptop is turnes on");
}
@Override
public void poweroff() {
state=false;
[Link]("laptop is turned off");
}
void access_internet() {
if(ispoweron())
[Link]("accessing internet using laptop");
else
[Link]("please turn on laptop");
}
}
class tablet extends electronic_devices{
public void poweron() {
state=true;
[Link]("tablet is turned on");
}
@Override
public void poweroff() {
state=false;
[Link]("tablet off");
}
void takephoto() {
if(ispoweron())
[Link]("taking photo using tablet");
else
[Link]("please turn on tablet");
}
}
public class Lab_Program3b {
public static void main(String[] args) {
smart_phone1 oneplus=new smart_phone1();
[Link]();
[Link]();
[Link]();
[Link]();
[Link]();
tablet ipad=new tablet();
[Link]();
[Link]();
[Link]();
[Link]();
}
}
OUTPUT:
smart phone is turned on
call my friend using smart phone
smart phone is turned off
hey idiot,first turn on the phone
tablet is turned on
taking photo using tablet
tablet off
please turn on tablet
4. A) Develop a Java program that emulates a library system. Create two packages,
`library` and `patron`. In the `library` package, define a `Book` class with a private title
field. In the `patron` package, implement a `Patron` class that can borrow books.
Demonstrate the use of packages, access protection, and class imports. Ensure that the
book title remains inaccessible from outside the `library` package due to the `private`
access modifier. Create a scenario where a patron, Alice, borrows a book from the library.
package library;
public class Book {
private String title=null;
public Book(String title)
{
[Link]=title;
[Link]("The book titled "+[Link]+" is added to the library");
}
public String getTitle()
{
return title;
}
}
package patron;
import [Link];
public class Patron
{
public String name=null;
Patron(String name)
{
[Link]=name;
[Link]("New patron "+[Link]+" is added to the database");
}
void borrow_books(Book b)
{
[Link]([Link]+" borrowed "+[Link]());
}
}
package patron;
import [Link];
public class Demo_Library_Management_System {
public static void main(String[] args) {
Book b1=new Book("AR sir's notes");
Book b2=new Book("Little champs");
Patron p1=new Patron("Chandana");
Patron p2=new Patron("Deeksha");
p1.borrow_books(b1);
p1.borrow_books(b2);
}
}
OUTPUT:
The book titled AR sir's notes is added to the library
The book titled Little champs is added to the library
New patron Chandana is added to the database
New patron Deeksha is added to the database
Chandana borrowed AR sir's notes
Chandana borrowed Little champs
4.B) Develop a Java lab program that handles exceptions for division by zero and invalid
input. Use `try-catch` blocks to catch `ArithmeticException` for division by zero and
`InputMismatchException` for non-integer input and provide user-friendly error messages.
package p1;
import [Link];
import [Link];
public class Lab_Program4b {
public static void main(String[] args) {
Scanner s=new Scanner([Link]);
int c=0,a=0,b=0;
try {
while(true) {
try {
[Link]("Enter the numerator:");
a=[Link]();
break;
}
catch(InputMismatchException f) {
[Link]("Give only integer input for numerator");
[Link]();
}
}
while(true){
try {
[Link]("Enter the denominator:");
b=[Link]();
c=a/b;
break;
}
catch(InputMismatchException f) {
[Link]("Give only integer input for denominator");
[Link]();
}
catch(ArithmeticException f) {
[Link]("The denominator value should be greater than zero");
[Link]();
}
}
}
finally{
[Link]();
}
[Link]("c="+c);
}
}
OUTPUT:
Enter the numerator:
4
Enter the denominator:
3
c=1
5. A) 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.
package p1;
import [Link];
class RandomNumbers implements Runnable{
@Override
public void run() {
for(int i=0;i<10;i++) {
Random rn=new Random();
int num=[Link](10);
[Link](num);
new Sq_Of_Random(num);
new Cube_Of_Random(num);
try {
[Link](1000);
}catch(InterruptedException e) {
[Link]();
}
}
}
}
class Sq_Of_Random implements Runnable{
int num;
Sq_Of_Random(int num){
[Link]=num;
new Thread(this).start();
}
@Override
public void run() {
num=num*num;
[Link](num);
}
}
class Cube_Of_Random implements Runnable{
int num;
Cube_Of_Random(int num){
[Link]=num;
new Thread(this).start();
}
@Override
public void run() {
num=num*num*num;
[Link](num);
}
}
public class Lab_Program_5a {
public static void main(String[] args) {
Runnable r1=new RandomNumbers();
new Thread(r1).start();
}
}
OUTPUT:
4
16
64
4
16
64
6
36
216
1
1
5.B) Design a Java lab program to demonstrate string handling, including creating strings
using constructors and literals, concatenating strings, extracting characters at a specified
index, and comparing strings for equality.
package p1;
import [Link];
public class Lab_Program_5b {
public static void main(String[] args) {
Scanner Scn = new Scanner([Link]);
[Link]("Enter the first string :");
String S1 = [Link]();
[Link]("Enter the second string : ");
String S2 = [Link]();
String S3 = new String([Link](S2));
[Link]("The concatenation string is :" + S3);
[Link]("Extract portion from concatenated string is :" + [Link](3));
if ([Link](S2)) {
[Link]("Strings you have entered are equal ");
} else {
[Link]("Strings you have entered are not equal");
}
[Link]();
}
}
OUTPUT:
OUTPUT 1: OUTPUT 2:
Enter the first string : Enter the first string :
CAT CAT
Enter the second string : Enter the second string :
RAT CAT
The concatenation string is :CATRAT The concatenation string is :CATCAT
Extract portion from concatenated string is Extract portion from concatenated string is
:RAT :CAT
Strings you have entered are not equal Strings you have entered are equal