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:
class Car begin /** * Car's model name */ property String model; /** * Car's value in dollars */ property int value; /** * Car's serial number */ property int serialNumber; /** * Global serial number counter */ private classVar int serialCounter = 1000; constructor Car(String aModel, int aValue) begin model = aModel; value = aValue; serialNumber = serialCounter; serialCounter = serialCounter + 1; end public method String toString() begin return "I am a car, model=" + model + ", value=" + value + ", serial number=" + serialNumber; end end class Owner begin /** * Owner's full name */ property String name; /** * Owner's money in dollars */ property int money; /** * Owner's car */ property Car ownersCar; constructor Owner(String aName, int aMoney, Car aCar) begin name = aName; money = aMoney; ownersCar = aCar; end public method String toString() begin return "I am a car owner, name=" + name + ", money=" + money + ", car=" + ownersCar; end public method void describe() begin System.out.println(toString()); end end class CarTest begin beginMain var Car ford = new Car("Ford Escort",1000); var Car nissan = new Car("Nissan Nivara",2000); var Owner joe = new Owner("Joe Bloggs",500,ford); ) // Mary has no car to start with. var Owner mary = new Owner("Mary Smith",600,null); joe.describe(); endMain end
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]