/* Starting code provide by James Tam. Constructor(), generateCoordinates() and display() methods */ public class Galaxy { public static final int SIZE = 4; private StarShip [][] grid; public Galaxy () { boolean squareOccupied = false; grid = new StarShip [SIZE][SIZE]; int r; int c; int hull; // Initialize all default locations to null. for (r = 0; r < SIZE; r++) { for (c = 0; c < SIZE; c++) { grid[r][c] = null; } } grid[0][0] = new FedStarShip(); grid[0][1] = new KlingStarShip(); grid[1][0] = new StarShip(); } public void display () { int r; int c; for (r = 0; r < SIZE; r++) { for (c = 0; c < SIZE; c++) { if (grid[r][c] != null) { System.out.print(grid[r][c].getAppearance()); } else { System.out.print(" "); } } System.out.println(); } } public void runSimulatedAttacks() { int damage; damage = grid[0][0].attack(); System.out.println("Fed ship attacks for: " + damage); System.out.println(); damage = grid[0][1].attack(); System.out.println("Kling ship attacks for: " + damage); System.out.println(); damage = grid[1][0].attack(); System.out.println("Old style ship attacks for: " + damage); System.out.println(); /* Won't work because it's array of references to StarShips not KlingStarShips. grid[1][0].utterBattleCry(); */ if (grid[0][0] instanceof KlingStarShip) ((KlingStarShip) grid[0][0]).utterBattleCry(); if (grid[0][1] instanceof KlingStarShip) ((KlingStarShip) grid[0][1]).utterBattleCry(); if (grid[1][0] instanceof KlingStarShip) ((KlingStarShip) grid[1][0]).utterBattleCry(); } }