In: Computer Science
1. Create a base class called Vehicle that has the manufacturer’s name (type String), number of cylinders in the engine (type int). Then create a class called Truck that is derived from Vehicle and has additional properties: the load capacity in tons (type double) and towing capacity in tons (type double).
Add the following methods to the Vehicle class:
Add the following methods to the Truck class:
Write a driver program that tests all your methods. Create at least two objects of truck class, and two objects of vehicle class.
(JAVA)
// Screenshot of the code
// Sample output
// Code to copy
Vehicle.java
public class Vehicle {
private String maker;
private int cylinders;
public Vehicle()
{
maker="none";
cylinders=1;
}
public Vehicle(String m,int n)//m is maker,n is the
number of cylinders
{
maker=m;
cylinders=n;
}
public void setMaker(String m)
{
maker=m;
}
public void setCylinders(int newNum)
{
cylinders=newNum;
}
public String getMaker()
{
return maker;
}
public int getCylinders()
{
return cylinders;
}
public String writeOutput()
{
return "\nManufacturer: "+maker+"\nEngine:
"+cylinders;
}
public boolean equals(Vehicle other)
{
return
maker.equals(other.maker)&&cylinders==other.cylinders;
}
}
Truck.java
public class Truck extends Vehicle{
private double loadCap;
public double towCap;
public Truck()
{
super();
loadCap=0.00;
towCap=0.00;
}
public Truck(String maker,int cylinders,double
loadCapa,double towCapa)
{
super(maker,cylinders);
loadCap=loadCapa;
towCap=towCapa;
}
public void setLoadCap(double newLoadCap)
{
loadCap=newLoadCap;
}
public void setTowCap(double towCap2)
{
towCap=towCap2;
}
public double getLoadCap()
{
return loadCap;
}
public double getTowCap()
{
return towCap;
}
public String writeOutput()
{
return super.toString()+","+loadCap+" pound load,
"+towCap+" tow";
}
public boolean equals(Truck other)
{
return
super.equals(other)&&loadCap==other.loadCap&&towCap==other.towCap;
}
}
VehicleDemo.java
import java.util.Scanner;
public class VehicleDemo {
public static void main(String[] args) {
Scanner input =new
Scanner(System.in);
Vehicle aCar1=new Vehicle("Chevrolet",5);
Vehicle aCar2=new Vehicle();
String maker1;
int cylinders1;
System.out.println("Input your cars info:");
System.out.print("What is the manufacture name?
");
maker1=input.nextLine();
System.out.print("What is the number of cylinders?
");
cylinders1=input.nextInt();
aCar2.setMaker(maker1);
aCar2.setCylinders(cylinders1);
Truck aTruck1=new Truck();
aTruck1.setMaker("Toyota");
aTruck1.setCylinders(6);
aTruck1.setLoadCap(250.55);
aTruck1.setTowCap(3000);
Truck aTruck2=new Truck();
String maker2;
int cylinders2;
double towCap;
double loadCap;
System.out.println("\nInput your trucks
info:");
System.out.print("What is the manufacture name?
");
maker2=input.next();
System.out.print("What is the number of cylinders?
");
cylinders2=input.nextInt();
System.out.print("What is the load capacity? ");
loadCap=input.nextDouble();
System.out.print("What is the tow capacity? ");
towCap=input.nextDouble();
aTruck2.setMaker(maker2);
aTruck2.setCylinders(cylinders2);
aTruck2.setLoadCap(loadCap);
aTruck2.setTowCap(towCap);
System.out.println("\nFirst Cars info:");
System.out.println(aCar1.getMaker());
System.out.println(aCar1.getCylinders());
System.out.println("\nSecond Cars info:");
System.out.println(aCar2.getMaker());
System.out.println(aCar2.getCylinders());
System.out.println("\nFirst Trucks info:");
System.out.println(aTruck1.getMaker());
System.out.println(aTruck1.getCylinders());
System.out.println(aTruck1.getLoadCap());
System.out.println(aTruck1.getTowCap());
System.out.println("\nSecond Trucks info:");
System.out.println(aTruck2.getMaker());
System.out.println(aTruck2.getCylinders());
System.out.println(aTruck2.getLoadCap());
System.out.println(aTruck2.getTowCap());
if(aTruck1.equals(aTruck2)==true)
{
System.out.println("The trucks are both the
same.");
}
else
{
System.out.println("The trucks are not the
same.");
}
if(aCar1.equals(aCar2)==true)
{
System.out.println("The cars are both the
same.");
}
else
{
System.out.println("The cars are not the
same.");
}
input.close();
}
}