package inheritance; /** * This class represents a special kind of bicycle, a road bike. */ public class RoadBike extends Bicycle { // additional instance field private int tireWidth; // In millimeters (mm) /** * This constructor initializes the fields to the passed values. * * @param startCadence MountainBike's initial cadence. * @param startSpeed MountainBike's initial speed. * @param startGear MountainBike's initial gear. * @param startTireWidth MountainBike's initial tire width. */ public RoadBike(int startCadence, int startSpeed, int startGear, int startTireWidth) { super(startCadence, startSpeed, startGear); tireWidth = startTireWidth; } /** * This is a copy constructor. It initializes the fields of the object being created to the same * values as the fields in the object passed as an argument. * * @param bikeObject The object being copied. */ public RoadBike(RoadBike bikeObject) { super(bikeObject); //super(bikeObject.getCadence(), bikeObject.getSpeed(), bikeObject.getGear()); if (bikeObject != null) { tireWidth = bikeObject.tireWidth; } } /** * The getTireWidth method returns a RoadBike object's tireWidth. * * @return The value in the tireWidth field. */ public int getTireWidth() { return tireWidth; } /** * The setTireWidth method stores a value in the tireWidth field. * * @param newTireWidth the value to store in tireWidth. */ public void setTireWidth(int newTireWidth) { tireWidth = newTireWidth; } /** * The toString method returns a string representing the state of a RoadBike object. * * @return A string containing the bicycle information: cadence, speed, gear, and tireWidth. */ @Override public String toString() { // Create a string representing the object. String output = super.toString() + "Tire Width: " + tireWidth + "\n"; return output; } /** * The equals method compares two RoadBike objects. The result is true if the argument is not * null and is a RoadBike object with the same values for all fields as this object. * * @param obj The object with which to compare the calling object. * @return true if the given object has the same value for all fields as the calling object. */ @Override public boolean equals(Object obj) { // check that the type of the parameter is RoadBike if (!(obj instanceof RoadBike)) { return false; } // we already know that obj is of type RoadBike, so it's safe to cast RoadBike bike = (RoadBike) obj; // return true or false depending on whether cadence, speed, gear, and suspension have the same value return super.equals(bike) && tireWidth == bike.tireWidth; } /** * The copy method creates a new RoadBike object and initializes it with the same data in the * calling object. * * @return a reference to the new object. */ @Override public RoadBike copy() { return new RoadBike(this); } /** * The printDescription method overrides the method in the parent class. It prints information * about this RoadBike object. */ @Override public void printDescription() { super.printDescription(); System.out.println("The RoadBike" + " has " + getTireWidth() + " MM tires."); } }