Question: Answer the following in Java: You must use the starting interface and class definitions [Location of starting code] because this assignment will build on the
Answer the following in Java:
You must use the starting interface and class definitions [Location of starting code] because this assignment will build on the code provided below. Unless explicitly mentioned in the assignment you cannot modify the existing code in the interface and these classes. You need to add new code specified to the classes.
Class Car:
- Implement the body of the method start() which sets the engineOn attribute to true.
- Write the header definition1 for the abstract method travelOneUnit() which takes no arguments and returns void. Because the method is abstract it should not have curly braces to enclose a body.
Class Compact:
- Write the body for method travelOneUnit() which is non-abstract in this class. The method will reduce the fuel of the car by the amount specified by the named constant defined in this class FUEL_CONSUMPTION. Because the fuel attribute is private to class Car, the travelOneUnit() method must make the change via the consumeFuel() method of class Car.
Class SUV:
- Write the body for method travelOneUnit() which is non-abstract in this class. The method will reduce the fuel of the car by the amount specified by the value stored in the named constant defined in this class, SUV, FUEL_CONSUMPTION. (Note: the value of the constant will be different value than the value stored in class Compact!) Again the consumeFuel() method of class Car must be used to change the value of the fuel attribute.
- Write the body the method toggleAllWheelDrive() which flips the state of the attribute allWheelDriveEngaged.
Class Driver (the previous features need to be implemented first)
- In specified location in main() before the second set of output messages write the code to get the cars in each of the array elements to travel 1 unit using the travelOneUnit() method.
- In specified location in main() before the third set of output messages write the code to get the SUV to toggle the AWD mechanism using the toggleAllWheelDrive() method.
1 A header definition includes: method name, return type and parameter information.
UML class diagram
Draw out a UML diagram for the following classes: Car, Compact, SUV, as well the Vehicle interface. Make sure you use the correct notation for the attributes, methods, access permissions and relationships. Don't use the 'lollipop' notation for the interface because you need to specify the method signatures.
Car Class:
public abstract class Car implements Vehicle { public static final int MAX_FEUL = 40; private int fuel; private boolean engineOn; // The no argument constructor public Car() { fuel = MAX_FEUL; engineOn = false; } // The consumeFeul method was written by James Tam public void consumeFeul(int amount) { int temp = fuel - amount; if (temp >= 0) fuel = temp; else System.out.println("Consuming " + amount + " of fuel puts current " + "fuel at " + temp + " units."); } //Students must implement the method start() public String toString() { String s = "Fuel: " + this.fuel + ", " + "Engine on: " + engineOn; return(s); } //You need to write the header definition for the abstract method travelOneUnit() } Compact Class:
public class Compact extends Car { public static final int FUEL_CONSUMPTION = 1; //You need to write the code for travelOneUnit() } Driver Class:
public class Driver { public static void main (String [] args) { final int SIZE = 2; Car [] myCars = new Car[SIZE]; myCars[0] = new Compact(); myCars[1] = new SUV(); //First set of output messages System.out.println(myCars[0]); System.out.println(myCars[1] + " "); //Second set of output messages //<<< You must write the code to get the cars at both // array elements travel 1 unit >>> //<<< End of your code >>> System.out.println(myCars[0]); System.out.println(myCars[1] + " "); //Third set of output messages //<<< You must write the code to get the SUV to toggle // the state of the AWD >>> //<<< End of your code >>> System.out.println(myCars[0]); System.out.println(myCars[1]); } }
SUV class:
public class SUV extends Car { public static final int FUEL_CONSUMPTION = 3; private boolean allWheelDriveEngaged; // The no argument constructor public SUV() { allWheelDriveEngaged = false; } //You must write the code for consumeFeul() //You must write the code for toggleAllWheelDrive() } Vehicle Class:
public interface Vehicle { public void start(); public void travelOneUnit(); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
