UTA 2D Arrays Loop with Menu Drawing Data & Split Method Project Task 1 Project: HW8_RobotSanta Objectives: review split to build array from string, write

UTA 2D Arrays Loop with Menu Drawing Data & Split Method Project Task 1
Project: HW8_RobotSanta
Objectives: review split to build array from string, write code for problem discussed in class for problem solving, ‘distribute’ data from one array to other arrays.

Write the program for this Robot Santa problem. Assume that each present is given by a name consisting of a label (A for Alice or B for Bob) and a toy name. For example “A doll” means it is a doll and it should go to Alice (A), “B train set” means it is a train set and will go to Bob (B).
The presents are given by the user as a list separated by ;.
a) Write a program that reads the list from the user and creates 2 arrays with the presents for Alice and Bob with just the toy name, not the label. See sample run below.
You must create two arrays (AliceArr and BobArr) and store the presents for Alice in AliceArr and the presents for Bob in BobArr.
Hint: Use the split method to quickly turn the string of labelled presents into a 1D array of strings

———-Sample run:
Enter the presents:A doll;B train set;B drawing pad;A car;A Teddy bear;A puzzle;A pen

Alice gets:
Teddy bear

Bob gets:
train set
drawing pad

EXTEND YOUR THINKING (this part will not be graded): A method can only return one ‘thing’ in Java (e.g. an int, String, array,…). How would you “put together” AliceArr and BobArr into one 2D array that you could then return from a method?
Task 2a
Project: HW8_PlaceQueens
Objectives: writing a complex program that includes: updating and printing a 2D table (printed table will have labelled rows and columns), error checking. Practice modularization by having to structure the program using methods (reuse written methods to help you achieve new tasks – see recommended use of add queen to implement delete queen-). Good code alignment (for if/else, loops, and method definitions).

This program will maintain and print a chessboard that allows the user to place Queen pieces. The board will show the Queens and the row, column and diagonals the Queens will attack on.
Implement the behaviour shown in the sample run below. Make sure you align your code as it should be for each if-else, loop (for or while), method definition.
Code structure (a method to do the work for each user option):

in main – implement a loop that displays the menu information and takes the user option and whatever other data is needed for that option. Next, implement the actions for each choice, using METHODS. DO NOT write a big pile of code in main, but rather call other methods to do the work. Suggested method break-up is given below.
Note that since the same board must be used for as part of the actions for more than one option, the board object should be in main and passed as argument to other methods to work with it.
a method to create and return a board of a given dimension.
a method to print the board. (The board will be passed as an argument to this method. Same for the others.)
a method to reset the board
a method to add a queen on the board at a given row and column and show with a * in each cell that it can attack. The row and column will be given as arguments, NOT read from the user.
Hint, when you show mark (with *) the cells that are on the diagonal line of attack for a queen, it is easier to go ‘from the Q cell out then viceversa’:
a method to count queens on the board
a method to delete a queen from a specific row and column. In addition to the checks for valid board, valid row and valid column, this method should also check that there is a queen at the given position.
You cannot simply delete a queen from the existing board because it is not easy to know which * to remove and which to keep since another queen could be attaching on the same row as the one you are removing (see sample run). Solve this by resetting the board and replacing the queens that should remain.
Take advantage of already implemented methods such as reset board and add queen. (Note how the add queen method can be reused here because the row and column are given as arguments as opposed to read from the user inside the method.)
Other specifications:
All the data read from the user should be read in the main method and passed to the others as arguments.
You can assume the board is always square.
The table size can be larger than 10.
The table cells should be displayed on 3 spaces.
The * must be centered in the cell.
You can represent the board as a 2D of whatever type you prefer (int, char, String).
A queen attacks on the row, column and diagonal that it is on.
The program should be case insensitive. If the user enters upper case letters (Q/R/C…) it should work just as it does with the lower case (see Enter option: R below).
The methods should NOT crash. For example if print is called before a board is made, it should not crash. (See run3.txt below.) You do not need to produce identical error messages as mine, but the program should not crash (e.g. with index out of bound error).
The create board method should return null if asked to create a board of size 0 or negative.
The * and Q are part of the data in the matrix (however you choose how to represent them). The | is part of how you DISPLAY the matrix. The vertical bar, |, should NOT be recorded as data in the matrix (it makes your processing harder, and the code less flexible. For example, what if I asked you to store the matrix as a matrix of int? Or what if when the matrix is printed, the width of each cell is CHOSEN BY THE USER? In that case you cannot hardcode a specific spacing such as: ” * |”. In short, it is NOT good style to have entries in your matrix such as ” * |” or ” Q |”.
Suggested code organization for the methods:
public static …… {
if (bad_data1) {
//print a message


See sample runs:
general.txt – for general behaviour
insensitive.txt – for case insensitive commands
invalid.txt – for invalid row or column given by the user.
delete.txt – for delete queen examples
Suggested: start with the menu,argument passing and error checking and you slowly add the functionality. Use method stubs (i.e. a placeholder, or a “dummy” method that matches the signature needed). Ask in class if you want to discuss method stubs further.
Task 2b – Written part
Submit the answers for this part in a document called HW8_written.pdf. It must be an electronic document of type pdf, but it can be a scan or photo of a handwritten paper.

Part b
Draw the data for the delete queen method that is called FIRST in the delete.txt sample run. (You have the 3 queens that were added and need to delete the queen at row 3 and column 1.) Show the code on the left side and the drawing on the right side for each main step. A ‘main step’ is: a method call, a complete execution (all iterations) of a single loop, one iteration of the OUTTER loop in nested loops.

Submit a Comment