Background Positional number systems are not exactly modern: Babylonians used a
ID: 3854227 • Letter: B
Question
Background
Positional number systems are not exactly modern: Babylonians used a sexagesimal (60 symbols) representation for numbers, we can see their contribution in how we still measure time and angles; Mayans used a vigesimal system (20 symbols) in their astronomical computations; and the current decimal system was introduced first in India and then migrate to the Arabic world which taught it to Europeans. In this lab you will be ask to translate between different number systems.
Assignment
Write a java program that given two positive integers, a source base s, and a target base t, and string representing a non-negative integer in the source base (separated by commas) converts the number to a string in the target base. Use the decimal numbers from 0 to t1 and those from 0 to s1 to represent the symbols in base t and s respectively, and assume that symbols are read from left to right from greatest to lowest significance.
Example
Call nt and ns strings representing a number in base t and s respectively. Suppose that t = 10, s = 2, and ns = “1,0,1,1,1” then nt = “2,3” since 16 + 4 + 2 + 1 = 23.
Explanation / Answer
Updated Code:
import java.util.Scanner;
public class BaseConverter {
public static void main(String[] args) {
int sourceBase, targetBase;
String sourceNum , targetNum = "";
Scanner keybd = new Scanner(System.in);
long base10Num = 0;
System.out.print("Enter source base: ");
sourceBase = keybd.nextInt();
System.out.print("Enter target base: ");
targetBase = keybd.nextInt();
System.out.print("Enter a number in source base(comma separated digits): ");
sourceNum = keybd.next();
//we know how to convert from any base to base 10
//also we know how to convert from base 10 to any base.
//so we will first convert from source base to base 10 . Then convert the base 10 number to target base !
//converting to base10
String digits[] = sourceNum.split(",");
int n;
//multiply each part by its corresponding power of base
for(int idx = digits.length -1 , power = 0; idx >= 0 ; idx--, power ++ )
{
n = Integer.parseInt(digits[idx]);
base10Num += n * Math.pow(sourceBase, power);
}
//now convert from base10 to targetBase
//keep dividing by target base and collect remainder,
int rem;
while(base10Num > 0)
{
rem = (int)base10Num % targetBase;
targetNum = "," + rem + targetNum;
base10Num = base10Num / targetBase;
}
if(targetNum.equals(""))
targetNum = "0";
else
{
//remove the , in the beginning
targetNum = targetNum.substring(1);
}
System.out.println(sourceNum + " in base " + sourceBase + " = " + targetNum + " in base " + targetBase);
}
}
==========================================
Here is the code for the question. Post a comment in case of any issues, I shall respond. If happy with the answer, please rate it. Thank you
import java.util.Scanner;
public class BaseConverter {
//input is comma separated num, and source base
public static long convertToBase10(String sourceNum, int sourceBase)
{
String digits[] = sourceNum.split(",");
int n;
long base10Num = 0;
//multiply each part by its corresponding power of base
for(int idx = digits.length -1 , power = 0; idx >= 0 ; idx--, power ++ )
{
n = Integer.parseInt(digits[idx]);
base10Num += n * Math.pow(sourceBase, power);
}
return base10Num;
}
//convert from base 10 to target base. Returned value has comma separated numbers in target base
public static String convertFromBase10(long base10Num, int targetBase)
{
long n = base10Num;
String targetNum = "";
int rem;
//keep dividing by target base and collect remainder,
while(n > 0)
{
rem = (int)n % targetBase;
targetNum = "," + rem + targetNum;
n = n / targetBase;
}
if(targetNum.equals(""))
targetNum = "0";
else
{
//remove the , in the beginning
targetNum = targetNum.substring(1);
}
return targetNum;
}
public static void main(String[] args) {
int sourceBase, targetBase;
String sourceNum , targetNum;
Scanner keybd = new Scanner(System.in);
long base10Num;
System.out.print("Enter source base: ");
sourceBase = keybd.nextInt();
System.out.print("Enter target base: ");
targetBase = keybd.nextInt();
System.out.print("Enter a number in source base(comma separated digits): ");
sourceNum = keybd.next();
//we know how to convert from any base to base 10
//also we know how to convert from base 10 to any base.
//so we will first convert from source base to base 10 . Then convert the base 10 number to target base !
base10Num = convertToBase10(sourceNum, sourceBase);
targetNum = convertFromBase10(base10Num, targetBase);
System.out.println(sourceNum + " in base " + sourceBase + " = " + targetNum + " in base " + targetBase);
}
}
output
Enter source base: 2
Enter target base: 10
Enter a number in source base(comma separated digits): 1,0,1,1,1
1,0,1,1,1 in base 2 = 2,3 in base 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.