Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Main {
- public static void main(String[] args) {
- // Demonstrate the classes
- //program that has a Shiparray.
- // Assign objects to the array elements.
- Ship[] shipArray = new Ship[8];
- shipArray[0] = new Ship("Wario", 2000);
- shipArray[1] = new CruiseShip("Yoshi", 2001, 200);
- shipArray[2] = new CruiseShip("Daisy", 1999, 300);
- shipArray[3] = new Ship("Luigi", 2001);
- shipArray[4] = new Ship("Boo", 2001);
- shipArray[5] = new Ship("Koopa Troopa", 2000);
- shipArray[6] = new CargoShip("Peach", 2006, 100);
- shipArray[7] = new CargoShip("Mario", 2004, 350);
- for (Ship ship : shipArray) {
- System.out.println(ship.toString());
- }
- }
- }
- ==================================================
- /** Ship.java
- *
- * Title: Ship, CruiseShip, and CargoShip
- */
- public class Ship {
- private String shipName;
- private int year;
- public Ship(String shipName, int year) {
- this.shipName = shipName;
- this.year = year;
- }
- public String getShipName() {
- return shipName;
- }
- public void setShipName(String shipName) {
- this.shipName = shipName;
- }
- public int getYear() {
- return year;
- }
- public void setYear(int year) {
- this.year = year;
- }
- @Override
- public String toString() {
- return this.getShipName() + " built in " + this.getYear();
- }
- }
- ========================================================
- /**
- *
- * CruiseShip.java
- */
- public class CruiseShip extends Ship{
- private int maxPass;
- public CruiseShip(String shipName, int year, int maxPass) {
- super(shipName, year);
- this.maxPass = maxPass;
- }
- public int getMaxPass() {
- return maxPass;
- }
- public void setMaxPass(int maxPass) {
- this.maxPass = maxPass;
- }
- @Override
- public String toString() {
- return this.getShipName() + " holds " + this.getMaxPass() + " passengers";
- }
- }
- ===========================================================
- /**
- *
- * CargoShip.java
- */
- public class CargoShip extends Ship{
- private int capacity;
- public CargoShip(String shipName, int year, int capacity) {
- super(shipName, year);
- this.capacity = capacity;
- }
- public int getCapacity() {
- return capacity;
- }
- public void setCapacity(int capacity) {
- this.capacity = capacity;
- }
- @Override
- public String toString() {
- return this.getShipName() + " holds " + this.getCapacity() + " tons";
- }
- }
- ===========================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement