need help writing this java program HOME WORK 1. Ship, CruiseShip, and CargoShip
ID: 3777297 • Letter: N
Question
need help writing this java program
Explanation / Answer
package abc;
public class B {
public static void main(String[] args) {
Ship s = new Ship("Titanic", "1950");
CruiseShip cs = new CruiseShip("Titanic Cruise", "1960", 100);
CargoShip gs = new CargoShip("Titanic Cargo", "1970", 10000);
Ship[] ship_array = {s,cs,gs};
for(int i=0;i<ship_array.length;i++){
System.out.println(ship_array[i].toString());
}
}
}
class Ship{
private String shipName;
private String shipBuildYear;
Ship(String name, String year){
shipName = name;
shipBuildYear = year;
}
public String getShipName() {
return shipName;
}
public void setShipName(String shipName) {
this.shipName = shipName;
}
public String getShipBuildYear() {
return shipBuildYear;
}
public void setShipBuildYear(String shipBuildYear) {
this.shipBuildYear = shipBuildYear;
}
@Override
public String toString() {
return "Ship [shipName=" + shipName + ", shipBuildYear=" + shipBuildYear + "]";
}
}
class CruiseShip extends Ship{
private int maxPass;
CruiseShip(String name, String year, int max) {
super(name,year);
maxPass = max;
}
public int getMaxPass() {
return maxPass;
}
public void setMaxPass(int maxPass) {
this.maxPass = maxPass;
}
@Override
public String toString() {
return "CruiseShip [maxPass=" + maxPass + ", getShipName()=" + getShipName() + "]";
}
}
class CargoShip extends Ship{
int cargoCapacity;
public CargoShip(String name, String year, int capacity) {
super(name,year);
cargoCapacity = capacity;
}
public int getCargoCapacity() {
return cargoCapacity;
}
public void setCargoCapacity(int cargoCapacity) {
this.cargoCapacity = cargoCapacity;
}
@Override
public String toString() {
return "CargoShip [cargoCapacity=" + cargoCapacity + ", getShipName()=" + getShipName() + "]";
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.