Similar to in HW1, you'll implement a left shift of decimals ina string, with combining of multiple like digits into a singledigit of double the value. The main difference between HW1 and thisis that now the string representing a row in 2048 can be of anylength! You'll implement a Combine class in acomparable .java file.
As before, the input string can only include the characters '_','2', and '4'. Unlike before, it can be of any length (includinglength 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'sdisappear, and a '4' is left in one of their space). Likewise, '4'sbecome '8's. Any digit that is the result of a combination cannotbe combined with another digit. All digits are combined in this wayand shifted to the far left so that any '_' appear on the right ofthe string. The length of the string remains unchanged.
Your classes will implement the Combinable interface. Note thatwe're attaching an updated version so be sure to use the new one.You will implement multiple methods from the interfaceCombinable:
- public boolean validateChar(char ch)
The requirements remain unchanged from HW1.
- public boolean validateRow(String s)
The string must include only the characters discussed above, butit can be of any length. Return true if it includes only validcharacters, false otherwise. You must invokevalidateChar to determine validity of eachcharacter.
- 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 hasall '_' characters removed. You cannot use thereplace method in the String Class. Note that thereturned string will be of the same or lesser length than theargument.
- public String moveLeft(String s)
All non-'_' characters in the input string are shifted to theleft of the string, leaving only '_'s on the right-hand side of thestring. The resulting \"left-shifted\" String is returned. It ishighly suggested that you first useeliminateUnderscores to cluster all the non-'_'characters, and then to use repeat to concatenatethe appropriate number of '_' on to the right of the String that isreturned. Most other ways to implement this method are far morecomplicated.
You must invoke the validateRow method to testif
the input String is valid. If it is not, return the input stringdirectly without change.
- public String combineLeft(String s)
Given the input string, the output should be a string of thesame length, with all '_'s removed from the left side of thestring, and with all pairs of like digits with only '_'s in betweenthem, combined into a digit of twice the value. Return theresulting String.
It is highly suggested that you use themoveLeft method to help you with thisimplementation. When generating your new string, it is much easierto be able to ignore all '_' between digits.
You must invoke the validateRow method to testif the input String is valid. If it is not, return the input stringdirectly 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_\")));
Here is the first code:
public class Combine implements Combinable {
  public boolean validateChar(char ch)
  {
//Write your Code Here
     return true;
  }
 Â
  public boolean validateRow(String row)
{
//Write your Code Here
     return true;
  }
 Â
  public String repeat(char ch, int num)
{
//Write your Code Here
      return \"\";
  }
  public String eliminateUnderscores(Stringrow)
{
//Write your Code Here
return \"\";Â Â
}
  public String moveLeft(String row)
{
//Write your Code Here
     return \"\";
  }
  public String combineLeft(String row)
{
//Write your Code Here
     return \"\";
  }
 Â
   public static void main(String[] args)
{
Combine CM=new Combine();
     assert CM.validateChar('_');
     assert !CM.validateChar(' ');
     assertCM.validateRow(\"2222\");
     assertCM.validateRow(\"_24_\");
     assert!CM.validateRow(\"2234_\");
     assert CM.validateRow(\"\");
     assert !CM.validateRow(\"*\");
     assert\"***\".equals(CM.repeat('*', 3));
     assert \"\".equals(CM.repeat('*',0));
     assert\"44444444\".equals(CM.repeat('4', 8));
     assert\"4\".equals(CM.eliminateUnderscores(\"__4__\"));
     assert\"244\".equals(CM.eliminateUnderscores(\"2_4_4_\"));
     assert\"\".equals(CM.eliminateUnderscores(\"___\"));
     assert\"242442\".equals(CM.eliminateUnderscores(\"242442\"));
     assert\"4____\".equals(CM.moveLeft(\"__4__\"));
     assert\"244___\".equals(CM.moveLeft(\"2_4_4_\"));
     assert\"___\".equals(CM.moveLeft(\"___\"));
     assert\"_3_\".equals(CM.moveLeft(\"_3_\"));
     assert\"242442\".equals(CM.moveLeft(\"242442\"));
     assert\"484___\".equals(CM.combineLeft(\"224422\"));
     assert\"484________\".equals(CM.combineLeft(\"2_2_4_4_2_2\"));
     assert\"242424__\".equals(CM.combineLeft(\"242_42_4\"));
     assert\"828____\".equals(CM.combineLeft(\"__44244\"));
     assert\"\".equals(CM.combineLeft(\"\"));
     assert\"22344\".equals(CM.combineLeft(\"22344\"));
     assert\"88__________\".equals(CM.combineLeft(CM.combineLeft(\"_2_22_222_4_\")));
     System.out.println(\"All testspassed.\");
  }
}
Here is the second code:
public interface Combinable {
//checking if the character is a valid character
  public boolean validateChar(char ch);
 Â
//checking if a row contains the correct set of characters
  public boolean validateRow(String row) ;
//Create a string that consists of num, ch characters.
  public String repeat(char ch, int num) ;
 Â
//Removing the undescore from the row
  public String eliminateUnderscores(String row) ;
 Â
//Shifting the numbers to the left if there is any openspance
  public String moveLeft(String row) ;
//Combining the equal numbers in the row 22_ > 4__
  public String combineLeft(String row) ;
}