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

Python Lab Manual

The document is a laboratory manual for an Object Oriented Programming with Java course. It outlines the course objectives of demonstrating Java IDE usage, developing programs to solve real-world problems, and reinforcing OOP concepts. It then lists 12 experiments to be completed as part of the course, including writing programs for quadratic equations, creating student and staff classes, implementing currency converters using packages, and developing multi-threaded applications. It provides details and sample code for the first two experiments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views

Python Lab Manual

The document is a laboratory manual for an Object Oriented Programming with Java course. It outlines the course objectives of demonstrating Java IDE usage, developing programs to solve real-world problems, and reinforcing OOP concepts. It then lists 12 experiments to be completed as part of the course, including writing programs for quadratic equations, creating student and staff classes, implementing currency converters using packages, and developing multi-threaded applications. It provides details and sample code for the first two experiments.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 32

OOP with Java Lab /21CSL35

ANGADI INSTITUTE OF TECHNOLOGY & MANAGEMENT,

BELAGAVI -590009

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

LABORATORY MANUAL

“OBJECT ORIENTED PROGRAMMING WITH JAVA LAB – 21CSL35”

Semester: III Scheme: CBCS

Prepared By

Prof. PRIYANKA.SHEELAVANTAR
Assistant Professor, Dept. of CSE,
AITM, Belagavi.

2022-23
OOP with Java Lab /21CSL35

Course Objectives:
1. Demonstrate the use of Eclipse/Netbeans IDE to create Java Applications.
2. Using java programming to develop programs for solving real-world problems.
3. Reinforce the understanding of basic object-oriented programming concepts.

2022-23
OOP with Java Lab /21CSL35

EXPERIMENT LIST
PART A
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.
2.Create a Java class called Student with the following details as variables within it. USN,Name,
Branch and Phone Write a Java program to create n Student objects and print the USN, Name, Branch,
and Phone of these objects with suitable headings.

3..a. Write a program to check prime number.


b.Write a program for Arithmetic calculator using switch case menu.

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.

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

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.

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

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 computes the square of the number and
prints; third thread will print the value of cube of the number.

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.

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

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.

12. Develop an applet that displays a simple message in center of the screen. Develop a simple
calculator using Swings.

2022-23
OOP with Java Lab /21CSL35

PART B-Practical Based Learning

A problem statement for each batch is to be generated in consultation with the co-examiner and student
should develop an algorithm, program and execute the program for the given problem with appropriate
outputs.

2022-23
OOP with Java Lab /21CSL35

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, datatypes, operators in java

PROGRAM:

Import
java.util.Scanner;public
class Quadratic
{
Public static void main(String[]args)
{

Int a, b,c; //coefficients


double root1, root2;
System.out.println("Enter the
coefficients");
Scanner in=newScanner(System.in);
a=
in.nextInt();b =
in.nextInt();c=i
n.nextInt();

//calculate thedeterminant(b2-4ac)
double d = b * b - 4 * a * c;
System.out.println("Determinant="+d)
;
if(d >0) //checkifdeterminantis greaterthan0
{

//two real anddistinct roots


root1 = (-b + Math.sqrt(d)) / (2*
a);root2=(-b -Math.sqrt(d))/ (2 * a);
System.out.println("The roots are real and distinct");
System.out.format("root1=%.2f and root2=%.2f",root1,root2);
}

elseif (d ==0) //check ifdeterminantisequalto0


{

//two real and equal roots


//determinant is equal to 0
//so
-b+0==-b
root1 = root2 = -b / (2 * a);
System.out.println("The roots are real and
equal");

2022-23
OOP with Java Lab /21CSL35

System.out.format("root1=root2=%.2f;",rotot1);
}

else //if determinant is less than zero


{

//roots are complex number and distinct

doublereal=-b/ (2*a);
double imaginary = Math.sqrt(-d) / (2 *
a);System.out.println("The roots are
imaginary");System.out.format("root1 = %.2f+%.2fi", real,
imaginary);System.out.format("\nroot2=%.2f-
%.2fi",real,imaginary);
}
}
}

Output:

2022-23
OOP with Java Lab /21CSL35

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 java.util.*;
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()
{
System.out.println("**********************");
System.out.println("USN= "+usn);
System.out.println("NAME= "+name);
System.out.println("BRANCH= "+branch);
System.out.println("PHONE NUMBER= "+phone);
System.out.println("**********************");
}

public static void main(String args[])


{
Student st[]=new Student [100];
Scanner ip=new Scanner(System.in);
System.out.println("Enter the number of students");
int n=ip.nextInt();

for(int i=0;i<n;i++)
st[i]=new Student();
for(int j=0;j<n;j++)

2022-23
OOP with Java Lab /21CSL35

2022-23
OOP with Java Lab /21CSL35
System.out.println("E o
nter the n
Usn,Name,Branch,Ph g
one Number"); String
usn=ip.next(); p
S h
t o
r n
i e
n =
g i
p
n .
a n
m e
e x
= t
i L
p o
. n
n g
e (
x )
t ;
( st[j].insertS
) tudent(usn,
; name,branc
h,phone);
S
t }
r for( int
i m=0;m<n;m++)
n {
g S
y
b s
r t
a e
n m
c .
h o
= u
i t
p .
. f
n o
e r
x m
t a
( t
) (
; "
S
l t 2022-23
OOP with Java Lab /21CSL35
udent OOP with Java Lab /21CSL35
%d
details
are\
n",m+1
);
st[m].di
splaySt
udent();
}

}
}

OUTPUT:

2022-23
OOP with Java Lab /21CSL35

3. A. Write a program to check prime number

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

PROGRAM :3A

import java.util.Scanner;
class Prime
{
public static void main(String args[])
{
int i,n,count=0;
System.out.println("Enter the number");
Scanner inp=new Scanner(System.in);
n=inp.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
{
Count++;
}
}
if(count==2)
System.out.println("The given number is a Prime");
else
System.out.println("The given number is not a Prime")
}
}

Output:

2022-23
OOP with Java Lab /21CSL35

PROGRAM :3B
3. B.Write a program for Arithmetic calculator using switch case menu

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

2022-23
OOP with Java Lab /21CSL35

OUTPUT:

2022-23
OOP with Java Lab /21CSL35

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 java.util.Scanner;
class Staff
{
String staffId;
String name;
long phone;
float salary;
public void accept()
{
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Staff Id: ");
staffId = scanner.next();
System.out.print("Enter Name: ");
name = scanner.next();
System.out.print("Enter Phone: ");
phone = scanner.nextLong();
System.out.print("Enter Salary: ");
salary = scanner.nextFloat();

}
public void display()
{
System.out.println("Staff Id: " + staffId);
System.out.println("Name: " + name);
System.out.println("Phone: " + phone);
System.out.println("Salary: " + salary);
}
}
class Teaching extends Staff
{
String domain;
int n;
public void accept()
{
super.accept();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Domain: ");

2022-23
OOP with Java Lab /21CSL35

domain = scanner.next();
System.out.print("Enter Number of Publications: ");

2022-23
OOP with Java Lab /21CSL35

n = scanner.nextInt();
System.out.println("\n");
}
public void display()
{
super.display();
System.out.println("Domain: " + domain);
System.out.println("Publications:"+n);
System.out.println("\n");
}
}
class Technical extends Staff
{
String skill;
public void accept()
{
super.accept();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter technical Skills: "); skill =
scanner.nextLine(); System.out.println("\n");
}
public void display()
{
super.display();
System.out.println("Technical Skills: " + skill);
System.out.println("\n");
}

}
class Contract extends Staff
{
int period;
public void accept()
{
super.accept();
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Period: ");
period = scanner.nextInt();
System.out.println("\n");
}
public void display()
{
super.display();
System.out.println("Contract Period: " + period);
}
}
class Four
{
public static void main(String[] args)
{

2022-23
OOP with Java Lab /21CSL35

Teaching teaching = new Teaching(); System.out.println("Enter the


details of Teaching Staff"); teaching.accept();

Technical technical = new Technical(); System.out.println("Enter


the details of Technical Staff"); technical.accept();

Contract contract = new Contract(); System.out.println("Enter the


details of Contract Staff"); contract.accept();

System.out.println("The details of Teaching Staff");


teaching.display();
System.out.println("The details of Technical Staff");
technical.display();
System.out.println("The details of Contract Staff");
contract.display();
}
}

2022-23
OOP with Java Lab /21CSL35

OUTPUT :

2022-23
OOP with Java Lab /21CSL35

2022-23
OOP with Java Lab /21CSL35

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

Aim: Introduce concepts of method overloading, constructor overloading


Demonstrating Method overloading
class MOverloading
{
//adding two integer numbers
int add(int a, int b)
{
int sum = a+b; return
sum;
}

//addingthree 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=obj.add(10, 20);
int s2=obj.add(10, 20, 30); float
s3=obj.add(2.2f,2.2f);
System.out.println("Method Overload Sum1="+s1);
System.out.println("Method Overload Sum2="+s2);
System.out.println("Method Overload Sum3="+s3);
}
}

OUTPUT:

2022-23
OOP with Java Lab /21CSL35

5B. Constructor Overloading

Public class Constructor


{
int id;
String name;
Constructor()
{
System.out.println("This is Default constructor");
System.out.println("Student Id: "+id+"\nStudentName: "+name);
}
Constructor(int i, String n)
{
System.out.println("This is Parameterized Constructor:");
id = i;
name=n;
System.out.println("Student Id:"+id+"\nStudentName:"+name);
}
Public static void main(String[]args)
{
Constructor s=new Constructor();
Constructor student=newConstructor(10,"David");
}
}

Output:

2022-23
OOP with Java Lab /21CSL35

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:

CurrencyC.java

package cc;

import java.util.*;

public class CurrencyC

{
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*81.83;
System.out.println("Dollar ="+usd+" equal to INR="+inr);
System.out.println("\
n");
}
public void rupeetodollar()

{
System.out.println("Enter Rupee to convert into Dollars:");
inr=in.nextInt();
usd=inr/81.83;
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.06;
System.out.println("Euro ="+euro+" equal to INR="+inr);
System.out.println("\n");

2022-23
OOP with Java Lab /21CSL35

}
public void rupeetoeuro()

{
System.out.println("Enter Rupees to convert into Euro:");
inr=in.nextInt();
euro=(inr/79.06);
System.out.println("Rupee ="+inr +"equal to Euro="+euro); System.out.println("\n public void yentoruppe()

{
System.out.println("Enter Yen to convert into Rupees:"); yen=in.nextInt();
inr=yen*0.57;
System.out.println("Yen ="+yen+" equal to INR="+inr); System.out.println("\n");
}
public void ruppetoyen()

{
System.out.println("Enter Rupees to convert into Yen:"); inr=in.nextInt();
yen=(inr/0.57);
System.out.println("INR="+inr +"equal to YEN"+yen); System.out.println("\n");
}
}

DistaceC.Java
package dc;

import java.util.*;

public class DistanceC

{
double km,m,miles;

Scanner in=new Scanner(System.in);


public void mtokm()

{
System.out.println("Enter the distance in meter"); m=in.nextDouble();
km=(m/1000);
System.out.println(m+"m" +" is equal to "+km+"km"); System.out.println("\n");
}
public void kmtom()

{
System.out.println("Enter the distance in Kilometer"); km=in.nextDouble();
m=km*1000;
System.out.println(km+"km" +" is equal to "+m+"m"); System.out.println("\n");
}

2022-23
OOP with Java Lab /21CSL35

public void milestokm()

{
System.out.println("Enter the distance in miles"); miles=in.nextDouble();
km=(miles*1.60934);
System.out.println(miles+"miles" +" is equal to "+km+"km"); System.out.println("\n");
}

public void kmtomiles()

{
System.out.println("Enter the distance in km");
km=in.nextDouble();
miles=(km*0.621371);
System.out.println(km+"km" +" is equal to "+miles+"miles");
}
}

TimeC.java
package tc;

import java.util.*;

public class TimeC

{
int hours,seconds,minutes;
Scanner in = new Scanner(System.in);
public void hourstominutes()
{
System.out.println("Enter the no of Hours to convert into minutes");
hours=in.nextInt();
minutes=(hours*60); System.out.println("Minutes: " +
minutes);
}
public void minutestohours()

{
System.out.println("Enter the no of Minutes to convert into Hours");
minutes=in.nextInt();
hours=minutes/60; System.out.println("Hours: " +
hours);
}
public void hourstoseconds()

{
System.out.println("Enter the no of Hours to convert into Seconds");
2022-23
OOP with Java Lab /21CSL35

hours=in.nextInt();
seconds=(hours*3600); System.out.println("Seconds: "
+ seconds);
}
public void secondstohours()

{
System.out.println("Enter the no of Seconds to convert into Hours");
seconds=in.nextInt();
hours=seconds/3600;
System.out.println(seconds+"seconds"+ " is equal to "+hours+"hour");
}
}

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

obj.dollartorupee(); obj.rupeetodollar();

obj.eurotorupee(); obj.rupeetoeuro();

obj.yentoruppe(); obj.ruppetoyen();

obj1.mtokm();
obj1.kmtom();

obj1.milestokm(); obj1.kmtomiles();

obj2.hourstominutes(); obj2.minutestohours();

obj2.hourstoseconds(); obj2.secondstohours();
}

2022-23
OOP with Java Lab /21CSL35

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

2022-23
OOP with Java Lab /21CSL35

2022-23
OOP with Java Lab /21CSL35

2022-23
OOP with Java Lab /21CSL35

2022-23
OOP with Java Lab /21CSL35

2022-23
OOP with Java Lab /21CSL35

Prof. Imran Ulla Khan, Dept. of CSE,SKIT 2022-23


OOP with JavaLab/21CSL35

2022-23

You might also like