Question

Base Conversion One algorithm for converting a base 10 number to another base b involves repeatedly...

Base Conversion

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. Save the file below to your directory, then modify it one step at a time as follows:

// ***********************************************************************

// 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

int quotient; // quotient when dividing by the base

Scanner scan = new Scanner(System.in);

String baseBNum = new String (""); // the number in the new base

// read in the base 10 number and the base

// 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

// Do the conversion (see notes in lab)

// Print the result (see notes in lab)

}

}

  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 11112 which equals 15 in base 10 (or 24 - 1). In base 8, the maximum number is 77778 which equals 4095 in base 10 (or 84 - 1). In general, the maximum base 10 number that fits in 4 base b digits is b4 - 1. Add an assignment statement to the program to compute this value for the base that is input and assign it to the variable maxNumber. Add a statement that prints out the result (appropriately labeled). Compile and run the program to make sure it is correct so far.

  2. Now add the code to do the conversion. The comments below guide you through the calculations -- replace them with the appropriate Java statements.

    // First compute place0 -- the units place. Remember this comes

    // from the first division so it is the remainder when the

    // base 10 number is divided by the base (HINT %).

    // Then compute the quotient (integer division / will do it!) -

    // You can either store the result back in base10Num or declare a

    // new variable for the quotient


    // Now compute place1 -- this is the remainder when the quotient

    // from the preceding step is divided by the base.  

    // Then compute the new quotient


    // Repeat the idea from above to compute place2 and the next quotient


    // Repeat again to compute place3


So far the program does not print out the answer. Recall that the answer is the sequence of remainders written in reverse order -- note that this requires concatenating the four digits that have been computed. Since they are each integers, if we just add them the computer will perform arithmetic instead of concatenation. So, we will use a variable of type String. Note near the top of the program a variable named baseBNum has been declared as an object of type String and initialized to an empty string. Add statements to the program to concatenate the digits in the new base to baseBNum and then print the answer. Compile and run your program. Test it using the following values: Enter 2 for the base and 13 for the base 10 number -- the program should print 1101 as the base 2 value; enter 8 for the base and 1878 for the number -- the program should print 3526 for the base 8 value; enter 3 for the base and 50 for the number -- the program should print 1212.

Homework Answers

Answer #1

Explanation:I have written both the classes BaseConvert.java.I have shown the the output of the program, please find the images attached with the answer.Please upvote if you liked my answer and comment if you need any modification or explanation.

//BaseConvert.java

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

       int quotient; // quotient when dividing by the base

       Scanner scan = new Scanner(System.in);

       String baseBNum = new String(""); // the number in the new base
       System.out.print("Enter the base 10 number:");
       base10Num = scan.nextInt();
       System.out.print("Enter the new base:");
       base = scan.nextInt();
       maxNumber = base * base * base * base - 1;
       System.out.println(
               "The maximum base 10 number that will fit in 4 digits in the new base " + base + " is :" + maxNumber);
       place0 = base10Num % base;
       baseBNum += place0;
       quotient = base10Num / base;
       place1 = quotient % base;
       baseBNum = place1 + baseBNum;
       quotient = quotient / base;
       place2 = quotient % base;
       baseBNum = place2 + baseBNum;
       quotient = quotient / base;
       place3 = quotient % base;
       baseBNum = place3 + baseBNum;
       System.out.println("The base " + base + " value is :" + baseBNum);
       scan.close();

   }

}

Outputs:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers....
Write a Java program named BinaryConversion that will convert base 2 numbers to base 10 numbers. The data for this program will be entered from the keyboard using JOptionPane one 16-bit binary number at a time. Note that each base 2 number is actually read in as a String. The program should continue until a 16-bit base 2 number consisting of all 0’s is entered. Once the 16-bit number has been entered your program should make sure that the input...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These...
in Java In this exercise, you'll write a Java version of the infix-to-postfix conversion algorithm. These same mechanisms can be used as a part of writing a simple compiler. Write class InfixToPostfixConverter co convert an ordinary infix arithmetic expression (assume a valid expression is entered) with single-digit integers (to make things easier) such as (6 + 2) • 5 - 8 / 4 to a postfix expression. The postfix version (no parentheses are needed) of this infix expression is 6...
/* This program should check if the given integer number is prime. Reminder, an integer number...
/* This program should check if the given integer number is prime. Reminder, an integer number greater than 1 is prime if it divisible only by itself and by 1. In other words a prime number divided by any other natural number (besides 1 and itself) will have a non-zero remainder. Your task: Write a method called checkPrime(n) that will take an integer greater than 1 as an input, and return true if that integer is prime; otherwise, it should...
What would the pseudocode be for this? The code for this has already been answered. I...
What would the pseudocode be for this? The code for this has already been answered. I need the pseudocode. Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning...
Java : Modify the selection sort algorithm to sort an array of integers in descending order....
Java : Modify the selection sort algorithm to sort an array of integers in descending order. describe how the skills you have gained could be applied in the field. Please don't use an already answered solution from chegg. I've unfortunately had that happen at many occasion ....... ........ sec01/SelectionSortDemo.java import java.util.Arrays; /** This program demonstrates the selection sort algorithm by sorting an array that is filled with random numbers. */ public class SelectionSortDemo { public static void main(String[] args) {...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem...
Problem: Certain mathematical problems involve manipulation of the digits within a whole number. One such problem requires that the digits be re-arranged.In this project, we will reverse the order of the digits in a number. Assignment: Design, develop, and test an Object-Oriented C++program that reads a whole number from the user, removes the sign and all leading and trailing zeroes from that number, and then performs the following operations on that number: 1) reverses the digits, 2) sorts the digits...
Task #4 Calculating the Mean Now we need to add lines to allow us to read...
Task #4 Calculating the Mean Now we need to add lines to allow us to read from the input file and calculate the mean. Create a FileReader object passing it the filename. Create a BufferedReader object passing it the FileReader object. Write a priming read to read the first line of the file. Write a loop that continues until you are at the end of the file. The body of the loop will: convert the line into a double value...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately...
Objectives:The focus of this assignment is to create and use a recursive method given a moderately difficult problem. Program Description: This project will alter the EmployeeManager to add a search feature, allowing the user to find an Employee by a substring of their name. This will be done by implementing the Rabin-Karp algorithm. A total of seven classes are required. Employee (From previous assignment) HourlyEmployee (From previous assignment) SalaryEmployee (From previous assignment) CommissionEmployee (From previous assignment) EmployeeManager (Altered from previous...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that...
a. Define the class bankAccount to store a bank customer’s account number and balance. Suppose that account number is of type int, and balance is of type double. Your class should, at least, provide the following operations: set the account number, retrieve the account number, retrieve the balance, deposit and withdraw money, and print account information. Add appropriate constructors. b. Every bank offers a checking account. Derive the class checkingAccount from the class bankAccount (designed in part (a)). This class...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts,...
For this assignment, you will be creating a simple “Magic Number” program. When your program starts, it will present a welcome screen. You will ask the user for their first name and what class they are using the program for (remember that this is a string that has spaces in it), then you will print the following message: NAME, welcome to your Magic Number program. I hope it helps you with your CSCI 1410 class! Note that "NAME" and "CSCI...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT