## Description

For this assignment you will write a program which can convert a binary number to decimal, or a decimal number to binary. You are to accomplish this by coding specific algorithms for these processes, as described below (note the use of recursion in the Decimal to Binary operation). Also, the algorithms given should implemented in two separate classes: Bin2Dec.java and Dec2Bin.java. These classes will be instantiated (depending on which process is being performed) using the start value and then will return both the result and the display lines for the ‘process’ area shown on the form below.

__Part A (Binary to Decimal – 50 pts):__

A ‘standard’ algorithm for a binary to decimal conversion evaluates the positional values of the binary digits. For example, the binary value of: 101 is 5 because you have the following:

Binary powers of 2: 2^2 2^1 2^0

Positional value: __4__ __2__ __1__

Binary digits: 1 0 1

So the value = (1*4) + (0*2) + (1*1) = 5

Likewise, the binary value of 11111101 = 253 because you have the following:

Powers of 2: 2^7 2^6 2^5 2^4 2^3 2^2 2^1 2^0

Positional values: __128__ __ 64__ __32__ __16__ __8__ __4__ __2__ __1__

Binary digits: 1 1 1 1 1 1 0 1

So the value = 128 + 64 + 32 + 16 + 8 + 4 + 1 = 253

The form to be developed for the program will look like this:

The overall project should be called BinDecConv so the form (as a result of using the Swing Application Framework) will be housed in a java source file call BinDecConvView. The actual conversion processes, however, should be developed using separate “business” classes for each process: Dec2Bin and Bin2Dec. When the convert button is pressed the form will instantiate the appropriate class based on the radio-button selected, and will then call for the results from the instantiated class. The classes will perform the internal validation of the input value.

The methods needed in the Bin2Dec.java class are:

- A constructor to receive the starting value
- A String getResult() method that returns a string representation of converted starting value
- An ArrayList<String> getResultSteps() method that returns an array list of each step in the conversion process (the array list elements will be transferred to the Process display area)
- A String getErrorMsg() method which returns an empty string if the conversion was successful, or an error message if the starting value could not be processed

For Bin2Dec you validate the input value as containing only ones and zeros (remember that the input via the form is a string). Any error message returned from the business object should be placed in the ‘statusMessageLabel’ area at the bottom of the form. A sample run would look as follows:

For a complete and correct Part A make sure you include the following:

- Form as show above
- conversion algorithm based on the process described above (using positional values) implemented in a Dec2Bin.java business object with methods as described
- instantiation of Dec2Bin by calling a constructor that received the start value
- Display results from process methods of Dec2Bin as described above

.

__Part B (Decimal to Binary – 50 pts):__

An algorithm for converting a decimal value into its binary equivalent involves repeated (integer) division by 2 (down to a result of 0), collection of the __remainders__ for each division, and displaying those remainders in reverse order (of creation). So, for example, the algorithm applied to decimal 321 would look like the following:

remainders

2|__321__

2|__160__ à 1

2__| 80__ à 0

2__| 40__ à 0

2__| 20__ à 0

2__| 10__ à 0

2__| 5__ à 0

2__| 2__ à 1

2__| 1__ à 0

2__| 0__ à 1

Thus the answer is: 101000001 (i.e., remainders from bottom up)

In this assignment you will implement this algorithm (in the Dec2Bin.java class) through __recursion__, which is a looping technique wherein the processing method __calls itself__. Recursion is not discussed in the text, but will be demonstrated by the instructor through the use of the ‘simple recursion’ example program which is posted on blackboard.

A sample run for the Decimal to Binary conversion would be:

Use the data type ‘long’ when converting the input value from the form to a data type used in the recursive algorithm. Only positive integer values are converted (no fractions and no negatives). __Remember: you must implement a recursive algorithm using a separate method for the conversion process, and the only parameter allowed for this method is the value to be divided by 2.__

__ __

The Dec2Bin.java class which is instantiated to perform the operation will implement the recursive algorithm and have the same methods available as the Bin2Dec.java class:

- A constructor to receive the starting value
- A String getResult() method that returns a string representation of the binary value resulting from the conversion of the starting value
- An ArrayList<String> getResultSteps() method that returns an array list of each step in the conversion process (the array list elements will be transferred to the Process display area)
- A String getErrorMsg() method which returns an empty string if the conversion was successful, or an error message if the starting value could not be processed

__ __

__ __

__Extra Credit (10 points):__

__ __

(5 points) In the Bin-to-Dec program, input validation can be done by stepping through each position of the input string, checking for the presence of only a zero or a one (when demonstrated in class, this is the approach used). There are, however, simpler, more efficient methods of checking that the input string contains only binary digits 0 or 1. For extra credit, submit a version of the program with a better validation method (your choice) inside the Bin2Dec.java class. (Note: validation can be done with a single command).

(5 points) Again in the Bin-to-Dec algorithm, the algorithm presented (if done in class) worked backward through the input string using a separate pointer for the ‘power of two’ calculation. It will be observed, however, that if we reverse the characters of the input string, a simpler loop can be used with the loop control variable operating as the power of two indicator. Implement such an alternate algorithm (i.e., one that reverses the string before processing).