Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. public class Main {
  2.  
  3. public static void main(String[] args) {
  4.  
  5. // Demonstrate the classes
  6. //program that has a Shiparray.
  7. // Assign objects to the array elements.
  8. Ship[] shipArray = new Ship[8];
  9. shipArray[0] = new Ship("Wario", 2000);
  10. shipArray[1] = new CruiseShip("Yoshi", 2001, 200);
  11. shipArray[2] = new CruiseShip("Daisy", 1999, 300);
  12. shipArray[3] = new Ship("Luigi", 2001);
  13. shipArray[4] = new Ship("Boo", 2001);
  14. shipArray[5] = new Ship("Koopa Troopa", 2000);
  15. shipArray[6] = new CargoShip("Peach", 2006, 100);
  16. shipArray[7] = new CargoShip("Mario", 2004, 350);
  17.  
  18. for (Ship ship : shipArray) {
  19. System.out.println(ship.toString());
  20. }
  21. }
  22. }
  23.  
  24. ==================================================
  25.  
  26.  
  27. /** Ship.java
  28. *
  29. * Title: Ship, CruiseShip, and CargoShip
  30. */
  31.  
  32. public class Ship {
  33. private String shipName;
  34. private int year;
  35.  
  36. public Ship(String shipName, int year) {
  37. this.shipName = shipName;
  38. this.year = year;
  39. }
  40.  
  41. public String getShipName() {
  42. return shipName;
  43. }
  44.  
  45. public void setShipName(String shipName) {
  46. this.shipName = shipName;
  47. }
  48.  
  49. public int getYear() {
  50. return year;
  51. }
  52.  
  53. public void setYear(int year) {
  54. this.year = year;
  55. }
  56.  
  57. @Override
  58. public String toString() {
  59. return this.getShipName() + " built in " + this.getYear();
  60. }
  61. }
  62.  
  63. ========================================================
  64.  
  65. /**
  66. *
  67. * CruiseShip.java
  68. */
  69.  
  70. public class CruiseShip extends Ship{
  71.  
  72. private int maxPass;
  73.  
  74. public CruiseShip(String shipName, int year, int maxPass) {
  75. super(shipName, year);
  76. this.maxPass = maxPass;
  77. }
  78.  
  79. public int getMaxPass() {
  80. return maxPass;
  81. }
  82.  
  83. public void setMaxPass(int maxPass) {
  84. this.maxPass = maxPass;
  85. }
  86.  
  87. @Override
  88. public String toString() {
  89. return this.getShipName() + " holds " + this.getMaxPass() + " passengers";
  90. }
  91. }
  92.  
  93. ===========================================================
  94.  
  95. /**
  96. *
  97. * CargoShip.java
  98. */
  99.  
  100. public class CargoShip extends Ship{
  101.  
  102. private int capacity;
  103.  
  104. public CargoShip(String shipName, int year, int capacity) {
  105. super(shipName, year);
  106. this.capacity = capacity;
  107. }
  108.  
  109. public int getCapacity() {
  110. return capacity;
  111. }
  112.  
  113. public void setCapacity(int capacity) {
  114. this.capacity = capacity;
  115. }
  116.  
  117. @Override
  118. public String toString() {
  119. return this.getShipName() + " holds " + this.getCapacity() + " tons";
  120. }
  121. }
  122.  
  123. ===========================================================
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement