Question

Exercise 2 Write a class named Vehicle Holds make and model (strings) and year (int) Contains...

Exercise 2

Write a class named Vehicle
Holds make and model (strings) and year (int)

Contains this method:

Vehicle(String make, String model, int year) {

this.make = make;

this.model = model;

this.year = year;

}

Add getters and setters for each variable

Write a class called FordMustang that extends the Vehicle class

              Create instance of object
              Pass variables
              Print

Homework Answers

Answer #1

Please find the code for the following:

Code:

Vehicle.java:
//Write a class named Vehicle
public class Vehicle
{
   //Created 3 variables to hold model, make and year
   protected String make;
   protected String model;
   protected int year;

   //Constructor for the Vehicle class
   Vehicle(String make, String model, int year)
   {
       this.make = make;
       this.model = model;
       this.year = year;
   }
   //getters and setters for each variable
   public String getMake() {
       return make;
   }

   public void setMake(String make) {
       this.make = make;
   }

   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;
   }

}

FordMustang.java:
//Write a class called FordMustang that extends the Vehicle class
public class FordMustang extends Vehicle
{
   //This is for calling the constructor of the super class which is Vehicle
   FordMustang(String make, String model, int year) {
       super(make, model, year);
   }

   /*Main method*/
   public static void main(String[] args)
   {
       //Create an instance of object and pass the variables
       FordMustang veh=new FordMustang("Ford","GT 500",2001);
      
       veh.setYear(2020);
      
       //Print the variables
       System.out.println(veh.getMake());
       System.out.println(veh.getModel());
       System.out.println(veh.getYear());
   }
}

Please check the compiled program and its output for your reference:


Output:

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Know the answer?
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
Similar Questions
public class Auto { private String make; private String model; private int year; } a) write...
public class Auto { private String make; private String model; private int year; } a) write a default constructor for the Auto Class n) Write a constructor to initialize all instance variables c) write accessor and mutator for make variable d) create an onject name it myAuto with values of Toyota, Camry, and 2016
Write a program in Java to have a Car class which is comparable. Make the Car...
Write a program in Java 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...
write a c++ program A class called car (as shown in the class diagram) contains: o...
write a c++ program A class called car (as shown in the class diagram) contains: o Four private variables: carId (int), carType (String), carSpeed (int) and numOfCars (int). numOfCars is a static counter that should be  Incremented whenever a new Car object is created.  Decremented whenever a Car object is destructed. o Two constructors (parametrized and copy constructor) and one destructor. o Getters and setters for the Car type, ID, and speed. And static getter function for numOfCars....
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType...
Write code for a JAVA class for a "Car". The Car class has 2 properties: modelType and currentSpeed. Make sure to write code for instance variables (the properties), getters/setters and a default constructor, overloaded constructor. ALSO, include one method called "accelerate" that increases the speed by "1.0" miles per hour
C++ Design a class named Computer that holds the make, model, and amount of memory of...
C++ Design a class named Computer that holds the make, model, and amount of memory of a computer. Include methods to set the values for each data field, and include a method that displays all the values for each field. Create the class diagram and write the pseudocode that defines the class. Pseudocode help please
Java Programming In this lab students continue to write programs using multiple classes. By the end...
Java Programming In this lab students continue to write programs using multiple classes. By the end of this lab, students should be able to Write classes that use arrays and ArrayLists of objects as instance variables Write methods that process arrays and ArrayLists of objects Write getter and setter methods for instance variables Write methods using object parameters and primitive types Question- class StringSet. A StringSet object is given a series of up to 10 String objects. It stores these...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and...
1- Use inheritance to implement the following classes: A: A Car that is a Vehicle and has a name, a max_speed value and an instance variable called the number_of_cylinders in its engine. Add public methods to set and get the values of these variables. When a car is printed (using the toString method), its name, max_speed and number_of_cylinders are shown. B: An Airplane that is also a vehicle and has a name, a max_speed value and an instance variable called...
We encourage you to work in pairs for this challenge to create a Student class with...
We encourage you to work in pairs for this challenge to create a Student class with constructors. First, brainstorm in pairs to do the Object-Oriented Design for a Student class. What data should we store about Students? Come up with at least 4 different instance variables. What are the data types for the instance variables? Write a Student class below that has your 4 instance variables and write at least 3 different constructors: one that has no parameters and initializes...
Create a class called Employee that should include four pieces of information as instance variables—a firstName...
Create a class called Employee that should include four pieces of information as instance variables—a firstName (type String), a lastName (type String), a mobileNumber (type String) and a salary (type int). Your class (Employee) should have a full argument constructor that initializes the four instance variables. Provide a set and a get method for each instance variable. The validation for each attribute should be like below: mobileNumber should be started from “05” and the length will be limited to 10...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String),...
Create a class called Employee that contains three instance variables (First Name (String), Last Name (String), Monthly Salary (double)). Create a constructor that initializes these variables. Define set and get methods for each variable. If the monthly salary is not positive, do not set the salary. Create a separate test class called EmployeeTest that will use the Employee class. By running this class, create two employees (Employee object) and print the annual salary of each employee (object). Then set the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT