Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java Lab - Inheritance Practice Purpose: To improve understanding of Java inheri

ID: 669921 • Letter: J

Question

Java Lab - Inheritance Practice

Purpose:

To improve understanding of Java inheritance.

Description:

Many types of vehicles have some attributes in common (e.g. a method of propulsion, number of wheels, a maximum speed), and other attributes that are unique to their type (e.g. dump trucks have a maximum hauling weight while motorcycles do not). This makes them ideal for studying inheritance.

For this lab, you will need to:

1) Start from scratch, and create a new NetBeans project called VehicleLab

2) Add a new class called "app" that will contain your main() method.

3) Add a "vehicle" class with the following attributes:

weight (double)

numTires (int)

topSpeed (double)

propulsionType (String)

- Your vehicle class must have a constructor to set those values, and a getInfo() method that returns a string containing those fields.

4) Add a motorcycle class that extends the vehicle class.  It should have a hasSidecar (boolean) attribute and a getInfo() method that uses the getInfo() method of its superclass (vehicle).

5) Add a dumptruck class that extends the vehicle class.  It should have a maxPoundsHauling (double) attribute and a getInfo() method that uses the getInfo() method of its superclass (vehicle).

6) In app.java, create an instance of your motorcycle class and dumptruck class (you may make up any values you like for the attributes). Call the getInfo() methods of each from app.java and display the results.

Explanation / Answer

public class vehicle{
   private double weight;
   private int numTires;
   private double topSpeed;
   private String propulsionType;
   public vehicle(double w,int n,double top,String pt){
       weight=w;
       numTires=n;
       topSpeed=top;
       propulsionType=pt;
   }
   public String getInfo(){
       return "Vehicle Weight: "+weight+"Number of tires: "+numTires+"topspeed: "+topSpeed+"propulsion type of the vehicle: "+propulsionType;
   }
}

public class dumptruck extends vehicle{
double maxPoundsHauling;
   public dumptruck(double w, int n, double top, String pt,double p) {
       super(w, n, top, pt);
       maxPoundsHauling=p;
       // TODO Auto-generated constructor stub
   }
   public String getInfo(){
       return super.getInfo()+"Maximum weight it can pull: "+maxPoundsHauling;
   }
  

}

public class motorcycle extends vehicle{
boolean sideCar;
   public motorcycle(double w, int n, double top, String pt,boolean b) {
       super(w, n, top, pt);
       sideCar=b;
   }
   public String getInfo(){
       return super.getInfo()+"Has a SideCar: "+sideCar;
   }
  
}

public class App
{
  
     
public static void main(String[] args){
   motorcycle m=new motorcycle(1200, 2, 140, "none", false);
   dumptruck d=new dumptruck(2600, 4, 100, "None", 2000);
   System.out.println(m.getInfo());
   System.out.println(d.getInfo());
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote