First, you will complete a class called HazMath (in the HazMath.java file) that implements the interface Mathematical (in the Mathematical.java file). These should have the following definitions:...

1 answer below »

First, you will complete a class called HazMath (in the HazMath.java file) that implements the interface Mathematical (in the Mathematical.java file). These should have the following definitions:



  • public boolean isPrime(int n)

    You cannot change the signature for the method. This method must be public static. This method is similar to the ones we've discussed in the lab and will return true or false depending on if the passed in integer values is prime or not, respectively. Return false if the invoker passes in a number less than 1. A prime number is one that is not evenly divisible by any other number. For example, if we have number 2, it should return True, and if we have number 55, it will return False. Some sample assertions:


    assert isPrime(2); assert isPrime(53); assert !isPrime(55); assert !isPrime(24); assert !isPrime(-37337);

    Prime numbers form the basis of cryptography! Read into why this is a little bit. Really cool stuff.



  • public int sumOfSums(int n)
    You cannot change the signature for the method. This method must be public static. This method takes an integer, n, and computes the sum of each of the sums of each integer between 1 and n. For example, the sum of the sums of 3 is (1) + (1 + 2) + (1 + 2 + 3) = 10. Some sample assertions:


    assert sumOfSums(1) == 1; assert sumOfSums(3) == 10; assert sumOfSums(6) == 56; assert sumOfSums(25) == 2925; assert sumOfSums(-5) == 0;





2048 Left Shift


Similar to in HW1, you'll implement a left shift of decimals in a string, with combining of multiple like digits into a single digit of double the value. The main difference between HW1 and this is that now the string representing a row in 2048 can be of any length! You'll implement aCombineclass in a comparable .java file.


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 themcombineto 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 invokevalidateCharto determine validity of each character.



  • public String repeat(char ch, int num)

    Create a string that consists ofnum,chcharacters.



  • public String eliminateUnderscores(String s)

    Given a Strings, return a new String that has all '_' characters removed. You cannot use thereplacemethod 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 useeliminateUnderscoresto cluster all the non-'_' characters, and then to userepeatto 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 thevalidateRowmethod 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 themoveLeftmethod 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 thevalidateRowmethod 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_")));
Answered Same DayMar 05, 2021

Answer To: First, you will complete a class called HazMath (in the HazMath.java file) that implements the...

Pratik answered on Mar 08 2021
156 Votes
Combinable.java
Combinable.java
public interface Combinable {
  //checking if the character is a valid character
    public  boolean validate
Char(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 open spance 
    public  String moveLeft(String row) ;

  //Combining the equal numbers in the row 22_ > 4__ 
    public  String combineLeft(String row) ;

}
Combine.java
Combine.java
import java.lang.*;
class Combine implements Combinable {
    // YOUR CODE UNDER THIS COMMENT
    public  boolean validateChar(char ch)
    {
        if(ch=='_'||ch=='2'||ch=='4') 
            return true;
        return false;
    }

    public  boolean validateRow(String row) 
    {
        for (int i = 0;i < row.length() ;i++) {
            if(false==validateChar(row.charAt(i)))
                return false;
        }
        return true;
    }

    public  String repeat(char ch, int num) 
    {
        String ans = "";
        for(int i = 0; i < num; ++i)
        {
            ans += ch;
        }
        return ans;
    }
    public  String eliminateUnderscore...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here