Lab # 3 Journal Waleed Khalid [OOP]
Lab # 3 Journal Waleed Khalid [OOP]
March 2, 2025
LAB TASK # 1: Implement a class BankAccount with
the following:
Private variables: accountNumber, balance.
Public methods: deposit(), withdraw(), getBalance().
Implement encapsulation using getter and setter
methods.
Program:
import java.util.Scanner;
while (true) {
System.out.println("Choose an option: 1) Deposit 2) Withdraw 3) Check
Balance 4) View Account Holder Name 5) Exit");
int choice = scanner.nextInt();
switch (choice) {
case 1:
System.out.print("Enter amount to deposit: $");
double depositAmount = scanner.nextDouble();
account.deposit(depositAmount);
System.out.println("Balance after deposit: $" +
account.getBalance());
break;
case 2:
System.out.print("Enter amount to withdraw: $");
double withdrawAmount = scanner.nextDouble();
account.withdraw(withdrawAmount);
System.out.println("Balance after withdrawal: $" +
account.getBalance());
break;
case 3:
System.out.println("Current Balance: $" +
account.getBalance());
break;
case 4:
System.out.println("Account Holder: " +
account.getAccountHolderName());
break;
case 5:
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid option. Please try again.");
}
}
}
}
Output:
LAB TASK # 2: Create a LibraryBook class with:
Private attributes: bookTitle, author, ISBN.
A static variable to count total books.
Methods to display book details.
A static method to show total books.
Program:
public class LibraryBook {
private String bookTitle;
private String author;
private String ISBN;
private static int totalBooks = 0;
book1.displayBookDetails();
book2.displayBookDetails();
LibraryBook.showTotalBooks();
}
}
Output:
LAB TASK # 3: Create a Triangle Class that takes
three sides of a triangle from the user, validate
the input to ensure that the side lengths are positive
numbers, then check whether the
given sides can form a triangle or not by Triangle
Inequality Theorem
a + b > c , b + c > a, a + c > b
Implement a method to determine the type of
triangle based on its side lengths
If all sides are equal, the triangle is “Equilateral”, If
at least two sides are equal, the triangle
is “Isosceles”, If all sides are different, the triangle is
“Scalene”
Methods for computing the perimeter and area of a
triangle
𝑃𝑒𝑟𝑖𝑚𝑒te𝑟 = 𝑎+𝑏+c
𝐴𝑟𝑒𝑎= √(s ( s −𝑎)( s −𝑏)(s)
S =(𝑎+𝑏+c )/2
Program:
import java.util.Scanner;
if (!triangle.isValid()) {
System.out.println("Sides must be positive numbers.");
} else if (!triangle.canFormTriangle()) {
System.out.println("The given sides cannot form a triangle.");
} else {
System.out.println("Triangle Type: " + triangle.getTriangleType());
System.out.println("Perimeter: " + triangle.getPerimeter());
System.out.println("Area: " + triangle.getArea());
}
scanner.close();
}
}
Output:
Post LAB TASK # 1: Create a TemperatureConverter
Class with attribute temperature. The class should
have methods for converting temperature from
Celsius to Fahrenheit and from Fahrenheit to
Celsius. It should display both the entered
temperature and the converted temperature. The
program should prompt the user to choose between
entering a temperature in Celsius or Fahrenheit.
Based on the user's input, the program should
perform the conversion accordingly. Ensure proper
encapsulation of data within the class.
Program:
import java.util.Scanner;
switch (choice) {
case 1:
{
System.out.print("Enter temperature in Celsius: ");
double celsius = scanner.nextDouble();
converter.setTemperature(celsius);
double fahrenheit = converter.celsiusToFahrenheit();
System.out.println("Celsius: " + celsius + " -> Fahrenheit: "
+ fahrenheit);
break;
}
case 2:
{
System.out.print("Enter temperature in Fahrenheit: ");
double fahrenheit = scanner.nextDouble();
converter.setTemperature(fahrenheit);
double celsius = converter.fahrenheitToCelsius();
System.out.println("Fahrenheit: " + fahrenheit + " ->
Celsius: " + celsius);
break;
}
default:
System.out.println("Invalid choice.");
break;
}
scanner.close();
}
}
Output:
Post LAB TASK # 2: Create a Circle Class with radius
as an attribute and methods to calculate area and
circumference of the circle. The program should
allow user input for the radius. Ensure that the
radius input is validated to accept only positive
numbers. If the user enters a non-positive number,
prompt them to enter a valid input. After processing
each radius input, ask the user if they want to enter
another radius. If the user chooses to continue,
repeat the process. If not, terminate the program.
Display the calculated area and circumference for
each entered radius. Ensure proper encapsulation.
Program:
import java.util.Scanner;
do {
System.out.print("Enter radius: ");
double inputRadius = scanner.nextDouble();
circle.setRadius(inputRadius);
System.out.println("Area: " + circle.getArea());
System.out.println("Circumference: " + circle.getCircumference());
scanner.close();
}
}
Output: