The first laboratory exercise requires you to code two conversion schema’s in Java.

Question 1 – Base number conversion.

The first programming exercise requires you to code the general algorithm to convert any

numeric base system number to base 10 (decimal). This implies any base system from base

2 (binary) to base 9. The following algorithm gives the description of the procedure.

Algorithm 1 Base b conversion to decimal

Input: A number dn−1dn−2 . . . d1d0 in base b

Output: Equivalent decimal number

1: procedure The digits of the input number are processed from left to

right one digit at a time.

2: Result = 0

3: for (i = n −1 downto 0) do

4: Result = (Result x b) + di

5: end for

6: return Result

7: end procedure

Using Algorithm 1 as the template, code the conversion routine. You are not allowed to use

any inbuilt language specific conversion commands. Your code should be able to convert any

base from 2 till 9 to decimal.

The algorithm should do the following:

1. Ask the user to enter the required base.

2. Check if the base is between 2 and 9 inclusive.

3. If not, ask the user again to enter the base.

4. Using the given equation, compute the decimal equivalent number in a for loop starting

from the left to the right.

1

5. Display the decimal number in console.

The output should be formatted exactly in the shown invocation samples.

Please enter a base from 2 – 9: 2

Enter a base 2 number: 100101

The equivalent number in base 10 format is 37

Please enter a base from 2 – 9: 16

Incorrect base system. Please enter a base from 2 – 9:

Please enter a base from 2 – 9: 8

Enter a base 8 number: 247

The equivalent number in base 10 format is 167

The submission is a program named BaseToDecimal. All codes should have appropriate

documentation.

Question 2 – Decimal to binary, octal and hexadecimal number conversion.

The second programming exercise requires you to code the general algorithm to convert a

base 10 (decimal) number to a base 2 (binary), base 8 (octal) and base 16 (hexadecimal)

number. The following Algorithm 2 gives the description of the procedure.

Algorithm 2 Decimal to base b conversion

Input: A number dn−1dn−2 . . . d1d0 in decimal

Output: Equivalent number in the target base b number system

1: procedure Result digits are obtained from left to right. In the follow-

ing, MOD represents the modulo operator and DIV the integer divide

operator.

2: Quotient = decimal number to be converted

3: while (Quotient ∕= 0) do

4: Remainders (next most significant digit of result) = Quotient MOD b

5: Quotient = Quotient DIV b

6: end while

7: return Remainders

8: end procedure

An example for converting the decimal number 167 into its equivalent binary, octal and

hexadecimal numbers is shown in Tables 1 – 3.

2

Table 1: Decimal to binary conversion

Quotient Remainder

167/2 = 83 1

83/2 = 41 1

41/2 = 20 1

20/2 = 10 0

10/2 = 5 0

5/2 = 2 1

2/2 = 1 0

1/2 = 0 1

Table 2: Decimal to octal conversion

Quotient Remainder

167/8 = 20 7

20/8 = 2 4

2/8 = 0 2

Table 3: Decimal to hexadecimal conversion

Quotient Remainder

167/16 = 10 7

10/16 = 0 A

3

The desired base numbers can be obtained by writing the remainders generated in the reverse

order from left to right. For this example, the binary number is 10100111B , octal number is

247O and hexadecimal number is A7H .

Using Algorithm 2 as the template, code the conversion routine. You are not allowed to use

any inbuilt language specific conversion routines for bases.

The output should be formatted exactly as in the shown invocation sample below.

Please enter a base 10 number: 167

Base 2: 1 0 1 0 0 1 1 1

Base 8: 2 4 7

Base 16: A 7

The submission is a program named DecimalToBase. All codes should have appropriate

documentation