Changing Bases
(and I don't mean baseball!)

Submit a compressed file (either .zip or .jar) containing both .java files. Please name your compressed file this way: LastNameHW3.<extension> In otherwords, your last name, then the homework number. Thanks.

You've been hired by the Seattle School District to write a program to help children convert numbers to different bases. Your program will convert from decimal (base-10) to either binary (base-2), octal (base-8), or hexadecimal (base-16).
Go herefor an explanation of bases and how to convert from one to another.

Objectives

  • control structures
  • console-based user input using Scanner class
  • writing complete programs using main()
  • precondition testing

User Interface Specification

This is a console-based I/O program. Display should go to System.out (print or println) and the program will get user input using the Scanner class. The flow of execution should be as follows:

  • When the program starts, display a 1 to 2 line short description to the user
  • prompt the user to input the number and the base to convert to
  • display the original input as well as the new value with its base
  • the program repeats until the user enters some exit code (your choice)
  • valid bases are 2, 8, and 16. Valid numbers to convert are >= 0
  • if the user enters an invalid base (not 2, 8, or 16),an invalid number (< 0), or an invalid type (such as a letter or a double) print a helpful error message and re-prompt for input

Here is a sample execution of the program. User input is underlined bold:
Welcome to the Decimal Converter program. Enter a positive integer and either base 2, 8, or 16 and the program will display the equivalent number in the new base. Enter 0 0 to quit.
Enter an integer and the new base:252
25 (base-10) is equivalent to 11001 (base-2)
Enter an integer and the new base:15 16
15 (base-10)is equivalent to F (base-16)
Enter an integer and the new base:22 9
Sorry, that is an invalid base. Please enter 2, 8, or 16 only.
Enter an integer and the new base:22 8
22 (base-10) is equivalent to26 (base-8)
Enter an integer and the new base:0 0
Good bye!
Your text doesn't have to match mine exactly, but you get the idea.

Code Specifications

For this assignment, you will write TWO different classes. The goal is to separate the user interface code from the code that performs calculations. The sample programsMyMath.javaandTestMyMath.javaare an essential reference.
In a class namedBaseConversions, write one or more static methods thatuseparametersto get information andreturna valueto provide results. You may organize the code in this class any way you want. Note that the BaseConversions class does not need a 'main' method. However, you may write one for testing purposes if you wish. Here are a few important notes:

  • Reminder: "returning" a value is not the same thing as displaying something to the console.
  • No method in the BaseConversions class should use System.out.println or the Scanner class. In other words, no method in BaseConversions should interact with the user; that's the job of the other class.
  • You must use loop(s) to implement the base conversions according to the algorithm given in these instructions to get full credit. Implementing the conversion is part of the project.
  • Your methods must implement precondition tests on their parameters and throw exceptions when appropriate. You can read about this in section 4.4

In a class namedBaseConversionApp, create a console-based user interface that behaves like the one described above. This class must use a 'main' method to start things off. However, you are free to use other methods to organize your code.
No class variables may be used, but classconstantsare ok.
Your program should not throw any exceptions to the user. If a user enters an invalid input, the program should display a helpful error message and ask for new input. You must implement this using loops for input validation.

Testing & Documentation

  • As always, I recommend doing this in pieces. For example, get the conversions working first (test as you go). Then focus on the user interface.
  • Your documentation in both classes is imperative in your solution since you are the designer/author of these classes. Be sure to include a comment before each method that describes what it does, describes the parameters and the return.
  • Make sure to include internal comments to guide the reader through your algorithms.

Submission

You will submit two different .java files for this assignment:BaseConversions.javaandBaseConversionApp.java. You will need to compress them into one .zip file, or use BlueJ to make a .jar file (under the Project menu, choose Create Jar File, make sure to select Include source)

Grading

/15 execution and design
/5 documentation and style

Good Luck!

Bases,Converting from decimal,Converting to decimal (fyi)

The number system that you are used to is known as the decimal, or base-10, number system. We use 10 digits, 0 through 9, to represent numbers. After we go through the single digits 0, 1, 2, 3, ..., 9, we then start to group these digits together: 10, 11, 12, 13, ..., 19, 20, 21, 22, 23,...99, 100, 101, 102, etc Sometimes it helps to see these written vertically:

  • Notice there are 10 numbers in each column. That's because we have 10 digits in this numbering system.
  • Within 1 column, the far right digit cycles from 0 through 9.
  • After every 10 numbers, something changes (either a digit is incremented or a new digit is added).
/ 0
1
2
3
4
5
6
7
8
9 / 10
11
12
13
14
15
16
17
18
19 / 20
21
22
23
24
25
26
27
28
29 / ... / 100
101
102
103
104
etc

Base-10 is not the only numbering system though. Base-16, or hexadecimal, means there are 16 symbols used to represent numbers. Since 0 through 9 are only 10 symbols, we also include the symbols A, B, C, D, E, and F. Here are some base-16 values:

Here you find 16 numbers in each column. / 0
1
2
3
4
5
6
7
8
9
A
B
C
D
E
F / 10
11
12
13
14
15
16
17
18
19
1A
1B
1C
1D
1E
1F / 20
21
22
23
24
25
26
27
28
29
2A
2B
2C
2D
2E
22 / ... / 110
111
112
113
114
115
116
117
118
119
11A
11B
11C
11D
11E
11F etc

The base-2, or binary, number system has only 2 digits, 0 and 1. It is an important number system to know because computers are based on this. Since we only have 2 digits, as we count we quickly need to group digits together: 0, 1, 10, 11, 100, 101, 110, 110, etcOr, written vertically:

0
1 / 10
11 / 100
101 / 110
111 / 1000
1001 / 1010
1011 / 1100
1101 etc.

Converting from decimal

The basis of the algorithm to convert from decimal to another base is to repeatedly divide the integer by the base value and find both the quotient and the remainder from that division. We will look at an example by converting to binary. The remainder from the division operation give us our binary digits. We only want the integer part of the quotient; throw away the decimal. The process stops when we divide by 2 and get 0. Let's perform the steps converting from decimal number 25 to base 2.

operation / quotient (integer division only) / remainder
25divided by 2 / 12 / 1
12divided by 2 / 6 / 0
6divided by 2 / 3 / 0
3divided by 2 / 1 / 1
1divided by 2 / 0 (stop the process) / 1

The final answer is110012where the little 2 means "base-2". Notice that the first binary digit calculated (the one at the top) is in the ones place.
Programming hint:as you collect these binary digits, you don't want to just add them using +, because that will do arithmetic, adding the 1's and 0's. What you really want is to concatenate them. That means you need to convert them to strings.
The process is the same with any base: 2, 8, or 16. The issue with base 16 though is that the remainder may be a number that is more than one digit (i.e. any value from 10 through 15). Those must be replaced by their letter equivalents A through F.
You can check if you've done the conversion properly by converting back to decimal. If you're not familiar with that process, you can read about it below.

Converting to decimal

In order to understand converting to decimal, first let's review what a decimal number is in expanded notation. Given the decimal number 8597 we know that the 8 is in the thousands place, the 5 is in the hundreds place, the 9 is in the tens place, and the 7 is in the ones place. In expanded notation, we get:8* 103+5* 102+9* 101+7* 100
The original digits are inred. If we think of 8 in position 3, the 5 in position 2, the 9 in position 1, and the 7 in position 0, we see how these place numbers coincide with the exponent for the 10. We use 10 because this is a base-10 number.
Let's now write a base-2 number in expanded notation. Consider the number 10110012First, figure out the place number for each digit. Then write it out.
Expanded notation:1* 26+0* 25+1* 24+1* 23+0* 22+0* 21+1* 20
Do the math:64 + 0 + 16 + 8 + 0 + 0 + 1 = 89in base-10 or8910.
So that's how we convert a number from another base to base 10. Calculate the expanded notation of the original number.