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*).

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter3: Assignment, Formatting, And Interactive Input
Section3.4: Program Input Using Cin
Problem 9E
icon
Related questions
Question

 

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.

  1. Multiply each binary digit by its corresponding base 2 placement value.
Objective:
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.
Press F11 to exit full screen
Requirements:
• Functionality. (80pts)
o 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.
o 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.
o 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 l'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.
o Adding Values (10pts)
· Both decimal values must be added together and printed out.
o You may NOT use Integer.parseInt(<<STRING>>, 2) or any automatic converter (80pts*).
• *The use of specifically Integer.parseInt(<<STRING>>,2) wil1 result in a 0 for this entire section.
· You may use Integer.parseInt(<<STRING>>).
• Coding Style. (10pts)
o 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)
o Your name in the file. (5pts)
o At least 5 meaningful comments in addition to your name. These must describe the function of the code it is near. (5pts)
Hint:
A simple way to convert a binary value to a decimal value.
1. Multiply each binary digit by its corresponding base 2 placement value.
Binary Digit
bo
bị
b2
b3
Base 2 Value
23
22
21
20
Result
bo x 23
b; x 22
b, x 21
b; x 20
Example:
Binary Digit
Base 2 Value
1
1
23
22
20
Result
4.
1
2. Add the values together to get the decimal value.
Binary Value = bo x 23 + bị x 22 + b2 x 21 + b3 x 2°
Example:
Binary Value = 0 +4 + 2+1= 7
Transcribed Image Text:Objective: 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. Press F11 to exit full screen Requirements: • Functionality. (80pts) o 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. o 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. o 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 l'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. o Adding Values (10pts) · Both decimal values must be added together and printed out. o You may NOT use Integer.parseInt(<<STRING>>, 2) or any automatic converter (80pts*). • *The use of specifically Integer.parseInt(<<STRING>>,2) wil1 result in a 0 for this entire section. · You may use Integer.parseInt(<<STRING>>). • Coding Style. (10pts) o 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) o Your name in the file. (5pts) o At least 5 meaningful comments in addition to your name. These must describe the function of the code it is near. (5pts) Hint: A simple way to convert a binary value to a decimal value. 1. Multiply each binary digit by its corresponding base 2 placement value. Binary Digit bo bị b2 b3 Base 2 Value 23 22 21 20 Result bo x 23 b; x 22 b, x 21 b; x 20 Example: Binary Digit Base 2 Value 1 1 23 22 20 Result 4. 1 2. Add the values together to get the decimal value. Binary Value = bo x 23 + bị x 22 + b2 x 21 + b3 x 2° Example: Binary Value = 0 +4 + 2+1= 7
Expert Solution
Step 1

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

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 3 images

Blurred answer
Knowledge Booster
Array
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr