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

OOP_Lab 9

Uploaded by

Imad Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

OOP_Lab 9

Uploaded by

Imad Rehman
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Sheikh obaid (062)

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.

public class Calculator {

public void add(double a, double b) {

double result = a + b;

System.out.println("Addition: " + result);

public void sub(double a, double b) {

double result = a - b;

System.out.println("Subtraction: " + result);

public void div(double a, double b) {

if (b != 0) {

double result = a / b;

System.out.println("Division: " + result);

} else {

System.out.println("Error: Division by zero is not allowed.");

public void power(double a, double b) {


CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
1
Sheikh obaid (062)
2023f-BCNS-062 Section: B

double result = Math.pow(a, b);

System.out.println("Power: " + result);

public void remainder(double a, double b) {

if (b != 0) {

double result = a % b;

System.out.println("Remainder: " + result);

} else {

System.out.println("Error: Division by zero is not allowed.");

public static void main(String[] args) {

Calculator calc = new Calculator();

calc.add(10, 5);

calc.sub(10, 5);

calc.div(10, 5);

calc.power(2, 3);

calc.remainder(10, 3);

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
2
Sheikh obaid (062)
2023f-BCNS-062 Section: B

Output :

Method with return:

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.

public class Calculator {

public double add(double a, double b) {

return a + b;

public double sub(double a, double b) {

return a - b;

public double div(double a, double b) {

if (b == 0) {

throw new IllegalArgumentException("Division by zero is not allowed.");

return a / b;

public double power(double base, double exponent) {

return Math.pow(base, exponent);


CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
3
Sheikh obaid (062)
2023f-BCNS-062 Section: B

public double remainder(double a, double b) {

if (b == 0) {

throw new IllegalArgumentException("Division by zero is not allowed.");

return a % b;

public static void main(String[] args) {

Calculator calc = new Calculator();

double addResult = calc.add(10, 5);

double subResult = calc.sub(10, 5);

double divResult = calc.div(10, 5);

double powerResult = calc.power(2, 3);

double remainderResult = calc.remainder(10, 5);

System.out.println("Addition: " + addResult);

System.out.println("Subtraction: " + subResult);

System.out.println("Division: " + divResult);

System.out.println("Power: " + powerResult);

System.out.println("Remainder: " + remainderResult);

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
4
Sheikh obaid (062)
2023f-BCNS-062 Section: 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 class Time {

private int hour;

private int minute;

private int second;

public Time() {

this.hour = 0;

this.minute = 0;

this.second = 0;

public Time(int hour, int minute, int second) {

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

public int getHour() {

return hour;

public int getMinute() {

return minute;

public int getSecond() {

return second;

public void setHour(int hour) {

this.hour = hour;

public void setMinute(int minute) {

this.minute = minute;

public void setSecond(int second) {

this.second = second;

public void displayTime() {

System.out.printf("Time: %02d:%02d:%02d\n", hour, minute, second);

public static void main(String[] args) {

Time defaultTime = new Time();

defaultTime.displayTime();

Time specificTime = new Time(10, 30, 45);


CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
6
Sheikh obaid (062)
2023f-BCNS-062 Section: B

specificTime.displayTime(); // Output: Time: 10:30:45

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 class Employee {

private String name;

private int id;

private String designation;

private double salary;

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

public Employee(String name, int id, String designation, double salary) {

this.name = name;

this.id = id;

this.designation = designation;

this.salary = salary;

public String getName() {

return name;

public int getId() {

return id;

public String getDesignation() {

return designation;

public double getSalary() {

return salary;

public void setName(String name) {

this.name = name;

public void setId(int id) {

this.id = id;

public void setDesignation(String designation) {


CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
8
Sheikh obaid (062)
2023f-BCNS-062 Section: B

this.designation = designation;

public void setSalary(double salary) {

this.salary = salary;

public void displayEmployee() {

System.out.println("Employee Details:");

System.out.println("Name: " + name);

System.out.println("ID: " + id);

System.out.println("Designation: " + designation);

System.out.println("Salary: $" + salary);

public void inputEmployee(String name, int id, String designation, double salary) {

this.name = name;

this.id = id;

this.designation = designation;

this.salary = salary;

public void inputEmployee() {

Scanner sc = new Scanner(System.in);

System.out.print("Enter Name: ");

this.name = sc.nextLine();

System.out.print("Enter ID: ");

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

System.out.print("Enter Designation: ");

this.designation = sc.nextLine();

System.out.print("Enter Salary: ");

this.salary = sc.nextDouble();

public static void main(String[] args) {

Employee emp1 = new Employee("Alice", 1, "Manager", 75000);

emp1.displayEmployee();

Employee emp2 = new Employee();

emp2.inputEmployee("Bob", 2, "Developer", 60000);

emp2.displayEmployee();

Employee emp3 = new Employee();

emp3.inputEmployee();

emp3.displayEmployee();

Output :

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
10
Sheikh obaid (062)
2023f-BCNS-062 Section: B

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

C generate random number in between the value given by user

D w=axb Use default for Irrelevant case

import java.util.Random;

import java.util.Scanner;

public class Calculation {

public double add(double a, double b) {

return a + b;

public double parallelResistance(double r1, double r2) {

return 1 / ((1 / r1) + (1 / r2));

public int generateRandom(int min, int max) {

Random random = new Random();

return random.nextInt(max - min + 1) + min;

public double multiply(double a, double b) {

return a * b;

public static void main(String[] args) {

Calculation calc = new Calculation();

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
11
Sheikh obaid (062)
2023f-BCNS-062 Section: B

Scanner scanner = new Scanner(System.in);

System.out.println("Choose an option:");

System.out.println("A: c = a + b");

System.out.println("B: 1/rt = 1/r1 + 1/r2");

System.out.println("C: Generate random number between values given by user");

System.out.println("D: w = a * b");

char option = scanner.next().charAt(0);

switch (option) {

case 'A':

System.out.print("Enter value for a: ");

double a = scanner.nextDouble();

System.out.print("Enter value for b: ");

double b = scanner.nextDouble();

double resultAdd = calc.add(a, b);

System.out.println("Result (c = a + b): " + resultAdd);

break;

case 'B':

System.out.print("Enter value for r1: ");

double r1 = scanner.nextDouble();

System.out.print("Enter value for r2: ");

double r2 = scanner.nextDouble();

double resultParallelResistance = calc.parallelResistance(r1, r2);

System.out.println("Result (1/rt = 1/r1 + 1/r2): " + resultParallelResistance);

break;

case 'C':
CNS151L Object Oriented Programing
Sir Syed University of Engineering & Technology
12
Sheikh obaid (062)
2023f-BCNS-062 Section: B

System.out.print("Enter minimum value: ");

int min = scanner.nextInt();

System.out.print("Enter maximum value: ");

int max = scanner.nextInt();

int randomNumber = calc.generateRandom(min, max);

System.out.println("Generated Random Number: " + randomNumber);

break;

case 'D':

System.out.print("Enter value for a: ");

a = scanner.nextDouble();

System.out.print("Enter value for b: ");

b = scanner.nextDouble();

double resultMultiply = calc.multiply(a, b);

System.out.println("Result (w = a * b): " + resultMultiply);

break;

default:

System.out.println("Invalid option chosen. Please choose a valid option.");

break;

scanner.close();

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
13
Sheikh obaid (062)
2023f-BCNS-062 Section: B

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 class Circle {

private double radius;

public Circle() {

this.radius = 0;

public Circle(double radius) {

this.radius = radius;

public double calculateArea() {

return Math.PI * radius * radius;

public static void main(String[] args) {

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
14
Sheikh obaid (062)
2023f-BCNS-062 Section: B

Circle circle1 = new Circle();

System.out.println("Circle 1:");

System.out.println("Radius: " + circle1.radius);

System.out.println("Area: " + circle1.calculateArea());

Circle circle2 = new Circle(5);

System.out.println("\nCircle 2:");

System.out.println("Radius: " + circle2.radius);

System.out.println("Area: " + circle2.calculateArea());

Circle circle3 = new Circle(7);

System.out.println("\nCircle 3:");

System.out.println("Radius: " + circle3.radius);

System.out.println("Area: " + circle3.calculateArea());

Output :

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
15
Sheikh obaid (062)
2023f-BCNS-062 Section: B

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 class Cylinder {

private double radius;

private double height;

public Cylinder() {

this.radius = 0;

this.height = 0;

public Cylinder(double radius, double height) {

this.radius = radius;

this.height = height;

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
16
Sheikh obaid (062)
2023f-BCNS-062 Section: B

public double calculateSurfaceArea() {

double baseArea = Math.PI * radius * radius;

double lateralSurfaceArea = 2 * Math.PI * radius * height;

return 2 * baseArea + lateralSurfaceArea;

public static void main(String[] args) {

Cylinder cylinder1 = new Cylinder();

System.out.println("Cylinder 1:");

System.out.println("Radius: " + cylinder1.radius);

System.out.println("Height: " + cylinder1.height);

System.out.println("Surface Area: " + cylinder1.calculateSurfaceArea());

Cylinder cylinder2 = new Cylinder(3, 5);

System.out.println("\nCylinder 2:");

System.out.println("Radius: " + cylinder2.radius);

System.out.println("Height: " + cylinder2.height);

System.out.println("Surface Area: " + cylinder2.calculateSurfaceArea());

Cylinder cylinder3 = new Cylinder(4, 7);

System.out.println("\nCylinder 3:");

System.out.println("Radius: " + cylinder3.radius);

System.out.println("Height: " + cylinder3.height);

System.out.println("Surface Area: " + cylinder3.calculateSurfaceArea());

Output :

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
17
Sheikh obaid (062)
2023f-BCNS-062 Section: B

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 class Circle {

private double radius;

public Circle() {

this.radius = 0;

public Circle(double radius) {

this.radius = radius;

public double calculateArea() {

return Math.PI * radius * radius;

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
18
Sheikh obaid (062)
2023f-BCNS-062 Section: B

public double getRadius() {

return radius;

public static void main(String[] args) {

Circle circle1 = new Circle();

System.out.println("Circle 1:");

System.out.println("Radius: " + circle1.getRadius());

System.out.println("Area: " + circle1.calculateArea());

Circle circle2 = new Circle(5);

System.out.println("\nCircle 2:");

System.out.println("Radius: " + circle2.getRadius());

System.out.println("Area: " + circle2.calculateArea());

Circle circle3 = new Circle(7);

System.out.println("\nCircle 3:");

System.out.println("Radius: " + circle3.getRadius());

System.out.println("Area: " + circle3.calculateArea());

Output :

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
19
Sheikh obaid (062)
2023f-BCNS-062 Section: B

CNS151L Object Oriented Programing


Sir Syed University of Engineering & Technology
20

You might also like