Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

program should: . Denote ships by the first letter of their name (CA\' for aircr

ID: 3734115 • Letter: P

Question



program should: . Denote ships by the first letter of their name (CA' for aircraft carrier, 'B' for battleship, 'S' for submarine, 'D' for destroyer, and 'P' for patrol boat). . Create a 10x10 grid of characters. Initially all squares of the grid should be set to 'E' for empty. .Create a method that prints the current game board. Print a period in spaces that are empty. Print the ship letter in spaces that are occupied. . Create a second method that hides a ship. The method should take 5 parameters: the character representing the ship's name, the length of the ship, and the (row, column) coordinate of the starting location of the ship, and a character indicating whether the ship should be placed horizontally beginning at the (row, column) coordinate or vertically beginning at the (row, column) coordinate. .In your main program, call the hide() method 5 times. Allow the user to enter the values passed to the method. For the time being, you can assume that your user is smart enough not to place ships on top of each other. But it would be a great idea to think through how you could detect such misplacements.

Explanation / Answer


Given below is the code for the question.
To indent code in eclipse , select code by pressing ctrl+a and then indent using ctrl+i
Please do rate the answer if it was helpful. Thank you


import java.util.Scanner;

public class Board {
private char[][] grid;

public Board(int size)
{
grid = new char[size][size];
initialize();
}

private void initialize()
{
int rows = grid.length , cols = grid[0].length;
for(int i= 0; i < rows; i++)
for(int j = 0; j < cols; j++)
grid[i][j] = 'E';
}

public void print()
{
int rows = grid.length , cols = grid[0].length;
for(int i= 0; i < rows; i++)
{
for(int j = 0; j < cols; j++)
{
if(grid[i][j] == 'E')
System.out.print(".");
else
System.out.print(grid[i][j]);

System.out.print(" ");
}
System.out.println();
}
System.out.println();
}

public void hide(char ship,int len, int row, int col, char dir)
{
int dr = 0, dc = 0;

if(dir == 'V')
dr = 1;
else
dc = 1;

int r = row, c = col;

for(int i = 0 ; i < len; i++)
{
grid[r][c] = ship;
r += dr;
c += dc;
}
}

public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
Board g = new Board(10);
int row, col, len;
char ship, dir;

System.out.println("Initial Board");
g.print();

System.out.println("Enter details for 5 ships to be placed");
for(int i = 1; i <= 5; i++)
{
System.out.print("Which ship do you want to place? ");
ship = keyboard.next().toUpperCase().charAt(0);

System.out.print("How long is the ship? ");
len = keyboard.nextInt();

System.out.print("Please enter row to place a ship (starts from 0): ");
row = keyboard.nextInt();

System.out.print("Please enter col to place a ship (starts from 0): ");
col = keyboard.nextInt();

System.out.print("Do you want the ship to be vertical or horizontal (V or H): ");
dir = keyboard.next().toUpperCase().charAt(0);

g.hide(ship, len, row, col, dir);

System.out.println("Updated Board:");
g.print();

}
}
}

output
------
Initial Board
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

Enter details for 5 ships to be placed
Which ship do you want to place? S
How long is the ship? 3
Please enter row to place a ship (starts from 0): 2
Please enter col to place a ship (starts from 0): 4
Do you want the ship to be vertical or horizontal (V or H): V
Updated Board:
. . . . . . . . . .
. . . . . . . . . .
. . . . S . . . . .
. . . . S . . . . .
. . . . S . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

Which ship do you want to place? B
How long is the ship? 4
Please enter row to place a ship (starts from 0): 0
Please enter col to place a ship (starts from 0): 0
Do you want the ship to be vertical or horizontal (V or H): H
Updated Board:
B B B B . . . . . .
. . . . . . . . . .
. . . . S . . . . .
. . . . S . . . . .
. . . . S . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

Which ship do you want to place? D
How long is the ship? 3
Please enter row to place a ship (starts from 0): 1
Please enter col to place a ship (starts from 0): 7
Do you want the ship to be vertical or horizontal (V or H): V
Updated Board:
B B B B . . . . . .
. . . . . . . D . .
. . . . S . . D . .
. . . . S . . D . .
. . . . S . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .
. . . . . . . . . .

Which ship do you want to place? P
How long is the ship? 2
Please enter row to place a ship (starts from 0): 6
Please enter col to place a ship (starts from 0): 7
Do you want the ship to be vertical or horizontal (V or H): V
Updated Board:
B B B B . . . . . .
. . . . . . . D . .
. . . . S . . D . .
. . . . S . . D . .
. . . . S . . . . .
. . . . . . . . . .
. . . . . . . P . .
. . . . . . . P . .
. . . . . . . . . .
. . . . . . . . . .

Which ship do you want to place? A
How long is the ship? 5
Please enter row to place a ship (starts from 0): 9
Please enter col to place a ship (starts from 0): 2
Do you want the ship to be vertical or horizontal (V or H): H
Updated Board:
B B B B . . . . . .
. . . . . . . D . .
. . . . S . . D . .
. . . . S . . D . .
. . . . S . . . . .
. . . . . . . . . .
. . . . . . . P . .
. . . . . . . P . .
. . . . . . . . . .
. . A A A A A . . .