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

Please do the equals method as well! Very important! 1. (25 marks) Complete thes

ID: 3851800 • Letter: P

Question

Please do the equals method as well! Very important!

1. (25 marks) Complete these classes in the question1 package of the 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 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 an d appropriate accessors and mutators. . AtoString 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. . AtoString 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

Explanation / Answer

The answer is as follows:

The code is as follows:

#include<iostream.h>
#include<string>

class Ship {
   protected:
      string name;
      int year;
   public:
      Ship(string a, int b){
          name = a;
          year = b;
      }
      void setName(string a){
           name = a;
      }
      void setYear(int a){
           year = a;
      }
      int getYear(){
           cout << year;
      }
      string getName(){
           cout << name;
      }
      void toString(){
           cout << name << " " << year << endl;
      }
      bool equals(Ship sh){
          if (name == sh.getName() && year == sh.getYear())
             return true;
          else
             return false;
      }
};

class CruiseShip : public Ship {
     private:
         int numOfPassangers;
     public:
         CruiseShip( string a, int b, int c){
             name = a;
             year = b;
             numOfPassangers = c;
         }
         void setnumOfPassangers(int a){
             numOfPassangers = a;
         }
         int getnumOfPassangers(){
             return numOfPassangers;
         }
         void toString(){
              cout << name << " " << year << " " << numOfPassangers << endl;
         }
};

class CargoShip: public Ship {
     private:
         int capacity;
     public:
         CargoShip( string a, int b, int c){
             name = a;
             year = b;
             capacity = c;
         }

         void setCapacity(int a){
             capacity = a;
         }
         int getCapacity(){
             return capacity;
         }
         void toString(){
              cout << name << " " << year << " " << capacity << endl;
         }

};

void main(){

Ship sh0("Queen Annes Revenge", 1701);
CruiseShip sh1("USS Enterprise", 2245, 2400);
CargoShip   sh2("Black Pearl", 1699, 50000);
CruiseShip sh3("USS Voyager", 2371, 2800);
CargoShip   sh4("The Victory", 1790, 33100);

cout << "Ship 2 Details" << endl << endl;
cout << "Name: " << sh2.getName();
cout << "Year Built: " << sh2.getYear();
cout << "Cargo Tonnage: " << sh2.getCapacity();

sh3.setName("USS Enterprise");
sh3.setYear(2245);

cout << "Comparing Ship 0 to Ship 1 = Equal?: " << sh0.equals(sh1) << endl;
cout << "Comparing Ship 1 to Ship 3 = Equal?: " << sh1.equals(sh3) << endl;
cout << "Comparing Ship 4 to Ship 2 = Equal?: " << sh4.equals(sh2) << endl;
}