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

JAVA!!! UTF8 Please don\'t change the method type or use additional import state

ID: 3733102 • Letter: J

Question

JAVA!!! UTF8

Please don't change the method type or use additional import statements. Two methods before main need to be written.

Thank you!

import java.util.Scanner;
import java.util.Random;
import java.lang.StringBuilder;

public class Battleship {


public static int coordAlphaToNum(String coord) {
int ret = 0, mul = 1;
coord = coord.toUpperCase();
for(int i = coord.length() - 1; i >= 0; i--){
ret += ((int)coord.charAt(i) - (int)'A') * mul;
mul *= 26; //pow is not necessary in this simple version, saves time complexity
}
return ret;
}


public static String coordNumToAlpha(int coord) {
String ret = "";
while(coord > 0){
int mod = coord % 26;
coord /= 26;
ret += (char)('A' + mod);   
}
return new StringBuilder(ret).reverse().toString();
}


public static int promptInt(Scanner sc, String valName, int min, int max) {
System.out.print("Enter the " + valName + " (" + min + " to " + max + "): ");
int line = sc.nextInt();

if(min <= line && line <= max)
return line;
else{
System.out.println("Invalid value.");
return promptInt(sc, valName, min, max);
}
}


public static String promptStr(Scanner sc, String valName, String min, String max) {
System.out.print("Enter the " + valName + " (" + min + " to " + max + "): ");
String line = sc.nextLine();
line = line.trim(); //trim spaces leading and trailing

String min_ = min.toUpperCase(); //for ignoring case
String max_ = max.toUpperCase();
line = line.toUpperCase();
if(min_.compareTo(line) <= 0 && line.compareTo(max_) <= 0)
return line;
else{
System.out.println("Invalid value.");
return promptStr(sc, valName, min, max);
}
}


/**
* Initializes a game board so that all the entries are Config.WATER_CHAR.
*
* @param board The game board to initialize.
*/
public static void initBoard(char board[][]) {
//FIXME
}

/**
* Prints the game boards as viewed by the user. This method is used to print the
* game boards as the user is placing their ships and during the game play.
*
* Some notes on the display:
* - Each column printed will have a width of Config.MAX_COL_WIDTH.
* - Each row is followed by an empty line.
* - The values in the headers and cells are to be right justified.
*
* @param board The board to print.
* @param caption The board caption.
*/
public static void printBoard(char board[][], String caption) {
//FIXME
}


public static void main(String[] args) {
  
System.out.println("Welcome to Battleship!");
Scanner sc = new Scanner(System.in);
  
char again = 'y';
while(again == 'y' || again == 'Y'){
promptInt(sc, "board height", Config.MIN_HEIGHT, Config.MAX_HEIGHT);
promptInt(sc, "board width", Config.MIN_WIDTH, Config.MAX_WIDTH);

System.out.print(" Would you like to play again? (y/n): ");
again = sc.next().charAt(0);
}
System.out.println("Thanks for playing!");
}

public class Config {

/**
* Minimum and maximum values used in the program
*/
public static final int MIN_WIDTH = 1; //Minimum number of columns
public static final int MAX_WIDTH = 675; //Maximum number of columns
public static final int MIN_HEIGHT = 1; //Minimum number of rows
public static final int MAX_HEIGHT = 99; //Maximum number of rows
public static final int MAX_COL_WIDTH = 3; //Maximum number of characters per column
public static final int MIN_SHIPS = 1; //Minimum number of ships
public static final int MAX_SHIPS = 9; //Maximum number of ships
public static final int MIN_SHIP_LEN = 1; //Minimum ship length

/**
* Character values for displaying the different statuses of the game board cells.
*/
public static final char WATER_CHAR = '~'; // Water character (not yet targeted)
public static final char HIT_CHAR = '*'; // Hit character
public static final char MISS_CHAR = '@'; // Miss character

/**
* Constants for the random processes.
*/
public static final long SEED = 1234; // The random seed
public static final int RAND_SHIP_TRIES = 20; // Maximum number of tries to place a ship
  
}
}


SAMPLE OUTPUT:
Welcome to Battleship!
Enter the board height (1 to 99): 8
Enter the board width (1 to 675): 5

Enter the number of ships (1 to 9): 3
My Ships:   
A B C D E
0 ~ ~ ~ ~ ~

1 ~ ~ ~ ~ ~

2 ~ ~ ~ ~ ~

3 ~ ~ ~ ~ ~

4 ~ ~ ~ ~ ~

5 ~ ~ ~ ~ ~

6 ~ ~ ~ ~ ~

7 ~ ~ ~ ~ ~

Vertical or horizontal? (v/h): v
Enter the ship length (1 to 8): 3
Enter the x-coord (A to E): C
Enter the y-coord (0 to 5): 3
My Ships:   
A B C D E
0 ~ ~ ~ ~ ~

1 ~ ~ ~ ~ ~

2 ~ ~ ~ ~ ~

3 ~ ~ 1 ~ ~

4 ~ ~ 1 ~ ~

5 ~ ~ 1 ~ ~

6 ~ ~ ~ ~ ~

7 ~ ~ ~ ~ ~

Vertical or horizontal? (v/h): H
Enter the ship length (1 to 5): 4
Enter the x-coord (A to B): A
Enter the y-coord (0 to 7): 0
My Ships:   
A B C D E
0 2 2 2 2 ~

1 ~ ~ ~ ~ ~

2 ~ ~ ~ ~ ~

3 ~ ~ 1 ~ ~

4 ~ ~ 1 ~ ~

5 ~ ~ 1 ~ ~

6 ~ ~ ~ ~ ~

7 ~ ~ ~ ~ ~

Vertical or horizontal? (v/h): v
Enter the ship length (1 to 8): 5
Enter the x-coord (A to E): e
Enter the y-coord (0 to 3): 2
Would you like to play again? (y/n): y
Enter the board height (1 to 99): 2
Enter the board width (1 to 675): 1

Enter the number of ships (1 to 9): 1
My Ships:
A
0 ~

1 ~

Vertical or horizontal? (v/h): h
Enter the ship length (1 to 1): 1
Enter the x-coord (A to A): a
Enter the y-coord (0 to 1): 1
Would you like to play again? (y/n): w
Thanks for playing!

Explanation / Answer

import java.util.Scanner; import java.util.Random; import java.lang.StringBuilder; public class Battleship { public static int coordAlphaToNum(String coord) { int ret = 0, mul = 1; coord = coord.toUpperCase(); for(int i = coord.length() - 1; i >= 0; i--){ ret += ((int)coord.charAt(i) - (int)'A') * mul; mul *= 26; //pow is not necessary in this simple version, saves time complexity } return ret; } public static String coordNumToAlpha(int coord) { String ret = ""; while(coord > 0){ int mod = coord % 26; coord /= 26; ret += (char)('A' + mod); } return new StringBuilder(ret).reverse().toString(); } public static int promptInt(Scanner sc, String valName, int min, int max) { System.out.print("Enter the " + valName + " (" + min + " to " + max + "): "); int line = sc.nextInt(); if(min