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

Design a ship class that has the following data fields: A data field for the nam

ID: 665123 • Letter: D

Question

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 toString 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 int).

A constructor and appropriate accessors and mutators.

A toString method that overrides the toString 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 int).

A constructor and appropriate accessors and mutators.

A toString method that overrides the toString method in the base class. The CargoShip class's toString method should also include the cargo tonnage.

In the appropriate class include 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 that has an array of Ship objects. Assign various Ship, CruiseShip, and CargoShip objects to the array elements . The program should then step through the array, calling each objects toSting method

Explanation / Answer

Coding:

public class Ship {

        private String name;

        private String builtDate;

       

        public Ship(String n, String date)

        {

               name = n;

               builtDate = date;

        }

       

        public Ship()

        {

               name = "";

               builtDate = "";

        }

       

        public void setName(String n)

        {

               name = n;

        }

       

        public void setBuiltDate(String b)

        {

               builtDate = b;

        }

       

        public String getName()

        {

               return name;

        }

       

        public String getBuiltDate()

        {

               return builtDate;

        }

       

        public String toString()

        {

               String str = "Name: " + name;

              

               str += (" BuiltDate: " + builtDate);

               return str;

        }

}

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