Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

We will represent a single row of the 2048 square that includes only three blocks with a 3-length string. Each character in the string is

We will represent a single row of the 2048 square that includes only three blocks with a 3-length string. Each character in the string is either a '4', a '2', or a 'which designates a "blank" space. Example rows are "42_", "4 4", and "242". Your program will execute a "shift blocks left" operation which will include combining like blocks into one of double the value. Note that newly created (double valued) blocks are not combined with other blocks. Examples of proper shift left operations include: shifted original "4 " " 4" "42_""222" "4,-""2-2" "242" "242" "44" "422" . .

As before, the input string can only include the characters '_', '2', and '4'. Unlike before, it can be of any length (including length 0). Any '2's that have no other characters or any number of '_'s between them combine to become a '4'(i.e. two '2's disappear, and a '4' is left in one of their space). Likewise, '4's become '8's. Any digit that is the result of a combination cannot be combined with another digit. All digits are combined in this way and shifted to the far left so that any '_' appear on the right of the string. The length of the string remains unchanged.

Your classes will implement the Combinable interface. Note that we're attaching an updated version so be sure to use the new one. You will implement multiple methods from the interface Combinable:

  • public static boolean validateChar(char ch)

    The requirements remain unchanged from HW1.

  • public boolean validateRow(String s)

    The string must include only the characters discussed above, but it can be of any length. Return true if it includes only valid characters, false otherwise. You must invoke validateChar to determine validity of each character.

  • public String repeat(char ch, int num)

    Create a string that consists of num, ch characters.

  • public String eliminateUnderscores(String s)

    Given a String s, return a new String that has all '_' characters removed. You cannot use the replace method in the String Class. Note that the returned string will be of the same or lesser length than the argument.

  • public String moveLeft(String s)

    All non-'_' characters in the input string are shifted to the left of the string, leaving only '_'s on the right-hand side of the string. The resulting "left-shifted" String is returned. It is highly suggested that you first use eliminateUnderscores to cluster all the non-'_' characters, and then to use repeat to concatenate the appropriate number of '_' on to the right of the String that is returned. Most other ways to implement this method are far more complicated.

    You must invoke the validateRow method to test if the input String is valid. If it is not, return the input string directly without change.

  • public String combineLeft(String s)

    Given the input string, the output should be a string of the same length, with all '_'s removed from the left side of the string, and with all pairs of like digits with only '_'s in between them, combined into a digit of twice the value. Return the resulting String.

    It is highly suggested that you use the moveLeft method to help you with this implementation. When generating your new string, it is much easier to be able to ignore all '_' between digits.

    You must invoke the validateRow method to test if the input String is valid. If it is not, return the input string directly without change.

Some example assertions:

assert validateChar('_'); assert !validateChar(' '); assert validateRow("2222"); assert validateRow("_24_"); assert !validateRow("2234_"); assert validateRow(""); assert !validateRow("*"); assert "***".equals(repeat('*', 3)); assert "".equals(repeat('*', 0)); assert "44444444".equals(repeat('4', 8)); assert "4".equals(eliminateUnderscores("__4__")); assert "244".equals(eliminateUnderscores("2_4_4_")); assert "".equals(eliminateUnderscores("___")); assert "242442".equals(eliminateUnderscores("242442")); assert "4____".equals(moveLeft("__4__")); assert "244___".equals(moveLeft("2_4_4_")); assert "___".equals(moveLeft("___")); assert "_3_".equals(moveLeft("_3_")); assert "242442".equals(moveLeft("242442")); assert "484___".equals(combineLeft("224422")); assert "484________".equals(combineLeft("2_2_4_4_2_2")); assert "242424__".equals(combineLeft("242_42_4")); assert "828____".equals(combineLeft("__44244")); assert "".equals(combineLeft("")); assert "22344".equals(combineLeft("22344")); assert "88__________".equals(combineLeft(combineLeft("_2_22_222_4_")));

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Explain the various methods of job evaluation

Answered: 1 week ago

Question

Differentiate Personnel Management and Human Resource Management

Answered: 1 week ago

Question

Describe the functions of Human resource management

Answered: 1 week ago