Next: Tutorial 14 Linked lists, Previous: Tutorial 12 Overloading methods, Up: J.T.W. Tutorials [Contents][Index]
Question 4.13.1: Study, compile and run the following code:
classCarbegin/** * Car's model name */propertyString model; /** * Car's value in dollars */propertyint value; /** * Car's serial number */propertyint serialNumber; /** * Global serial number counter */privateclassVarint serialCounter = 1000;constructorCar(String aModel, int aValue)beginmodel = aModel; value = aValue; serialNumber = serialCounter; serialCounter = serialCounter + 1;endpublicmethodString toString()beginreturn "I am a car, model=" + model + ", value=" + value + ", serial number=" + serialNumber;endendclassOwnerbegin/** * Owner's full name */propertyString name; /** * Owner's money in dollars */propertyint money; /** * Owner's car */propertyCar ownersCar;constructorOwner(String aName, int aMoney, Car aCar)beginname = aName; money = aMoney; ownersCar = aCar;endpublicmethodString toString()beginreturn "I am a car owner, name=" + name + ", money=" + money + ", car=" + ownersCar;endpublicmethodvoid describe()beginSystem.out.println(toString());endendclassCarTestbeginbeginMainvarCar ford = new Car("Ford Escort",1000);varCar nissan = new Car("Nissan Nivara",2000);varOwner joe = new Owner("Joe Bloggs",500,ford); ) // Mary has no car to start with.varOwner mary = new Owner("Mary Smith",600,null); joe.describe();endMainend
Question 4.13.2: What is the purpose of the class variable serialCounter?
Question 4.13.3: Write a method sellCar that increases the owner’s
money by half the value of their car and the owner’s car reference
gets set to null, for no car. If the owner owns no car (null) simply
do nothing.
Question 4.13.4: Write a method in the Owner class called purchase so that:
Car newCar = new Car("Mini Cooper",1000);
joe.purchase(newCar);
results in Joe’s money going down by $1000 and Joe’s car being set to
newCar. Before Joe purchases their new car, call the sellCar
method so that Joe sells his current car before
Question 4.13.5: Write a function in the Owner class called
netWorth so that:
System.out.println("Joe's net worth = " + joe.netWorth());
prints out Joes’ money plus the value of his car, if he has a car.
Question 4.13.6: Write a method in the Owner class called smashCar
so that:
mary.smashCar();
halves the value of Mary’s car.
Question 4.13.7: Write a method in the Owner class called stealCarFrom so that:
mary.stealCarFrom(joe);
results in Mary selling his current car (if he has one) for its market
value and Mary acquiring ownership of Joe’s car. Also call Mary’s
sellCar method so that Mary sells her current car before stealing
Joe’s car.
Question 4.13.8: Write a function in the Owner class called
swapMoney so that:
Owner.swapMoney(joe,mary);
swaps the money of Joe and Mary.
Question 4.13.9: Write a function in the Owner class called swapCars so that:
Owner.swapCars(joe,mary);
swaps the cars of Joe and Mary.
Question 4.13.10: Write a function in the Car class called
swapSerialNumbers so that:
Car.swapSerialNumbers(ford,nissan);
swaps the serial numbers of ford and nissan.
Question 4.13.11: Write a function in the Owner class called
sellCarTo so that
joe.sellCarTo(mary);
results in Joe’s money going up by the value of his car and Mary’s money going down by the value of his car, and the ownership of Mary’s car gets transferred to Joe.
Next: Tutorial 14 Linked lists, Previous: Tutorial 12 Overloading methods, Up: J.T.W. Tutorials [Contents][Index]