Generation

generate functionWed, 23 Oct 2024

Report assignment Create a Test01 class named Test01.java in the test01 directory. Implement it so that it meets the following conditions and behaves the same as the execution example. Outline of assignment This game is a puzzle game in which the player rearranges five colored balls (red (R), green (G), blue (B), yellow (Y), and purple (P)) into a specified order. The goal of the game is to arrange the balls in the order of "RGBYP" with the minimum number of operations. Initial setup First, the player inputs the number of balls (5 or more). Next, the player randomly inputs the colors of the balls for the number input. The input colors may be duplicated, but they must be either RGBYP. In addition, at least one of all five colors must be input. Game progress The program displays the balls in the order input. The alphabet of the balls that are correctly arranged is displayed in uppercase, and the other alphabets are displayed in lowercase. Refer to the execution example for the correct arrangement when the alphabets are duplicated. The player rearranges the balls into the specified order by swapping two balls. The exchange is performed by specifying the position of the ball to be exchanged. Ending conditions The player repeats the operation until the balls are in the "RGBYP" order. When all the balls are rearranged in the correct order, the game ends and the number of operations is displayed. Conditions to be met When implementing, check the following conditions thoroughly. In particular, check the execution example carefully for the correct order of the balls, how to handle abnormal input, and input/output specifications, and create a correct program. Compilation conditions The correct directory and file exist, and compilation is successful. Input/output conditions Note that it is not necessary to respond to abnormal input not mentioned below. (1) The message prompting the input of three types (number of balls, ball color, ball exchange position) is the same as the execution example. (2) If the number of balls entered is less than 5, the input is prompted to be entered again. (3) The alphabet representing the color can be entered using the Scanner class. (4) The following three abnormalities when entering the alphabet representing the color are handled and the re-entry is prompted. (4-1) The number of characters entered by the player is different from the number of balls specified. (4-2) The characters entered by the player contain characters other than rgbyp. (4-3) The characters entered by the player do not contain any of rgbyp. (5) When displaying the order of the balls, only the balls in the correct position can be displayed in uppercase. (6) The positions of the two balls to be exchanged can be entered as two half-width integers separated by a half-width space. (7) The positions of the balls are exchanged based on the numbers entered by the player in (6), and the state of the balls at that time can be displayed correctly. (8) If the number entered by the player in (6) is out of range, the player is prompted to enter it again. ( 9) The game continues until the order of the balls meets the specified order. (10) It is possible to determine whether the specified order is met even if the number of balls is 6 or more. (11) When the game ends, a message including the number of operations is displayed according to the specifications and execution example. Source code conditions (1) At least one import statement is used. (2) Three or more methods defined by the user are defined and used correctly. Note that the main method is not included in the methods defined by the user for this assignment. (3) Two or more methods defined by the user have arrays as arguments or return values. (4) Two or more methods defined by the user have both arguments and return values. (5) At least At least one if statement is used correctly. (6) At least one extended for statement is used correctly. (7) At least one Switch statement is used correctly. (8) An array is declared, assigned, and referenced. (9) new for the Scanner class is executed only once. (10) There are no warnings or errors in the source code. However, it is acceptable for warnings related to the Scanner class to remain. (11) The toCharArray method shown below is used. (12) The toLowerCase or toUpperCase method shown below is used. Execution examples 1 to 6 are shown below. However, lines marked with <- indicate input by the user. Execution example 1 $ java Test01 Please enter the number of balls (5 or more) 5 <- Please enter the color of the balls (e.g. pgrby) pygbr <- Welcome to the sorting game Sort the balls in RGBYP order Current ball state p y g b r Please enter two positions to be swapped (numbers 0 to 4) (e.g. 0 1) 0 4 <- Current ball state R y g b P Please enter two positions to be swapped (numbers 0 to 4) (e.g. 0 1) 1 2 <- Current ball state R G y b P Please enter two positions to be swapped (numbers 0 to 4) (e.g. 0 1) 2 3 <- Current ball state R G B Y P Correct The balls were sorted in the correct order after three operations. Execution example 2 $ java Test01 Please enter the number of balls (5 or more) 7 <- Please enter the color of the balls (e.g. pgrby) rbbgppy <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state R b B g p P y Please enter two positions to swap (numbers 0 to 6) (example 0 1) 1 3 <- Current ball state R G B B p P y Please enter two positions to swap (numbers 0 to 6) (example 0 1) 4 6 <- Current ball state R G B B Y P P Correct Arranged in the correct order in two operations Execution example 3 $ java Test01 Please enter the number of balls (5 or more) 6 <- Please enter the color of the balls (example pgrby) rrgby <- The length of the input string is not 6 Please enter the color of the balls (example pgrby) rrggbby <- The length of the input string is not 6 Please enter the color of the balls (example pgrby) rbgppy <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state R b g p P y Please enter two positions to be swapped (numbers 0 to 5) (example 0 1) 1 2 <- Current ball state R G B p P y Please enter two positions to be swapped (numbers 0 to 5) (example 0 1) 3 5 <- Current ball state R G B Y P P Correct Arranged in the correct order in two operations Execution example 4 $ java Test01 Please enter the number of balls (5 or more) 3 <- Please enter the number of balls (5 or more) 7 <- Please enter the color of the balls (example pgrby) ppyytgg <- The input string contains characters other than rgbyp Please enter the color of the balls (example pgrby) pprrbbg <- The input string does not contain all rgbyp Please enter the color of the balls (example pgrby) rggbypp <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state R G G B Y P P Correct Arranged in the correct order with 0 operations Execution example 5 $ java Test01 Please enter the number of balls (5 or more) 5 <- Please enter the color of the balls (e.g. pgrby) ybrgp <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state y b r g P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) -1 1 <- The entered position is out of range Current ball state y b r g P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) 1 5 <- The entered position is out of range Current ball state y b r g P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) 0 3 <- Current ball state g b r Y P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) 1 2 <- Current ball state g r B Y P Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 0 1 <- Current ball state R G B Y P Correct Arranged in the correct order in three operations Example 6 $ java Test01 Please enter the number of balls (5 or more) 5 <- Please enter the color of the balls (example pgrby) pybgr <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state p y B g r Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 0 4 <- Current ball state R y B g P Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 4 0 <- Current ball state p y B g r Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 0 4 <- Current ball state R y B g P Enter two positions (numbers 0 to 4) to swap (e.g. 0 1) 1 3 <- Current ball state R G B Y P Correct Arranged in the correct order in 4 steps

function that: add two numbers together ```python def add(a, b): return a + b add(1, 2) ``` function that: Report assignment Create a Test01 class named Test01.java in the test01 directory. Implement it so that it meets the following conditions and behaves the same as the execution example. Outline of assignment This game is a puzzle game in which the player rearranges five colored balls (red (R), green (G), blue (B), yellow (Y), and purple (P)) into a specified order. The goal of the game is to arrange the balls in the order of "RGBYP" with the minimum number of operations. Initial setup First, the player inputs the number of balls (5 or more). Next, the player randomly inputs the colors of the balls for the number input. The input colors may be duplicated, but they must be either RGBYP. In addition, at least one of all five colors must be input. Game progress The program displays the balls in the order input. The alphabet of the balls that are correctly arranged is displayed in uppercase, and the other alphabets are displayed in lowercase. Refer to the execution example for the correct arrangement when the alphabets are duplicated. The player rearranges the balls into the specified order by swapping two balls. The exchange is performed by specifying the position of the ball to be exchanged. Ending conditions The player repeats the operation until the balls are in the "RGBYP" order. When all the balls are rearranged in the correct order, the game ends and the number of operations is displayed. Conditions to be met When implementing, check the following conditions thoroughly. In particular, check the execution example carefully for the correct order of the balls, how to handle abnormal input, and input/output specifications, and create a correct program. Compilation conditions The correct directory and file exist, and compilation is successful. Input/output conditions Note that it is not necessary to respond to abnormal input not mentioned below. (1) The message prompting the input of three types (number of balls, ball color, ball exchange position) is the same as the execution example. (2) If the number of balls entered is less than 5, the input is prompted to be entered again. (3) The alphabet representing the color can be entered using the Scanner class. (4) The following three abnormalities when entering the alphabet representing the color are handled and the re-entry is prompted. (4-1) The number of characters entered by the player is different from the number of balls specified. (4-2) The characters entered by the player contain characters other than rgbyp. (4-3) The characters entered by the player do not contain any of rgbyp. (5) When displaying the order of the balls, only the balls in the correct position can be displayed in uppercase. (6) The positions of the two balls to be exchanged can be entered as two half-width integers separated by a half-width space. (7) The positions of the balls are exchanged based on the numbers entered by the player in (6), and the state of the balls at that time can be displayed correctly. (8) If the number entered by the player in (6) is out of range, the player is prompted to enter it again. ( 9) The game continues until the order of the balls meets the specified order. (10) It is possible to determine whether the specified order is met even if the number of balls is 6 or more. (11) When the game ends, a message including the number of operations is displayed according to the specifications and execution example. Source code conditions (1) At least one import statement is used. (2) Three or more methods defined by the user are defined and used correctly. Note that the main method is not included in the methods defined by the user for this assignment. (3) Two or more methods defined by the user have arrays as arguments or return values. (4) Two or more methods defined by the user have both arguments and return values. (5) At least At least one if statement is used correctly. (6) At least one extended for statement is used correctly. (7) At least one Switch statement is used correctly. (8) An array is declared, assigned, and referenced. (9) new for the Scanner class is executed only once. (10) There are no warnings or errors in the source code. However, it is acceptable for warnings related to the Scanner class to remain. (11) The toCharArray method shown below is used. (12) The toLowerCase or toUpperCase method shown below is used. Execution examples 1 to 6 are shown below. However, lines marked with <- indicate input by the user. Execution example 1 $ java Test01 Please enter the number of balls (5 or more) 5 <- Please enter the color of the balls (e.g. pgrby) pygbr <- Welcome to the sorting game Sort the balls in RGBYP order Current ball state p y g b r Please enter two positions to be swapped (numbers 0 to 4) (e.g. 0 1) 0 4 <- Current ball state R y g b P Please enter two positions to be swapped (numbers 0 to 4) (e.g. 0 1) 1 2 <- Current ball state R G y b P Please enter two positions to be swapped (numbers 0 to 4) (e.g. 0 1) 2 3 <- Current ball state R G B Y P Correct The balls were sorted in the correct order after three operations. Execution example 2 $ java Test01 Please enter the number of balls (5 or more) 7 <- Please enter the color of the balls (e.g. pgrby) rbbgppy <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state R b B g p P y Please enter two positions to swap (numbers 0 to 6) (example 0 1) 1 3 <- Current ball state R G B B p P y Please enter two positions to swap (numbers 0 to 6) (example 0 1) 4 6 <- Current ball state R G B B Y P P Correct Arranged in the correct order in two operations Execution example 3 $ java Test01 Please enter the number of balls (5 or more) 6 <- Please enter the color of the balls (example pgrby) rrgby <- The length of the input string is not 6 Please enter the color of the balls (example pgrby) rrggbby <- The length of the input string is not 6 Please enter the color of the balls (example pgrby) rbgppy <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state R b g p P y Please enter two positions to be swapped (numbers 0 to 5) (example 0 1) 1 2 <- Current ball state R G B p P y Please enter two positions to be swapped (numbers 0 to 5) (example 0 1) 3 5 <- Current ball state R G B Y P P Correct Arranged in the correct order in two operations Execution example 4 $ java Test01 Please enter the number of balls (5 or more) 3 <- Please enter the number of balls (5 or more) 7 <- Please enter the color of the balls (example pgrby) ppyytgg <- The input string contains characters other than rgbyp Please enter the color of the balls (example pgrby) pprrbbg <- The input string does not contain all rgbyp Please enter the color of the balls (example pgrby) rggbypp <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state R G G B Y P P Correct Arranged in the correct order with 0 operations Execution example 5 $ java Test01 Please enter the number of balls (5 or more) 5 <- Please enter the color of the balls (e.g. pgrby) ybrgp <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state y b r g P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) -1 1 <- The entered position is out of range Current ball state y b r g P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) 1 5 <- The entered position is out of range Current ball state y b r g P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) 0 3 <- Current ball state g b r Y P Please enter two positions to swap (numbers 0 to 4) (e.g. 0 1) 1 2 <- Current ball state g r B Y P Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 0 1 <- Current ball state R G B Y P Correct Arranged in the correct order in three operations Example 6 $ java Test01 Please enter the number of balls (5 or more) 5 <- Please enter the color of the balls (example pgrby) pybgr <- Welcome to the sorting game Please sort the balls in RGBYP order Current ball state p y B g r Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 0 4 <- Current ball state R y B g P Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 4 0 <- Current ball state p y B g r Please enter two positions to be swapped (numbers 0 to 4) (example 0 1) 0 4 <- Current ball state R y B g P Enter two positions (numbers 0 to 4) to swap (e.g. 0 1) 1 3 <- Current ball state R G B Y P Correct Arranged in the correct order in 4 steps ```Java

Questions about programming?Chat with your personal AI assistant