Professional Documents
Culture Documents
Submitted by:
[Alibasa, Sheik aishir S.]
[Danilo Abilul]
[Al-khabir Hajihil]
[Wiljem Laureano]
Submitted to:
Mr. Jason A. Catadman
do {
displayMenu();
choice = Integer.parseInt(scanner.nextLine());
switch (choice) {
case 1:
addBook(scanner);
break;
case 2:
borrowBook(scanner);
break;
case 3:
returnBook(scanner);
break;
case 4:
Page 2 of 21
searchBook(scanner);
break;
case 5:
displayLibraryBooks();
break;
case 6:
System.out.println("Exiting Library Management System.
Goodbye!");
break;
default:
System.out.println("Invalid choice. Please try again.");
}
} while (choice != 6);
scanner.close();
}
Page 3 of 21
private void displayLibraryBooks() {
System.out.println(library);
}
Library.java
import java.util.ArrayList;
import java.util.List;
public Library() {
this.books = new ArrayList<>();
}
Page 4 of 21
public void returnBook(String title) {
Book book = searchBook(title);
if (book != null && !book.isAvailable()) {
book.setAvailable(true);
System.out.println("You have successfully returned: " +
book.getTitle());
} else {
System.out.println("Invalid book or it is already available in the
library.");
}
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("Library Books:\n");
for (Book book : books) {
sb.append(book).append("\n");
}
return sb.toString();
}
}
Book.java
public class Book {
private String title;
private String author;
private boolean available;
Page 5 of 21
}
@Override
public String toString() {
return "Title: " + title + ", Author: " + author + ", Available: " +
available;
}
}
Screenshot (output):
Page 6 of 21
Page 7 of 21
Page 8 of 21
Inheritance: Shape Activity
Suppose we have a base class called Shape with attributes name and color, and a
method displayInfo() to display the name and color of the shape. Create two
subclasses Circle and Rectangle that inherit from the Shape class. The Circle class
should have an additional attribute radius, and the Rectangle class should have
additional attributes length and width. Implement a method calculateArea() in both
subclasses to calculate the area of the circle and rectangle respectively. Finally, create
objects of both subclasses and call the displayInfo() and calculateArea() methods.
Page 9 of 21
rectangle.displayInfo();
rectangle.calculateArea();
scanner.close();
}
}
Shape.java
public class Shape {
protected String name;
protected String color;
Rectangle.java
Page 10 of 21
Circle.java
Screenshot (output):
Page 11 of 21
Encapsulation: BankAccount Activity
Suppose we have a BankAccount class with private attributes accountNumber, balance, and
customerName. Implement methods to set and get the values of these attributes using
encapsulation. Additionally, create methods deposit() and withdraw() to deposit and withdraw
money from the account respectively. Implement validation to ensure that the balance doesn't
go negative during withdrawals. Finally, create objects of the BankAccount class, set initial
values, deposit and withdraw money, and display account information.
scanner.close();
}
}
Page 12 of 21
BankAccount.java
public class BankAccount {
private String accountNumber;
private double balance;
private String customerName;
// Constructor
public BankAccount(String accountNumber, double balance, String
customerName) {
this.accountNumber = accountNumber;
this.balance = balance;
this.customerName = customerName;
}
// Getter methods
public String getAccountNumber() {
return accountNumber;
}
// Setter methods
public void setAccountNumber(String accountNumber) {
this.accountNumber = accountNumber;
}
Page 13 of 21
}
Screenshot (output):
Page 14 of 21
Polymorphism: Employee Payroll Activity
Create a program to calculate the payroll for different types of employees: full-time and part-
time. Implement polymorphism by defining a base class Employee and its subclasses
FullTimeEmployee and PartTimeEmployee. Each employee has a method calculatePay() to
calculate their pay. Calculate the total payroll for a list of employees using polymorphism.
if (type.equalsIgnoreCase("FullTime")) {
employees.add(new FullTimeEmployee(name, hoursWorked,
hourlyRate));
} else if (type.equalsIgnoreCase("PartTime")) {
employees.add(new PartTimeEmployee(name, hoursWorked,
hourlyRate));
} else {
Page 15 of 21
System.out.println("Invalid employee type. Please enter
FullTime or PartTime.");
i--; // decrement i to re-prompt for the current employee
}
}
Employee.java
import java.util.Scanner;
Page 16 of 21
public double getHourlyRate() {
return hourlyRate;
}
FullTimeEmployee.java
public class FullTimeEmployee extends Employee {
public FullTimeEmployee(String name, double hoursWorked, double
hourlyRate) {
super(name, hoursWorked, hourlyRate);
}
@Override
public double calculatePay() {
return getHoursWorked() * getHourlyRate();
}
}
PartTimeEmployee.java
public class PartTimeEmployee extends Employee {
public PartTimeEmployee(String name, double hoursWorked, double
hourlyRate) {
super(name, hoursWorked, hourlyRate);
}
@Override
public double calculatePay() {
return getHoursWorked() * getHourlyRate();
}
Page 17 of 21
}
Screenshot (output):
Page 18 of 21
Abstraction: Bank Account Management Activity
Create a program to manage different types of bank accounts: savings and checking.
Implement abstraction by defining a base class BankAccount and its subclasses
SavingsAccount and CheckingAccount. Each account has methods for depositing,
withdrawing, and checking the balance. Create instances of both account types and
perform transactions.
// Perform transactions
System.out.print("Enter deposit amount for savings account: ");
double savingsDeposit = scanner.nextDouble();
savingsAccount.deposit(savingsDeposit);
Page 19 of 21
BankAccount.java
// Base class for BankAccount
public class BankAccount {
protected String accountNumber;
protected double balance;
CheckingAccount.java
// Subclass for CheckingAccount
public class CheckingAccount extends BankAccount {
private double overdraftLimit;
@Override
public void withdraw(double amount) {
if (amount <= balance + overdraftLimit) {
balance -= amount;
Page 20 of 21
System.out.println("Withdrawn: $" + amount);
displayBalance();
} else {
System.out.println("Exceeded overdraft limit!");
}
}
}
SavingAccount.java
// Subclass for SavingsAccount
public class SavingsAccount extends BankAccount {
private double interestRate;
Screenshot (output):
Page 21 of 21