Question

13.2 Write a class that specifies the characteristics of a car, like a type (sedan, jeep,...

13.2 Write a class that specifies the characteristics of a car, like a type (sedan, jeep, mini, SUV, etc), gear (auto, manual), maximum speed (mph), average fuel consumption (mpg), etc. Create a few objects to illustrate your desired cars.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
public class Car {

    private String type;
    private String gear;
    private double maxSpeed;
    private double averageFuelConsumption;

    public Car() {
    }

    public Car(String type, String gear, double maxSpeed, double averageFuelConsumption) {
        this.type = type;
        this.gear = gear;
        this.maxSpeed = maxSpeed;
        this.averageFuelConsumption = averageFuelConsumption;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getGear() {
        return gear;
    }

    public void setGear(String gear) {
        this.gear = gear;
    }

    public double getMaxSpeed() {
        return maxSpeed;
    }

    public void setMaxSpeed(double maxSpeed) {
        this.maxSpeed = maxSpeed;
    }

    public double getAverageFuelConsumption() {
        return averageFuelConsumption;
    }

    public void setAverageFuelConsumption(double averageFuelConsumption) {
        this.averageFuelConsumption = averageFuelConsumption;
    }

    @Override
    public String toString() {
        return "type='" + type + '\'' + ", gear='" + gear + '\'' +
                ", maxSpeed=" + maxSpeed + ", averageFuelConsumption=" + averageFuelConsumption;
    }
}

class CarTest {

    public static void main(String[] args) {
        Car car1 = new Car("SUV", "auto", 85, 20);
        Car car2 = new Car("jeep", "manual", 100, 25);
        Car car3 = new Car("sedan", "auto", 75, 32);

        System.out.println(car1);
        System.out.println(car2);
        System.out.println(car3);
    }
}

Add a comment
Know the answer?
Add Answer to:
13.2 Write a class that specifies the characteristics of a car, like a type (sedan, jeep,...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT