OOP_Lab 9
OOP_Lab 9
2023f-BCNS-062 Section: B
Lab # 9
Task # 1:
Method without return:
To for calculator using class and object, write method for add, sub, div, power and
remainder.
Method without return. And over each method at least for 2 and 3 input.
double result = a + b;
double result = a - b;
if (b != 0) {
double result = a / b;
} else {
if (b != 0) {
double result = a % b;
} else {
calc.add(10, 5);
calc.sub(10, 5);
calc.div(10, 5);
calc.power(2, 3);
calc.remainder(10, 3);
Output :
To for calculator using class and object, write method for add, sub, div, power and
remainder.
Method with return. And over each method at least for 2 and 3 input.
return a + b;
return a - b;
if (b == 0) {
return a / b;
if (b == 0) {
return a % b;
Output :
Task # 2:
To a class time having hidden information of hour, minute and second of three-time input
given. Write Constructor for Info. The class must have the facility to utilize the time to zero
at object creation.
public Time() {
this.hour = 0;
this.minute = 0;
this.second = 0;
this.hour = hour;
this.minute = minute;
this.second = second;
}
CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
5
Sheikh obaid (062)
2023f-BCNS-062 Section: B
return hour;
return minute;
return second;
this.hour = hour;
this.minute = minute;
this.second = second;
defaultTime.displayTime();
Output :
Task # 3:
To class to solve hidden information of Employee, Name, ID, Designation and Salary of
Employees. Write method for I/O and also overload them to give the user open hand input
scenario.
import java.util.Scanner;
public Employee() {
this.name = "";
this.id = 0;
this.designation = "";
this.salary = 0.0;
}
CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
7
Sheikh obaid (062)
2023f-BCNS-062 Section: B
this.name = name;
this.id = id;
this.designation = designation;
this.salary = salary;
return name;
return id;
return designation;
return salary;
this.name = name;
this.id = id;
this.designation = designation;
this.salary = salary;
System.out.println("Employee Details:");
public void inputEmployee(String name, int id, String designation, double salary) {
this.name = name;
this.id = id;
this.designation = designation;
this.salary = salary;
this.name = sc.nextLine();
this.id = sc.nextInt();
sc.nextLine();
CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
9
Sheikh obaid (062)
2023f-BCNS-062 Section: B
this.designation = sc.nextLine();
this.salary = sc.nextDouble();
emp1.displayEmployee();
emp2.displayEmployee();
emp3.inputEmployee();
emp3.displayEmployee();
Output :
Task # 4:
To a program code with respect to class, object and function to take the input through
command line and use switch to calculate the following.
A c=a+b
B 1/rt=1/r1+1/r2
import java.util.Random;
import java.util.Scanner;
return a + b;
return a * b;
System.out.println("Choose an option:");
System.out.println("A: c = a + b");
System.out.println("D: w = a * b");
switch (option) {
case 'A':
double a = scanner.nextDouble();
double b = scanner.nextDouble();
break;
case 'B':
double r1 = scanner.nextDouble();
double r2 = scanner.nextDouble();
break;
case 'C':
CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
12
Sheikh obaid (062)
2023f-BCNS-062 Section: B
break;
case 'D':
a = scanner.nextDouble();
b = scanner.nextDouble();
break;
default:
break;
scanner.close();
Output :
Task # 5:
To a program code to calculate the area of two circles, The class must have the facility to
utilize value to time zero at object creation. Use Constructor over loading to find out area
of different circles.
public Circle() {
this.radius = 0;
this.radius = radius;
System.out.println("Circle 1:");
System.out.println("\nCircle 2:");
System.out.println("\nCircle 3:");
Output :
Task # 6:
To program code to calculate the area of two cylinders, The class must have the facility to
utilize value to time zero at object creation. Use Constructor over loading to find out area
of different cylinders.
public Cylinder() {
this.radius = 0;
this.height = 0;
this.radius = radius;
this.height = height;
System.out.println("Cylinder 1:");
System.out.println("\nCylinder 2:");
System.out.println("\nCylinder 3:");
Output :
Task # 7:
To program code to calculate the area of 3 circles, The class must have the facility to utilize
value to time zero at object creation. Use Constructor over loading to find out area of
different circles and use private data encapsulation.
public Circle() {
this.radius = 0;
this.radius = radius;
return radius;
System.out.println("Circle 1:");
System.out.println("\nCircle 2:");
System.out.println("\nCircle 3:");
Output :