Question
Looking at this method, it returns true if a given location contains a real ship that is still afloat. It says real ship because the
Looking at this method, it returns true if a given location contains a real ship that is still afloat. It says "real" ship because the 'emptySea' class extends Ship class with an inherited length of one, saving the need for null checks as empty locations now contain a non-null value meaning it does not contain a ship. 'isOccupied' will return true if that location contains a ship. The 'ships' array is used to determine which ship is in that location. 'allShips' is an ArrayList to which all ships were added too.
Given this information, I would like to understand how this method works - line by line. If someone could comment each line, that would be really helpful. Thank you.
public boolean shootAt(int row, int column) { int hit = 0; int sunkNum = 0; if (isOccupied(row, column) && !ships[row][column].isSunk()) { this.hitCount += 1; hit = 1; } this.shotsFired += 1; this.ships[row][column].shootAt(row, column); for (Ship ship: this.allShips) { if (ship.isSunk()){ sunkNum += 1; } } this.shipsSunk = sunkNum; if (hit == 1) { return true; } return false; }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started