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...
Choose one of the following cryptography techniques and implement it using (java )programming language. Your program...
Choose one of the following cryptography techniques and implement it using (java )programming language. Your program should provide the user with two options Encryption and Decryption, with a simple UI to get the input from the user, and view the result. You can use any restriction you need for the user input but you need to clarify that and validate the user input base on your restriction. ● Feistel ● Keyword columnar ● Any cryptosystem of your choice (needs to...
(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()
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...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters,...
its java language. 1. Create a class called Car. A Car has instance variables, constructor parameters, setters, and getters for “year manufactured” and “make” and “model” (e.g. 2016 Honda Civic) 2. Create a class called CarLot which has an array of 5 Car references. There are two constructors: (a) One constructor takes no parameters and simply populates the array with these cars: cars[0] = new Car(2016, “honda”, “civic”); cars[2] = new Car(2017, “Lamborghini”, “aventador”); cars[3] = new Car(2000, null, “caravan”);...
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...
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 =...
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...
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.
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor...
This is for Javascript programming language: A. Class and Constructor Creation Book Class Create a constructor function or ES6 class for a Book object. The Book object should store the following data in attributes: title and author. Library Class Create a constructor function or ES6 class for a Library object that will be used with the Book class. The Library object should be able to internally keep an array of Book objects. B. Methods to add Library Class The class...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT