Create a class called Vehicle in Vehicle.java with instance variables make (such
ID: 3555642 • Letter: C
Question
Create a class called Vehicle in Vehicle.java with instance variables make (such as Ford, GM, Chrysler, Toyota, or Honda), year, and horsepower (200, 300, and 400). Add the necessary constructor and set and get methods. Then create classes Bus and Truck in program files Bus.java and Truck.java, respectively. Bus has an instance variable called numOfPassengers (1-50) and Truck has an instance variable called towingCapacity (0.5, 1.0, 2.0, or 3.0 tons). Both Bus and Truck inherit from Vehicle and contain their own toString methods for displaying all their attributes (including the inherited ones). Compile all three classes.
Explanation / Answer
//Vehicle Class
import java.util.*;
import java.lang.*;
import java.io.*;
class Vehicle
{
String make;
int year;
int horsepower;
Vehicle(){
make = "Honda";
year=2001;
horsepower=400;
}
public void setMake(String make1){
this.make = make1;
}
public void setDate(int year1){
this.year = year1;
}
public void setHorsePower(int horsepower1){
this.horsepower = horsepower1;
}
public String getMake()
{
return make;
}
public int getYear(){
return year;
}
public int getHorsepower(){
return horsepower;
}
}
//Bus Class
import java.util.*;
import java.lang.*;
import java.io.*;
class Bus extends Vehicle{
int numOfPassengers;
Bus(){
super();
numOfPassengers = 20;
}
public void setPassengers(int number){
this.numOfPassengers = number;
}
public int getPassengers(){
return numOfPassengers;
}
}
//Truck Class
import java.util.*;
import java.lang.*;
import java.io.*;
class Truck extends Vehicle{
float towingCapacity;
Truck(){
super();
towingCapacity = 2.03;
}
public void setTowingCapacity(float number){
this.towingCapacity = number;
}
public float getTowingCapacity(){
return towingCapacity;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.