Computer Networking: A Top-Down Approach (7th Edition)
Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN: 9780133594140
Author: James Kurose, Keith Ross
Publisher: PEARSON
Bartleby Related Questions Icon

Related questions

Question
100%

Java Programming Language only: I am asking this questio because I want to compare it to the code that I have and to see if I am in the right check or does it make sense. Thank you!

Design and implement a GUI program to convert a positive number given in one base to another base. For this problem, assume that both bases are less than or equal to 10. Consider the sample data:

number = 2010, base = 3, and new base = 4. In this case, first  convert 2010 in base 3 into the equivalent number in base 10 as follows:

2 * 3^3 + 0 * 3^2 + 1 * 3 + 0 = 54 + 0 + 3 + 0 = 57

To convert 57 to base 4, you need to finde the remainders obtained by dividing by 4, as shown in the following;

 

57 % 4 = 1, quotient = 14

14 % 4 2, quotient = 3

3 % 4  =3, quotient = 0. Therefore, 57 in base 4 is 321.

 

Here is my code:

String inputnumber;

int base;
//int outputbase;
int newbase;


System.out.println("Enter the base number to convert from: ");

base = console.nextInt();
System.out.println("The base number you want to convert from is: " + base);
System.out.println("Enter the number: ");
inputnumber = console.next();
System.out.println("The number you entered is: " + inputnumber);
System.out.println("Enter the new base to convert to: ");
newbase = console.nextInt();
System.out.println("The new base you want to convert to is: " + newbase);
// System.out.println("The number " + inputnumber + " in base " + base
// + "is" + + " in" + newbase);
if (base < 0 || base >= 11) {
System.out.println("You entered an invalid base. The base should be between 1 and 10");
}



int x;
x = inputnumber.length();
int number = Integer.parseInt(inputnumber);
int sum = 0;

for (int i = 0; i < x; i++)

{ sum = sum + number%10 * (int)Math.pow(base, i);
number = (number - number%10)/10;}

System.out.println(sum);
String reverse = " "; String output = " ";
int i ;
int y ;

do {

y = sum % newbase;
reverse += String.valueOf(y);
sum = (sum / newbase) ;
// i++;
// System.out.print(y);
}
while (sum > 0 );
for (i = reverse.length() - 1; i > 0; i--)
{
output += reverse.charAt(i);

}System.out.println("The number " + inputnumber + " in base " + base
+ " is " + output + " in base " + newbase) ;
System.out.println();

 

 

Expert Solution
Check Mark
Knowledge Booster
Background pattern image
Similar questions
SEE MORE QUESTIONS
Recommended textbooks for you
Text book image
Computer Networking: A Top-Down Approach (7th Edi...
Computer Engineering
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:PEARSON
Text book image
Computer Organization and Design MIPS Edition, Fi...
Computer Engineering
ISBN:9780124077263
Author:David A. Patterson, John L. Hennessy
Publisher:Elsevier Science
Text book image
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:9781337569330
Author:Jill West, Tamara Dean, Jean Andrews
Publisher:Cengage Learning
Text book image
Concepts of Database Management
Computer Engineering
ISBN:9781337093422
Author:Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:Cengage Learning
Text book image
Prelude to Programming
Computer Engineering
ISBN:9780133750423
Author:VENIT, Stewart
Publisher:Pearson Education
Text book image
Sc Business Data Communications and Networking, T...
Computer Engineering
ISBN:9781119368830
Author:FITZGERALD
Publisher:WILEY