Programming 4. Numeral Systems in Computing
In computer programming, there are several numeral systems that are essential. These include binary, octal, hexadecimal, and decimal numeral systems. This programming project serves two purposes, 1) we learn to convert a numeral in a numeral system to another by hand, and 2) write a program to do the conversion automatically.
This project consists of the following parts:
- Codelab exercises for binary, hexadecimal, octal numeral systems. These exercises do not require you to write complete programs and the purpose is to understand these numeral systems and how to convert form one to another.
- Subprojects where you write complete programs to convert between two numeral systems. These are divided into several programming exercises for which you write complete programs.
Exercise 4.1 Converting Octal Number to Decimal
Write a program called OctalToDecimal
that converts an octal numeral to its corresponding decimal numeral.
The program should work as follows,
- prompt the user to enter a valid octal numeral on the standard input
- read the octal numeral
- convert the octal numeral to its corresponding decimal numeral
- print out the conversion result on the standard output
Below are several runs of the program that exhibits additional requirements, such as, how you should format the output of the program:
$ java OctalToDecimal
Enter a valid octal number: 123
DEBUG: the trimmed input is <123>
DEBUG: the character at index 2 is 3
DEBUG: the value of the power term at index 2 is 3
DEBUG: the accumulated decimal number is now 3
DEBUG: the character at index 1 is 2
DEBUG: the value of the power term at index 1 is 16
DEBUG: the accumulated decimal number is now 19
DEBUG: the character at index 0 is 1
DEBUG: the value of the power term at index 0 is 64
DEBUG: the accumulated decimal number is now 83
Octal number 123 is decimal number 83
$ java OctalToDecimal
Enter a valid octal number: 4567
DEBUG: the trimmed input is <4567>
DEBUG: the character at index 3 is 7
DEBUG: the value of the power term at index 3 is 7
DEBUG: the accumulated decimal number is now 7
DEBUG: the character at index 2 is 6
DEBUG: the value of the power term at index 2 is 48
DEBUG: the accumulated decimal number is now 55
DEBUG: the character at index 1 is 5
DEBUG: the value of the power term at index 1 is 320
DEBUG: the accumulated decimal number is now 375
DEBUG: the character at index 0 is 4
DEBUG: the value of the power term at index 0 is 2048
DEBUG: the accumulated decimal number is now 2423
Octal number 4567 is decimal number 2423
Exercise 4.2 Converting Decimal Number to Octal
Write a program called DecimalToOctal
that converts a decimal numeral to its corresponding octal numeral.
The program should work as follows,
- prompt the user to enter a valid decimal numeral on the standard input
- read the decimal numeral
- convert the decimal numeral to its corresponding octal numeral
- print out the conversion result on the standard output
Below are several runs of the program that exhibits additional requirements, such as, how you should format the output of the program:
$ java DecimalToOctal
Enter a valid decimal number: 123
DEBUG: the user input is <123>
DEBUG: the remainder of <123/8> is <3>
DEBUG: the quotient of <123/8> is <15>
DEBUG: the octal number is now <3>
DEBUG: the remainder of <15/8> is <7>
DEBUG: the quotient of <15/8> is <1>
DEBUG: the octal number is now <73>
DEBUG: the remainder of <1/8> is <1>
DEBUG: the quotient of <1/8> is <0>
DEBUG: the octal number is now <173>
Decimal number 123 is octal number 173
$ java DecimalToOctal
Enter a valid decimal number: 456
DEBUG: the user input is <456>
DEBUG: the remainder of <456/8> is <0>
DEBUG: the quotient of <456/8> is <57>
DEBUG: the octal number is now <0>
DEBUG: the remainder of <57/8> is <1>
DEBUG: the quotient of <57/8> is <7>
DEBUG: the octal number is now <10>
DEBUG: the remainder of <7/8> is <7>
DEBUG: the quotient of <7/8> is <0>
DEBUG: the octal number is now <710>
Decimal number 456 is octal number 710
$ java DecimalToOctal
Enter a valid decimal number: 7890
DEBUG: the user input is <7890>
DEBUG: the remainder of <7890/8> is <2>
DEBUG: the quotient of <7890/8> is <986>
DEBUG: the octal number is now <2>
DEBUG: the remainder of <986/8> is <2>
DEBUG: the quotient of <986/8> is <123>
DEBUG: the octal number is now <22>
DEBUG: the remainder of <123/8> is <3>
DEBUG: the quotient of <123/8> is <15>
DEBUG: the octal number is now <322>
DEBUG: the remainder of <15/8> is <7>
DEBUG: the quotient of <15/8> is <1>
DEBUG: the octal number is now <7322>
DEBUG: the remainder of <1/8> is <1>
DEBUG: the quotient of <1/8> is <0>
DEBUG: the octal number is now <17322>
Decimal number 7890 is octal number 17322
Exercise 4.3 Simple but Robust Text Menu
A program often has several functionalities, and a user may want to use the program in the way their needs dictates. For console programs (i.e., programs with text user interface) like those we have been writing, we can design a text menu from which the user can select an option to tell the program what to do.
In this exercise, we learn to design a text menu. For this you are to write a program called TextMenu
.
The program is expected to do the following:
-
It prints out a text menu, i.e., an output like below
1. Convert octal number to decimal 2. Decimal decimal number to octal 0. Exit Enter option (0, 1, or 2) and hit enter:
- It reads the user’s input
- It checks if the user’s input is a valid option. In this case, whether it is 0, 1, or 2. Anything other than those are invalid. If the user’s input is invalid, the program goes back to step 1; otherwise, it proceeds.
- The program then behaves according to the user’s input. If the option is 1 or 2, the program prints out a message matching the option and goes back to 1; or exit if the option is 0.
If suffices to say that you will need a loop since we are repeating steps 1 - 4 and when the option is 0, we break out of the loop or directly exit from the program.
Below are several test runs of the program:
$ java TextMenu
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user entered <0>
User selected option 0 to exit the program
$ java TextMenu
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
DEBUG: user entered <1>
DEBUG: the user input is <1>, a valid option
User selected option 1 to convert octal number to decimal
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user entered <0>
User selected option 0 to exit the program
$ java TextMenu
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
DEBUG: user entered <2>
DEBUG: the user input is <2>, a valid option
User selected option 2 to convert decimal number to octal
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user entered <0>
User selected option 0 to exit the program
$ java TextMenu
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: abc 999
DEBUG: user entered <abc 999>
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user entered <0>
User selected option 0 to exit the program
$ java TextMenu
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 3
DEBUG: user entered <3>
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user entered <0>
User selected option 0 to exit the program
The program should be robust in sense that it should not crash regardless what input the user gives.
Subproject 4.1 Conversion between Decimal and Octal Numbers
Write a program called NumberConverter
that can convert either an octal numeral to a decimal or a decimal to an octal. It should repeat steps 1 and 2 below in a loop:
-
It displays a text menu as follows:
1. Convert octal number to decimal 2. Decimal decimal number to octal 0. Exit Enter option (0, 1, or 2) and hit enter:
-
When the user selects option 1 from the menu, converts the user entered octal numeral to its corresponding decimal numeral; (3) when the user selects option 2 from the menu, converts the user entered decimal numeral to its corresponding decimal numeral, and (4) when the user selects option 0, terminates the loop and exits.
The program expects that the user always gives valid octal or decimal numerals.
Below is a test run of the program:
$ java NumberConverter
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter:
DEBUG: user's input for the menu option is <>
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1234
DEBUG: user's input for the menu option is <1234>
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
DEBUG: user's input for the menu option is <1>
DEBUG: user's option is <1>
Enter an valid octal number (no more than 4 digits): 1234
DEBUG: user entered octal number <1234>
DEBUG: the character at index 3 is 4
DEBUG: the value of the power term at index 3 is 4
DEBUG: the accumulated decimal number is now 4
DEBUG: the character at index 2 is 3
DEBUG: the value of the power term at index 2 is 24
DEBUG: the accumulated decimal number is now 28
DEBUG: the character at index 1 is 2
DEBUG: the value of the power term at index 1 is 128
DEBUG: the accumulated decimal number is now 156
DEBUG: the character at index 0 is 1
DEBUG: the value of the power term at index 0 is 512
DEBUG: the accumulated decimal number is now 668
Octal number 1234 is decimal number 668.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
DEBUG: user's input for the menu option is <2>
DEBUG: user's option is <2>
Enter an valid decimal number (no more than 4 digits): 4321
DEBUG: user entered decimal number <4321>
DEBUG: the remainder of <4321/8> is <1>
DEBUG: the quotient of <4321/8> is <540>
DEBUG: the octal number is now <1>
DEBUG: the remainder of <540/8> is <4>
DEBUG: the quotient of <540/8> is <67>
DEBUG: the octal number is now <41>
DEBUG: the remainder of <67/8> is <3>
DEBUG: the quotient of <67/8> is <8>
DEBUG: the octal number is now <341>
DEBUG: the remainder of <8/8> is <0>
DEBUG: the quotient of <8/8> is <1>
DEBUG: the octal number is now <0341>
DEBUG: the remainder of <1/8> is <1>
DEBUG: the quotient of <1/8> is <0>
DEBUG: the octal number is now <10341>
Decimal number 4321 is octal number 10341.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user's input for the menu option is <0>
Exit the program
Exercise 4.4 Checking Valid Octal Number Input
Write a program called CheckingOctalNumber
that checks whether the user entered string in the line represents a valid octal numeral. For this program, the user’s input is a valid octal numeral if the input satisfies the
following conditions:
- the length of the input is no less than 1 digit and no more than 4 digits;
- it can have leading and trailing white spaces;
- between the leading and trailing white spaces (if any) are 1 to 4 characters from ‘0’ to ‘7’.
Below are several test runs of the program:
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits):
DEBUG: user's input is <>
DEBUG: trimmed input is <>
DEBUG: the length of the trimmed input is <0>
Is user's input <> valid as required? <false>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 1
DEBUG: user's input is <1>
DEBUG: trimmed input is <1>
DEBUG: the length of the trimmed input is <1>
DEBUG: character at index <0> is <1>
DEBUG: is character at index <0> valid? <true>
Is user's input <1> valid as required? <true>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 8
DEBUG: user's input is <8>
DEBUG: trimmed input is <8>
DEBUG: the length of the trimmed input is <1>
DEBUG: character at index <0> is <8>
DEBUG: is character at index <0> valid? <false>
Is user's input <8> valid as required? <false>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 12 3
DEBUG: user's input is <12 3>
DEBUG: trimmed input is <12 3>
DEBUG: the length of the trimmed input is <4>
DEBUG: character at index <0> is <1>
DEBUG: is character at index <0> valid? <true>
DEBUG: character at index <1> is <2>
DEBUG: is character at index <1> valid? <true>
DEBUG: character at index <2> is < >
DEBUG: is character at index <2> valid? <false>
Is user's input <12 3> valid as required? <false>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 123a
DEBUG: user's input is <123a>
DEBUG: trimmed input is <123a>
DEBUG: the length of the trimmed input is <4>
DEBUG: character at index <0> is <1>
DEBUG: is character at index <0> valid? <true>
DEBUG: character at index <1> is <2>
DEBUG: is character at index <1> valid? <true>
DEBUG: character at index <2> is <3>
DEBUG: is character at index <2> valid? <true>
DEBUG: character at index <3> is <a>
DEBUG: is character at index <3> valid? <false>
Is user's input <123a> valid as required? <false>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 123
DEBUG: user's input is <123>
DEBUG: trimmed input is <123>
DEBUG: the length of the trimmed input is <3>
DEBUG: character at index <0> is <1>
DEBUG: is character at index <0> valid? <true>
DEBUG: character at index <1> is <2>
DEBUG: is character at index <1> valid? <true>
DEBUG: character at index <2> is <3>
DEBUG: is character at index <2> valid? <true>
Is user's input <123> valid as required? <true>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 4567
DEBUG: user's input is <4567>
DEBUG: trimmed input is <4567>
DEBUG: the length of the trimmed input is <4>
DEBUG: character at index <0> is <4>
DEBUG: is character at index <0> valid? <true>
DEBUG: character at index <1> is <5>
DEBUG: is character at index <1> valid? <true>
DEBUG: character at index <2> is <6>
DEBUG: is character at index <2> valid? <true>
DEBUG: character at index <3> is <7>
DEBUG: is character at index <3> valid? <true>
Is user's input <4567> valid as required? <true>
$ java CheckingOctalNumber
Enter an valid octal number (no more than 4 digits): 0012
DEBUG: user's input is <0012>
DEBUG: trimmed input is <0012>
DEBUG: the length of the trimmed input is <4>
DEBUG: character at index <0> is <0>
DEBUG: is character at index <0> valid? <true>
DEBUG: character at index <1> is <0>
DEBUG: is character at index <1> valid? <true>
DEBUG: character at index <2> is <1>
DEBUG: is character at index <2> valid? <true>
DEBUG: character at index <3> is <2>
DEBUG: is character at index <3> valid? <true>
Is user's input <0012> valid as required? <true>
Subproject 4.2 Robust Handling of Conversion between Decimal and Octal Numbers with Debug Messages
Subproject 4.1 does not require us to design a program that is robust against rogue user inputs. In this
subproject, you are to create a program called RobustNumberConverterDebugger
that is a revision of
the program you wrote for subject 4.1 by integrating the logic in Exercise 4.4. It is important to note
that your program should be robust against both rogue user inputs for both of the conversions
(i.e., converting an octal numerical to a decimal and converting a decimal numerical to an octal).
In this program you are also required to include several statements that print out “DEBUG” messages. As discussed, using the “DEBUG” messages we can examine whether the program behaves as expected, which serves as an essential tool to aid the logic design of the program by removing defects (or errors), or “de-bug” where a bug is a defect. For history buffs, this page (linked) of the National Museum of American History is worth a visit.
Below are a test run of the program:
$ java RobustNumberConverterDebugger
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
DEBUG: user's input for menu option is <1>
DEBUG: user's option is <1>
Enter an valid octal number (no more than 4 digits): 1234
DEBUG: for a valid octal number, the user entered <1234>
DEBUG: is the input a valid actal number? <true>
Octal number 1234 is decimal number 668.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
DEBUG: user's input for menu option is <2>
DEBUG: user's option is <2>
Enter an valid decimal number (no more than 4 digits): 4321
DEBUG: for a valid octal number, the user entered <4321>
DEBUG: is the input a valid decimal number? <true>
Decimal number 4321 is octal number 10341.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
DEBUG: user's input for menu option is <1>
DEBUG: user's option is <1>
Enter an valid octal number (no more than 4 digits): 678
DEBUG: for a valid octal number, the user entered <678>
DEBUG: is the input a valid actal number? <false>
Enter an valid octal number (no more than 4 digits): 567
DEBUG: for a valid octal number, the user entered <567>
DEBUG: is the input a valid actal number? <true>
Octal number 567 is decimal number 375.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
DEBUG: user's input for menu option is <1>
DEBUG: user's option is <1>
Enter an valid octal number (no more than 4 digits): 12 7
DEBUG: for a valid octal number, the user entered <12 7>
DEBUG: is the input a valid actal number? <false>
Enter an valid octal number (no more than 4 digits): 124a
DEBUG: for a valid octal number, the user entered <124a>
DEBUG: is the input a valid actal number? <false>
Enter an valid octal number (no more than 4 digits): 7777
DEBUG: for a valid octal number, the user entered <7777>
DEBUG: is the input a valid actal number? <true>
Octal number 7777 is decimal number 4095.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
DEBUG: user's input for menu option is <2>
DEBUG: user's option is <2>
Enter an valid decimal number (no more than 4 digits): 98 7
DEBUG: for a valid octal number, the user entered <98 7>
DEBUG: is the input a valid decimal number? <false>
Enter an valid decimal number (no more than 4 digits): 98FE
DEBUG: for a valid octal number, the user entered <98FE>
DEBUG: is the input a valid decimal number? <false>
Enter an valid decimal number (no more than 4 digits): 668
DEBUG: for a valid octal number, the user entered <668>
DEBUG: is the input a valid decimal number? <true>
Decimal number 668 is octal number 1234.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
DEBUG: user's input for menu option is <0>
Exit the program
Subproject 4.3 Robust Handling of Conversion between Decimal and Octal Numbers
In this
subproject, you are to create a program called RobustNumberConverter
that is a revision of
the program you wrote for subject 4.2. The simplest method to complete this is to comment out
or remove those statements that print out the “DEBUG” messages. However, you are free to
explore different design.
The following is a test run of the program:
$ java RobustNumberConverter
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
Enter an valid octal number (no more than 4 digits): 1278
Enter an valid octal number (no more than 4 digits): 12 7
Enter an valid octal number (no more than 4 digits): 12 ab
Enter an valid octal number (no more than 4 digits): 1267
Octal number 1267 is decimal number 695.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 4 digits): 12345
Enter an valid decimal number (no more than 4 digits): 12 3
Enter an valid decimal number (no more than 4 digits): 12FE
Enter an valid decimal number (no more than 4 digits): 12 ab
Enter an valid decimal number (no more than 4 digits): 695
Decimal number 695 is octal number 1267.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 1
Enter an valid octal number (no more than 4 digits): 7777
Octal number 7777 is decimal number 4095.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 2
Enter an valid decimal number (no more than 4 digits): 9999
Decimal number 9999 is octal number 23417.
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 9
1. Convert octal number to decimal
2. Decimal decimal number to octal
0. Exit
Enter option (0, 1, or 2) and hit enter: 0
Exit the program