Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVAAAAAAA CAN SOMEBODY HELP ME FIX THIS!!!!!!!????????? /** * This method conve

ID: 3914529 • Letter: J

Question

JAVAAAAAAA

CAN SOMEBODY HELP ME FIX THIS!!!!!!!?????????

/**
     * This method converts a String representing a base (or radix) 26 number into a decimal (or
     * base 10) number. The String representation of the base 26 number uses the letters of the
     * Latin alphabet to represent the 26 digits. That is, A represents 0, B represents 1, C
     * represents 2, ..., Y represents 24, and Z represents 25.
     *
     * A couple of examples:
     * BAAA = 1 * 26^3 + 0 * 26^2 + 0 * 26^1 + 0 * 26^0 = 17576
     * ZERTY = 25 * 26^4 + 4 * 26^3 + 17 * 26^2 + 19 * 26^1 + 24 * 26^0 = 11506714
     *
     * For this method:
     *   - use Math.pow to calculate the powers of 26.
     *   - don't assume that the input is in any particular case; use toUpperCase().
     *   - don't check that the input is only 'A' to 'Z'.
     *   - calculate the value of each digit relative to 'A'.
     *   - start from either the first or last character, and calculate the exponent based on the
     *     index of each character.
     *
     * @param coord The coordinate value in base 26 as described above.
     * @return The numeric representation of the coordinate.
     */
  
   public static int coordAlphaToNum(String coord) {

           int ans = 0;

           for(int i=0; i<coord.length(); i++) {

               int e = Character.toUpperCase(coord.charAt(i)) - 'A';

               ans = ans + e*(int)Math.pow(26, coord.length()-i-1);

           }

           return ans;

       }

WHEN I GET IT THROUGH TESTING FILES...

public class TestBattleship {

   public static void main(String[] args) {
        // Milestone 1
        testCoordAlphaToNum();
        testCoordNumToAlpha();
        // Milestone 2
        testCheckWater();
        testPlaceShip();
        // Milestone 3
        testTakeShot();
        testCheckLost();
    }
  
    private static void testCoordAlphaToNum() {
        int numTests = 4;
        int passed = numTests;
        int res;
        if((res = Battleship.coordAlphaToNum("BAAA")) != 17576) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("BAAA") != 17576, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("ZERTY")) != 11506714) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("ZERTY") != 11506714, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("zerty")) != 11506714) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("zerty") != 11506714, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("&é"")) != -14747) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("&é\"") != -14747, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("baaa")) != 17576) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("baaa") != 17576, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("ABCDE")) != 731) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("ABCDE") != 17576, but " + res);
            passed--;
        }
      
        System.out.println("testCoordAlphatoNum: Passed " + passed + " of " + numTests + " tests.");
    }

ERROR COMES OUT AS..

FAILED: Battleship.coordAlphaToNum("&é"") != -14747, but -383999
FAILED: Battleship.coordAlphaToNum("ABCDE") != 17576, but 19010

Explanation / Answer

The answer is correct, you mistaken...

0×26^4 + 1×26^3 + 2×26^2 + 3 ×26^1 + 4 x 26^0 = 19010

Check condition...

If you write if((res = Battleship.coordAlphaToNum("ABCDE")) != 19010) { then it will true...

public class TestBattleship {

   public static void main(String[] args) {
        // Milestone 1
        testCoordAlphaToNum();
        testCoordNumToAlpha();
        // Milestone 2
        testCheckWater();
        testPlaceShip();
        // Milestone 3
        testTakeShot();
        testCheckLost();
    }
  
    private static void testCoordAlphaToNum() {
        int numTests = 4;
        int passed = numTests;
        int res;
        if((res = Battleship.coordAlphaToNum("BAAA")) != 17576) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("BAAA") != 17576, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("ZERTY")) != 11506714) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("ZERTY") != 11506714, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("zerty")) != 11506714) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("zerty") != 11506714, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("&é"")) != -383999) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("&é\"") != -383999, but " + res);
            passed--;
        }

        if((res = Battleship.coordAlphaToNum("baaa")) != 17576) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("baaa") != 17576, but " + res);
            passed--;
        }
        if((res = Battleship.coordAlphaToNum("ABCDE")) != 19010) {
            System.out.println("FAILED: Battleship.coordAlphaToNum("ABCDE") != 19010, but " + res);
            passed--;
        }

      
        System.out.println("testCoordAlphatoNum: Passed " + passed + " of " + numTests + " tests.");
    }

If you still have any doubts, please give me comment...