CS102 Lab session 5
Complete the following exercises pasted below. For each exercise, give an algorithm/java code. This lab provides practice for inheritance concepts learned in class
Write a Base Class: Vehicle
Attributes (Fields):
protected String brand (e.g., "Toyota")
protected int speed (e.g., 60)
Methods:
Constructor: Accepts brand and speed to initialize fields.
void accelerate(int increase): Increases speed by the given
amount and prints the new speed.
void displayInfo(): Prints the brand and current speed.
Write Subclass: Car (inherits from Vehicle)
Attributes (Fields):
private int numDoors (e.g., 4)
Methods:
Constructor: Accepts brand, speed, and numDoors.
Use super(...) to initialize inherited fields.
void displayInfo() (Override): Calls super.displayInfo()
and additionally prints numDoors.
void ringHorn(): Prints "Beep beep!".
write setters and getter as appropriate
Write a Main class with a main method to:
Instantiate a Car object myCar("Honda", 50, 4).
Call accelerate(20) on myCar to check inherited behavior.
Call displayInfo() to verify method overriding.
Call ringHorn() to test subclass-specific behavior.
Write the Base class Phone; it is inherited by Apple and Samsung
Write private String model
Write private double price
Write parametrized constructor to initialize
model, and price.
Write setters and getters.
Write powerOn method that prints "Phone is powering on"
Write class Apple that inherits Phone
Write private String version
Write parametrized constructor to initialize
model, price and version. Use super(..) as needed
Write powerOn method that prints model and
Apple logo
Write class Samsung that inherits Phone
Write private String version
Write parametrized constructor to initialize
model, price and version. Use super(..) as needed
Write powerOn method that prints model and
Samsung logo
Observe the main method created to test. It has errors, fix it!! Change the code to add two new object (Phones) of your choice. Modify the program to print information for each phone
Write
Club (Base Class)
Fields:
String clubName,
String homeCity,
private budget
Methods:
displayClubDetails()
constructors
setters and getters
Write class:
League Inherits from Club.
Fields:
public int Ranking,
public int Titles
Methods:
displayClubDetails() (Override),
playMatch() //improve ranking by 1
Write class
Team (Subclass Level 1) Inherits from class Club.
Fields:
String leagueName,
Player[] squad,
int playerCount (tracks array items)
static int SQUAD_SIZE=25 //size of array squad
Methods:
addPlayer(Player p): calls calculateSalaries() to
adjust Club budget
displayClubDetails() (Override), prints details
calculateSalaries() //adds salaries for all players
and subtract from budget
Write a Player (Helper Class)
Fields:
String name,
String position,
private double salary
Methods:
Write appropraite methods
Write constructors
make a club "Arsenal FC";
in "London" with budget "100,000,000"
in League "Premier League", with Ranking 3,
and Team size: 0/25 having won titles:2
displayClubDetails for Arsenal
make two players Bukayo Saka; Declan Rice
use Team.addPlayers to add Saka and Rice to arsenal
this should update club budget
displayClubDetails for Arsenal
playMatch
displayClubDetails for Arsenal
Expected output for this program
=== CLUB ===
Club Name: Arsenal FC
Location: London
League: Premier League
Squad Size:0 / 25 max players
Rank: #3
Titles:2
--- Financials & Matches ---
Budget: 100000000
--- Signing Players ---
Signed Bukayo Saka to Arsenal FC
Signed Declan Rice to Arsenal FC
=== CLUB ===
Club Name: Arsenal FC
Location: London
League: Premier League
Squad Size:2 / 25 max players
Rank: #3
Titles:2
--- Financials & Matches ---
Budget: 4000000
---Playing match---
=== CLUB ===
Club Name: Arsenal FC
Location: London
League: Premier League
Squad Size:2 / 25 max players
Rank: #2
Titles:2
--- Financials & Matches ---
Budget: 4000000