CarTest.java Questions

Instructions

  1. With the left mouse button pressed, drag the mouse across the contents of the program listing that is shown below. This will select the program listing. Then choose Copy from the Edit menu of your web browser to copy the program listing to the clipboard.
  2. Start your text editor and create a new file called CarTest.java. Choose Paste to paste the contents of the program listing from the clipboard into the new file.
  3. Save the file to disk and begin answering the questions that are shown below.
  4. When you need to look at the model answer, click here.
  5. Click here to go back to the tutorials menu.

Program listing

// REFERENCES TUTORIAL
// Copyright (C) 2001 Davin Pearson
// Website: http://www.davinpearson.com

class Car {

    /*
     * Properties of the class...
     */
    private String     model;
    public  int        value;
    private int        serialNumber;
    private static int serial = 0;

    /*
     * Constructor of the class...
     */
    public Car(String aModel, int aValue) {

       model  = aModel;
       value  = aValue;

       serialNumber = serial;

       serial++;
    }

}

class Driver {

    /*
     * Properties of the class...
     */
    private String     name;
    private int        money;
    private Car        ownersCar;

    /*
     * Constructor of the class...
     */
    public Driver(String aName, int aMoney) {
       name   = aName;
       money  = aMoney;
    }
}

class CarTest {

    /*
     *
     * The main method is the point of entry into the program...
     *
     */
    public static void main(String[] args) {

       Car f = new Car("Ford Escort",1000);
       Car n = new Car("Nissan Nivara",2000);

       Driver joe  = new Driver("Joe Bloggs",500);
       Driver mark = new Driver("Mark Smith",600);

       Car joesCar = new Car("Ford Escort",1000);
       joe.purchase(joesCar);

       mark.stealCarFrom(joe);

       mark.smashCar();                 // halves the value of
                                        // Mark's car.

       Driver.swapMoney(joe,mark);

       Driver.swapCars(joe,mark);

       Car.swapSerialNumbers(f,n);      // Swaps the serial numbers
                                        // of the cars.

       Car joesTradeIn = new Car("Datsun Coupe", 3000);
       joe.tradeIn(joesTradeIn);
                                        // Joe gets
                                        // half the value
                                        // of his old car

       joe.sellCarTo(mark);

       System.out.println("Joe's net worth = " + joe.netWorth());
                                        // Prints out Joes' money
                                        // plus the value of his
                                        // car, if he has a car.
      
   }

}

// QUESTIONS:
//
// (1) What does the line "serial++" do in the constructor for class Car?
// 
// (2) Write toString methods for the Car and Driver classes so that
// you use them in the next question for debugging.
// 
// (3) Write the methods listed in the main method. 
// 

Hosted by www.Geocities.ws

1