-1

I've got:

  • interface - ISpacecraft
  • abstract class - Spacecraft (implements the interface above)
  • Classes - 4 Kind of ships (derived children of Spacecraft)

Task: Return the count of every ship, if I have 2 cargoShips, then return 2.

Problem: When I iterate through the ArrayList It prints '2' twice, because I have 2 cargoShips.

Expected output: 2,1,1,2

Current output: 2,2,1,1,2,2

Question: How can I iterate through the number of types of ships, instead of all instances of them?

Note: I can't change the signature of 'printInstanceNumberPerClass'.

Code:

StarFleetManagerTester

public static void printInstanceNumberPerClass (ArrayList<ISpacecraft> fleet) 
{
    ArrayList<ISpacecraft> objects = new ArrayList<>(fleet);
    ArrayList<Integer> cnt = new ArrayList<Integer>();

    for(ISpacecraft obj : objects)
    {
        cnt.add(obj.getCount());
    }
    for(ISpacecraft obj : objects)
        System.out.println(obj.getCount() +"  "+obj.getName());
}

Spacecraft

protected static int countCruiser;

ISpacecraft

int getCount();

cargoShip, ReserarchShip, etc..

private static int count
@Override
    public int getCount() {
        return this.count;
    }
Ilan Aizelman WS
  • 1,630
  • 2
  • 21
  • 44
  • Did you have a question for us? – Joe C May 19 '17 at 20:35
  • You should keep track of the _types_ of ships. Here, you are counting the instances of a type of ship, per ship. Instead try keeping a record of the instances of a ship type, per ship type – Devin Snyder May 19 '17 at 20:35
  • @JoeC Well, I think it is trivial, I've provided the expected output. – Ilan Aizelman WS May 19 '17 at 20:36
  • @DevinSnyder Exactly. How can I do that? it will solve other problems too, like types of weapons for every type of ship. – Ilan Aizelman WS May 19 '17 at 20:37
  • If your question is "how do I fix it?", then this is not what SO is about. You need to narrow it down to a specific technical question. Do you want to remove duplicates from your list? Or would you prefer to find out the type of ship and count based on that? Or is there another approach you wish to take that you need our help with? Only once we have that, can we really be helpful. – Joe C May 19 '17 at 20:39
  • You're right. How can I iterate through the number of types of ships, instead of all instances of them? @JoeC – Ilan Aizelman WS May 19 '17 at 20:39
  • Look at `getClass()` and the `Map` interface. You should be able to work it out from there. – Joe C May 19 '17 at 20:49

1 Answers1

1

You could define a String member called typeName like this

protected String typeName;

public String getTypeName() {
    return typeName;
}

into your abstract class and define the member in child class constructors like this

typeName = "Some Particular Type";

Now, you need a static Map for your abstract class:

public static Map<String, int> typeCounts;

and in your constructor of the abstract class do something like this:

if (typeCounts == null) {
    typeCounts = new HashMap<String, int>();
}
if (typeCounts.get(getTypeName()) === null) {
    typeCounts.put(getTypeName(), 1);
} else {
    typeCounts.put(getTypeName(), typeCounts.get(getTypeName()) + 1);
}

Finally you will need to iterate typeCounts and use the keys and values for your output.

Community
  • 1
  • 1
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175