package inheritance; /** * This class represents a general bicycle. */ public class Bicycle { // instance variables private int cadence; private int speed; private int gear; /** * This constructor initializes the fields to the passed values. * * @param startCadence Bicycle's initial cadence. * @param startSpeed Bicycle's initial speed. * @param startGear Bicycle's initial gear. */ public Bicycle(int startCadence, int startSpeed, int startGear) { cadence = startCadence; speed = startSpeed; gear = startGear; } /** * 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 Bicycle(Bicycle bikeObject) { if (bikeObject != null) { cadence = bikeObject.cadence; speed = bikeObject.speed; gear = bikeObject.gear; } } /** * The getCadence method returns a Bicycle object's cadence. * * @return The value in the cadence field. */ public int getCadence() { return cadence; } /** * The getSpeed method returns a Bicycle object's speed. * * @return The value in the speed field. */ public int getSpeed() { return speed; } /** * The getGear method returns a Bicycle object's gear. * * @return The value in the gear field. */ public int getGear() { return gear; } /** * The setCadence method stores a value in the cadence field. * * @param newCadence the value to store in cadence. */ public void setCadence(int newCadence) { cadence = newCadence; } /** * The setSpeed method stores a value in the speed field. * * @param newSpeed the value to store in speed. */ public void setSpeed(int newSpeed) { speed = newSpeed; } /** * The setGear method stores a value in the gear field. * * @param newGear the value to store in gear. */ public void setGear(int newGear) { gear = newGear; } /** * The applyBrake method reduces the Bicycle object's speed by the amount passed as an argument. * * @param decrement amount by which to reduce speed. */ public void applyBrake(int decrement) { speed -= decrement; } /** * The speedUp method increments the Bicycle object's speed by the amount passed as an argument. * * @param increment amount by which to increment speed. */ public void speedUp(int increment) { speed += increment; } /** * The toString method returns a string representing the state of a Bicycle object. * * @return A string containing the bicycle information: cadence, speed, and gear. */ @Override public String toString() { // Create a string representing the object. String output = "Cadence: " + cadence + "\n" + "Speed: " + speed + "\n" + "Gear: " + gear + "\n"; return output; } /** * The equals method compares two Bicycle objects. The result is true if the argument is not * null and is a Bicycle 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 Bicycle if (!(obj instanceof Bicycle)) { return false; } // we already know that obj is of type Bicycle, so it's safe to cast Bicycle bike = (Bicycle) obj; // return true or false depending on whether cadence, speed, and gear have the same value return cadence == bike.cadence && speed == bike.speed && gear == bike.gear; } /** * The copy method creates a new Bicycle object and initializes it with the same data in the * calling object. * * @return a reference to the new object. */ public Bicycle copy() { return new Bicycle(this); } /** * The printDescription method prints information about this Bicycle object. */ public void printDescription() { System.out.println("Bike is in gear " + this.gear + " with a cadence of " + this.cadence + " and travelling at a speed of " + this.speed + "."); } }