Question

Write 3 Java classes called Car, OffroadCar and HybridCar. Demonstrate the concept of inheritance by making...

Write 3 Java classes called Car, OffroadCar and HybridCar. Demonstrate the concept of inheritance by making the Car class your superclass. Give it the following attributes:

int num_doors; // number of doors the car has

int hp; // the horse power of the car

boolean manual; // true if manual transmission, false if automatic

____________

OffroadCar Class

should be derived from the Car class and have the additional attribute:

int wheel_diameter; // diameter of wheel in mm.

_____________________

HybridCar

should be derived from the Car class and have the additional attribute:

int batteries; // number of batteries in the car

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java code

/**
 * Class Car
 */
public class Car {
/**
 * Attributes
 *
 */
private int num_doors; // number of doors the car has

   private int hp; // the horse power of the car
    private boolean manual; // true if manual transmission, false if automatic

    /**
     * Constructor
     */
    public Car(int num_doors, int hp, boolean manual) {
        this.num_doors = num_doors;
        this.hp = hp;
        this.manual = manual;
    }
    //getters and setters

    public int getNum_doors() {
        return num_doors;
    }

    public void setNum_doors(int num_doors) {
        this.num_doors = num_doors;
    }

    public int getHp() {
        return hp;
    }

    public void setHp(int hp) {
        this.hp = hp;
    }

    public boolean isManual() {
        return manual;
    }

    public void setManual(boolean manual) {
        this.manual = manual;
    }

    /**
     * @return string format of Object Car
     */
    @Override
    public String toString() {
        return "Number of doors: "+num_doors+"\nHorse power of car: "+hp+" hp"+
                "\nIs Manual Transmission: "+manual;
    }
}

============================================================

/**
 * OffroadCar Class
 */
public class OffroadCar extends Car {
    private int wheel_diameter; // diameter of wheel in mm.

    public OffroadCar(int num_doors, int hp, boolean manual, int wheel_diameter) {
        /**
         * super keyword call the super class constructor
         * in this case Car
         */
        super(num_doors, hp, manual);
        this.wheel_diameter = wheel_diameter;
    }

    public int getWheel_diameter() {
        return wheel_diameter;
    }

    public void setWheel_diameter(int wheel_diameter) {
        this.wheel_diameter = wheel_diameter;
    }

    @Override
    public String toString() {
        System.out.println("=============================================");
        return super.toString()+"\nWheel diameter: "+wheel_diameter+" mm"
                +"\n=============================================\n";
    }
}

=============================================================================

public class HybridCar extends Car {
    private int batteries; // number of batteries in the car

    public HybridCar(int num_doors, int hp, boolean manual, int batteries) {
        super(num_doors, hp, manual);
        this.batteries = batteries;
    }
    //getters and setters

    public int getBatteries() {
        return batteries;
    }

    public void setBatteries(int batteries) {
        this.batteries = batteries;
    }

    @Override
    public String toString() {
        System.out.println("=============================================");
        return super.toString()+"\nNumber of batteries: "+batteries
                +"\n=============================================\n";
    }
}

================================================================

public class TestCar {
    public static void main(String[] args)
    {
        HybridCar hybridCar = new HybridCar(4,300,true,20);
        OffroadCar offroadCar= new OffroadCar(2,450,true,4);
        //printinfo
        System.out.println(hybridCar);
        System.out.println(offroadCar);
    }
}

//output

//if you need any help regarding this solution ............ please leave a comment .......... thanks.......

Add a comment
Know the answer?
Add Answer to:
Write 3 Java classes called Car, OffroadCar and HybridCar. Demonstrate the concept of inheritance by making...
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
  • using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship...

    using java write a code with notepad++ Create the following classes using inheritance and polymorphism Ship (all attributes private) String attribute for the name of ship float attribute for the maximum speed the of ship int attribute for the year the ship was built Add the following behaviors constructor(default and parameterized) , finalizer, and appropriate accessor and mutator methods Override the toString() method to output in the following format if the attributes were name="Sailboat Sally", speed=35.0f and year built of...

  • UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships...

    UML Class Diagram with Inheritance Objectives Use UML Correctly indicate inheritance Demonstrate permissions Understand inheritance relationships Labwork Please read all of the directions carefully. You will create a UML class diagram reflecting the class hierarchy for a fictional program that manages university personnel as constructed according to the graph displayed below. You will need to think about the attributes and behaviors that are unique to each class, and also those attributes and behaviors that are common amongst the subclasses and...

  • Define an example of inheritance with a base class that has 2 attributes, 2 derived classes...

    Define an example of inheritance with a base class that has 2 attributes, 2 derived classes that each have 2 attributes. Implement this in C++. Create a driver program that creates instances of both of the derived classes and exercises the classes’ member functionsl. Student answer should include the following: 1 base class with 2 attributes, 2 constructors (default and with parameters), get/set methods for each attribute, and display methods 2 derived classes with 2 attributes, 2 constructors (default and...

  • Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student...

    Can you please pick Automobile For this assignment. Java Programming Second Inheritance OOP assignment Outcome: Student will demonstrate the ability to use inheritance in Java programs. Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following: Person Automobile Animal Based on your choice: If you choose Person, you will create a subclass of Person called Student. If you choose Automobile, you will create...

  • Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in...

    Project 7: Vehicles 1 Objective In the last couple projects, you’ve created and used objects in interesting ways. Now you’ll get a chance to use more of what objects offer, implementing inheritance and polymorphism and seeing them in action. You’ll also get a chance to create and use abstract classes (and, perhaps, methods). After this project, you will have gotten a good survey of object-oriented programming and its potential. This project won’t have a complete UI but will have a...

  • java. do it on eclipse. the same way its instructed Second Inheritance OOP assignment Outcome: ...

    java. do it on eclipse. the same way its instructed Second Inheritance OOP assignment Outcome:  Student will demonstrate the ability to understand inheritance Program Specifications: Programming assignment: Classes and Inheritance You are to pick your own theme for a new Parent class. You can pick from one of the following:  Person  Automobile  Animal  If you choose Person, you will create a subclass of Person called Student.  If you choose Automobile, you will create a...

  • How do I start this Java: Inheritance problem? • person.java • student.java • studentAthlete.java • app.java...

    How do I start this Java: Inheritance problem? • person.java • student.java • studentAthlete.java • app.java • Students should apply consistent indenting in all submissions. This can be done via the NetBeans Source menu. Contents person First name Last name app Creates 1 student-athlete object Hometown Retinfo) 1-Displays the complete information about the student-athlete object student New attributes Maior attributes getinfo) method from the superlas person Student-athlete student's rankine Overrides the method from the superclass student Video from Professor Fisher...

  • Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and...

    Please help me with the following question. This is for Java programming. In this assignment you are going to demonstrate the uses of inheritance and polymorphism. You will create an Animals class and a Zoo class that holds all the animals. You should create a general Animalclass where all other classes are derived from (except for the Zoo class). You should then create general classes such as Mammal, Reptile, and whatever else you choose based off of the Animalclass. For...

  • Use inheritance to create a new class AudioRecording based on Recording class that: it will retain...

    Use inheritance to create a new class AudioRecording based on Recording class that: it will retain all the members of the Recording class, it will have an additional field bitrate (non-integer numerical; it cannot be modified once set), its constructors (non-parametrized and parametrized) will set all the attributes / fields of this class (reuse code by utilizing superclass / parent class constructors): Non-parametrized constructor should set bitrate to zero, If arguments for the parametrized constructor are illegal or null, it...

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