Car's state is set to hold the speed of travel and distance to travel at that speed Precondition: none Postcondition: Car's state holds information on distance to travel and speed to travel Parameters: Average Speed to be driven, Distance to drive Develop and use an algorithm that calculates the amount of fuel used and the actual distance driven in the drive() method. The algorithm must use a formula that gives proportionately poorer mileage when the Car is driven faster or slower than its optimal speed. When a new Car object is instantiated, it is initialized with an optimal speed variable. Your fuel usage algorithm should set limits on how poor of
public setUpTrip(double, double): void
- Car's state is set to hold the speed of travel and distance to travel at that speed
- Precondition: none
- Postcondition: Car's state holds information on distance to travel and speed to travel
- Parameters: Average Speed to be driven, Distance to drive
Develop and use an
You may add other methods and fields as needed.
When a new Car object is created,
- the car’s odometer is set to a random number between 0.0 and 5.99,
- the car’s trip odometer is set to 0.0,
- its best fuel economy (MPG) is set to a random number between 15.0 and 54.99,
- its optimal speed is set to a random number between 45.0 and 64.99,
- and its fuel tank size is set to a random number between 8 and 34.99 gallons.
Hint: Use “helper” methods to generate these random values.
- Use Math.random( ) to generate your random numbers. Remember Math.random( ) generates a random double number from 0.0 to but not including 1.0.
- So, to get a random number between 0.0 and 99.99 you must multiply the result of Math.random( ) by 100.
- To get a random number between 5 and 15(excluding 15), subtract 5 from 15 to get 10, multiply Math.random() by 10 then add 5.
- Example: If Math.random( ) produced 0.4584, multiplying it by 10 would produce 4.584. Then adding 5 would produce 9.584, which is a value between 5 and 15.
Since the new class Car inherits the .equals() and .toString() methods from the Java Object class, you will need to overload the .equals( ) method and override the .toString( ) method.
Part 2:
After you are comfortable with the Car class, create a driver program, CarTestDriver that uses a Garage object to store Cars. The Garage object is an instantiation of a Garage class that contains “parking”, an array of Car types. You must use a Car[] not an ArrayList<Car> for the “parking” in the garage. You will use Car objects to fill the garage. I suggest setting up the Car class with a default constructor that generates random values to create each new Car object.
The rules for driving the cars from the garage are:
- The size of the garage is specified by the user.
- The user may only use cars from the garage
- A Car is removed from the Garage when a user retrieves a Car from the Garage.
- The Car is returned to the Garage, after it is driven if it does not run out of fuel.
- The user interacts with the Car object after the Car object is retrieved from the garage.
- The program should not fail due to a user selection.
- A car may only be refueled when the user selects the Car for use, prior to being removed from the Garage
- The user may select to drive any car that is currently in the garage
- The user is the only one that may request that a car be refueled(do not refuel a car automatically)
- The program may not prompt the user to refuel.
- After the user gets a Car, they set up the drive by entering in the average speed and the driving distance.
See the Car methods above. - the driving distance is the round-trip distance from the garage and back again.
- The driver program is only allowed to use the public methods listed above, and those you create for the Garage class.
- The user drives the car by telling that car to drive. Again, you may use menus to offer options to the user.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 5 images