Question

In: Computer Science

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 getOdometer():double

    public getFuelLevel():double

    public getFuelTankSize():double

    public setUpTrip(double, double): void

Solutions

Expert Solution

/**
* Car Class Implementation
*/
public class Car {

   private String make;
   private String model;
   private String color;
   private int year;
   private double tankSize;
   private double fuelEconomy;
   private double optimalSpeed;
   private double fuelLevel;
   private double odometer;
   private double tripOdometer;
  

//default constructor  
   public Car() {
       make="";
       model="";
       color="";
       year=9999;
       tankSize=0.0;
       fuelEconomy=0.0;
       optimalSpeed=0.0;
   }
  
//parameterised constructor
   public Car(String make, String model, String color, int year, double tankSize, double fuelEconomy,
           double optimalSpeed) {
       super();
       this.make = make;
       this.model = model;
       this.color = color;
       this.year = year;
       this.tankSize = tankSize;
       this.fuelEconomy = fuelEconomy;
       this.optimalSpeed = optimalSpeed;
   }

   //copy constructor
   public Car(Car car) {
       make = car.make;
       model = car.model;
       color = car.color;
       year = car.year;
       tankSize = car.tankSize;
       fuelEconomy = car.fuelEconomy;
       optimalSpeed = car.optimalSpeed;
   }
  
//fill the tank will fuel
   public double fillTank(double fuel) {
       double remainingCapacity = tankSize - fuelLevel;
       if (fuel > remainingCapacity) {
           System.out.println("Current capacity of the tank is: " + remainingCapacity);
           System.out.println(remainingCapacity + " fuel is sufficient for the tank to fill");
           fuelLevel = tankSize;
       }
       else if (fuel < remainingCapacity){
           System.out.println("Fuel is less than the tank current Capacity: " + remainingCapacity);
           fuelLevel = fuelLevel + fuel;
       }
       else if (fuel == remainingCapacity) {
           System.out.println("Tank is full with the fuel");
           fuelLevel = tankSize;
       }
       return fuelLevel;
   }
  


   @Override
   public String toString() {
       return "Car [make=" + make + ", model=" + model + ", color=" + color + ", year=" + year + ", tankSize="
               + tankSize + ", fuelEconomy=" + fuelEconomy + ", optimalSpeed=" + optimalSpeed + ", fuelLevel="
               + fuelLevel + "]";
   }

   //compare the two car objects
   public boolean equals(Car obj) {
       if (this == obj)
           return true;
       if (obj == null)
           return false;
       if (getClass() != obj.getClass())
           return false;
       Car other = (Car) obj;
       if (color == null) {
           if (other.color != null)
               return false;
       } else if (!color.equals(other.color))
           return false;
       if (Double.doubleToLongBits(fuelEconomy) != Double.doubleToLongBits(other.fuelEconomy))
           return false;
       if (make == null) {
           if (other.make != null)
               return false;
       } else if (!make.equals(other.make))
           return false;
       if (model == null) {
           if (other.model != null)
               return false;
       } else if (!model.equals(other.model))
           return false;
       if (Double.doubleToLongBits(optimalSpeed) != Double.doubleToLongBits(other.optimalSpeed))
           return false;
       if (Double.doubleToLongBits(tankSize) != Double.doubleToLongBits(other.tankSize))
           return false;
       if (year != other.year)
           return false;
       return true;
   }
  
//check if the car is started
   public boolean driveCar() {
       if (this.tripOdometer > 0) {
           return true;
       }
       return false;
   }

   //return the tripOdometer
   public double getTripOdometer() {
       return this.tripOdometer;
   }
//clearing the tripOdometer after the ride or before ride
   public void clearTripOdometer() {
       this.tripOdometer = 0.0;
   }
//get the odometer reading
   public double getOdometer() {
       return this.odometer;
   }
//get the fuel level
   public double getFuelLevel() {
       return this.fuelLevel;
   }
//get the tank size
   public double getFuelTankSize() {
       return this.tankSize;
   }
//start the trip
   public void setUpTrip(double fuel, double optimalSpeed) {
       this.clearTripOdometer();
       this.fillTank(fuel);
       this.optimalSpeed = optimalSpeed;
   }
  
}


Related Solutions

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...
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...
This code in java: Create a class named car. A car has color, model, company, registration...
This code in java: Create a class named car. A car has color, model, company, registration number. You can stear a car. A car can move forward. A car has a gear box. A typical gear decide weather car is moving forward or backward. A person owns a car. Kindly add some other functionalities like refuel
MATLAB based problem: Create a motoring model for the engine with the following specifications, and plot...
MATLAB based problem: Create a motoring model for the engine with the following specifications, and plot the in-cylinder pressure against (i) cylinder volume and (ii) crank angle degrees (20 points)  Bore = 107mm, stroke = 124mm, compression ratio = 17.3, Length of connecting rod = 192mm. Assume the ratio of specific heats is 1.4 (constant), atmospheric pressure is 1 bar and ambient temperature is 298 K.
Create a car class with three attributes: year, mpg, speed and list of owners. The class...
Create a car class with three attributes: year, mpg, speed and list of owners. The class also should have 2 methods called accelerate and brake. Whenever the car accelerates, its speed is increased by 30 and whenever the car brakes, its speed is reduced by 60. add a constructor to the class, which takes year, mpg, and speed as input implement a magic method that prints the car information including year, speed and mpg. That is, for a car object,...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to...
1. Create a class named Mobile 2. Add IEMICode, processor, make, model and color properties to the Mobile class 3. Create a methods connectBlueTooth, sendMessage, changeColor, displayInfo in the class Mobile. 4. Write a python script that creates two instances of the class Mobile, changes their colors to Black and Pink and prints a message to the console to display the object attributes using the displayInfo method 5. Run the program and observe the message in Console
Car Class Write a class named Car that has the following member variables: • year. An...
Car Class Write a class named Car that has the following member variables: • year. An int that holds the car’s model year. • make. A string object that holds the make of the car. • speed. An int that holds the car’s current speed. In addition, the class should have the following member functions. • Constructor. The constructor should accept the car’s year and make as arguments and assign these values to the object’s year and make member variables....
Create an Automobile class for a dealership. Include fields for an ID number, make, model, color,...
Create an Automobile class for a dealership. Include fields for an ID number, make, model, color, year, vin number, miles per gallon, and speed. Include get and set methods for each field. Do not allow the ID to be negative or more than 9999; if it is, set the ID to 0. Do not allow the year to be earlier than 2000 or later than 2017; if it is, set the year to 0. Do not allow the miles per...
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...
Write a program to have a Car class which is comparable. Make the Car class comparable...
Write a program to have a Car class which is comparable. Make the Car class comparable and use speed to compare cars. Write a method in Main class that finds the minimum of 4 things (generically). Create 4 cars and pass the cars to the minimum method and find the smallest car. Have a toString method for the Car class. The main program should be able to find out how many cars are created so far by calling a static...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT