Professional Documents
Culture Documents
Divyanshi 05401172023 OOPJ Practical
Divyanshi 05401172023 OOPJ Practical
LABMANUAL
For
OOPSUSINGJAVA
(BAI-102)
SUBMITTED TO:
Department MR Rahul Intelligence
of Artificial Sachdeva & Data
SUBMITTED BY: Divyanshi CSE-AI
Sciences
(Enrollment no. = 05401172023)
INDEX
S. No. Experiment
1 1. Write a program to multiply two floating point
Introduction numbers
to 2. Write a program to swap two numbers
Data Types 3. Write a program to add two binary strings
4. Write a program to add two complex numbers
5. Write a program to calculate simple interest
6. Write a program to calculate compound interest
7. Write a program to find perimeter of rectangle
8. Write a program to find area of circle for radius
entered by user
2 1. * print without loops
Practice in
* *
Loops
* * *
* * * *
2. Print (a+(b*c))/(b-c) taking a, b, c as input
3. Print table of a number taken from user
4. Convert rupees, taken as input, to paise
Program 8: Write a program to find area of circle for radius entered by user
Soln:
CODE:
import java.util.*;
public class circlearea {
public static void main(String[]args){
Scanner sc=new Scanner(System.in); //system.in is standard input stream
* *
* * *
* * * *
Soln:
CODE:
public class patternwithoutloop {
public static void main(String[]args){
System.out.println("*");
System.out.println("* *");
System.out.println("* * *");
System.out.println("* * * *");
}
}
Program 2: Print (a+(b*c))/(b-c) taking a, b, c as input
Soln:
CODE:
import java.util.*;
public class abc {
public static void main(String[]args){
Scanner sc=new
Scanner(System.in);
System.out.println("enter a ");
int a=sc.nextInt();
System.out.println("enter b ");
int b=sc.nextInt();
System.out.println("enter c ");
int c=sc.nextInt(); double
x=(a+(b*c))/(b-c);
System.out.println(x);
}
}
}
}
Experiment 3: Conditional Statements
Program 1: Convert degree Celsius, taken as input, to degree Fahrenheit
Soln:
CODE:
import java.util.*;
public class CtoF {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("enter temp in Celcius
:"); float c=sc.nextFloat(); float
f=(c*(9/5))+32;
System.out.println("temp in Farheinheit :"+
f); }
Program 2: Take student’s information like name, roll number, marks, phone
number from user and print it.
Soln:
CODE:
import java.util.*;
public class studentinfo {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("enter name :");
String name=sc.next();
System.out.println("enter roll no.
:"); int roll=sc.nextInt();
System.out.println("marks :"); float
marks=sc.nextFloat();
System.out.println("enter phone no. :");
double phone=sc.nextDouble();
System.out.print("Details :"+"\n"+
"\n name :"+name+
"\n roll number :"+roll+
"\n marks :"+marks);
System.out.printf("\n phone number : %.0f\n", phone); }
}
Experiment 6: Calculator
dist.read(a,b);
dist.print();
dist.add();
dist1.read(a,b);
dist1.print(a,b);
dist1.add(a,b);
dist2.read(a,b);
dist2.print(a,b);
dist2.add(a,b);
}
}
Program 2:Create an employee class with private members name address age
gender taken as input, use constructor to initialize values to the variable, display
all. Derive employee class into full time employee, with members salary and
designation (display all) and part time employee with members work hour and
rate per hour from which pay can be calculated (work hour * rate per hour),
display pay.
Soln:
CODE:
import java.util.*;
class emp {
private String name;
private int age;
private String gender;
private String
address;
emp(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter name: "); name=sc.next();
System.out.println("Enter age: "); age=sc.nextInt();
System.out.println("Enter gender: "); gender=sc.next();
System.out.println("Enter address: "); address=sc.next(); }void
print(){
System.out.println("Name: "+name);
System.out.println("Age: "+age);
System.out.println("Gender: "+gender);
System.out.println("Address: "+address);
}
}class fullTime extends
emp{ private String desig;
private int salary;
fullTime(){
Scanner ab=new Scanner(System.in);
System.out.println("Desig: "); desig=ab.next();
System.out.println("Salary: "); salary=ab.nextInt();
this.desig=desig; this.salary=salary;
}void print(){
super.print();
System.out.println("designation: "+desig);
System.out.println("salary: "+salary);
}
}class partTime extends emp{
private int workingHours;
private int rateOfWorking;
partTime(){
Scanner bc=new Scanner(System.in);
System.out.println("Working Hours: "); workingHours=bc.nextInt();
System.out.println("Rate per Hour: "); rateOfWorking=bc.nextInt(); }void
print(){
super.print();
int salary=workingHours*rateOfWorking;
System.out.println("salary: "+salary);
}
}public class Employee{ public static
void main(String[]args){
Scanner a=new Scanner(System.in);
System.out.println("Enter number of employees: "); int
n=a.nextInt();
System.out.println("Enter 1 for full time and 0 for part time: "); int
t=a.nextInt();
emp[] e=new emp[n]; for (int
i=0;i<n;i++){ if(t==1) {
e[i]=new fullTime(); } else {
e[i]=new partTime(); } }for(int
i=0;i<n;i++){ e[i].print(); }}
Program 3:Create abstract class Shapes. Derive circle triangle rectangle from
shapes. Use abstract method area with different definition in derived class.
Create object of type circle triangle rectangle and print their area.
Soln:
CODE:
import java.util.*;
abstract class Shape{
int length, breadth, radius;
Scanner sc= new
Scanner(System.in); abstract void
Area();
} class Circle extends
Shape{
void Area(){
System.out.println("\n Calculating area of circle");
System.out.print("Enter radius of circle");
radius=sc.nextInt();
System.out.println("The area of circle is:
"+(3.14f*radius*radius));
}
} class Triangle extends
Shape{
void Area(){
System.out.println("\n Calculating area of triangle");
System.out.print("Enter base of triangle");
breadth=sc.nextInt();
System.out.print("Enter height of triangle");
length=sc.nextInt();
System.out.println("The area of triangle is:
"+(0.5*breadth*length));
}
} class Rectangle extends
Shape{
void Area(){
System.out.println("\n Calculating area of rectangle");
System.out.print("Enter width of rectangle");
breadth=sc.nextInt();
System.out.print("Enter length of rectangle");
length=sc.nextInt();
System.out.println("The area of rectangle is:
"+(breadth*length)); }
} public class
abstractShapes {
public static void main(String[]
args){ Circle c=new Circle();
c.Area();
Triangle t= new Triangle();
t.Area();
Rectangle r= new Rectangle();
r.Area();
}
}
Program 4:Create class vehicles with members color type weight and read()
print(). Derive it into two classes, Car with members model engine number of
seats and read() print(), Bike with members engine tyre and read() print().
Soln:
CODE:
import
java.util.Scanner; class
Vehicle{ String color,
type; double weight;
void read(String color, String type, double weight){
this.color=color;
this.type=type;
this.weight=weight;
}
void print(){
System.out.println("Color of the vehicle: "+color);
System.out.println("Type of the vehicle: "+type);
System.out.println("Weight of the vehicle: "+weight); }
} class Car extends Vehicle{ String cType, cEngine,cColor; int
seats; void readc(String cColor,String cType, String cEngine, int
seats){
this.cColor=cColor;
this.cType=cType;
this.cEngine=cEngine;
this.seats=seats;
}
void printc(){
System.out.println("Color of the car: "+cColor);
System.out.println("Type of the car: "+cType);
System.out.println("Engine of the car: "+cEngine);
System.out.println("Number of seats in the car: "+seats); }
}
class Bike extends
Vehicle{ String bEngine,
bColor; int tyres;
void readb(String bColor, String bEngine, int tyres){
this.bColor=bColor;
this.bEngine=bEngine;
this.tyres=tyres;
} void
printb(){
System.out.println("Color of the bike: "+bColor);
System.out.println("Engine of the bike: "+bEngine);
System.out.println("Number of tyres: "+tyres); }
} public class
vehicleCB{
public static void main(String[] args){
Scanner sc= new Scanner(System.in);
Vehicle v=new Vehicle();
System.out.println("Enter the details of the vehicle: ");
System.out.print("Color: ");
String color=sc.next();
System.out.print("Type: ");
String type=sc.next();
System.out.print("Weight: ");
double weight=sc.nextDouble();
v.read(color,type,weight);
System.out.println("Details of the vehicle are: ");
v.print();
if(weight>100){
Car c=new Car();
System.out.println("Enter the details of car: ");
System.out.print("Car type: ");
String cType=sc.next();
System.out.print("Car engine: ");
String cEngine=sc.next();
System.out.print("Number of seats: "); int
seats=sc.nextInt();
c.readc(color,cType,cEngine,seats);
System.out.println("Details of the car are: "); c.printc();
}else{
Bike b=new Bike();
System.out.println("Enter the details of bike: ");
System.out.print("Bike engine: ");
String bEngine=sc.next();
System.out.print("Number of tyres: ");
int tyres=sc.nextInt();
b.readb(color,bEngine,tyres);
System.out.println("Details of the bike are: "); b.printb();
}
}
}
Experiment 8: Inheritance
Program 1:Create class addTime that can add two times in hours, minutes,
seconds.
Soln:
CODE:
import java.util.*;
public class addTime {
public static void
main(String[]args){ Scanner sc=new
Scanner(System.in);
System.out.print("enter hours "); int
HR=sc.nextInt();
System.out.print("enter minutes ");
int MIN=sc.nextInt();
System.out.print("enter seconds ");
int SEC=sc.nextInt();
System.out.print("enter hours ");
int HR2=sc.nextInt();
System.out.print("enter minutes ");
int MIN2=sc.nextInt();
System.out.print("enter seconds
"); int SEC2=sc.nextInt(); int
a=HR+HR2; int b=MIN+MIN2; int
c=SEC+SEC2; int Hour=a+(b/60); int
Min=b+(c/60); int Sec= c%60; int
Mins=Min%60;
System.out.println(Hour+":"+Mins+":"+Sec);
}
}
Program 2:Create a base class account having acc number, acc type, name of
holder, read(), print().Derive this class into savings and current acc class where
savings acc has interest rate 10%, update balance; and current acc has
maximum number of transactions. If number of transactions >30, deduct 1500
Soln:
CODE:
import java.util.Scanner;
class bank{
int Acc_No;
String
Acc_type;
String Name;
void Welcome(){
System.out.println("Welcome to XYZ Bank");}
void print(String Name){
System.out.println("Have a nice day "+Name);
}
void read(String Name, String Acc_Type){
this.Name=Name;
this.Acc_type=Acc_type;
}
} class Current extends
bank{
double Acc_balance; int no_of_transactions; void
read(int no_of_transactions){
this.no_of_transactions=no_of_transactions; } void
read1(double d){
this.Acc_balance=d; }
void accBal(){
if(no_of_transactions>30){
Acc_balance=Acc_balance-1500.0;}
System.out.println("Your balance in Current Account is:
"+Acc_balance);
}
} class Savings extends
bank{
double
Acc_balance; int
rate; void
read(int d){
this.rate=d; }
void read1(double d){
this.Acc_balance=d; }
void accBal(){
Acc_balance=Acc_balance*1.10;
System.out.println("Your balance in Savings Account is
"+Acc_balance);
}
} public class
accounts {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
bank a= new bank();
Savings s= new Savings();
Current c= new Current();
s.Welcome();
System.out.print("Enter your name: ");
String Name=sc.next();
System.out.print("Enter account number: "); int
Acc_no=sc.nextInt();
System.out.print("Enter account type: (Savings/Current) "); String
Acc_type=sc.next();
System.out.println("Account Number: "+Acc_no);
if(Acc_type=="Current"){
c.read(31);
c.read1(1500.50);
c.accBal();
}else{
s.read(15);
s.read1(1000.67);
s.accBal();
}
a.print(Name);
}
}
Program 3:Create a class to read and print details of students. Also create
function to get average, minimum and maximum marks.
Soln:
CODE:
import java.util.*;
class students {
Scanner sc = new Scanner(System.in);
int eng; int phy; int chem; int math;
int marks[] = new int[4];
String name; String fname;
students(String name){
this.name = name; }
public void
readDetails(){
System.out.println("enter fathers name ");
this.fname = sc.next();
System.out.println("enter marks in
eng"); this.eng = sc.nextInt(); marks[0]
= this.eng;
System.out.println("enter marks in
phy"); this.phy = sc.nextInt(); marks[1]
= this.phy;
System.out.println("enter marks in
chem"); this.chem = sc.nextInt();
marks[2] = this.chem;
System.out.println("enter marks in
math"); this.math = sc.nextInt();
marks[3] = this.math; } public void
printDetails(){
System.out.println("Name: " + name);
System.out.println("Fathers name: " + fname);
System.out.println("marks: ");
System.out.println("eng: " + eng);
System.out.println("hindi: " + phy);
System.out.println("maths: " + chem);
System.out.println("sst: " + math);
} public int avg(){ int av = (eng +
phy + chem + math)/4;
return av;
}
public int maximum() {
int m = marks[0];
for(int i = 0; i<4;
i++){ if(marks[i]>m){ m
= marks[i];
}
}return m;
}
public int minimum() {
int m = marks[0];
for(int i = 0; i<4; i++){
if(marks[i]<m){
m = marks[i]; } }
return m; } }
public class marksavgminmax {
public static void main(String[] args) {
students s1 = new students("XYZ");
s1.readDetails(); s1.printDetails();
int max1 = s1.maximum(); int min1 = s1.minimum(); int avg1 = s1.avg();
System.out.println("max: " + max1);
System.out.println("min: " + min1);
System.out.println("avg: " + avg1);
} }
Experiment 9: Interfaces
Program 1:Design an interface with a method reverse that reverses the digits of
a number.
Soln:
CODE:
import java.util.*;
interface number{
int reverse(int x);
}class reverseNum implements
number{ public int reverse(int x){
int y=0; while (x!=0){ int r=x%10;
y=(y*10)+r; x=x/10;
}return y;
}public static void main(String[]args){
Scanner sc= new Scanner(System.in);
System.out.println("enter number");
int x=sc.nextInt();
System.out.println("reversed number");
reverseNum obj= new reverseNum();
System.out.println(obj.reverse(x));
}
Program 2:Design an interface having method to get velocity with parameter r
that calculates and returns velocity of a satellite given by the formula
velocity=root mu/r where r is the altitude of satellite taken from the earth and
mu is Kepler’s constant with value 3.986004418* (10**5). Create a nested
interface with a method to get acceleration with parameter r that calculates and
returns the acceleration of the satellite given by the formula mu/(r**2)
Soln:
CODE:
import java.util.*;
interface v{ float
velocity(float r);
interface a{
float acceleration(float r);
}} class va implements
v,v.a {
public float velocity(float r) { double mu =
3.986004418 * Math.pow(10, 5); float velocity =
(float) Math.pow((mu / r), 0.5);
return velocity;
}
Program 1:Write a program that calculates area of a room and the cost of white
washing, include provision for window on any of the walls. The parameters for
the room length height width are taken from the user. If there is a window then
its length and width are taken from the user. In any case of parameter, if input is
less than 1 then throw an invalid dimension exception; otherwise calculate and
display area and cost of white washing
Soln:
CODE:
Program 2:Write a program that reads data for name class roll number marks
of a student and print them to a file. Also read details from the file and show on
the monitor
Soln:
CODE:
import java.io.*;
public class studentFile {
public static void main(String args[]) throws IOException {
String name = "XYZ";
String cLaSs="XA";
double roll =
1234567; int
marks=80; try {
FileWriter output = new FileWriter("src/student.txt");
output.write(name+" "+cLaSs+" "+roll+" "+marks);
System.out.println("Data is written to the file."); output.close();
}
catch (Exception e) {
e.getStackTrace();
}
File fileName = new File("src/student.txt");
FileInputStream inFile = new FileInputStream("src/student.txt");
int fileLength = (int) fileName.length(); byte Bytes[] = new
byte[fileLength];
System.out.println("File size is: " + inFile.read(Bytes)); String
file1 = new String(Bytes);
System.out.println("File content is:\n" + file1);
inFile.close();
}
}
Program 3:Write a program to copy contents of one file into another.
Soln:
CODE:
import java.io.*;
import java.util.*;
public class copyFilecontent {
public static void copyContent(File a, File b)
throws Exception {
FileInputStream in = new FileInputStream(a);
FileOutputStream out = new FileOutputStream(b);
try {
int n;
while ((n = in.read()) != -1) {
out.write(n);
} } finally { if
(in != null) {
in.close();
} if (out !=
null) {
out.close();
}
}
System.out.println("File Copied");
}
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the source filename:"); String a
= sc.nextLine();
File x = new File(a);
System.out.println("Enter the destination filename:"); String b
= sc.nextLine(); File
y = new File(b);
copyContent(x, y);
}
}
Experiment 11: File Handling-2
Program 1:Write a program to check whether the contents in two files is same
or not.
Soln:
CODE:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class compareFiles {
public static void main(String[] args) throws IOException {
BufferedReader reader1 = new BufferedReader(new
FileReader("src/source.txt"));
BufferedReader reader2 = new BufferedReader(new
FileReader("src/destination.txt"));
String l1 = reader1.readLine();
String l2 = reader2.readLine();
boolean Equal = true; int lineNum
= 1; while (l1 != null || l2 !=
null) { if(l1 == null || l2 ==
null) { Equal = false; break;
}
else if(! l1.equalsIgnoreCase(l2)) {
Equal = false;
break;
}
l1 = reader1.readLine();
l2 = reader2.readLine();
lineNum++;
}
if(Equal) {
System.out.println("Two files have same content."); }
else {
System.out.println("Two files have different content. They differ at
line "+lineNum);
System.out.println("File1 has "+l1+" and File2 has "+l2+" at line
"+lineNum);
}
reader1.close();
reader2.close();
}
Program 2:Write a program to count the number of digits stored in the file
Soln:
CODE:
import java.io.*; import
java.util.Scanner;
public class integersINfile{
public static void main (String[] args)throws FileNotFoundException {
Scanner reader = new Scanner(new File("src/integers.txt")); int count=0;
while (reader.hasNextInt()) {
count++;
reader.nextInt();
}
System.out.println("The file had " + count + " number(s)"); }
}
}
Program 4: Read the data from the keyboard using buffered reader class.
Soln:
CODE:
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class readwBuffered {
public static void main(String[] args) throws Exception{ char
ch;
BufferedReader din=new BufferedReader(new
InputStreamReader(System.in));
System.out.print("enter some characters:
"); for(int i=0; i<5; i++){
ch=(char)din.read();
System.out.println(ch);
}din.close();
}
}
Experiment 12: Application Program
Program 41: Create class with members name, roll number, stream, marks,
write it in a file for 10 student such that when you enter roll number, only the
details of that student are printed.
Soln:
CODE:
Student1(){
sc.next();
marks = sc.nextInt(); }
int getRoll_no(){
getName(){ return
name; } int
getMarks(){ return
void main(String[]
args) throws
Exception{ Scanner sc
= new
Scanner(System.in);