Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

JAVA: This is a horizontal sweep strategy for finding battleship on a 25x25 board. The board is represented by a 25x25 integer array called map

JAVA:

This is a horizontal sweep strategy for finding battleship on a 25x25 board. The board is represented by a 25x25 integer array called map and is passed into the HorizontalSweep stretegy class. How could you change this code to make it more efficient?

public class HorizontalSweep implements SearchStrategy {

int searchCount = 0;

int foundCount = 0;

String shipHead;

String shipTail;

String subHead;

String subTail;

public void search(int[][] map) {

System.out.println("Strategy: Horizontal Sweep");

for (int row = 0; row < map.length; row++) {

for (int col = 0; col < map[row].length; col++) {

if (map[row][col] != 0) {

foundCount++;

if (map[row][col] == 1) shipHead = "("+row+","+col+")";

else if (map[row][col] == 2) shipTail = "("+row+","+col+")";

else if (map[row][col] == 3) subHead = "("+row+","+col+")";

else if (map[row][col] == 4) subTail = "("+row+","+col+")";

}

searchCount++;

if (foundCount > 7) break;

}

if (foundCount > 7) break;

}

System.out.println("Number of cells searched: " + searchCount);

System.out.println("Carrier found: "+shipHead+" to "+shipTail+" Submarine found: "+subHead+" to "+subTail);

}

}

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

Big Data Concepts, Theories, And Applications

Authors: Shui Yu, Song Guo

1st Edition

3319277634, 9783319277639

More Books

Students also viewed these Databases questions

Question

b. Does senior management trust the team?

Answered: 1 week ago

Question

What are the Five Phases of SDLC? Explain each briefly.

Answered: 1 week ago

Question

How can Change Control Procedures manage Project Creep?

Answered: 1 week ago