I need assistance with this method. Don\'t change the method type, decoding UTF8
ID: 3728617 • Letter: I
Question
I need assistance with this method. Don't change the method type, decoding UTF8
import java.util.Scanner;
/**
* This method converts an int value into a base (or radix) 26 number, where the digits are
* represented by the 26 letters of the Latin alphabet. That is, A represents 0, B represents 1,
* C represents 2, ..., Y represents 24, and Z represents 25.
* A couple of examples: 17576 is BAAA, 11506714 is ZERTY.
*
* The algorithm to convert an int to a String representing these base 26 numbers is as follows:
* - Initialize res to the input integer
* - The next digit is determined by calculating the remainder of res with respect to 26
* - Convert this next digit to a letter based on 'A'
* - Set res to the integer division of res and 26
* - Repeat until res is 0
*
* @param coord The integer value to covert into an alpha coordinate.
* @return The alpha coordinate in base 26 as described above. If coord is negative, an empty
* string is returned.
*/
public static String coordNumToAlpha(int coord) {
//FIXME
return "";
}
public static void main(String[] args) {
//FIXME
}
}
Explanation / Answer
import java.util.Scanner;
import java.util.Random;
public class Battleship {
/**
* 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) {
//FIXME
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;
}
/**
* This method converts an int value into a base (or radix) 26 number, where the digits are
* represented by the 26 letters of the Latin alphabet. That is, A represents 0, B represents 1,
* C represents 2, ..., Y represents 24, and Z represents 25.
* A couple of examples: 17576 is BAAA, 11506714 is ZERTY.
*
* The algorithm to convert an int to a String representing these base 26 numbers is as follows:
* - Initialize res to the input integer
* - The next digit is determined by calculating the remainder of res with respect to 26
* - Convert this next digit to a letter based on 'A'
* - Set res to the integer division of res and 26
* - Repeat until res is 0
*
* @param coord The integer value to covert into an alpha coordinate.
* @return The alpha coordinate in base 26 as described above. If coord is negative, an empty
* string is returned.
*/
public static String coordNumToAlpha(int coord) {
//FIXME
String result = "";
while(coord > 0) {
int r = coord%26;
char c = (char)(r + 'A');
result = c + result;
coord = coord/26;
}
return result;
}
public static void main(String[] args) {
System.out.println(coordAlphaToNum("BAAA"));
System.out.println(coordAlphaToNum("ZERTY"));
System.out.println(coordNumToAlpha(17576));
System.out.println(coordNumToAlpha(11506714));
}
}
/*
Sample run:
17576
11506714
BAAA
ZERTY
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.