Question

In: Computer Science

JAVA PROBLEM Part 1: Create a Car class in accordance with the following specifications. I will...

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):

  • make
  • model
  • year
  • fuel tank size
  • fuel economy – fuel economy at best speed
  • 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:

  • odometer
  • trip odometer
  • color
  • fuel level

The Car class will also need at least 3 constructors:

  • Car() – a no argument constructor that initializes an instance using random values. Similar to what we did with the Dog class.
  • 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.
  • 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

  • Adds fuel to the car's fuel tank
  • Precondition: Car has a fuel tank
  • Postcondition: Car's fuel tank may have added fuel
  • Parameter available fuel to fill tank
  • 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

  • Converts the Car object's state variables to a String representation
  • Precondition: All state variables are initialized
  • Postcondition: no change
  • Returns a string representation of state variables

public equals(Car):boolean

  • Checks to see if the calling Car and the argument Car have the same state
  • Precondition: Both the calling Car and argument Car are fully initialized
  • Postcondition: no change
  • parameter pCarObject
  • 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

  • drives the Car a predefined distance and speed.
  • Precondition: Car's trip state variables have been initialized
  • 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.
  • Return: true if the car travels the distance with fuel remaining, false if the car runs out of fuel

public getTripOdometer():double

  • gets trip odometer
  • Precondition: none
  • Postcondition: no change of state
  • Return: double value of trip odometer to nearest tenth of mile

public clearTripOdometer():void

  • sets trip odometer mileage to 0.0
  • Precondition: none
  • Postcondition: trip odometer set to 0.0

public getOdometer():double

  • gets odometer mileage
  • Precondition: none
  • Postcondition: no change to state
  • Return: double value of odometer to nearest tenth of mile

public getFuelLevel():double

  • retrieves fuel level in gallons
  • Precondition: fuel level is initialized
  • Postcondition: no change in state
  • Return: fuel level in gallons with decimal values

public getFuelTankSize():double

  • retrieves fuel level in gallons
  • Precondition: fuel level is initialized
  • Postcondition: no change is state
  • Return: fuel level in gallons with decimal values

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 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,

  • 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 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:

  • 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.

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.

Solutions

Expert Solution


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.


Related Solutions

Part 1: Create a Car class in accordance with the following specifications. make model year fuel...
Part 1: Create a Car class in accordance with the following specifications. make model year fuel tank size fuel economy – fuel economy at best speed optimal speed Some of the other fields: odometer trip odometer color fuel level The Car class will also need at least 3 constructors: Car() Car(String, String, String, int, double, double, double) Car(Car) 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...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include...
Part 1 – Create a Stock Class Write a class named Stock. Stock Class Specifications Include member variables for name (string), price (double), shares (double). Write a default constructor. Write a constructor that takes values for all member variables as parameters. Write a copy constructor. Implement Get/Set methods for all member variables. Implement the CalculateValue function. This function should multiply the prices by the shares and return that value. Use the following function header: double CalculateValue(). Add a member overload...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following:...
Simple Java class. Create a class Sportscar that inherits from the Car class includes the following: a variable roof that holds type of roof (ex: convertible, hard-top,softtop) a variable doors that holds the car's number of doors(ex: 2,4) implement the changespeed method to add 20 to the speed each time its called add exception handling to the changespeed method to keep soeed under 65 implement the sound method to print "brooom" to the screen. create one constructor that accepts the...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED...
JAVA CODING PLEASE Create a class SportsCar that inherits from the Car class (CAR CLASS LISTED BELOW THIS QUESTION). Include the following: A variable Roof that holds the type of roof (Ex: Convertible, Hard-Top, Soft-Top) A variable Doors that holds the car’s number of doors (Ex: 2, 4) Implement the ChangeSpeed method to add 20 to the speed each time it is called. Add exception handling to the ChangeSpeed method to keep the speed under 65. Implement the sound method...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called  Dog that contains instance data that represent the dog's name and age.   define the Dog constructor to accept and initialize instance data.   create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age).   Include a toString...
In java program format Submit your completed UML class diagram and Java file. Part I: Create...
In java program format Submit your completed UML class diagram and Java file. Part I: Create a UML diagram for this assignment PartII: Create a program that implements a class called Dog that contains instance data that represent the dog's name and age. define the Dog constructor to accept and initialize instance data. create a method to compute and return the age of the dog in "person-years" (note: dog age in person-years is seven times a dog's age). Include a...
Using the Java programming language: Create and implement a class Car that models a car. A...
Using the Java programming language: Create and implement a class Car that models a car. A Car is invented to have a gasoline tank that can hold a constant max of 12.5 gallons, and an odometer that is set to 0 for a new car. All cars have an original fuel economy of 23.4 miles per gallon, but the value is not constant. Provide methods for constructing an instance of a Car (one should be zero parameter, and the other...
I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination...
I am stuck on this Java problem: Create an Animal class with: Attributes Age Rabies Vaccination Status Name Owner Name A constructor to set values Getters and Setters for all private attributes A toString method that gives the data of all attributes Create a Dog Class Attributes Distemper Vaccination Status A constructor to set values in sub class A constructor to set values in sub and super class Getters and Setters for private attributes A toString method that overrides the...
Create a Java class file for a Car class. In the File menu select New File......
Create a Java class file for a Car class. In the File menu select New File... Under Categories: make sure that Java is selected. Under File Types: make sure that Java Class is selected. Click Next. For Class Name: type Car. For Package: select csci2011.lab7. Click Finish. A text editor window should pop up with the following source code (except with your actual name): csci1011.lab7; /** * * @author Your Name */ public class Car { } Implement the Car...
Part A Java netbeans ☑ Create a project and in it a class with a main....
Part A Java netbeans ☑ Create a project and in it a class with a main. We will be using the Scanner class to read from the user. At the top of your main class , after the package statement, paste import java.util.Scanner; As the first line inside your main method, paste Scanner scan = new Scanner(System.in); Remember when you are getting a value from the user, first print a request, then use the Scanner to read the right type...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT