Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle Database Foundations Technology Fundamentals For IT Success

Authors: Bob Bryla

1st Edition

0782143725, 9780782143720

More Books

Students also viewed these Databases questions