Lab # 4 Journal [OOP]
Lab # 4 Journal [OOP]
Output:
LAB TASK # 2:
1. Create a class AreaCalculator
2. To compute the area of various geometric shapes including
squares, rectangles, circles, triangles, parallelograms, and
trapezoids
3. The class should overload calculateArea methods with
different parameters corresponding to the dimensions of each
shape
4. The program should prompt the user to choose a shape for
which the area needs to be calculated
5. It should request the necessary inputs (such as side lengths,
radius, base, and height) from the user
6. Based on the user's choice, the appropriate method from the
class should be invoked to compute the area
Program:
import java.util.Scanner;
double area = 0;
switch (choice) { //Used Switch to get the choice of user and call the
different method to calculate the area of the shape
case 1:
System.out.print("Enter the side length of the square:
"); //case 1 to get the side of the square
double side = scanner.nextDouble();
area = calculator.calculateArea(side);
break;
case 2:
System.out.print("Enter the length and width of the rectangle:
"); //case 2 to get the length and width of the rectangle
double length = scanner.nextDouble();
double width = scanner.nextDouble();
area = calculator.calculateArea(length, width);
break;
case 3:
System.out.print("Enter the radius of the circle:
"); //case 3 to get the radius of the circle
double radius = scanner.nextDouble();
area = calculator.calculateAreaCircle(radius);
break;
case 4:
System.out.print("Enter the base and height of the triangle:
"); //case 4 to get the base and height of the triangle
double baseTriangle = scanner.nextDouble();
double heightTriangle = scanner.nextDouble();
area = calculator.calculateAreaTriangle(baseTriangle,
heightTriangle);
break;
case 5:
System.out.print("Enter the base and height of the parallelogram:
"); //case 5 to get the base and height of the parallelogram
double baseParallelogram = scanner.nextDouble();
double heightParallelogram = scanner.nextDouble();
area = calculator.calculateAreaParallelogram(baseParallelogram,
heightParallelogram);
break;
case 6:
System.out.print("Enter the lengths of the two bases and the
height of the trapezoid: "); //case 6 to get the base and height of the
trapezoid
double base1 = scanner.nextDouble();
double base2 = scanner.nextDouble();
double heightTrapezoid = scanner.nextDouble();
area = calculator.calculateAreaTrapezoid(base1, base2,
heightTrapezoid);
break;
default:
System.out.println("Invalid choice."); //default case to print
if the user enters any other number
return;
}
}
}
Output:
Post LAB TASK # 1:
Create a class Bank with:
Attributes accountNumber and balance
Three overloaded constructors
No argument constructor
Parameterized that takes an accountNumber as a parameter
and initializes balance to 0.0
Parameterized that takes both an accountNumber and balance
as parameters
Methods
To deposit amount deposit(amount)
To withdraw amount withdraw(amount), withdraw method
should check whether
balance is greater than withdrawn amount, if not then it should
display
"Insufficient funds. Cannot withdraw”
To check balance checkBalance()
Get method to get accountNumber
Main method to demonstrates how to create bank objects using
different combinations of constructors and perform operations
such as depositing, withdrawing, checking the balance.
Program:
import java.util.Scanner; // Importing Scanner for user input
// Performing operations
bank1.deposit(500);
System.out.println("Account Number: " + bank1.getAccountNumber() + ",
Balance: " + bank1.checkBalance()); //calling methods
Output: