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


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



CSC 201<br>Car Garage and Driving Project<br>Part 1: Create a Car class in accordance with the following specifications. I will provide the<br>CarClassTester class as a test driver to test your Car class for its basic structure and operation.<br>Do not change the CarClassTester class source code. After you get your Car class working<br>correctly using this test driver, proceed to part 2 below.<br>Car Class Specifications:<br>The Car class must be in a separate package from any driver/tester program.<br>The Car class will contain, at a minimum, the following information as constants (in Java use<br>final to specify a constant):<br>make<br>model<br>year<br>fuel tank size<br>fuel economy – fuel economy at best speed<br>optimal speed – speed at which the car has the most efficient fuel economy<br>You will need other fields besides those listed above. These other fields will not be constants.<br>Some of the other fields:<br>odometer<br>trip odometer<br>color<br>fuel level<br>The Car class will also need at least 3 constructors:<br>Car() - a no argument constructor that initializes an instance using random values.<br>Similar to what l did with the Phonelisting class.<br>Car(String, String, String, int, double, double, double) – accepts arguments to initialize<br>the new Car object with make, model, color, year, tank size, fuel economy, and best<br>speed. You should also initialize the two odometers and the fuel level with random<br>values.<br>The Car class must implement the following methods.<br>package addFuelToTank(double): double<br>Adds fuel to the car's fuel tank<br>Precondition: Car has a fuel tank<br>Postcondition: Car's fuel tank may have added fuel<br>Parameter available fuel to add to fuel tank<br>returns: Negative number indicating the amount of fuel the tank will still take, Positive<br>nonzero value of the amount of argument fuel not used, if 0 it just filled the tank<br>public toString():String<br>Converts the Car object's state variables to a String representation<br>Precondition: All state variables are initialized<br>Postcondition: no change<br>Returns a string representation of state variables<br>

Extracted text: CSC 201 Car Garage and Driving Project 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 and operation. 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 l did with the Phonelisting 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 with random values. The Car class must implement the following methods. package addFuelToTank(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 add to fuel 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
Car Garage and Driving Project<br>public equals(Car):boolean<br>Checks to see if the calling Car and the argument Car have the same state<br>Precondition: Both the calling Car and argument Car are fully initialized<br>Postcondition: no change<br>parameter pCarObject<br>returns true if the calling Car and the argument Car have the same state values for year,<br>make, and model, else returns false<br>public driveCar():boolean<br>drives the Car a predefined distance and speed.<br>Precondition: Car's trip state variables have been initialized<br>Postcondition: Car's fuel is reduced proportional to the distance and speed driven or<br>depleted if the distance and speed are too great. Odometer and trip odometer are<br>updated with the miles traveled added. Car's trip state variables distance of travel and<br>speed of travel are set to zero.<br>Return: true if the car travels the distance with fuel remaining, false if the car runs out<br>of fuel<br>public getTripodometer():double<br>gets trip odometer<br>Precondition: none<br>Postcondition: no change of state<br>Return: double value of trip odometer to nearest tenth of mile<br>public clearTripOdometer():void<br>sets trip odometer mileage to 0.0<br>Precondition: none<br>Postcondition: trip odometer set to 0.0<br>public getOdometer():double<br>gets odometer mileage<br>Precondition: none<br>Postcondition: no change to state<br>Return: double value of odometer to nearest tenth of mile<br>public getFuellevell):double<br>retrieves fuel level in gallons<br>Precondition: fuel level is initialized<br>Postcondition: no change in state<br>Return: fuel level in gallons with decimal values<br>public getFuelTankSize():double<br>retrieves fuel level in gallons<br>Precondition: fuel level is initialized<br>Postcondition: no change is state<br>Return: fuel level in gallons with decimal values<br>

Extracted text: Car Garage and Driving Project 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 getFuellevell):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

Jun 03, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here