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 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....
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...
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...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName...
Write the Game class, Java lanuage. A Game instance is described by three instance variables: gameName (a String), numSold (an integer that represents the number of that type of game sold), and priceEach (a double that is the price of each of that type of Game). I only want three instance variables!! The class should have the following methods: A constructor that has two parameter – a String containing the name of the Game and a double containing its price....
Write the following problem in Java Create a class Dog, add name, breed, age, color as...
Write the following problem in Java Create a class Dog, add name, breed, age, color as the attributes. You need to choose the right types for those attributes. Create a constructor that requires no arguments. In this constructor, initialize name, breed, age, and color as you wish. Define a getter and a setter for each attribute. Define a method toString to return a String type, the returned string should have this information: “Hi, my name is Lucky. I am a...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes...
4. Programming Problem: In this problem we will keep the Mechanism interface and the two classes Car and Computer from the previous problem. In addition, we will add a new class called Mechanic. We will also create a new TestMechanic class that will contain the main method. Create a class called Mechanic with following instance variable and constants: private int cost; public static int TEST_PRICE = 10; public static int REPAIR_PRICE = 50; Have a default constructor that will set...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h...
c++ C++ CLASSES and objects DO ADD COMMENTS DISPLAY OUTPUT First make three files: episode.cpp, episode.h andRun.cpp to separate class header and implementation. In this program, we are going to create a small scale Telivision Management System. A) Create a class Episode with following variables: char* episode_name, char* episode_venue, char episode_date[22] and char episode_time[18]. Input format for episode_date: dd-mm-yyyy Input format for episode_time: hh:mm am/pm B) Implement default constructor and overloaded constructor. Print “Default Constructor Called” and “Overloaded Constructor Called”...
Design a Java class named Polygon that contains:  A private int data field named numSides...
Design a Java class named Polygon that contains:  A private int data field named numSides that defines the number of sides of the polygon. The default value should be 4.  A private double data field named sideLength that defines the length of each side. The default value should be 5.0.  A private double data field named xCoord that defines the x-coordinate of the center of the polygon. The default value should be 0.0.  A private double...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT