I have problem with the test class, the output should looks like this my codes f
ID: 2247436 • Letter: I
Question
I have problem with the test class, the output should looks like this
my codes for each class :
:Vehicle
public class Vehicle {
protected int year;
protected double weight;
public Vehicle() {
super();
}
public Vehicle(int year, double weight) {
this.year = year;
this.weight = weight;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getweight() {
return weight;
}
public void setYear(double weight) {
this.weight = weight;
}
public String toString() {
return "Year:" + year + " Weight:" + weight + "";
}
}
TANK
public class Tank extends Vehicle {
protected double armorwidth;
public Tank() {
super();
}
public Tank(int year, double weight , double armorwidth )
{super(year, weight);
this.armorwidth = armorwidth;
}
public double getArmorWidth() {
return armorwidth;
}
public void setarmorwidth(double armorwidth) {
this.armorwidth = year;
}
public String toString() {
return super.toString() + armorwidth;
}
}
public class Automobile extends Vehicle {
protected String licensePlate, make;
Automobile
public Automobile() {
super();
}
public Automobile(int year, double weight,String licensePlate , String make) {
super(year, weight);
this.licensePlate = licensePlate;
this.make= make;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getMake() {
return make;
}
public void setYear(String make) {
this.make = make;
}
public String toString() {
return super.toString() + licensePlate + make;
}
}
CAR
public class Car extends Automobile {
protected int maxPassengers;
public Car() {
super();
}
public Car(int year, double weight, String licensePlate, String make, int maxPassengers) {
super(year, weight, licensePlate, make);
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
public String toString() {
return "Honda: " +super.toString()+ " MaxPassengers:" +maxPassengers+ "";
}
}
TRUCK
public class Truck extends Automobile {
protected double towCapacity;
public Truck() {
super();
}
public Truck(int year, double weight, String licensePlate, String make, double towCapacity)
{ super(year, weight, licensePlate, make);
this.towCapacity = towCapacity;
}
public double getTowCapacity() {
return towCapacity;
}
public void setTowCapacity(double towCapacity) {
this.towCapacity = towCapacity;
}
public String toString() {
return "Dodge: " +super.toString() + " Tow Capacity:" +towCapacity+ "";
}
}
TESTCLASS
public class TestVehicle {
public static void main(String[] args){
Car C = new Car ();
Truck T = new Truck ();
Tank T1 = new Tank ();
System.out.println(C.toString());
System.out.println(T.toString());
System.out.println(T1.toString());
}
}
my codes for each class :
:Vehicle
public class Vehicle {
protected int year;
protected double weight;
public Vehicle() {
super();
}
public Vehicle(int year, double weight) {
this.year = year;
this.weight = weight;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getweight() {
return weight;
}
public void setYear(double weight) {
this.weight = weight;
}
public String toString() {
return "Year:" + year + " Weight:" + weight + "";
}
}
TANK
public class Tank extends Vehicle {
protected double armorwidth;
public Tank() {
super();
}
public Tank(int year, double weight , double armorwidth )
{super(year, weight);
this.armorwidth = armorwidth;
}
public double getArmorWidth() {
return armorwidth;
}
public void setarmorwidth(double armorwidth) {
this.armorwidth = year;
}
public String toString() {
return super.toString() + armorwidth;
}
}
public class Automobile extends Vehicle {
protected String licensePlate, make;
Automobile
public Automobile() {
super();
}
public Automobile(int year, double weight,String licensePlate , String make) {
super(year, weight);
this.licensePlate = licensePlate;
this.make= make;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getMake() {
return make;
}
public void setYear(String make) {
this.make = make;
}
public String toString() {
return super.toString() + licensePlate + make;
}
}
CAR
public class Car extends Automobile {
protected int maxPassengers;
public Car() {
super();
}
public Car(int year, double weight, String licensePlate, String make, int maxPassengers) {
super(year, weight, licensePlate, make);
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers() {
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers) {
this.maxPassengers = maxPassengers;
}
public String toString() {
return "Honda: " +super.toString()+ " MaxPassengers:" +maxPassengers+ "";
}
}
TRUCK
public class Truck extends Automobile {
protected double towCapacity;
public Truck() {
super();
}
public Truck(int year, double weight, String licensePlate, String make, double towCapacity)
{ super(year, weight, licensePlate, make);
this.towCapacity = towCapacity;
}
public double getTowCapacity() {
return towCapacity;
}
public void setTowCapacity(double towCapacity) {
this.towCapacity = towCapacity;
}
public String toString() {
return "Dodge: " +super.toString() + " Tow Capacity:" +towCapacity+ "";
}
}
TESTCLASS
public class TestVehicle {
public static void main(String[] args){
Car C = new Car ();
Truck T = new Truck ();
Tank T1 = new Tank ();
System.out.println(C.toString());
System.out.println(T.toString());
System.out.println(T1.toString());
}
}
Explanation / Answer
Hi,
1. Vehicle.java
public class Vehicle {
private int year;
private double weight;
public Vehicle() {
// set default values if unknown
this.year = 2017;
this.weight = 2000.0;
}
public Vehicle(int year, double weight) {
this.year = year;
this.weight = weight;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getweight() {
return weight;
}
public void setYear(double weight) {
this.weight = weight;
}
public String toString() {
return "Year: " + year + " Weight: " + weight;
}
}
2. Automobile.java
public class Automobile extends Vehicle {
private String licensePlate, make;
public Automobile() {
// set default values if unknown
this.licensePlate = "1D123";
this.make = "Honda";
}
public Automobile(int year, double weight, String licensePlate, String make) {
super(year, weight);
this.licensePlate = licensePlate;
this.make= make;
}
public String getLicensePlate() {
return licensePlate;
}
public void setLicensePlate(String licensePlate) {
this.licensePlate = licensePlate;
}
public String getMake() {
return make;
}
public void setYear(String make) {
this.make = make;
}
public String toString() {
return super.toString() + " License Plate:"+licensePlate + " Make: "+make;
}
}
3. Tank.java
public class Tank extends Vehicle {
private double armorwidth;
public Tank() {
// set default values if unknown
this.armorwidth = 4.0;
}
public Tank(int year, double weight , double armorwidth ){
super(year, weight);
this.armorwidth = armorwidth;
}
public double getArmorWidth() {
return armorwidth;
}
public void setarmorwidth(double armorwidth) {
this.armorwidth = armorwidth;
}
public String toString() {
return super.toString() + " Armor Width:"+armorwidth;
}
}
4. Truck.java
public class Truck extends Automobile {
private double towCapacity;
public Truck() {
// set default values if unknown
this.towCapacity = 1000.0;
}
public Truck(int year, double weight, String licensePlate, String make, double towCapacity) {
super(year, weight, licensePlate, make);
this.towCapacity = towCapacity;
}
public double getTowCapacity() {
return towCapacity;
}
public void setTowCapacity(double towCapacity) {
this.towCapacity = towCapacity;
}
public String toString() {
return "Dodge: " +super.toString()+ " Tow Capacity: " +towCapacity;
}
}
5. Car.java
public class Car extends Automobile
{
private int maxPassengers;
public Car(){
// set default values if unknown
this.maxPassengers = 5;
}
public Car(int year, double weight, String licensePlate, String make, int maxPassengers){
super(year, weight, licensePlate, make);
this.maxPassengers = maxPassengers;
}
public int getMaxPassengers(){
return maxPassengers;
}
public void setMaxPassengers(int maxPassengers){
this.maxPassengers = maxPassengers;
}
public String toString(){
return "Honda: "+super.toString()+" MaxPassengers: "+maxPassengers;
}
}
6. TestVehicle.java
public class TestVehicle {
public static void main(String[] args){
Car C = new Car (2006, 2100.56, "1RT245", "Honda", 5);
Truck T = new Truck (2009, 2500.45, "2tu123", "Donge", 1200.0);
Tank T1 = new Tank (2011, 7000.56, 4.56);
System.out.println(C.toString());
System.out.println(T.toString());
System.out.println(T1.toString());
}
}
Let me know if any change required