Java

OOP GUIDE / WORK

 

VEHICLE SUPERCLASS

 

Topics

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

 

 

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

 

No solution required.

 

 

TASK – PART 2 – SETUP – VEHICLE CLASS

 

No solution required.

 

 

TASK – PART 3 – TESTING TRUCK

 

Here is my solution:

 

 

public class VehicleTester

{

     public static void main(String[] args)

     {

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

           System.out.println(v);

          

           Truck t = new Truck("JT", 123457, false, 1000, 8000);

           System.out.println(t.requiredHitch());

           System.out.println(t);

     }

}

 

 

The above outputs the following:

 

Vehicle (Owner:Jimmy Twoshoes, Reg#123456)

Class III

Vehicle (Owner:JT, Reg#123457)

 

 

TASK – PART 4 - TOSTRING

 

Here is the toString method that I added to truck:

     public String toString()

     {

           String s = "Truck (Owner:" + owner + ", Reg#" + registrationNumber;

           s = s + ", MaxPayLoad:" + payloadCapacity +  "lbs";

           s = s + ", MaxTowing:" + towingCapacity + "lbs";

           if (hasExtendedCab == true)

                s = s + ", ExtendedCab? Yes)";

           else

                s = s + ", ExtendedCab? No)";

           return s;

     }

 

The above will now output the following:

 

Vehicle (Owner:Jimmy Twoshoes, Reg#123456)

Class III

Truck (Owner:JT, Reg#123457, MaxPayLoad:1000lbs, MaxTowing:8000lbs, ExtendedCab? No)

 

 

 

TASK – PART 5 – CAR CLASS

 

public class Car extends Vehicle

{

     public int seatingSpots;

     public boolean hasLeatherInterior;

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

 

     public Car(String ow, int rn, int ss, boolean hli, double fep100)

     {

           super(ow, rn);

           seatingSpots = ss;

           hasLeatherInterior = hli;

           fuelEfficiencyPer100km = fep100;

     }

    

     public boolean isFullEfficient()

     {

           if (fuelEfficiencyPer100km <= 6.0)

                return true;

           else

                return false;

     }

    

     public String toString()

     {

           String s = "Car (Owner:" + owner + ", Reg#" + registrationNumber;

           s = s + ", Seats:" + seatingSpots +  "ppl";

           s = s + ", FuelEfficiency:" + fuelEfficiencyPer100km + "L/100Km";

           if (hasLeatherInterior == true)

                s = s + ", Leather? Yes)";

           else

                s = s + ", Leather? No)";

           return s;

     }

}

 

 

TASK – PART 6 – TESTING

 

Here is my final version of the VehicleTester class:

 

 

public class VehicleTester

{

     public static void main(String[] args)

     {

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

           System.out.println(v);

          

           Truck t = new Truck("JT", 123457, false, 1000, 8000);

           System.out.println(t.requiredHitch());

           System.out.println(t);

          

           Car c = new Car("Little J", 123458, 5, true, 7);

           System.out.println("This car is fuel efficient. " + c.isFullEfficient());

           System.out.println(c);

     }

}

 

 

The above will output:

 

 

Vehicle (Owner:Jimmy Twoshoes, Reg#123456)

Class III

Truck (Owner:JT, Reg#123457, MaxPayLoad:1000lbs, MaxTowing:8000lbs, ExtendedCab? No)

This car is fuel efficient. false

Car (Owner:Little J, Reg#123458, Seats:5ppl, FuelEfficiency:7.0L/100Km, Leather? Yes)