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

Lab_Exercises_Solutions

Oop lab exercise

Uploaded by

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

Lab_Exercises_Solutions

Oop lab exercise

Uploaded by

minasetilayee
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Lab Exercises 2 and 3

Lab Exercise 2

Problem 1: Invoice Class


This problem involves creating an `Invoice` class to manage information about items sold in
a hardware store. Below is the implementation and explanation of the class.

Code:

public class Invoice {


private String partNumber;
private String partDescription;
private int quantity;
private double pricePerItem;

// Constructor
public Invoice(String partNumber, String partDescription, int quantity, double
pricePerItem) {
this.partNumber = partNumber;
this.partDescription = partDescription;
this.quantity = Math.max(quantity, 0);
this.pricePerItem = Math.max(pricePerItem, 0.0);
}

// Getters and Setters


public String getPartNumber() { return partNumber; }
public void setPartNumber(String partNumber) { this.partNumber = partNumber; }

public String getPartDescription() { return partDescription; }


public void setPartDescription(String partDescription) { this.partDescription =
partDescription; }

public int getQuantity() { return quantity; }


public void setQuantity(int quantity) { this.quantity = Math.max(quantity, 0); }

public double getPricePerItem() { return pricePerItem; }


public void setPricePerItem(double pricePerItem) { this.pricePerItem =
Math.max(pricePerItem, 0.0); }

// Method to calculate Invoice Amount


public double getInvoiceAmount() {
return quantity * pricePerItem;
}
}

// InvoiceTest class
public class InvoiceTest {
public static void main(String[] args) {
Invoice invoice = new Invoice("1234", "Hammer", 2, 15.5);
System.out.println("Invoice Amount: " + invoice.getInvoiceAmount());
}
}

Problem 2: SavingsAccount Class

This problem involves creating a `SavingsAccount` class to manage savings balances and
calculate interest. Below is the implementation.

Code:

public class SavingsAccount {


private static double annualInterestRate;
private double savingsBalance;

// Constructor
public SavingsAccount(double balance) {
this.savingsBalance = balance;
}

// Method to calculate monthly interest


public void calculateMonthlyInterest() {
double monthlyInterest = (savingsBalance * annualInterestRate) / 12;
savingsBalance += monthlyInterest;
}

// Static method to modify interest rate


public static void modifyInterestRate(double newRate) {
annualInterestRate = newRate;
}
// Getter for savingsBalance
public double getSavingsBalance() {
return savingsBalance;
}
}

// Test Program
public class SavingsAccountTest {
public static void main(String[] args) {
SavingsAccount saver1 = new SavingsAccount(2000.0);
SavingsAccount saver2 = new SavingsAccount(3000.0);

SavingsAccount.modifyInterestRate(0.04);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("Saver 1 Balance: " + saver1.getSavingsBalance());
System.out.println("Saver 2 Balance: " + saver2.getSavingsBalance());

SavingsAccount.modifyInterestRate(0.05);
saver1.calculateMonthlyInterest();
saver2.calculateMonthlyInterest();
System.out.println("Saver 1 Balance: " + saver1.getSavingsBalance());
System.out.println("Saver 2 Balance: " + saver2.getSavingsBalance());
}
}

Lab Exercise 3

Problem 1: Ticket Superclass

The `Ticket` class serves as a superclass for different types of event tickets. Below is the
implementation.

Code:

public abstract class Ticket {


private int ticketNumber;

// Constructor
public Ticket(int ticketNumber) {
this.ticketNumber = ticketNumber;
}

// Abstract method to get price


public abstract double getPrice();

// toString method
public String toString() {
return "Number: " + ticketNumber + ", Price: " + getPrice();
}
}

Problem 2: WalkupTicket Class

The `WalkupTicket` class represents tickets purchased on the event day, priced at $50.

Code:

public class WalkupTicket extends Ticket {


// Constructor
public WalkupTicket(int ticketNumber) {
super(ticketNumber);
}

@Override
public double getPrice() {
return 50.0;
}
}

Problem 3: AdvanceTicket Class

The `AdvanceTicket` class represents tickets purchased in advance with price variations
based on days before the event.

Code:
public class AdvanceTicket extends Ticket {
private int daysInAdvance;

// Constructor
public AdvanceTicket(int ticketNumber, int daysInAdvance) {
super(ticketNumber);
this.daysInAdvance = daysInAdvance;
}

@Override
public double getPrice() {
return daysInAdvance >= 10 ? 30.0 : 40.0;
}
}

Problem 4: StudentAdvanceTicket Class

The `StudentAdvanceTicket` class represents student tickets purchased in advance with


discounted prices.

Code:

public class StudentAdvanceTicket extends AdvanceTicket {


// Constructor
public StudentAdvanceTicket(int ticketNumber, int daysInAdvance) {
super(ticketNumber, daysInAdvance);
}

@Override
public double getPrice() {
return super.getPrice() / 2;
}

@Override
public String toString() {
return super.toString() + " (ID required)";
}
}

You might also like