Content
Each section maps to a module. Exercises progress in difficulty across the week — Module 1 builds the foundation, and by Module 4 you are combining inheritance, interfaces, polymorphism, and exception handling in realistic scenarios.
For each pair, identify the relationship as "is-a" (use inheritance) or "has-a" (use composition). Justify each in one sentence.
ElectricScooter and VehiclePlayer and InventorySavingsAccount and BankAccountInvoice and AddressAdminUser and UserPodcast and AudioFileGiven the following class:
public class Person {
private String name;
protected int age;
public String country;
public Person(String name, int age, String country) {
this.name = name;
this.age = age;
this.country = country;
}
private void logCreation() { System.out.println("Person created"); }
protected void greet() { System.out.println("Hi from Person"); }
public String getName() { return name; }
}
Answer without running any code:
super?logCreation()? Why?super() with no arguments, what happens and why?The following code has four mistakes. Identify each, explain why it fails, and write the corrected version.
public class Animal {
private String name;
private String species;
public Animal(String name, String species) {
this.name = name;
this.species = species;
}
public String getName() { return name; }
}
public class Dog extends Animal {
private String breed;
public Dog(String name, String breed) {
this.name = name; // bug 1
this.breed = breed;
}
@Override
public String makeSound() { // bug 2
return "Woof!";
}
public void displayInfo() {
System.out.println(name + " - " + breed); // bug 3
}
}
public class GoldenRetriever extends Dog, Animal { // bug 4
public GoldenRetriever(String name) {
super(name, "Golden Retriever");
}
}
Create the following hierarchy in com.company.model:
Employee (superclass)
name (String), employeeId (String), baseSalary (double)calculateMonthlyPay() → returns baseSalarytoString() → "[EMP-001] Alice — Monthly: €3500.00"Manager extends Employee
teamSize (int)super()calculateMonthlyPay() → base salary + (€200 per team member)toString() to include team sizeIntern extends Employee
durationMonths (int)super()calculateMonthlyPay() → 60% of base salarytoString() to include durationIn Main, create one of each, print them, and print each one's monthly pay.
toString and equals from ObjectCreate a Book class with fields isbn (String), title (String), author (String), year (int).
toString() — readable single-line outputequals() — two books are equal if their isbn is the sameMain, demonstrate:
true from equals()false from ==System.out.println(book) uses your toString()== not sufficient for comparing objects?Answer the following without writing a full program:
default method and a regular interface method?default void reset(). What must the class do?new? What happens if you try?int MAX = 100. What are its implicit modifiers?The following code has five mistakes. Find each one, explain why it's wrong, and fix it.
public interface Discountable {
private double DISCOUNT_RATE = 0.10;
double applyDiscount(double price) {
return price - (price * DISCOUNT_RATE);
}
double getOriginalPrice();
}
public class Product extends Discountable {
private String name;
private double price;
public Product(String name, double price) {
this.name = name;
this.price = price;
}
public double getOriginalPrice() {
return price;
}
}
public class Main {
public static void main(String[] args) {
Discountable d = new Discountable();
System.out.println(d.applyDiscount(100.0));
}
}
Define the following interfaces in com.delivery:
Trackable → String getTrackingId(); String getCurrentStatus()
Insurable → double calculateInsurance(); boolean isCovered()
Fragile → String getHandlingInstructions(); int getMaxStackHeight()
Create three delivery item classes:
| Class | Implements | Extra fields |
|---|---|---|
StandardParcel |
Trackable |
trackingId, status |
ValuableItem |
Trackable, Insurable |
trackingId, status, value |
GlasswareShipment |
Trackable, Insurable, Fragile |
all of the above + maxStack |
Add a static method to Trackable:
static void printAllStatuses(Trackable[] items)
that prints each item's tracking ID and current status.
Add a default method to Insurable: