Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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
expand button
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
Check Mark
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));
        }
}

Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education