Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Part 2.1 Create a new subclass instance with a superclass reference and use polymorphism. In jGrasp open the VehicleProject.gpj project (from the top menu->Project->Open->). It

Part 2.1 Create a new subclass instance with a superclass reference and use polymorphism.

In jGrasp open the VehicleProject.gpj project (from the top menu->Project->Open->). It has the files:

VehicleTester.java, Vehicle.java, Truck.java, Car.java. Compile the project.

Examine the Vehicle, Car and Truck classes. Identify the superclass and the sub classes and the attributes and methods that are common to both classes.

//TODO 1

Complete the static method in the VehicleTester class called makeVehicleArray that takes no parameters and returns an array of type Vehicle. In the method body, create an array of Vehicles using these data.

Hint:

The first one has been done for you.

Type Attributes

Car "Ford", "Pinto", 2.5, "F1234", "1972", "green", 2.3, 5

Truck "Ford", "F150", 3.5, "F2345", 1990", "red", 2.25

Car "Mazda", "Miata", 2.5, "M9862","2013", "black", 15.3, 5

Truck "Chevy", "Silverado", 3.5, "C34521", "2011", "green", 2.25

Note that you are using a superclass reference, Vehicle, to refer to a subclass instance: Car or Truck.

//TODO 2

Complete the static method in the VehicleTester class called printVehicles. This method takes an array of type Vehicle as a parameter, does not return a value, and prints the toString method of each member of the array to the console.

Compile and run the VehicleTester to see if the method works. Think about why each type of vehicle exhibits different behavior in that different attributes are printed.

Hint: Check the toString methods of the Car and Truck classes.

Part 2.2. Extending a class

//TODO 3

Add a class called HybridCar that is a subclass of the Car class. In this class do the following tasks:

A hybrid car has both electric and gasoline powered engines, so it will have an attribute of double type called batteryCap in addition to all the other Car attributes.

Create a constructor that allows this attribute to be passed in and assign it to batteryCap.

Write a getBatteryCap method that returns the batteryCap value.

Write a toString method that calls its superclass toString method and then prints out the batteryCap value.

Hints:

Check the Car class to see how to extend another class.

The car class has methods and attributes that can be inherited by the HybridCar.

//TODO 4

In the VehicleTester add the following two hybrid cars to the vehicle array in the makeVehicleArray method with these data: maker, model, engine size, VIN, year, color, luggage cap, pass cap, battery cap.

Type Attributes

Hybrid Car "Chevy", "Volt", 1.4, "V23423", "2015","silver", 10.6, 4, 17.1

Hybrid Car "Toyota", "Prius", 1.8, "TP36453", "2015","red", 15.8, 4, 20.0

Compile and run. Verify your results.

Hint: Increase the array size in the makeVehicleArray, before you add the hybrid cars to the array.

3

Lab 10 Inheritance

//TODO 5

Add another class that models a race car. Call this class, you guessed it, RaceCar. A RaceCar has two attributes: numGears and maxTorque, both integers. The question is, what class does RaceCar extend? A RaceCar has no need of luggage or passenger capacity, so think about which class it can extend.

In the VehicleTester create a new RaceCar with these data and add to the vehicles array in the makeVehicleArray method: make, model, engine, VIN, year, color, gears, max torque.

Type Attributes

RaceCar "Lotus", "Elise", 1.6, "LE945756", "2015", "blue", 6, 118

Compile and run. Verify your results.

Hint: Increase the array size in the makeVehicleArray, before you add the race car to the array.

//TODO 6

Generate the UML diagram that should look like this. (Click on the icon with the little, connected green boxes just underneath the Window menu at the top of the editor pane.)

Hurrah! You have now completed the required section.

Zip the Lab10Inheritance folder file.

Remember to upload your LAB10Inheritance.zip file to Moodle.

4

Lab 10 Inheritance

ACTIVITY 3. To be completed only if you want to challenge yourself further.

Part 3.1

Add a static method to the VehicleTester class called numMakeModel which takes the parameters Vehicle array and Strings make and model. It returns the number of vehicles in the array with the make and model. It should return 2 for the make and model Honda, Fit.

Part 3.2

Implement the equals method in the class Vehicle. (Look up the class Object in the Java API. Since all classes are subclasses of Object, you are technically overriding the equals method since it is defined in Object). The equals method takes a Vehicle as a parameter and returns true if the Vehicle passed in is equal to this one. Two Vehicles are equal if they have the same makerName and model. Note that you are using the String class implementation of the equals method to help write the Vehicle classs equals method. Test your method:

System.out.println(vehicles[5].equals(vehicles[6]));

true

System.out.println(vehicles[5].equals(vehicles[7]));

false

Finally, comment out the equals method you just wrote. Compile and retest. You should get false in both cases. Why? What test would get a return of true?

public class Car extends Vehicle{

double luggageCap;

double passengerCap;

public Car(String make, String model, double engSz, String id, String year, String color, double lugCap, double passCap){

super(make, model, engSz, id, year, color);

luggageCap = lugCap;

passengerCap = passCap;

}

public double getLuggageCap(){

return luggageCap;

}

public double getPassengerCap(){

return passengerCap;

}

public String toString(){

return super.toString()+", "+luggageCap+", "+passengerCap;

}

}

public class Truck extends Vehicle{

private double maxWeight;

public Truck(String make, String model, double engSz, String id, String year, String color, double maxWt){

super(make, model, engSz, id, year, color);

maxWeight = maxWt;

}

public double getMaxWeight(){

return maxWeight;

}

public String toString(){

return super.toString()+", "+maxWeight;

}

}

public class Vehicle{

private String makerName;

private String model;

private double engineLiters;

private String VIN;

private String color;

private String year;

public Vehicle(String make, String model, double engSz,

String id, String year, String color){

makerName = make;

this.model = model;

engineLiters = engSz;

VIN = id;

this.color=color;

this.year=year;

}

public String getMake(){

return makerName;

}

public String getModel(){

return model;

}

public String getColor(){

return color;

}

public String getYear(){

return year;

}

public double getEngineLiters(){

return engineLiters;

}

public String getVIN(){

return VIN;

}

public boolean equals(Vehicle other){

return (this.makerName.equals(other.getMake()) && this.model.equals(other.getModel()));

}

public String toString(){

return makerName+" "+model+" "+engineLiters+" "+VIN+" "+year+" "+color;

}

}

public class VehicleTester { public static void main(String[] args){ Vehicle[] vehicles = makeVehicleArray(); printVehicles(vehicles); }//end main

public static Vehicle[] makeVehicleArray(){ Vehicle[] vehicles = new Vehicle[4]; vehicles[0] = new Car("Ford", "Pinto", 2.5, "F1234", "1972", "green", 2.3, 5); //ToDo1 vehicles[1] = new Truck ("Ford", "F150", 3.5, "F2345", "1990", "red", 2.25);

vehicles[2] = new Car ("Mazda", "Miata", 2.5, "M9862","2013", "black", 15.3, 5);

vehicles[3] = new Truck ("Chevy", "Silverado", 3.5, "C34521", "2011", "green", 2.25); //ToDo 4 //ToDo 5 return vehicles; }

//ToDo 2 public static void printVehicles(Vehicle[] vehicles) { //Todo for ( int j=0; j

} This is what i do.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Systems Design Implementation And Management

Authors: Peter Robb,Carlos Coronel

5th Edition

061906269X, 9780619062699

More Books

Students also viewed these Databases questions

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago