CS102 Lab session 6
Complete the following exercises pasted below. For each exercise, give an algorithm/java code. This lab provides practice on using abstract classes and interfaces
1. Create abstract class Metro 2. Variables: String name; 3. Methods Constructor with name parameter abstract method move() concrete method stop() that prints "name has stopped"
1. Create class Bus extended from Metro
2. Variables:
None
3. Methods
Constructor with name parameter; calls Metro constructor
override method move() that prints "name moves on roads"
1. Create class Train extended from Metro
2. Variables:
None
3. Methods
Constructor with name parameter; calls Metro constructor
override method move() that prints "name moves on rail tracks"
Instructions: Write main method Use polymorphism to define two Metro objects B for bus T for train call move() and stop() methods for both objects
1. Create interface Taxable
2. Variables:
double TaxRate=0.15;
3. Methods
calculateTax() return double
1. Create abstract class BankAccount
2. Variables:
private String accountNumber
protected double balance
3. Methods
Constructor with two parameters;
Concrete method deposit takes amount as parameter
adds amount to balance
prints "deposited" amount to "Account #"
abstract method called withdraw returns boolean.
it takes one double parameter
getters for all class variables
1. Create class CheckingAccount extended from BankAccount
2. Variables:
private double overDraftLimit
3. Methods
Constructor with three parameters;
calls super constructor
initialize overDraftLimit
override method withdraw() takes double amount as parameter
print "Withdrawing money from checking account:" account
if amount>0 and balance >amount then
subtract amount from balance
return true otherwise display error msg and return false
1. Create class SavingAccount extended from BankAccount
implements Taxable
2. Variables:
private double interestRate
3. Methods
Constructor with three parameters;
calls super constructor
initialize interestRate
override method withdraw() takes double amount as parameter
print "Withdrawing money from saving account:" account
if amount>0 and balance >amount then
subtract amount from balance
return true otherwise display error msg and return false
override method calculateTax()
compute-> double interestEarned = balance * interestRate;
compute-> double tax = interestEarned * TaxRate
print "Tax on saving account:" tax
return tax
//Copy the following code in Customer.java to be added to the same package
class Customer {
private String name;
private BankAccount account;
public Customer(String name, BankAccount account) {
this.name = name;
this.account = account;
}
public void executeOperations() {
System.out.println("--- Processing transactions for " + name + " ---");
account.deposit(500);
account.withdraw(200);
// Check if customer's account is taxable using polymorphism
if (account instanceof Taxable) {
Taxable taxableAccount = (Taxable) account;
taxableAccount.calculateTax();
}
}
}
//Copy the following code in Test.java class. Complete the code and run using the test/driver class
public class Test {
public static void main(String[] args) {
// Customer 1 with a Checking Account
BankAccount checking = new CheckingAccount("CHK-1001", 1000.0, 500.0);
Customer customer1 = new Customer("Alice", checking);
customer1.executeOperations();
System.out.println();
// Customer 2 with a Savings Account
BankAccount savings = new SavingsAccount("SAV-2002", 5000.0, 0.04);
Customer customer2 = new Customer("Bob", savings);
customer2.executeOperations();
}
}