In this assignment, you will create a graphical grid where you can add and remov
ID: 3715711 • Letter: I
Question
In this assignment, you will create a graphical grid where you can add and remove points. Here is a sample run of the program (user input is in red):
Requirements
This program should contain a single class (called Proj4) with a main method. Your program should compile and run in BlueJ.
Your program should look exactly like the screenshot above when it runs, except for the location of the *s based on user input.
Here are some additional requirements:
Here is how to read in a single character, assuming that s is the name of your Scanner:
You must store the grid in a two-dimensional array of characters.
Before getting any user input, use nested loops to store ‘_’ at every position in the array.
When the user chooses to insert, replace the specified position with a ‘+’. When the user chooses to delete, replace the specified position with a ‘_’.
Print the grid (using nested loops) before getting any user input and after each insert/delete.
You should allow for either upper- or lower-case menu options
If the user enters something other than i/I, d/D, or q/Q, you should print an error.
Notice that the user will be entering rows and columns in the range 1-10, while your two-dimensional array will be indexed from 0-9. Your program will need to do this adjustment. (For example, if the user wants to change the spot at row 1/column 1, your program should update the array at position [0][0].)
If the user enters a row or column that is invalid (less than 1 or greater than 10), print an error.
Explanation / Answer
Here is the required code you asked. Defined everything as simple as possible, if you have any queries, drop a comment. Thank you!
// Proj4.java
import java.util.Scanner;
public class Proj4 {
public static void main(String[] args) {
/**
* creating a 2D array of 10*10 size and filling with underscore
* characters
*/
char grid[][] = new char[10][10];
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
grid[i][j] = '_';
}
}
/**
* Scanner to read user input
*/
Scanner scanner = new Scanner(System.in);
char input = ' ';
int row, col;
/**
* Loops until user wishes to quit
*/
while (input != 'q') {
// displaying the grid
printGrid(grid);
// showing the menu
System.out.print(" Enter (i)nsert, (d)elete or (q)uit: ");
/**
* getting input choice, converting to lower case and extracting
* first character
*/
input = scanner.nextLine().toLowerCase().charAt(0);
switch (input) {
case 'i':
/**
* Inserting a point
*/
System.out.print("Enter row (1-10): ");
row = Integer.parseInt(scanner.nextLine());
row--; // converting to array index
if (row < 0 || row >= grid.length) {
System.out.println("Invalid row");
break;
}
System.out.print("Enter column (1-10): ");
col = Integer.parseInt(scanner.nextLine());
col--; // converting to array index
if (col < 0 || col >= grid[0].length) {
System.out.println("Invalid column");
break;
}
grid[row][col] = '+';
break;
case 'd':
/**
* Deleting a point
*/
System.out.print("Enter row (1-10): ");
row = Integer.parseInt(scanner.nextLine());
row--; // converting to array index
if (row < 0 || row >= grid.length) {
System.out.println("Invalid row");
break;
}
System.out.print("Enter column (1-10): ");
col = Integer.parseInt(scanner.nextLine());
col--; // converting to array index
if (col < 0 || col >= grid[0].length) {
System.out.println("Invalid column");
break;
}
grid[row][col] = '_';
break;
case 'q':
System.out.println("Bye!");
break;
default:
System.out.println("Invalid choice");
break;
}
}
}
/**
* method to print the grid
*
* @param grid
* - 2d array of chars
*/
static void printGrid(char[][] grid) {
for (int i = 0; i < grid.length; i++) {
for (int j = 0; j < grid[0].length; j++) {
System.out.print(grid[i][j] + " ");
}
System.out.println();
}
}
}
/*OUTPUT*/
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
Enter (i)nsert, (d)elete or (q)uit: i
Enter row (1-10): 11
Invalid row
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
Enter (i)nsert, (d)elete or (q)uit: i
Enter row (1-10): 2
Enter column (1-10): 2
_ _ _ _ _ _ _ _ _ _
_ + _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
Enter (i)nsert, (d)elete or (q)uit: i
Enter row (1-10): 5
Enter column (1-10): 7
_ _ _ _ _ _ _ _ _ _
_ + _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ + _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
Enter (i)nsert, (d)elete or (q)uit: d
Enter row (1-10): 2
Enter column (1-10): 2
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ + _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _ _ _
Enter (i)nsert, (d)elete or (q)uit: q
Bye!