; }
double accel = newSpeed - speed;
speed = newSpeed;
return accel;
}
}
5.8 Write a Bus class that inherits from Vehicle. An instance of the Bus class must always have a named
driver. In the constructor for a Bus, make sure that your code expects and stores the name of the
driver. Also, the Bus class should have accessor and mutator methods for returning and changing the
name of the driver.
class Bus extends Vehicle {
private String driver;
public Bus( String driver ) {
// A "convenience constructor" that defaults all the
// parameters except for driver name. The "this"
// says create a bus passing the 4 default parameter
// values, plus the driver name, to the other
// constructor for the Bus class.
this( "GM", "Metro", 42, "Silver", driver);
}
public Bus( String mk, String mdl, int maxOcc,
String clr, String driver ) {
// super says create a vehicle instance for this bus
// super invokes the vehicle (superclass) constructor
super( mk, mdl, maxOcc, clr );
// We also need to store the name of the bus driver.
// this.driver refers to the private instance variable
// that has the same name as the constructor
parameter
this.driver = driver;
}
public String getDriver() { return driver; }
public void setDriver( String driver ) {
this.
Pages:
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515