CarTest.java Questions
Instructions
- 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.
- 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.
- Save the file to disk and begin answering the
questions that are shown below.
- When you need to look at the model answer, click here.
- Click here to go back to the
tutorials menu.
Program listing
|
class Car
{
private String model;
public int value;
private int serialNumber;
private static int serial = 0;
public Car(String aModel, int aValue)
{
model = aModel;
value = aValue;
serialNumber = serial;
serial++;
}
}
class Driver
{
private String name;
private int money;
private Car ownersCar;
public Driver(String aName, int aMoney)
{
name = aName;
money = aMoney;
}
}
class CarTest
{
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();
Driver.swapMoney(joe,mark);
Driver.swapCars(joe,mark);
Car.swapSerialNumbers(f,n);
Car joesTradeIn = new Car("Datsun Coupe", 3000);
joe.tradeIn(joesTradeIn);
joe.sellCarTo(mark);
System.out.println("Joe's net worth = " + joe.netWorth());
}
}