Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

One algorithm for converting a base 10 number to another base b involves repeate

ID: 3542422 • Letter: O

Question

One algorithm for converting a base 10 number to another base b involves repeatedly dividing by b. Each time a division is performed the remainder and quotient are saved. At each step, the dividend is the quotient from the preceding step; the divisor is always b. The algorithm stops when the quotient is 0. The number in the new base is the sequence of remainders in reverse order (the last one computed goes first; the first one goes last).

In this exercise you will use this algorithm to write a program that converts a base 10 number to a 4-digit number in another base (you don't know enough programming yet to be able to convert any size number). The base 10 number and the new base (between 2 and 9) will be input to the program. The start of the program is in the file BaseConvert.java. Save this file to your directory, then modify it one step at a time as follows:

1.        The program will only work correctly for base 10 numbers that fit in 4 digits in the new base. We know that in base 2 the maximum unsigned integer that will fit in 4 bits is 1111 which equals 15 in base 10 (or 24

Explanation / Answer

// ***********************************************
// BaseConvert.java
//
// Converts base 10 numbers to another base
// (at most 4 digits in the other base). The
// base 10 number and the base are input.
// ***********************************************
import java.util.Scanner;
public class BaseConvert
{
public static void main (String[] args)
{
int base; // the new base
int base10Num; // the number in base 10
int maxNumber; // the maximum number that will fit
// in 4 digits in the new base
int place0; // digit in the 1's (base^0) place
int place1; // digit in the base^1 place
int place2; // digit in the base^2 place
int place3; // digit in the base^3 place
String baseBNum = new String(""); // the number in the new base
Scanner scan = new Scanner(System.in);
// read in the base 10 number and the base
System.out.println();
System.out.println ("Base Conversion Program");
System.out.println() ;
System.out.print ("Please enter a base (2-9): ");
base = scan.nextInt();
// Compute the maximum base 10 number that will fit in 4 digits
// in the new base and tell the user what range the number they
// want to convert must be in
System.out.print ("Please enter a base 10 number to convert: ");
base10Num = scan.nextInt();
// Do the conversion (see notes in lab)
place0 = base10Num % base;
base10Num = base10Num/base;
place1 = base10Num % base;
base10Num = base10Num/base;
place2 = base10Num % base;
base10Num = base10Num/base;
place3 = base10Num % base;
baseBNum = baseBNum + place3 + place2 + place1 + place0;
// Print the result (see notes in lab)
System.out.println(baseBNum);
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote