Objectives: Practice loop, text processing, and modular design with static metho
ID: 3785114 • Letter: O
Question
Objectives: Practice loop, text processing, and modular design with static methods. An alien species uses 14-based numbering system. Their ten digits, 0 through 9, are the same as our decimal system. They use A J, o and K to represent decimal 10, 11, 12 and 13, respectively. They hire you to write a Java program to do the summation of their two numbers. The program should prompt users for two 14-based numbers and then display the summation and the product of those two entered 14-based numbers. The outputs should also be 14- based. They are unwilling to learn our decimal systems!) For example, if inputs are 17 and 96, their sum should be AK User interface specifications: Input o The program prompts users for two 14-based numbers. A 14-based number is a string that contains a combination of digit characters and letter characters of A, J, Q and K, case insensitive. Your program should direct users to input the two numbers, separated by space(s), on the same line The data input line is immediately below the prompt message line. In other words, after the prompt message is displayed, your program should force users to enter the two numbers on a new line immediately below the prompt message. Your program has to check the validity of input numbers. o An example of input session is illustrated as below: Please enter two 14-based numbers on next line I kn 17 96 Output Two 14-based numbers that are sum and product of the two input 14-based numbers. pwvduct Code specifications: The header comment lines at the top of the file contain a brief description of the program. The description should be one or 2 lines long describing the purpose of the program.Explanation / Answer
import java.util.Scanner;
/**
* Class with a main method. Main method prompt user for
* space separated two base 14 number and output space
* separated sum and multiplication of these 2 number.
*
*/
public class Base14 {
/**
* Functionto validate if a given number as string is valid
* @param num number to be validated
* @return True if valid else false
*/
public static boolean validate(String num)
{
return num.matches("[0-9AJQK]+");
}
/**
* Convert a given char in Base14 to int
* @param c char to be converted
* @return corresponding int value
*/
private static int charToInt(char c)
{
if (c == 'A') return 10;
if (c == 'J') return 11;
if (c == 'Q') return 12;
if (c == 'K') return 13;
return Integer.parseInt(String.valueOf(c));
}
/**
* Convert given int to Base14 char as string
* @param i integer to be converted
* @return Base14 char as string
*/
private static String intToStr(int i)
{
if (i == 10) return "A";
if (i == 11) return "J";
if (i == 12) return "Q";
if (i == 13) return "K";
return String.valueOf(i);
}
/**
* Function to convert a integer to base14 string
* @param num integer to be converted
* @return base14 string
*/
private static String convertToBase14(int num)
{
StringBuffer num1 = new StringBuffer();
if (num == 0) return "0";
while(num > 0)
{
num1.append(num%14);
num = num / 14;
}
return num1.reverse().toString();
}
/**
* Return multiplication of two base14 string
* @param num1 first number in base14
* @param num2 second number in base 14
* @return multiplication of two numbers
*/
public static String multBase14(String num1, String num2)
{
num1 = new StringBuffer(num1).reverse().toString();
num2 = new StringBuffer(num2).reverse().toString();
String sum = "0";
// Loop over both number multiply digit by digit of first with whole second
// num. Do this repeatedly untill one is exhausted while adding it by shifting
// last bit by 0.
for(int i = 0; i < num1.length(); i++)
{
int val1 = charToInt(num1.charAt(i));
StringBuffer s = new StringBuffer();
int cr = 0;
for(int j = 0; j < num2.length(); j++)
{
int val2 = charToInt(num2.charAt(j));
int mul = val1*val2 + cr;
s.append(intToStr(mul%14));
cr = mul/14;
}
String tempResult = convertToBase14(cr) + s.reverse().toString();
StringBuffer temp = new StringBuffer(tempResult);
for(int j = 0; j < i; j++)
{
temp.append("0");
}
sum = sumBase14(sum, temp.toString());
}
return sum.replaceFirst("^0*(?!$)", "");
}
/**
* Return sum of two number in base14
* @param num1 first number in base14
* @param num2 second number in base14
* @return sum of two number in base14
*/
public static String sumBase14(String num1, String num2)
{
int len1 = num1.length();
int len2 = num2.length();
num1 = new StringBuffer(num1).reverse().toString();
num2 = new StringBuffer(num2).reverse().toString();
int n = len1;
int n2 = len2;
int v = 2;
if (len1 > len2)
{
n = len2;
n2 = len1;
v = 1;
}
StringBuilder result = new StringBuilder();
int carry = 0;
for(int i = 0; i < n; i++)
{
int val1 = charToInt(num1.charAt(i));
int val2 = charToInt(num2.charAt(i));
int sum = val1 + val2 + carry;
carry = sum / 14;
result.append(intToStr(sum%14));
}
String remaining = v == 1? num1 : num2;
for(int i = n; i <n2 ; i++)
{
int val1 = charToInt(remaining.charAt(i));
int sum = val1 + carry;
carry = sum / 14;
result.append(intToStr(sum%14));
}
if (carry > 0)
{
result.append(intToStr(carry));
}
return result.reverse().toString();
}
public static void main(String[] args)
{
System.out.println("Enter two numbers separated by space in base 14:");
Scanner sc = new Scanner(System.in);
String num1 = sc.next();
String num2 = sc.next();
sc.close();
num1 = num1.toUpperCase();
num2 = num2.toUpperCase();
boolean isValidnum1 = validate(num1);
boolean isValidnum2 = validate(num2);
if (!isValidnum1 || !isValidnum2)
{
System.out.println("Please enter valid values.");
}
else
{
String sum = sumBase14(num1, num2);
String mult = multBase14(num1, num2);
System.out.println(sum + " " + mult);
}
}
}
/*
Sample output
Enter two numbers separated by space in base 14:
17 96
AK 1020
*/
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.