Part 1: Create a Car class in accordance with the following specifications. I will provide the CarClassTester class as a test driver to test your Car class for its basic structure. Do not change the CarClassTester class source code. After you get your Car class working correctly using this test driver, proceed to part 2 below.
Car Class Specifications:
The Car class must be in a separate package from any driver/tester program.
The Car class will contain, at a minimum, the following information as constants (in Java use final to specify a constant):
1) make
2) model
3) year
4) fuel tank size
5) fuel economy – fuel economy at best speed
6) optimal speed – speed at which the car has the most efficient fuel economy
You will need other fields besides those listed above. These other fields will not be constants. Some of the other fields:
1) odometer
2) trip odometer
3) color
4) fuel level
The Car class will also need at least 3 constructors:
1)
Car() – a no argument constructor that initializes an instance using random values. Similar to what we did with the Dog class.
2)
Car(String, String, String, int, double, double, double) – accepts arguments to initialize the new Car object with make, model, color, year, tank size, fuel economy, and best speed. You should also initialize the two odometers and the fuel level.
3)
Car(Car) – accepts a Car argument to initialize the same state variables in the new Car object as in the Car(String, String, String, int, double, double, double) constructor.
The Car class must implement the following methods.
public fillTank(double): double
1) Adds fuel to the car's fuel tank
2) Precondition: Car has a fuel tank
3) Postcondition: Car's fuel tank may have added fuel
4) Parameter available fuel to fill tank
5) returns: Negative number indicating the amount of fuel the tank will still take, Positive nonzero value of the amount of argument fuel not used, if 0 it just filled the tank
public toString():String
1) Converts the Car object's state variables to a String representation
2) Precondition: All state variables are initialized
3) Postcondition: no change
4) Returns a string representation of state variables
public equals(Car):boolean
1) Checks to see if the calling Car and the argument Car have the same state
2) Precondition: Both the calling Car and argument Car are fully initialized
3) Postcondition: no change
4) parameter pCarObject
5) returns true if the calling Car and the argument Car have the same state values for year, make, and model, else returns false
public driveCar():boolean
1) drives the Car a predefined distance and speed.
2) Precondition: Car's trip state variables have been initialized
3) Postcondition: Car's fuel is reduced proportional to the distance and speed driven or depleted if the distance and speed are too great. Odometer and trip odometer are updated with the miles traveled added. Car's trip state variables distance of travel and speed of travel are set to zero.
4) Return: true if the car travels the distance with fuel remaining, false if the car runs out of fuel
public getTripOdometer():double
1) gets trip odometer
2) Precondition: none
3) Postcondition: no change of state
4) Return: double value of trip odometer to nearest tenth of mile
public clearTripOdometer():void
1) sets trip odometer mileage to 0.0
2) Precondition: none
3) Postcondition: trip odometer set to 0.0
public getOdometer():double
1) gets odometer mileage
2) Precondition: none
3) Postcondition: no change to state
4) Return: double value of odometer to nearest tenth of mile
public getFuelLevel():double
1) retrieves fuel level in gallons
2) Precondition: fuel level is initialized
3) Postcondition: no change in state
4) Return: fuel level in gallons with decimal values
public getFuelTankSize():double
1) retrieves fuel level in gallons
2) Precondition: fuel level is initialized
3) Postcondition: no change is state
4) Return: fuel level in gallons with decimal values
public setUpTrip(double, double): void
1) Car's state is set to hold the speed of travel and distance to travel at that speed
2) Precondition: none
3) Postcondition: Car's state holds information on distance to travel and speed to travel
4) Parameters: Average Speed to be driven, Distance to drive
Develop and use an algorithm that calculates the amount of fuel used and the accrued 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 MPG your car will get.
You may add other methods and fields as needed.
When a new Car object is created,
1) the car’s odometer is set to a random number between 0.0 and 5.99,
2) the car’s trip odometer is set to 0.0,
3) its best fuel economy (MPG) is set to a random number between 15.0 and 54.99,
4) its optimal speed is set to a random number between 45.0 and 64.99,
5) 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 has a garage, an instantiation of a Garage class that contains an array of Car types. You must use a Car[] not an ArrayList 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
The user interacts with the Car object after the Car object is retrieved from the garage.
If a car is driven and runs out of fuel while it is being driven it is removed from the garage
The user may not use a car that has been removed from the garage.
The program should not fail due to a user selection.
A car may only be refueled when the user retrieves it 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, however, when a car is selected from the garage you may display a menu of options for the user to choose from that includes a refueling option.
The user sets 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.
Example of running the car object tester:
Car One: 1927 Black Ford Model T
Car Two: 1923 Black Ford Model T
The two cars are different Car objects.
The two cars are not the same make, model, color, and year.
Car One: 1927 Black Ford Model T
Car Three: 1908 Red Ford Model T
The two cars are different Car objects.
The two cars are not the same make, model, color, and year.
Car One: 1927 Black Ford Model T
Car Clone: 1927 Black Ford Model T
The two cars are different Car objects.
The two cars are the same make, model, color, and year.
Car One: 1927 Black Ford Model T
Same Car object: 1927 Black Ford Model T
The two cars are in fact the same Car object.
The two cars are the same make, model, color, and year.
Let's take a car out for a drive!
1927 Black Ford Model T
Current fuel level in gallons is 31.9 gallons.
Fuel tank size is 31.9 gallons.
Current fuel level is 1.0 tank.
Odometer reading is 3 miles.
Trip Odometer reading is 0.0 miles.
We will drive the 1927 Black Ford Model T 100.4 miles at 45.6 miles per hour.
The state of the car you just drove.
*****The output***
Car One: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
Car Two: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 2.47 miles on the odometer, and 0.00 miles on the trip odometer.
The the two cars are different Car objects.
The the two cars are the same make, model, color, and year.
Car One: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
Car Three: 1903 Red Ford Model T, with 30.00 gallons of gas in the tank, 4.56 miles on the odometer, and 0.00 miles on the trip odometer.
The the two cars are different Car objects.
The the two cars are not the same make, model, color, and year.
Car One: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
Car Clone: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
The the two cars are different Car objects.
The the two cars are the same make, model, color, and year.
Car One: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
Same Car object: 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
The the two cars are in fact the same Car object.
The the two cars are the same make, model, color, and year.
Let's take a car out for a drive!
I have a garage with 3 cars.
Which car would you like to drive!
A) 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer.
B) 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 2.47 miles on the odometer, and 0.00 miles on the trip odometer.
C) 1903 Red Ford Model T, with 30.00 gallons of gas in the tank, 4.56 miles on the odometer, and 0.00 miles on the trip odometer.
Please select one of the menu choices.
a
You have chosen to drive the 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 1.84 miles on the odometer, and 0.00 miles on the trip odometer..
Please enter how far you would like to drive.
200
Please enter how fast you would like to drive.
55
Hooray, you did not run out of fuel!
Would you like to drive some more?
Please enter "yes" or "no".
yes
Let's take a car out for a drive!
I have a garage with 3 cars.
Which car would you like to drive!
A) 1903 Black Ford Model A, with 23.95 gallons of gas in the tank, 201.84 miles on the odometer, and 200.00 miles on the trip odometer.
B) 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 2.47 miles on the odometer, and 0.00 miles on the trip odometer.
C) 1903 Red Ford Model T, with 30.00 gallons of gas in the tank, 4.56 miles on the odometer, and 0.00 miles on the trip odometer.
Please select one of the menu choices.
a
You have chosen to drive the 1903 Black Ford Model A, with 23.95 gallons of gas in the tank, 201.84 miles on the odometer, and 200.00 miles on the trip odometer..
Please enter how far you would like to drive.
1000
Please enter how fast you would like to drive.
85
You only went 716.85 miles, before you ran out of gas.Oops, you ran out of fuel!
Would you like to drive some more?
Please enter "yes" or "no".
yes
Let's take a car out for a drive!
I have a garage with 3 cars.
Which car would you like to drive!
A) 1903 Black Ford Model A, with 0.00 gallons of gas in the tank, 918.69 miles on the oddometer, and 916.85 miles on the trip odometer.
B) 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 2.47 miles on the odometer, and 0.00 miles on the trip odometer.
C) 1903 Red Ford Model T, with 30.00 gallons of gas in the tank, 4.56 miles on the odometer, and 0.00 miles on the trip odometer.
Please select one of the menu choices.
c
You have chosen to drive the 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 2.47 miles on the odometer, and 0.00 miles on the trip odometer..
Please enter how far you would like to drive.
200
Please enter how fast you would like to drive.
55
Hooray, you did not run out of fuel!
Would you like to drive some more?
Please enter "yes" or "no".
no
The state of the cars are now.
A) 1903 Black Ford Model A, with 0.00 gallons of gas in the tank, 918.69 miles on the odometer, and 916.85 miles on the trip odometer.
B) 1903 Black Ford Model A, with 30.00 gallons of gas in the tank, 2.47 miles on the odometer, and 0.00 miles on the trip odometer.
C) 1903 Red Ford Model T, with 23.95 gallons of gas in the tank, 204.56 miles on the odometer, and 200.00 miles on the trip odometer.
1927 Black Ford Model T
Current fuel level in gallons is 29.0 gallons.
Fuel tank size is 31.9 gallons.
Current fuel level is 0.9 tank.
Odometer reading is 103 miles.
Trip Odometer reading is 100.4 miles.