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...
1, Create a class Car. The class has four instance fields: make (String - admissible values:...
1, Create a class Car. The class has four instance fields: make (String - admissible values: Chevy, Ford, Toyota, Nissan, Hyundai) size (String - admissible values: compact, intermediate, fullSized), weight (int - admissible values: 500 <= weight <= 4000) horsePower (int - admissible values: 30 <= horsePower <= 400) Provide a default constructor that sets String values to null and int values to 0 a parameterized constructor that sets all four instance fields to the parameter values setters and getters...
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
Java Write a class called Car that contains instance data that represents the make, model, and...
Java Write a class called Car that contains instance data that represents the make, model, and year of the car. Define the Car constructor to initialize these values Include getter and setter methods for all instance data and a toString method that returns a one-line description of the car. Add a method called isAntique that returns a boolean indicating if the car is an antique (if it is more than 45 years old). Create a driver class called CarTest, whose...
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....
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT