1

I'm new to Java and have been taking a course for a couple of weeks now and have been asked to complete a program with the following information:

  1. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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).

I understand the basis of what the question is asking and I have done the following:

Ship.java

public class Ship {

    private String name;
    private int yearBuilt;

    public Ship(String name, int yearBuilt) {

        this.name = name;
        this.yearBuilt = yearBuilt;

    }

    public String returnName() {

        return this.name;

    }

    public int returnYear() {

        return this.yearBuilt;

    }

    public boolean equals(Ship other) {

        return false;

    }

    public String toString() {

        return "[Name: " + this.name + ". Year built: " + this.yearBuilt + "]";

    }
}

CruiseShip.java

public class CruiseShip extends Ship {

    private int maxPassengers;

    public CruiseShip() {

        super("USS Enterprise", 2245);

        this.maxPassengers = 2400;

    }

    public CruiseShip(String name, int yearBuilt, int maxPassengers) {

        super(name, yearBuilt);
        this.maxPassengers = maxPassengers;

    }

    public String toString() {

        return "Cruise Ship" + super.toString() + ", Passengers: " + this.maxPassengers + "]";

    }

}

CargoShip.java

public class CargoShip extends Ship {

    private int cargoCapacity;

    public CargoShip() {

        super("Black Pearl", 1699);

        this.cargoCapacity = 50000;

    }

    public CargoShip(String name, int yearBuilt, int cargoCapacity) {

        super(name, yearBuilt);
        this.cargoCapacity = cargoCapacity;

    }

    public String toString() {

        return "Cargo Ship" + super.toString() + ", Tonnage: " + this.cargoCapacity + "]";

    }

}

and in my tester file - ShipTester.java

public class ShipTester {
    public static void main(String []args) {

        //I don't know what to put over here...

    }
}

I don't know what to put in the main method... I do know I have to create an array of ships but i don't know how to do that with the cargo ship, cruise ship and so on...

Output that i'm supposed to have:

Displaying Ships: Ship[ Name: Queen Annes Revenge Year built: 1701]

Cruise Ship[ Ship[ Name: USS Enterprise Year built: 2245], Passengers: 2400 ]

Cargo Ship[ Ship[ Name: Black Pearl Year built: 1699], Tonnage: 50000 ]

Cruise Ship[ Ship[ Name: USS Voyager Year built: 2371], Passengers: 2800 ]

Cargo Ship[ Ship[ Name: The Victory Year built: 1790], Tonnage: 33100 ]

20
  • Hint, you just said the answer: ...I have to create an array of ships... Commented Jan 30, 2014 at 0:58
  • 1
    Ship[] ships = new Ship[5]; and then assign ships[0] = ..... You can do this, I'm sure, just give it a go and see what happens. Commented Jan 30, 2014 at 0:58
  • 5
    I see the word new in your future... Commented Jan 30, 2014 at 1:03
  • 1
    By the way, you should always override hashCode when overriding equals, because otherwise containers like HashMap will malfunction. It's usually best to leave equals alone (using the default == implementation) until you write an actual implementation of it. Commented Jan 30, 2014 at 1:08
  • 1
    @ElliottFrisch Yeah, but that not a very good default, IMO. "Unnamed" or something like that would be less misleading. Commented Jan 30, 2014 at 1:11

2 Answers 2

2

Extending onto what Hovercraft said but you may want to use an ArrayList for arrays of objects.

Creating an Arraylist of Objects

Example

ArrayList<Ship> myArray = new ArrayList();
myArray.add(new CargoShip("TheKing",1990,10000));
myArray.add(new CruiseShip("Princess",2000,5000));

etc.

[edit] forgot you needed to print it too

You use a simple for loop to print, example:

for (int i = 0; i < myArray.size(); i++) {
  System.out.println(myArray.get(i).toString());
}

note - this is not the exact answer I dont want jump over your learning process. But this should at least give you an idea of a path you could take...

Sign up to request clarification or add additional context in comments.

1 Comment

1) His instructions mention creating an array, not a collection such as an ArrayList. 2) The toString() call is not needed inside of a println(...) statement since it is called by default. The latter is a minor quibble, but the first statement of mine is more major.
0

Ship is the "base" parent class which puts all the other classes into a common family. This means that CruiseShip and CargoShip can actually masquerade as Ship. This is the corner stone of Polymorphism and one of the features that makes Object Oriented Program so powerful.

Based on your example code, you would simply need to create an array of type Ship, for example...

Ship[] ships = new Ship[5];

You would then simply need to fill this arrays with an assortment of ships...

ships[0] = new CargoShip("Lolli Pop", 1965, 1990);
ships[1] = new CruiseShip("Love Boat", 1980, 8);

What this means is, that each element "acts" as a Ship, since CargoShip and CruiseShip inherited from Ship, this gives them the ability to do so, because they are "ships"

Because the display information for Ship is defined by it's toString method you can simply use System.out.println(instanceOfShip) to print it. For example...

for (Ship ship : ships) {
    System.out.println(ship);
}

Take a closer look at Inheritance and Arrays for more details

It might seem confusing and daunting, but once you understand the basics of inheritance, it will become very obvious and very powerful

2 Comments

thank you! can I get a hint on how to do the equals method? I'm thinking something along the lines of public boolean equals(ship[0], ship[1])
"they should be considered equal if they have the same name and were built in the same year" would suggest that returnName and returnYear need to be the same for both ships. equals is usually done by passing one instance to another ie ship[0].equals(ship[1]), based on the example in the answer

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.