I want you to email me your assignment this week. You can either zip together a set of files, or send them separately. My address: fickas@cs.uoregon.edu.
Read first two pages of chapter 42 (forty-two) in Dewdney. Pay attention to the general formula for converting a number in base b to a decimal number.
Also read page 684 (conversions in appendix b) in Lewis&Loftus. Pay attention to the approach on page 686 of converting a decimal number into binary.
Note that for both parts a and b below, you should only recode the main method. All the rest of the code I give you should not be touched.
Part a. I'll give you a skeleton piece of code to convert a binary number to decimal. The main method is below. Sorry, this code does not solve the problem for you. But it does have the pieces that you will need. First, getBinaryNumberFromUser will store a binary number that the user types in. It will store it in a special place. How do you access this number? You use the method getNextDigit. This will give you each digit in the binary number, starting on far right and moving left. How do you know when you are done? You use the predicate notDone. A predicate is a method that returns true or false. If notDone returns true, then there are still digits to get.
What does the code do below? First it gets a binary number from the user. Then it prints each digit, one on each line. Finally it uses the Math.pow method to show you how to do exponentiation.
Part b. I'll give you a skeleton piece of code to convert a decimal number (a base 10 integer) to binary. The main method is below. This code does not solve the problem for you. But it does have the pieces that you will need. First, getBase10IntFromUser will return a number that the user types in. How do you generate the binary number? Use the method appendNextDigit. Let's say that you have already generated a binary number 111. If you now call appendNextDigit(0), you will have 1110.
What does the code do below? First it gets a base 10 int from the user. Then it loops from 0 to n-1. On each loop iteration, it decides if i is odd or even. If it is even, it adds a 0 to the binary number. If it is odd, it adds a 1. In the end, you should have a binary number with n digits. Finally it prints out the binary number.
Turn-in: email me your two files called BinaryToDecimal.java and DecimalToBinary.java. I will run them to see if they work. Of course, you should test them yourself before turning them in.
No second problem this week. But you might review material for midterm coming up.