Question
/** * This program Performs various number base conversions. It also verifies if * a number is valid in its base. * Author: M. Rahman
/**
* This program Performs various number base conversions. It also verifies if
* a number is valid in its base.
* Author: M. Rahman
* Date: 06 September 2018
*/
public class NumberConversion {
public static String dec2any(String dec, int base) {
/**
* Converts a decimal value to a target base
* inputs:
* dec: the decimal value to be converted
* base: the target base
* output: 256-base as dotted decimal, hex as usual, bases
* over 10 except 256 and 16 are parenthesis separated.
*/
long rem = 0;
String any = "";
String remhex = "";
long num = Long.parseLong(dec);
if (base
any = "ERROR!";
else if (base == 10)
any = dec;
else {
while (num > 0) {
// get remainder first then get the quotient
rem = num % (long) base;
num = num / (long) base;
if ((base > 1) && (base
any = rem + any;
else if (base == 16) {
switch ((int)rem) {
case 10: remhex = "A";
break;
case 11: remhex = "B";
break;
case 12: remhex = "C";
break;
case 13: remhex = "D";
break;
case 14: remhex = "E";
break;
case 15: remhex = "F";
break;
default : remhex = Long.toString(rem);
break;
}
any = remhex + any;
}
else if (base == 256) {
if (any.equals(""))
any = Long.toString(rem);
else
any = Long.toString(rem) + "." + any;
}
else {
any = "(" + Long.toString(rem) + ")" + any;
}
}
}
return any;
} // end of method dec2any
/**
*/
public static String any2dec(String any, int base) {
/**
* Converts a number from any base to decimal
* inputs
* any: the given number in any base; must be formatted
* appropriately, 256-base as dotted decimal, hex as usual,
* bases over 10 except 256 and 16 are parenthesis separated.
* base: base of the given number
* output: a decimal number as string, "ERROR!" for invalid input
*/
String dec = "";
long num = 0;
// if given number is not valid in base then flag error
if (!isValidInBase(any, base)) {
dec = "ERROR!";
}
else {
// parse the input
if (base == 10) {
dec = any;
}
else if (base
int strlen = any.length();
for (int i = 0 ; i
char ch = any.charAt(strlen - i - 1);
long digit = Long.parseLong(String.valueOf(ch));
num = digit * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else if (base == 16) {
int strlen = any.length();
for (int i = 0 ; i
char ch = (any.toUpperCase()).charAt(strlen-i-1);
int digit = ch - 'A' + 10;
num = (long) digit * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else if (base == 256) {
String[] decimals = any.split("\\.");
int len = decimals.length;
for (int i = 0; i
num = Long.parseLong(decimals [len-i-1]) * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else {
String temp = any.replaceAll(" ", "");
if ((temp.charAt(0) == '(') &&
(temp.charAt(any.length() - 1) == ')')) {
temp = temp.substring(1,any.length()-1);
String[] decimals = temp.split("\\)\\(");
int len = decimals.length;
for (int i = 0; i
num = Long.parseLong(decimals [len-i-1]) * (long)Math.pow(base,i) + num;
}
dec = Long.toString(num);
}
else {
dec = "ERROR!";
}
}
}
return dec;
} // end of method any2dec
/**
*/
public static String any2any(String any, int frombase, int tobase) {
/**
* Converts a number from any base to any base
* inputs
* any: the given number to be converted
* frombase: the base of the given number
* tobase: target base
* output: a number in tobase as string, "ERROR!" for invalid input
* Example of a 43-base number: (34)(12)(42)(42)
*/
String temp = "ERROR!";
String retval = any2dec(any, frombase);
if (!retval.equals("ERROR!")) {
temp = retval;
retval = dec2any(temp, tobase);
}
else {
retval = temp;
}
return retval;
} // end of method any2any
/**
*/
public static boolean isValidInBase (String any, int base) {
/**
* Verifies whether the given number is a valid in the base
* inputs
* any: the given number to be converted
* base: the base of the given number
* output: true if valid number in the base, false otherwise
*/
String temp = "";
String regex = "";
boolean retval = false;
if (base
retval = false;
else if (base ==1) {
if (Integer.parseInt(any) != 0)
retval = false;
else
retval = true;
}
else if (base
regex = "[0-" + (base-1) + "]*";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else if (base == 10) {
regex = "[0-9]*";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else if (base == 16) {
regex = "[\\da-fA-F]+";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else if (base == 256) {
regex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4]" +
"[0-9]|25[0-5])\\.){0,}([0-9]|[1-9][0-9]|1" +
"[0-9]{2}|2[0-4][0-9]|25[0-5])$";
if (!any.matches(regex))
retval = false;
else
retval = true;
}
else {
// any other base
regex = "^([\\(]([0]|[1-9]|([1-9][0-9]+))[\\)])*$";
if (!any.matches(regex))
retval = false;
else {
temp = any.substring(1,any.length()-1);
String[] decimals = temp.split("\\)\\(");
retval = true;
int len = decimals.length;
for (int i = 0; i
if (Long.parseLong(decimals [len-i-1]) >= (long)base)
retval = false;
}
}
}
return retval;
} // end of method isValidInBase
}
4. The code listing for the Number Conversion Program is already given (NumberConversion.java - attached. This program converts numbers between bases (e.g., binary to decimal, decimal to hexadecimal, etc.), and verifies if a number is valid for its base. It has four methods (program units) named: dec2any, any2dec, any2any, and isValidInBase. Write a test class named TestNumberConversion.java to do the unit test of the ur program units. In this exercise you wil a) Develop and give (in Word document) the dynamic uni test cases for each of the units to run test in the JUnit framework (using table similar to Qla) Write all the appropriate methods in the test class TestNumberConversion.java b) c) Provide a windows command batch file (named Run_Test _Conversion.bat witlh your name prefix) that will contain commands for compiling and running of the files NumberConversion.java and TestNumberConversion.java from a command windowsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started