Write a program that accepts two four-digit binary numbers, converts them to decimal values, adds them together, and prints both the decimal values and the result of the addition. Requirements: Functionality. (80pts) No Syntax Errors. (80pts*) *Code that cannot be compiled due to syntax errors is nonfunctional code and will receive no points for this entire section. Clear and Easy-To-Use Interface. (10pts) Users should easily understand what the program does and how to use it. Users should be prompted for input and should be able to enter data easily. Users should be presented with output after major functions, operations, or calculations. All the above must apply for full credit. Users must be able to enter a 4-bit binary number in some way. (10pts) No error checking is needed here and you may assume that users will only enter 0’s and 1’s, and they will only enter 4 bits. Binary to Decimal Conversion (50pts) You may assume that users will only give numbers that add up to 15. See the section Hint for more details. Adding Values (10pts) Both decimal values must be added together and printed out. You may NOT use Integer.parseInt(<>, 2) or any automatic converter (80pts*).
Write a program that accepts two four-digit binary numbers, converts them to decimal values, adds them together, and prints both the decimal values and the result of the addition.
Requirements:
- Functionality. (80pts)
- No Syntax Errors. (80pts*)
- *Code that cannot be compiled due to syntax errors is nonfunctional code and will receive no points for this entire section.
- Clear and Easy-To-Use Interface. (10pts)
- Users should easily understand what the program does and how to use it.
- Users should be prompted for input and should be able to enter data easily.
- Users should be presented with output after major functions, operations, or calculations.
- All the above must apply for full credit.
- Users must be able to enter a 4-bit binary number in some way. (10pts)
- No error checking is needed here and you may assume that users will only enter 0’s and 1’s, and they will only enter 4 bits.
- Binary to Decimal Conversion (50pts)
- You may assume that users will only give numbers that add up to 15.
- See the section Hint for more details.
- Adding Values (10pts)
- Both decimal values must be added together and printed out.
- You may NOT use Integer.parseInt(<<STRING>>, 2) or any automatic converter (80pts*).
- *The use of specifically Integer.parseInt(<<STRING>>,2) will result in a 0 for this entire section.
- You may use Integer.parseInt(<<STRING>>).
- Coding Style. (10pts)
- Readable Code
- Meaningful identifiers for data and methods.
- Proper indentation that clearly identifies statements within the body of a class, a method, a branching statement, a loop statement, etc.
- All the above must apply for full credit.
- Comments. (10pts)
- Your name in the file. (5pts)
- At least 5 meaningful comments in addition to your name. These must describe the function of the code it is near. (5pts)
PLEASE WRITE IN JAVA USING ECLIPSE IED
Hint:
A simple way to convert a binary value to a decimal value.
- Multiply each binary digit by its corresponding base 2 placement value.
ANSWER:
we can convert binary to decimal in java using Integer.parseInt() and a custom method getDecimal().
Integer.parseInt(String s) takes 1 argument:
then Integer.ParseInt() will convert String of binary into integer and return it.
we will then convert this integer into decimal digit using a method int getDecimal(int binary).
code:
import java.util.*;
public class Main
{
//defining getDecimal method.
public static int getDecimal(int binary){
int decimal = 0;
int n = 0;
while(true){
if(binary == 0){
break;
} else {
int temp = binary%10;
decimal += temp*Math.pow(2, n);
binary = binary/10;
n++;
}
}
return decimal;
}
public static void main(String[] args) {
//taking 2 binary numbers as an input
Scanner sc= new Scanner(System.in);
System.out.println("Enter a 4-bit binary number");
String str1= sc.nextLine();
System.out.println("Enter another 4-bit binary number");
String str2= sc.nextLine();
//converting binary string number into binary integers.
int bin1=Integer.parseInt(str1);
int bin2=Integer.parseInt(str2);
// converting binary digits into decimal digits:
int dec1=getDecimal(bin1);
int dec2=getDecimal(bin2);
//printing out the decimal digits and their sum:
System.out.println("The first number is "+ dec1);
System.out.println("The second number is "+ dec2);
System.out.println("Added together is "+ (dec1+dec2));
}
}
Trending now
This is a popular solution!
Step by step
Solved in 4 steps with 3 images