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

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.
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
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...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model,...
(JAVA) Create three classes, Vehicle, Truck, and Person. The Vehicle class has a make and model, test emissions per mile, and a driver/owner (= Person object). (Each vehicle can only have one driver/owner.) The class Truck is a derived class from the Vehicle class and has additional properties load capacity in tons (type double since it can contain a fractional part) and a towing capacity in pounds (type int). Be sure that your classes have appropriate constructors, accessors, mutators, equals(),...
1. Three different makes of cars were tested for fuel efficiency. For each make of car...
1. Three different makes of cars were tested for fuel efficiency. For each make of car eighteen automobiles were randomly selected and subjected to standard driving procedures. Using the data from the following results compute the between treatments estimate of σ² and the within treatments estimate of σ². At the α=.05 level of significance, can the null hypothesis, that all fuel economies are the same, be rejected? Construct the ANOVA table for this data. (Use the formulas to compute the...
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another...
1. Create a class Car with data: Name, Price, Production and properly methods. 2. Create another class named GenericCar with a parameter of the T type. This class manages a collection of object T (may be LinkedList) named a. Implementing some methods for GenericCar: Add: add new item of T to a Display: display all items of a getSize: return the number item of a checkEmpty: check and return whether a is empty or not delete(int pos): remove the item...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT