In: Computer Science
JAVA PROBLEM
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):
You will need other fields besides those listed above. These other fields will not be constants. Some of the other fields:
The Car class will also need at least 3 constructors:
The Car class must implement the following methods.
public fillTank(double): double
public toString():String
public equals(Car):boolean
public driveCar():boolean
public getTripOdometer():double
public clearTripOdometer():void
public getOdometer():double
public getFuelLevel():double
public getFuelTankSize():double
public setUpTrip(double, double): void
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,
Hint: Use “helper” methods to generate these random values.
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<Car> 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:
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.
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.
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.
public class Car {
private final String MAKE;
private final String MODEL;
private final int YEAR;
private final double TANK_SIZE;
private final double FUEL_ECONOMY;
private final double BEST_SPEED;
private double odometer;
private double tripeOdometer;
private String color;
private double fuelLevel;
private double speed;
private double distance;
//default constructor
public Car(){
this.MAKE =
"Ford";
this.MODEL = "M1";
this.color =
"Black";
this.YEAR = 1999;
this.TANK_SIZE =
getRandom(8, 35);
this.FUEL_ECONOMY =
getRandom(15, 55);
this.BEST_SPEED =
getRandom(45, 65);
this.odometer =
getRandom(0.0, 6.00);
this.tripeOdometer =
0.0;
this.fuelLevel =
0;
this.speed =
BEST_SPEED;
}
// constructor with parameters
public Car(String MAKE, String MODEL, String
color, int YEAR, double TANK_SIZE, double FUEL_ECONOMY, double
BEST_SPEED) {
this.MAKE = MAKE;
this.MODEL =
MODEL;
this.color =
color;
this.YEAR = YEAR;
this.TANK_SIZE =
TANK_SIZE;
this.FUEL_ECONOMY =
FUEL_ECONOMY;
this.BEST_SPEED =
BEST_SPEED;
this.odometer =
getRandom(0.0, 6.00);
this.tripeOdometer =
0.0;
this.fuelLevel =
0;
this.speed =
BEST_SPEED;
}
// constructor with another car as
parameter
public Car(Car c) {
this.MAKE =
c.MAKE;
this.MODEL =
c.MODEL;
this.color =
c.color;
this.YEAR =
c.YEAR;
this.TANK_SIZE =
c.TANK_SIZE;
this.FUEL_ECONOMY =
c.FUEL_ECONOMY;
this.BEST_SPEED =
c.BEST_SPEED;
this.odometer =
c.odometer;
this.tripeOdometer =
c.tripeOdometer;
this.fuelLevel =
c.fuelLevel;
this.speed =
c.speed;
this.distance =
c.distance;
}
// helper method to get random number between
two numbers
private double getRandom(double low, double
high) {
double value =
Math.random()*(high - low)+low;
return value;
}
// method to fill tank
public double fillTank(double fuel){
double remainingFuel =
fuel - (this.TANK_SIZE-this.fuelLevel);
if(remainingFuel<=0)
this.fuelLevel += fuel;
else
this.fuelLevel = this.TANK_SIZE;
return
remainingFuel;
}
@Override
public String toString() {
return YEAR +" "+color+"
"+MAKE+" "+MODEL;
}
// method to check this car equals another
car
public boolean equals(Car c){
return this.YEAR==c.YEAR
&& this.MAKE.equals(c.MAKE) &&
this.MODEL.equals(c.MODEL);
}
// method to drive car
public boolean driveCar(){
double drivableDistance
= this.fuelLevel*this.FUEL_ECONOMY;
if(this.distance<=drivableDistance){
this.odometer += this.distance;
this.tripeOdometer += this.distance;
double fuelUsed = this.distance*this.FUEL_ECONOMY;
this.fuelLevel -= fuelUsed;
return true;
}
this.odometer +=
drivableDistance;
this.tripeOdometer +=
drivableDistance;
this.fuelLevel =
0.0;
return false;
}
// getter methods
public double getTripOdometer(){
return
Math.round(tripeOdometer);
}
public void clearTripOdometer(){
this.tripeOdometer =
0.0;
}
public double getOdometer(){
return
Math.round(odometer);
}
public double getFuelLevel() {
return
Math.round(fuelLevel);
}
public double getFuelTankSize() {
return
Math.round(TANK_SIZE);
}
// method to set trip
public void setUpTrip(double distance, double
speed){
this.speed =
speed;
this.distance =
distance;
}
}
Completed PART 1. For part 2 post a new post with part 2 question
and this answer.