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

Lab # 3 Journal Waleed Khalid [OOP]

The document outlines several lab tasks for an Object Oriented Programming course, including the implementation of classes such as BankAccount, LibraryBook, Triangle, TemperatureConverter, and Circle. Each class includes specific attributes, methods, and user interaction features, demonstrating encapsulation and object-oriented principles. The tasks require students to validate inputs, perform calculations, and manage user inputs effectively.

Uploaded by

apex.wali3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Lab # 3 Journal Waleed Khalid [OOP]

The document outlines several lab tasks for an Object Oriented Programming course, including the implementation of classes such as BankAccount, LibraryBook, Triangle, TemperatureConverter, and Circle. Each class includes specific attributes, methods, and user interaction features, demonstrating encapsulation and object-oriented principles. The tasks require students to validate inputs, perform calculations, and manage user inputs effectively.

Uploaded by

apex.wali3
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Student Name | Waleed Khalid | F24610019

Object Oriented Programming Lab


Lecturer Muntaha Noor

NUTECH UNIVERSITY ISLAMABAD


LAB#2 - Journal

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;

public class BankAccount {


private String accountNumber;
private double balance;
private String accountHolderName;

public BankAccount(String accountNum, String holderName, double


initialBalance) {
accountNumber = accountNum;
accountHolderName = holderName;
balance = initialBalance;
}

public void deposit(double amount) {


if (amount > 0) {
balance += amount;
} else {
System.out.println("Deposit amount must be positive.");
}
}

public void withdraw(double amount) {


if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds or invalid withdrawal
amount.");
}
}

public double getBalance() {


return balance;
}

public String getAccountNumber() {


return accountNumber;
}

public String getAccountHolderName() {


return accountHolderName;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
BankAccount account = new BankAccount("6856948", "Waleed Khalid",
2000.0); // constructor

System.out.println("Account Holder: " + account.getAccountHolderName());


System.out.println("Account Number: " + account.getAccountNumber());
System.out.println("Initial Balance: $" + account.getBalance());

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;

public LibraryBook(String title, String writer, String isbn) {


bookTitle = title;
author = writer;
ISBN = isbn;
totalBooks++;
}

public void displayBookDetails() {


System.out.println("Book Title: " + bookTitle);
System.out.println("Author: " + author);
System.out.println("ISBN: " + ISBN);
}

public static void showTotalBooks() {


System.out.println("Total Books: " + totalBooks);
}

public static void main(String[] args) {


LibraryBook book1 = new LibraryBook("1984", "George Orwell",
"123456789");
LibraryBook book2 = new LibraryBook("To Kill a Mockingbird", "Harper
Lee", "987654321");

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;

public class Triangle {


private double a, b, c;

public Triangle(double a, double b, double c) {


this.a = a; //used this. here cause the Name of the
constructor and the instance variable was same.
this.b = b;
this.c = c;
}

public boolean isValid() {


return a > 0 && b > 0 && c > 0;
}

public boolean canFormTriangle() {


return (a + b > c) && (b + c > a) && (a + c > b);
}

public String getTriangleType() {


if (a == b && b == c) {
return "Equilateral";
} else if (a == b || b == c || a == c) {
return "Isosceles";
} else {
return "Scalene";
}
}

public double getPerimeter() {


return a + b + c;
}

public double getArea() {


double s = getPerimeter() / 2;
return Math.sqrt(s * (s - a) * (s - b) * (s - c));
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
System.out.print("Enter side a: ");
double a = scanner.nextDouble();
System.out.print("Enter side b: ");
double b = scanner.nextDouble();
System.out.print("Enter side c: ");
double c = scanner.nextDouble();

Triangle triangle = new Triangle(a, b, c);

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;

public class TemperatureConverter {


private double temperature;

public void setTemperature(double temperature) {


this.temperature = temperature;
}

public double celsiusToFahrenheit() {


return (temperature * 9 / 5) + 32;
}

public double fahrenheitToCelsius() {


return (temperature - 32) * 5 / 9;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
TemperatureConverter converter = new TemperatureConverter();

System.out.println("Choose the input temperature scale:");


System.out.println("1. Celsius");
System.out.println("2. Fahrenheit");
int choice = scanner.nextInt();

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;

public class Circle {


private double radius;

public void setRadius(double radius) {


this.radius = radius;
}

public double getArea() {


return Math.PI * radius * radius;
}
public double getCircumference() {
return 2 * Math.PI * radius;
}

public static void main(String[] args) {


Scanner scanner = new Scanner(System.in);
Circle circle = new Circle();
String continueInput;

do {
System.out.print("Enter radius: ");
double inputRadius = scanner.nextDouble();

while (inputRadius <= 0) {


System.out.print("nter a positive radius: ");
inputRadius = scanner.nextDouble();
}

circle.setRadius(inputRadius);
System.out.println("Area: " + circle.getArea());
System.out.println("Circumference: " + circle.getCircumference());

System.out.print("Do you want to enter another radius? (yes/no): ");


continueInput = scanner.next();
} while (continueInput.equalsIgnoreCase("yes"));

scanner.close();
}
}

Output:

You might also like