Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a class called Car.java with the following variables, constructor and meth

ID: 3836769 • Letter: W

Question

Write a class called Car.java with the following variables, constructor and methods. Variables: All variables given below must be declared as private except the color variable. color is a string value indicating the color of the car. brand is a string value indicating the brand name of the car. model is a string value indicating the model name of the car. year is an integer value indicating the production year of the car. kilometerslnK is a double value indicating how many kilometers the car has travelled. fuelType is a boolean value indicating if the car uses Gasoline (true) or Diesel (false). Write a constructor which gets color of the car as parameter and sets the related variable. First, write get and set methods for private variables. Read Information reads user inputs as in the sample output given below and assigns them to related variables. Brand: Nissan Model: Juke Year: 2013 Kilometers (in thousand): 61.3 Fuel Type (Gasoline/Diesel): Diesel priceOfCar gets an integer value (1 for sale or 2 for rent) as parameter and calculates the price of the car according to the equations, and it prints the result as in sample output given below. (You should write either 'Price for sale' or 'Price for rent according to provided parameter.) Price for sale of this car is 214750.0 TL. Sale = 100000 - (kilometersitxK * 10) - (ageOfCar * 1000) Also, add 10000 to this amount if fuel type of the car is Diesel. Rent = 200 - (kilometersinK * 0.5) - (ageOfCar * 2) Also, subtract 10 from this amount if fuel type of the car is Gasoline. printInformation prints information as given in the sample output below. (If car uses Gasoline, you should write 'using gasoline as fuel", otherwise you should write 'using diesel as fuel'.) This red car is Nissan's "Duke". It has travelled 61300.0 KM since 2013 using diesel as fuel. areCarsEqual gets one Car object as parameter and compares two cars in terms of their brand, model, year. kilometersInK and fuelType variables. Finally, it returns a boolean value indicating whether all are same or not.

Explanation / Answer


import java.util.Calendar;

public class Car {

    public String color;
    private String brand;
    private String model;
    private int year;
    private double kilometersInk;
    private boolean fuel;

    public Car(String color) {
        this.color = color;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public double getKilometersInk() {
        return kilometersInk;
    }

    public void setKilometersInk(double kilometersInk) {
        this.kilometersInk = kilometersInk;
    }

    public boolean isFuel() {
        return fuel;
    }

    public void setFuel(boolean fuel) {
        this.fuel = fuel;
    }

    public void readInformation(String brand, String model, int year, double kilometers, String fuelType) {
        this.brand = brand;
        this.model = model;
        this.year = year;
        this.kilometersInk = kilometers;
        this.fuel = "Gasoline".equalsIgnoreCase(fuelType);
    }

    public void priceOfCar(int type) { // parameter type: 1 for sale and 2 from rent
        /* ageOfcar is calculated as the current year minus the car's production year */
        int ageOfCar = Calendar.getInstance().get(Calendar.YEAR) - this.year;
        switch (type) {
            case 1:
                double sale = 100000 - (this.kilometersInk * 10) - (ageOfCar * 1000);
                if (!this.fuel) {
                    sale += 10000;
                }
                System.out.println("Price for Sale of this Car is " + sale + " TL");
                break;
            case 2:
                double rent = 200 - (this.kilometersInk * 0.5) - (ageOfCar * 2);
                if (this.fuel) {
                    rent -= 10000;
                }
                System.out.println("Price for Rent of this Car is " + rent + " TL");
                break;
            default:
                System.out.println("Invalid Code selected");
        }
    }

    public void printInformation() {
        String carFuel = (this.fuel) ? "Gasoline" : "Diesel";
        System.out.println("This " + this.color + " car is " + this.brand + "'s "" + this.model + "". It has travelled " + this.kilometersInk * 1000 + " KM since " + this.year + " using " + carFuel + " as fuel");
    }

    public boolean areCarsEqual(Car car) {
        int result;
        result = this.brand.compareTo(car.getBrand());
        if (result != 0) {
            return result == 0;
        }
        result = this.model.compareTo(car.getModel());
        if (result != 0) {
            return result == 0;
        }
        result = ((Integer) this.year).compareTo((Integer) car.getYear());
        if (result != 0) {
            return result == 0;
        }
        result = ((Double) this.kilometersInk).compareTo((Double) car.getKilometersInk());
        if (result != 0) {
            return result == 0;
        }
        result = ((Boolean) this.fuel).compareTo((Boolean) car.isFuel());
        if (result != 0) {
            return result == 0;
        }
        return result == 0;
    }

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote