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 ]

Vanessa
  • 299
  • 1
  • 5
  • 12
  • Hint, you just said the answer: *...I have to create an array of ships...* – Paul Richter Jan 30 '14 at 00: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. – Hovercraft Full Of Eels Jan 30 '14 at 00:58
  • Also, your `equals` method is incorrect in your `Ship` class. It needs to test `yearBuilt` and `name` per your requirements. – Elliott Frisch Jan 30 '14 at 00:59
  • @ElliottFrisch, yeah I left that for later. thanks for reminding me – Vanessa Jan 30 '14 at 01:00
  • @HovercraftFullOfEels yeah but what I don't understand is: `ships[0] = //what?` like how do i give the name and year – Vanessa Jan 30 '14 at 01:01
  • You use a constructor from one of your ***concrete*** Ship-derived classes. Or call Ship's constructor if you want a plain vanilla Ship object. Again, give it a try. ;) – Hovercraft Full Of Eels Jan 30 '14 at 01:02
  • 5
    I see the word `new` in your future... – aliteralmind Jan 30 '14 at 01:03
  • Are all cruise ships named the "USS Enterprise"? I doubt it. – Eric Jablow Jan 30 '14 at 01:06
  • @ElliottFrisch, i thought the constructor would set the name for the ship from what it gets from the tester class – Vanessa Jan 30 '14 at 01:07
  • @user3148597 Req1 bullet point `c` (the third one). – Elliott Frisch Jan 30 '14 at 01:08
  • 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. – chrylis -cautiouslyoptimistic- Jan 30 '14 at 01:08
  • @user3148597 It does, but you should leave it to the caller of that constructor, and not hard-code a name for it at the class level. What happens if you want to create another cruise ship named "USS Ambassador"? – Santa Jan 30 '14 at 01:09
  • @Santa She's providing a default... it's not the worst idea ever - but you should use `this()` instead of `super()`. – Elliott Frisch Jan 30 '14 at 01:10
  • 1
    @ElliottFrisch Yeah, but that not a very good default, IMO. `"Unnamed"` or something like that would be less misleading. – Santa Jan 30 '14 at 01:11
  • @ElliottFrisch, so for the appropriate accessors and mutators, is the default constructor not enough? i have to create methods such as `setName`, `setYearBuilt` and so on? and for the mutator methods, should i have it as `changeName(String nameToChangeTo);`? – Vanessa Jan 30 '14 at 01:17
  • @user3148597 Sorry you don't have to do anything. This is 100% free advice. The mutators are normally named with `set` and the accessors are named with `get`. I strongly suspect your instructor wants to see what is normally referred to as a "[JavaBean](http://en.wikipedia.org/wiki/JavaBean)". – Elliott Frisch Jan 30 '14 at 01:18
  • @ElliottFrisch so for only the ship class i need the setName and setYear, getName and getYear, right? – Vanessa Jan 30 '14 at 01:30
  • @ElliottFrisch @santa 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])` – Vanessa Jan 30 '14 at 01:57
  • @Santa can i get a hint on the equals method? – Vanessa Jan 30 '14 at 01:59
  • @user3148597 Use the `@Override` annotation. Take a look at [this](http://stackoverflow.com/questions/8180430/how-to-override-equals-method-in-java) question. – Elliott Frisch Jan 30 '14 at 02:00

2 Answers2

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

Community
  • 1
  • 1
John Lee
  • 1,357
  • 1
  • 13
  • 26
  • 2
    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. – Hovercraft Full Of Eels Jan 30 '14 at 01:16
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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • 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])` – Vanessa Jan 30 '14 at 01:48
  • *"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 – MadProgrammer Jan 30 '14 at 02:00