Java

OOP GUIDE / WORK

 

VEHICLE SUPERCLASS

 

Topics

  • Intro to Inheritance
  • Using extends
  • Superconstructors
  • Testing inheritance

 

 

TASK – PART 1 – SETUP & EXAMINATION – THE VEHICLE CLASS

 

Copy and paste the following Vehicle and VehicleTester classes.  Examine them to make sure you understand how they work.

 

 

public class Vehicle

{

     public String owner;

     public int registrationNumber;

 

     public Vehicle(String o, int r)

     {

           owner = o;

           registrationNumber = r;

     }

    

     public String toString()

     {

           return "Vehicle (Owner:" + owner + ", Reg#" + registrationNumber + ")";

     }

}

 

 

 

public class VehicleTester

{

     public static void main(String[] args)

     {

           Vehicle v = new Vehicle("Jimmy Twoshoes", 123456);

           System.out.println(v);

     }

}

 

 

 

TASK – PART 2 – SETUP – VEHICLE CLASS

 

Copy and paste the following Truck class that inherits from Vehicle.  Examine it closely.

 

 

public class Truck extends Vehicle

{

     public boolean hasExtendedCab;

     public int payloadCapacity;    //usually 1000lbs, 1500lbs or 2000lbs

     public int towingCapacity;    //usually between 8000 and 14000lbs

    

     public Truck(String ow, int rn, boolean cab, int pc, int tc)

     {

           super(ow, rn);

           hasExtendedCab = cab;

           payloadCapacity = pc;

           towingCapacity = tc;

     }

    

     public String requiredHitch()

     {

           if (towingCapacity <= 2000)

                return "Class I";

           else if (towingCapacity <= 3500)

                return "Class II";

           else if (towingCapacity <= 8000)

                return "Class III";

           else

                return "Class IV";

     }

}

 

 

 

TASK – PART 3 – TESTING TRUCK

 

Inside the Vehicle tester class, inside main, create a Truck object.  You determine the parameters for your truck.

 

Call the requiredHitch() method from the Truck class.

 

Call the toString() method that is inherited from the Vehicle class.

 

 

TASK – PART 4 - TOSTRING

 

Create a toString() method inside truck that incorporates all instance variables from Truck and Vehicle.

 

Now run your tester class again.  Notice that the new toString() method is called.

 

 

TASK – PART 5 – CAR CLASS

 

Create the Car class with the following specifications:

 

·                         It extends the Vehicle class.

·                         It has the following instance variables:

 

   public int seatingSpots;

   public boolean hasLeatherInterior;

   public double fuelEfficiencyPer100km;   //usually 5L to 10L per 100km

 

·                         It has a constructor that gets a parameter for each of the instance variables.

 

·                         It has the following method that returns true is the fullEfficiencyPer100km is 6L or less.  Otherwise, it returns false.

 

                   public boolean isFullEfficient()

·                         It has a toString method that incorporates all instance variables from Car and Vehicle.

 

 

TASK – PART 6 – TESTING

 

Inside the Vehicle tester class, inside main, create a Car object.  You determine the parameters for your truck.

 

Call the isFullEfficient() method from the Car class.

 

Call the toString() method.