Each of the instance and static variables should have
an accessor (get) method that will return the appropriate value, and all except the vehicleCount
variable should also have a mutator (set) method so that the value can be modified.
You should also give the Vehicle class an instance method called changeSpeed. The changeSpeed
method should expect a floating-point value for the new speed, and it should return a floating-point value
representing the difference between the new speed and the previous speed of the vehicle.
Include a public static void main( String[] args) method that creates a few vehicles,
sets some speeds, and reads some variable values, so that you can test your code by launching the class
from the command line.
// The Vehicle class
public class Vehicle {
private String color;
private String make;
private String model;
private double speed;
private int maxOccupants;
private static int vehicleCount = 0;
public Vehicle( String mk, String mdl, int maxOcc, String clr )
{
make = mk;
model = mdl;
maxOccupants = maxOcc;
color = clr;
speed = 0.;
vehicleCount++;
}
public String getColor() { return color; }
ANSWERS TO REVIEW QUESTIONS 193
public String getMake() { return make; }
public String getModel() { return model; }
public double getSpeed() { return speed; }
public int getMaxOccupants() { return maxOccupants; }
public void setColor( String clr ) { color = clr; }
public void setMake ( String mk ) { make = mk; }
public void setModel( String mdl ) { model = mdl; }
public void setSpeed( double spd ) { speed = spd; }
public double changeSpeed( double newSpeed ) {
double accel = newSpeed - speed;
speed = newSpeed;
return accel;
}
public static void main( String[] args ) {
Vehicle v1, v2, v3;
v1 = new Vehicle( "Ford", "Mustang", 2, "red" );
v2 = new Vehicle( "BMW", "328i", 4, "silver" );
v3 = new Vehicle( "Chrysler", "PT Cruiser", 4, "gold" );
System.
Pages:
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513