Question: you will create a Java program containing three classes named GasTank, Engine, and Car. These classes will be used to create Car objects which can
you will create a Java program containing three classes named GasTank, Engine, and Car. These classes will be used to create Car objects which can execute methods to compute locations and fuel levels after driving given distances and directions.
- The GasTank class represents an object to keep track of the current and maximum levels of fuel. It must:
- have a private instance variable that keeps track of an integer (maximum) capacity
- have a private instance variable that keeps track of a double (current) level
- have a constructor which takes one argument, an int, which is used to set the instance variable for capacity (but if a negative value is passed in it gets set to 0), while the current level is initialized to 0
- have the following member functions implemented:
public int getCapacity() public double getLevel() public void setLevel(double levelIn)
- the ``get'' functions should return the capacity and level
- the ``set'' function should take a double argument to set the level, but if the argument is below 0 it gets set to 0, or above the capacity it gets set to the capacity
- The Engine class represents an engine object and stores a few attributes of the engine. It must:
- have a private instance variable which stores a String description of the engine (e.g. "V8")
- have a private instance variable that keeps track of the integer miles per gallon (mpg) value
- have a private instance variable that keeps track of the integer maximum speed value
- have a constructor which takes three arguments (a String and 2 integers) which are used to initialize the three instance variables. If the String argument has length 0, the description should be set to the value "Generic engine", and if either integer argument is negative, the corresponding instance variable should be set to 0.
- have public ``get'' functions to return each of the description, mpg, and max speed. The function that returns the description should return the String description as well as the mpg and max speed, clearly labeled (e.g. "V6 (MPG: 20, Max speed: 120)")
- The Car class represents a car object which contains an engine and a gas tank, and has methods that allow it to keep track of its location and fuel level while being ``driven''. It must:
- have a private instance variable which stores a String description of the car (e.g. "Sweet Ride")
- have private instance variables that keep track of integer values for the x-coordinate and y-coordinate (and are initialized to 0)
- have a private instance GasTank variable
- have a private instance Engine variable
- have a constructor which takes as arguments a String description, an integer for the maximum fuel capacity, and an Engine object reference (in that order). If the description is length 0, the corresponding instance variable should be set to "Generic car", and if a null reference is passed in for the Engine parameter, a new Engine should be created with an empty description and 0's for all values.
- have the following member functions implemented:
public String getDescription() public int getX() public int getY() public double getFuelLevel() public int getMPG() public void fillUp() public int getMaxSpeed() public double drive(int distance, double xRatio, double yRatio)
- getDescription() should return the description of the car as well as the description of its Engine, fuel level and capacity, and location, all clearly labeled (e.g. "Sweet Ride (engine: V6 (MPG: 20, Max speed: 120)), fuel: 0.00/15, location: (0,0)")
- fillUp() should cause the gas tank to be filled to its maximum capacity
- drive(int distance, double xRatio, double yRatio) should take the desired distance to travel as an integer, and two double values which, together, specify the direction as a ratio of horizonal to vertical distances. For example, if xRatio = 1 and yRatio = 2, for every 1 unit to the right (positive x), the car would travel 2 units up (positive y). Negative values for xRatio and yRatio correspond to moving left and down, respectively. Given the distance and direction, this method should correctly compute the ending coordinates (tuncated to integers). It should also correctly subtract the amount of fuel used. If the distance specified requires greater than the amount of fuel than the car currently has, the car should travel as far as it can until it is empty and print this text to the console: "Ran out of gas after driving x miles." (with the correct value substituted in for x). The method should return the number of miles which were actually driven.
- All double values which are printed out to the console should be formatted so that exactly 2 decimal places are displayed.
- Test your work. An example class named CarTest.java is provided (on the course site along with assignment 2) to help you test your code. Your classes must work with it, i.e. they must all compile correctly together and CarTest.java should work correctly when executed. However, when grading we will use more and/or different test cases, so you should also test various additional cases. Do not submit CarTest.java. Here is the output that my implementation gives with this file:
Tank level: 10.00 Engine1: V6 (MPG: 20, Max speed: 120) Car1: Car1 (engine: V6 (MPG: 20, Max speed: 120)), fuel: 0.00/15, location: (0,0) Car2: Generic car (engine: Generic engine (MPG: 0, Max speed: 0)), fuel: 0.00/0, location: (0,0) Car3: Car3 (engine: V6 (MPG: 20, Max speed: 120)), fuel: 0.00/50, location: (0,0) Car3: Car3 (engine: V6 (MPG: 20, Max speed: 120)), fuel: 50.00/50, location: (0,0) Car3 drove 600.00 miles Car3: Car3 (engine: V6 (MPG: 20, Max speed: 120)), fuel: 20.00/50, location: (-480,-360) Car3 drove 300.00 miles Car3: Car3 (engine: V6 (MPG: 20, Max speed: 120)), fuel: 5.00/50, location: (-230,-193) Ran out of gas after driving 100.00 miles. Car3 drove 100.00 miles Car3: Car3 (engine: V6 (MPG: 20, Max speed: 120)), fuel: 0.00/50, location: (-130,-193) Car3 drove a total of 1000.00 miles. Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 0.00/30, location: (0,0) Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 30.00/30, location: (0,0) Car4 drove 100.00 miles Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 26.67/30, location: (70,70) Car4 drove 200.00 miles Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 20.00/30, location: (-115,144) Car4 drove 300.00 miles Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 10.00/30, location: (153,278) Ran out of gas after driving 300.00 miles. Car4 drove 300.00 miles Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 0.00/30, location: (287,9) Ran out of gas after driving 0.00 miles. Car4 drove 0.00 miles Car4: Car4 (engine: V4 (MPG: 30, Max speed: 100)), fuel: 0.00/30, location: (287,9) Car4 drove a total of 900.00 miles.
Note that if your values are within 1.0, that is sufficient.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
