Question

Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand...

Answer the following:

1.        Create a Class Car with the following instance variables: model (String), Brand

(String), maxspeed (double) Note: use encapsulation (all variables are private). (3 Marks)

2.        Create a non-default constructor for Class Car which takes 3 parameters. (2 Marks)

3.        Create a get and set methods for instance variable model variable only. (2 Marks)

4.        Create PrintInfo() method which prints all three instance variables. (2 Marks)

5.        Create a subclass GasCar with instance variable TankSize (double).. (2 Marks)

6.        Create a non-default constructor for sub-class GasCar which takes 4 parameters. (2 Marks)

7.        Override PrintInfo() method which prints all four parameters. (2 Marks)

8.        Create a subclass ElectricCar with instance variable BatteryPower (double). (2 Marks)

9.        Create a non-default constructor for sub-class ElectricCar which takes 4 parameters. (2 Marks)

10.    Override PrintInfo() method which prints all four parameters. (2 Marks)

11. Using the Car, Gas, and Electrical classes create a java application program which demonstrate the polymorphism principles in Java as following:

Write the main program by creating two references of type Car and the objects of type GasCar and ElectricCar.

Call PrintInfo method for both objects. (4 Marks)

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

public class Car{

    // Create a Class Car with the following instance variables: model (String), Brand

    //(String), maxspeed (double) Note: use encapsulation (all variables are private).

    private String model;

    private String brand;

    private double maxSpeed;

    //Create a non-default constructor for Class Car which takes 3 parameters.

    public Car(String model, String brand, double speed){

        this.model=model;

        this.brand=brand;

        this.maxSpeed=speed;

    }

    //3.        Create a get and set methods for instance variable model variable

    public String getModel() {

        return model;

    }

    //3.        Create a get and set methods for instance variable model variable

    public void setModel(String model) {

        this.model = model;

    }

public class Carf Create a Class Car with the following instance variables: model (String), Brand (String), maxspeed (double) Note: use encapsulation (all variables are private). prlvate String model; prlvate String brand; prlvate double maxSpeed; Create a non-default constructor for Class Car which takes 3 parameters. publlc Car(String model, String brand, double speed)i thls.model-model thls brand-brand; thls.maxSpeed-speed; . Create a get and set methods for instance variable model variable publlc String getModelO f return model; . Create a get and set methods for instance variable model variable publlc vold setModel (String model) f thls.model model: 4.Create Printlnfo0 method which prints all three instance variables. publlc vold printlnfo0 System.outprintin(Model:+thls.model Brand:+thls.brand+ Max Speed: +thls.maxSpeed) publlc class GasCar extends Car prlvate double tankSlze; prlvate String model; prlvate String brand; prlvate double maxSpeed; publlc GasCar(String model, String brand, double speed, double tank) f superlmodel, brand, speed): thls.model-model; thls brand-brand; thls.maxSpeed-speed; thls.tankSlze-tank;

Add a comment
Know the answer?
Add Answer to:
Answer the following: 1.        Create a Class Car with the following instance variables: model (String), Brand...
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
  • C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (Strin...

    C# programming 50 pts Question 2:2 1- Create the base class Book that has the following instance variables, constructor, and methods title (String) isbn (String) authors (String) publisher (String) edition ( int) published year (int) Constructor that takes all of the above variables as input parameters. set/get methods ToString method// that return sting representation of Book object. 2-Create the sub class New_Book that is derived from the base class Book and has the following instance variables, constructor, and methods: title...

  • 1. Employees and overriding a class method The Java program (check below) utilizes a superclass named...

    1. Employees and overriding a class method The Java program (check below) utilizes a superclass named EmployeePerson (check below) and two derived classes, EmployeeManager (check below) and EmployeeStaff (check below), each of which extends the EmployeePerson class. The main program creates objects of type EmployeeManager and EmployeeStaff and prints those objects. Run the program, which prints manager data only using the EmployeePerson class' printInfo method. Modify the EmployeeStaff class to override the EmployeePerson class' printInfo method and print all the...

  • 1. Assume you have a Car class that declares two private instance variables, make and model....

    1. Assume you have a Car class that declares two private instance variables, make and model. Write Java code that implements a two-parameter constructor that instantiates a Car object and initializes both of its instance variables. 2. Logically, the make and model attributes of each Car object should not change in the life of that object. a. Write Java code that declares constant make and model attributes that cannot be changed after they are initialized by a constructor. Configure your...

  • Student is a class that has the following instance variables: name (a String), yearInSchool (an int),...

    Student is a class that has the following instance variables: name (a String), yearInSchool (an int), and gpa (a double). Write a constructor for the Student class that takes parameters for these instance variables as initial values of the variables. (in Java)

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Create the class Book which has the following members: 1. private members: 1. title as String...

    Create the class Book which has the following members: 1. private members: 1. title as String 2. isbn as String 3. author as String 4. price as double 2. public members: 1. default constructor which initializes all data members to their default values. 2. non-default constructor which initializes all data members to parameters values. 3. toString which returns a representing String of the Book. 4. add all the setters and getters. Create the class EBook which is a subclass of...

  • Create a class Circle with one instance variable of type double called radius. Then define an...

    Create a class Circle with one instance variable of type double called radius. Then define an appropriate constructor that takes an initial value for the radius, get and set methods for the radius, and methods getArea and getPerimeter. Create a class RightTriangle with three instance variables of type double called base, height, and hypotenuse. Then define an appropriate constructor that takes initial values for the base and height and calculates the hypotenuse, a single set method which takes new values...

  • In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in...

    In a project named 'DogApplication', create a class called 'Dog' 1. declare two instance variables in 'Dog' : name (String type) age (int type) 2. declare the constructor for the 'Dog' class that takes two input parameters and uses these input parameters to initialize the instance variables 3. create another class called 'Driver' and inside the main method, declare two variables of type 'Dog' and call them 'myDog' and 'yourDog', then assign two variables to two instance of 'Dog' with...

  • Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredi...

    Write a program that meets the following requirements: Sandwich Class Create a class called Sandwich which has the following instance variables. No other instance variables should be used: - ingredients (an array of up to 10 ingredients, such as ham, capicola, American cheese, lettuce, tomato) -condiments (an array of up to 5 condiments, such as mayonnaise, oil, vinegar and mustard) Create a two argument constructor Write the getters and setters for the instance variables Override the toString method using the...

  • Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type...

    Define a class named Payment that contains an instance variable "paymentAmount" (non-static member variable) of type double that stores the amount of the payment and appropriate accessor (getPaymentAmount() ) and mutator methods. Also create a method named paymentDetails that outputs an English sentence to describe the amount of the payment. Override toString() method to call the paymentDetails() method to print the contents of payment amount and any other details not included in paymentDetails(). Define a class named CashPayment that is...

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