CSE1020 Labtest 1E

Please read this part carefully

No collaboration is allowed during a labtest. Refrain from speaking with other students during the test. If you have a question, please ask the teaching assistants or the instructor.

In your home directory you will find a copy of a solution to the ColourConvert problem from Lab 02. You may use pens/pencils and blank sheets of paper. No other aids are allowed.

Your work will be evaluated based on its correctness and conformance to the style rules. Your code must compile to receive a passing grade.

Remember to save your work and submit your work at regular intervals during the test.

Test Problem

In our everyday lives, we normally use base-10 to represent numbers; all of our numbers are made up of the ten digits 0 through 9. Internally, computers use base-2 to represent numbers; this so called binary representation has numbers made up of only ones and zeros.

There are eight 3-digit binary numbers as shown in the table below. To convert from binary to base-10 you multiply the leftmost digit of the binary number by 4, the middle digit by 2, and the rightmost digit by 1, and then take their sum:

binary number meaning base-10 value
000 (0 * 4) + (0 * 2) + (0 * 1) 0
001 (0 * 4) + (0 * 2) + (1 * 1) 1
010 (0 * 4) + (1 * 2) + (0 * 1) 2
011 (0 * 4) + (1 * 2) + (1 * 1) 3
100 (1 * 4) + (0 * 2) + (0 * 1) 4
101 (1 * 4) + (0 * 2) + (1 * 1) 5
110 (1 * 4) + (1 * 2) + (0 * 1) 6
111 (1 * 4) + (1 * 2) + (1 * 1) 7

Write a program called Binary that reads one integer from the command line (as int); you can assume that the number is one of the eight 3-digit binary numbers shown in the table above. Your program should output the base-10 value of the input binary number.

Your program must not use an if statement or a switch statement; all you need are the arithmetic operators. Here are a few sample runs of a correct program:

java Binary 000
0

java Binary 001
1

java Binary 101
5

java Binary 111
7

 

Submit

Submit your program using the command:

submit 1020 test01E Binary.java

You may leave when you are finished.