IS187: Java Programming I Bin2Dec and Dec2Bin (100 points + 10 Ex.Cr.) 2016 For
ID: 3776396 • Letter: I
Question
IS187: Java Programming I
Bin2Dec and Dec2Bin (100 points + 10 Ex.Cr.) 2016
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:
1)Form as show above
2)conversion algorithm based on the process described above (using positional values) implemented in a Dec2Bin.java business object with methods as described
3)instantiation of Dec2Bin by calling a constructor that received the start value
4)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 developed Bin-to-Dec program we performed input validation by stepping through each position of the input string, checking for the presence of only a zero or a one. There are, however, simpler, more efficient methods of checking that the input string contains only binary digits. For extra credit, submit a version of the program with a better validation method (your choice) inside the Bin2Dec.java class.
(5 points) Again in the developed Bin-to-Dec algorithm, the algorithm presented for the process 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).
Decimal Binary Conversion P Daniel File Help Conversion type: CEinaYTsSeimal Decima TcEnary Value: on Process: Result O P Decimal/Binary conversion p. Daniel Binary Converior P. Danie File Help Fie Hep o Conversion type: Binary To Decimal O Decimal To-Binary Conversion type: 2 Eirany-T>Deinal To Einary Du Value: 13 Convert Value: Convert Process: Process: 13 divided by 2 6 w/remainder of 1 Ther is a (nl 1 in the value 2 01 6 divided by 2 3 w/remainder of: 0 There is (ral 2 in the value :2 1 3 divided by 2 1 w/remainder of: 1 There is a CI 8 in the value :2 3 1 divided by 2 0 w/remainder of: 1 Result: 1101 ResultExplanation / Answer
import java.util.Scanner;
public class DecimalFromBinary {
/**
*www.instanceofjava.com
*/
public static void main(String[] args) {
Scanner in = new Scanner( System.in );
System.out.println("Enter a binary number: ");
int binarynum =in.nextInt();
int binary=binarynum;
int decimal = 0;
int power = 0;
while(true){
if(binary == 0){
break;
} else {
int tmp = binary%10;
decimal += tmp*Math.pow(2, power);
binary = binary/10;
power++;
}
}
System.out.println("Binary="+binary+" Decimal="+decimal); ;
}
}
2)
class DecimalBinaryExample{
public void convertBinary(int num){
int binary[] = new int[40];
int index = 0;
while(num > 0){
binary[index++] = num%2;
num = num/2;
}
for(int i = index-1;i >= 0;i--){
System.out.print(binary[i]);
}
}
public static void main(String a[]){
DecimalBinaryExample obj = new DecimalBinaryExample();
System.out.println("Binary representation of 124: ");
obj.convertBinary(124);
System.out.println(" Binary representation of 45: ");
obj.convertBinary(45);
System.out.println(" Binary representation of 999: ");
obj.convertBinary(999);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.