Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Complete these classes m the question3 package of the assignmeat3 eclipse projec

ID: 3668684 • Letter: C

Question

Complete these classes m the question3 package of the assignmeat3 eclipse project. Design a ship class that has the following data fields: A data field for the name of the ship (a string). A data field for the year that the ship was built (an int). A constructor and appropriate accessors and mutators A to String method that displays the ship's name and the year it was built. Design a CruiseShip sub class that extends the Ship class. The CruiseShip class should have the following: An extra data field for the maximum number of passengers (an ine). A constructor and appropriate accessors and mutators A coString method that ovenides the sostring method in the base class. The CruiseShip class's toString method should also include the max passenger limit. Design a CargoShip class that extends the Ship class. The CargoShip class should have the following: An extra data field for the cargo capacity in tonnage (an ir.t). A constructor and appropriate accessors and mutators A toString method that ovenides the to3tring method in the base class. The CargoShip class's toString method should also include the cargo tonnage. In the appropriate class write an equals method to test if two ships are equal - they should be considered equal if they have the same name and were built in the same year. Demonstrate the classes in a program (ShipTester) that has an array of Ship objects (at least 5 of them). Assign various Ship. CruiseShip. and CargoShip objects to the array elements (you can hard-code the objects and data) and print out the initial ship configurations. Show that you can use both the accessor and mutator methods on a sampling of the ships (again, you can hard-code the method arguments here if you like). Also show that your equals method can determine whether or not two ships are equal (this means creating at least two ships that have "equal" data).

Explanation / Answer

public class Ship {
   private String name;
   private int year;
   public Ship(String name, int year) {
       super();
       this.name = name;
       this.year = year;
   }
  
   public String getName() {
       return name;
   }

   public void setName(String name) {
       this.name = name;
   }

   public int getYear() {
       return year;
   }

   public void setYear(int year) {
       this.year = year;
   }

   public String toString(){
       return getClass().getName()+"[ Name: "+name+" Year built: "+year+" ]";
   }
  
   @Override
   public boolean equals(Object obj){
       if(obj instanceof Ship){
           return this.getName().equals(((Ship) obj).getName()) && this.getYear()==((Ship) obj).getYear();
       }
       return false;
   }
  
}

class CruiseShip extends Ship{
  
   private int passenger;
   public CruiseShip(String name, int year, int passenger) {
       super(name, year);
       this.passenger = passenger;
   }
   public int getPassenger() {
       return passenger;
   }
   public void setPassenger(int passenger) {
       this.passenger = passenger;
   }
  
   public String toString(){
       return getClass().getName()+"["+super.toString()+", Passenger: "+passenger+"]";
   }  
}
class CargoShip extends Ship{

   private int capacity;
   public CargoShip(String name, int year, int capacity) {
       super(name, year);
       this.capacity = capacity;
   }
   public int getCapacity() {
       return capacity;
   }
   public void setCapacity(int capacity) {
       this.capacity = capacity;
   }
   public String toString(){
       return getClass().getName()+"["+super.toString()+", Tonnage: "+capacity+"]";
   }
  
}

class TestShip{
   public static void main(String[] args) {
       Ship[] ships = {new CargoShip("Balck Pearl", 1699, 50000),
                       new Ship("Queen Annes Revenge", 1701),
                       new CruiseShip("USS Enterprise", 2245, 2400),
                       new CruiseShip("USS Voyager", 2371, 2800),
                       new CargoShip("The Victory", 1790, 33100)
                       };
       for(Ship s: ships){
           System.out.println(s.toString());
       }
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote