Answered step by step
Verified Expert Solution
Question
1 Approved Answer
2 Vehicle Hierarchy In the vehicle domain, we could implement a Vehicle superclass as follows. public class Vehicle { private String myBrand, myModel; public Vehicle()
2 Vehicle Hierarchy In the vehicle domain, we could implement a Vehicle superclass as follows. public class Vehicle { private String myBrand, myModel; public Vehicle() { myBrand = "unknown"; myModel = "unknown"; public Vehicle (String brand, String model) { set Brand (brand); set Model (model); public String getBrand() { return myBrand; public void setBrand (String brand) { myBrand = brand; public String get Model() { return my Model; public void setModel (String model) { myModel = model; public String toString() { return getBrand() + " " + get Model(); This Vehicle class models a vehicle object by storing the brand and model attributes for that object and providing constructors, accessors and mutators for maintaining those attributes. Given this (super) class, we can now implement a Bicycle subclass as follows. public class Bicycle extends Vehicle { private int myGearCount; public Bicycle() { my GearCount = 1; public Bicycle (int gear Count) { setGearCount(gearCount); public int getGearCount() { return my Gear Count; public void setGearCount (int gear Count) { my GearCount = gearCount; This Bicycle class inherits all the features of the Vehicle class and adds additional features handling the number of gears of the bicycle. Given these class definitions, implement Vehicle and Bicycle classes in the package com.labs.lab..vehicles . To test your classes, write a client class using the following code segment. Bicycle trek74 = new Bicycle(); trek 74.setGearCount (27); System.out.println(trek74.getGears ()); trek 74.setBrand ("Trek"); trek 74.setModel("7.4 FX"); System.out.println(trek74); This code segment declares a bicycle object, trek74 , sets its number of gears to 27 and prints that number out. The next segment of code, however, sets the brand and model of the trek74 object to "Trek and 7.4FX respectively and prints the object itself out using the toString() method. This behavior is not implemented in the Bicycle class itself; it is inherited from the Vehicle class. Thus, we can say that a Bicycle object is a Vehicle object in that it can do anything that a Vehicle object can do. With the features shared by vehicle objects implemented in the Vehicle class, we can now define new vehicle subclasses that share those same features. The following class defines a skateboard class that reuses the vehicle features. public class Skateboard extends Vehicle{ private double myBoardLength; public Skateboard() { myBoardLength = 0; public Skateboard (double boardLength) { setBoardLength (boardLength); public double getBoardLength() { return my BoardLength; public void setBoardLength (double boardLength) { myBoardLength= boardLength; This Skateboard class inherits the vehicle features just as the Bicycle class does, as can be seen in the following code segment. Implement the Skateboard in the package com.labs.lab6.vehicles . You need to add this code segment to your client class to test your work. Skateboard board = new Skateboard(); board.setBoardLength (31.5); System.out.println(board.getBoardLength()); board.setBrand ("Ally"); board.set Model ("Rocket"); System.out.println(board); As with the Bicycle class, the Skateboard class inherits the support for the brand , model and the toString() method, to which it adds unique support for board length. This class structure for our vehicle domain can be viewed as follows Vehicle +myBrand +myModel +toString Bicycle +myGearCount Skateboard +myBoardLength
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started