a. Create a class called Auto, that has member for owner name, auto type, maximu
ID: 3545436 • Letter: A
Question
a. Create a class called Auto, that has member for owner name, auto type,
maximum speed, gasoline tank capacity. Provide methods set() and display()
for setting and displaying these values. Add two constructors to Auto class,
one that takes two arguments, name of owner and type of Auto, and the other
that does not take any. Create a Auto object to demo the methods. Add other methods
as needed.
b. Create a class called Truck that extends from Auto. Add members to Truck class
that would make this specific to Truck. Override the set() method and the display()
method of the Auto class. Provide a Truck constructor to initialize values.
Create a Truck object to demo its features using its methods.
Explanation / Answer
##########Auto.java###########################
public class Auto {
private String ownerName,type;
private double maxSpeed,fuelCapacity;
public String getOwnerName() {
return ownerName;
}
public void setOwnerName(String ownerName) {
this.ownerName = ownerName;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public double getMaxSpeed() {
return maxSpeed;
}
public void setMaxSpeed(double maxSpeed) {
this.maxSpeed = maxSpeed;
}
public double getFuelCapacity() {
return fuelCapacity;
}
public void setFuelCapacity(double fuelCapacity) {
this.fuelCapacity = fuelCapacity;
}
public Auto() {
// default constructor
}
public Auto(String ownerName,String type){
setOwnerName(ownerName);
setType(type);
}
public static void main(String[] args) {
Auto a = new Auto("abcd","bajaj");
a.setFuelCapacity(10);
a.setMaxSpeed(140);
System.out.println(a);
}
public String toString(){
return "Owner is : "+getOwnerName()+" Type is : "+getType()+" Maximum Speed is : "
+getMaxSpeed() +" Fuel Capacity is : "+getFuelCapacity();
}
}
public class Truck extends Auto{
public Truck(String name,String type,double maxSpeed,double fuelC) {
this.setOwnerName(name);
this.setType(type);
this.setMaxSpeed(maxSpeed);
this.setFuelCapacity(fuelC);
}
public static void main(String args[]){
Truck t = new Truck("abcd_truck","Truck",240,1000);
System.out.println(t);
}
}
Owner is : abcd_truck
Type is : Truck
Maximum Speed is : 240.0
Fuel Capacity is : 1000.0
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.