GNU   davin.50webs.com/J.T.W
a GNU world order – your home of everything that is free

Main Menu Research Projects Photo Album Curriculum Vitae The Greatest Artists
Email Address Computer Games Web Design Java Training Wheels The Fly (A Story)
Political Activism Scruff the Cat My Life Story Smoking Cessation Other Links
Tutorial 1 Tutorial 2 Tutorial 3 Tutorial 4 Tutorial 5
Tutorial 6 Tutorial 7 Tutorial 8 Tutorial 9 Tutorial 10
Tutorial 11 Tutorial 12 Tutorial 13 Tutorial 14 Tutorial 15
Tutorial 16 Tutorial 17 Tutorial 18 Using Emacs Download Links


J.T.W. tutorial 13: still more questions about references


§ 13 Tutorial 13

Question 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 

   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 13.2: What is the purpose of the class variable serialCounter?

Question 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 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 newCar.value and Joe's car being set to newCar. Call the sellCar method before Joe purchases his new car

Question 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. You will need to use an if (...) then ... statement to test whether or not a reference is pointing to a valid object or null for no object like so:

if (ownersCar == null) 
then begin 
   // do not access ownersCar.value as ownersCar points to no object 
end 
else begin 
   // do access ownersCar.value 
end 

Question 13.6: Write a method in the Owner class called smashCar so that:

mary.smashCar(); 

halves the value of Mary's car.

Question 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 make Joe invoke his sellCar method to relinquish ownership of his car if he has one.

Question 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 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 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 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.

Back to J.T.W
This page has the following hit count:
| Main Menu | Research Projects | Photo Album | Curriculum Vitae | The Greatest Artists |
| Email Address | Computer Games | Web Design | Java Training Wheels | The Fly (A Story) |
| Political Activism | Scruff the Cat | My Life Story | Smoking Cessation | Other Links |
| Tutorial 1 | Tutorial 2 | Tutorial 3 | Tutorial 4 | Tutorial 5 |
| Tutorial 6 | Tutorial 7 | Tutorial 8 | Tutorial 9 | Tutorial 10 |
| Tutorial 11 | Tutorial 12 | Tutorial 13 | Tutorial 14 | Tutorial 15 |
| Tutorial 16 | Tutorial 17 | Tutorial 18 | Using Emacs | Download Links
Last modified: Mon 15 Aug 2022 11:24:51 NZST
Best viewed at 1024x768 or above resolution.
© Copyright 1998-2022 Davin Pearson.
Please report any broken links to