Question

In: Computer Science

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 should take one parameter, namely a value for the fuel efficiency). Additional method simulates the car traveling a given number of miles (at the end of traveling that user-specified distance, the odometer is updated and the gas tank level is reduced by an elementary calculation using the miles driven and fuel efficiency), to fill a given number of gallons to the tank, to get the odometer reading, and to get the gas tank level in gallons. (Test case that makes sure the tank isn’t already at capacity)

Solutions

Expert Solution

Please find the code below::

Car.java

package classes4;

public class Car {
   private final double gasolineTankCapacity = 12.5;
   private double tankFuel;
   private double odometer;
   private double fuelEfficiency;

   Car(){
       fuelEfficiency =23.4;
       tankFuel = 12.5;
       odometer = 0;
   }
   Car(double effIn){
       fuelEfficiency =effIn;
       tankFuel = 12.5;
       odometer = 0;
   }

   void carTraveling(double miles){
       double fuelUsed = 1/fuelEfficiency * miles;
       tankFuel -= fuelUsed;
       System.out.println("Fuel remaining is : "+tankFuel+" gallons");
       odometer +=miles;
   }


   void fillGas(double gasIn){
       if(tankFuel+gasIn <= gasolineTankCapacity){
           tankFuel+=gasIn;
       }else{
           System.out.println("Gas tank cannot have "+gasIn+" gallons of gasoline");
       }
   }

   double getOdometer(){
       return odometer;
   }  

  
   public static void main(String[] args) {
       Car car = new Car();
       System.out.println("New car odometer reading : "+car.getOdometer());
       System.out.println("Travelling to 10 miles");
       car.carTraveling(10);
       System.out.println("Car odometer reading : "+car.getOdometer());
       System.out.println("Filling 20 gallons of gas");
       car.fillGas(20);
       System.out.println("Filling .001 gallons of gas");
       car.fillGas(.001);
      
   }

}

output:


Related Solutions

Java programming language should be used Implement a class called Voter. This class includes the following:...
Java programming language should be used Implement a class called Voter. This class includes the following: a name field, of type String. An id field, of type integer. A method String setName(String) that stores its input into the name attribute, and returns the name that was just assigned. A method int setID(int) that stores its input into the id attribute, and returns the id number that was just assigned. A method String getName() that return the name attribute. A method...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for...
Java Programming Part 1 (20%) Implement a class with a main method. Using an enhanced for loop, display each element of this array: String[] names = {"alice", "bob", "carla", "dennis", "earl", "felicia"}; Part 2 (30%) In a new class, implement two methods that will each calculate and return the average of an array of numeric values passed into it. Constraints: your two methods must have the same name one method should accept an array of ints; the other should accept...
(USING JAVA) Implement a class Moth that models a moth flying along a straight line. The...
(USING JAVA) Implement a class Moth that models a moth flying along a straight line. The moth has a position which is the distance from a fixed origin. When the moth moves toward a point of light its new position is halfway between its old position and the position of the light source. Supply a constructor public Moth(double initialPosition) and methods public void moveToLight(double lightPosition) public double getPosition()
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for...
Use Java programming to implement the following: Implement the following methods in the UnorderedList class for managing a singly linked list that cannot contain duplicates. Default constructor Create an empty list i.e., head is null. boolean insert(int data) Insert the given data into the end of the list. If the insertion is successful, the function returns true; otherwise, returns false. boolean delete(int data) Delete the node that contains the given data from the list. If the deletion is successful, the...
Answer the following in Java programming language Create a Java Program that will import a file...
Answer the following in Java programming language Create a Java Program that will import a file of contacts (contacts.txt) into a database (Just their first name and 10-digit phone number). The database should use the following class to represent each entry: public class contact {    String firstName;    String phoneNum; } Furthermore, your program should have two classes. (1) DatabaseDirectory.java:    This class should declare ArrayList as a private datafield to store objects into the database (theDatabase) with the...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is...
Programming Language: C# CheckingAccount class You will implement the CheckingAccount Class in Visual Studio. This is a sub class is derived from the Account class and implements the ITransaction interface. There are two class variables i.e. variables that are shared but all the objects of this class. A short description of the class members is given below: CheckingAccount Class Fields $- COST_PER_TRANSACTION = 0.05 : double $- INTEREST_RATE = 0.005 : double - hasOverdraft: bool Methods + «Constructor» CheckingAccount(balance =...
Java Programming In this assignment we are going to create our own programming language, and process...
Java Programming In this assignment we are going to create our own programming language, and process it Java. programming language has 6 commands enter add subtract multiply divide return enter, add, subtract, multiply, divide all take 1 parameter (a double value). return takes no parameters.
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class,...
JAVA PROGRAMMING. In this assignment, you are to create a class named Payroll. In the class, you are to have the following data members: name: String (5 pts) id: String   (5 pts) hours: int   (5 pts) rate: double (5 pts) private members (5 pts) You are to create no-arg and parameterized constructors and the appropriate setters(accessors) and getters (mutators). (20 pts) The class definition should also handle the following exceptions: An employee name should not be empty, otherwise an exception...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a...
PUT IN JAVA PROGRAMMING LANGUAGE The Rectangle class: Design a class named Rectangle to represent a rectangle. The class contains: • Two double data fields named width and height that specify the width and height of a rectangle. The default values are 1 for both width and height. • A no-arg (default) constructor that creates a default rectangle. • A constructor that creates a rectangle with the specified width and height. • A method named findArea() that finds the area...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a...
PROGRAMMING LANGUAGE : JAVA Problem specification. In this assignment, you will create a simulation for a CPU scheduler. The number of CPU’s and the list of processes and their info will be read from a text file. The output, of your simulator will display the execution of the processes on the different available CPU’s. The simulator should also display: -   The given info of each process -   CPU utilization - The average wait time - Turnaround time for each process...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT