Question

I'm working on a java program where I'm supposed to convert 4 bit binary into decimal....

I'm working on a java program where I'm supposed to convert 4 bit binary into decimal. I went with if statements and tried to use an else to print out an error message if the user enters a number that isn't a binary number but it prints no matter what. (so, it prints that it's a vowel and that it's not a vowel)

1 import java.util.Scanner;
2
3 public class HomeworkTwoQ2
4 {
5
6 public static void main(String[] args)
7 {
8
9 System.out.println("Please enter a four bit binary number: ");
10
11 Scanner binary = new Scanner(System.in);
12
13 int binaryInput = binary.nextInt();
14
15 if (binaryInput == 0000)
16 {
17 System.out.println("0");
18 }
19 if (binaryInput == 0001)
20 {
21 System.out.println("1");
22 }
23 if (binaryInput == 0010)
24 {
25 System.out.println("2");
26 }
27 if (binaryInput == 0011)
28 {
29 System.out.println("3");
30 }
31 if (binaryInput == 0100)
32 {
33 System.out.println("4");
34 }
35 if (binaryInput == 0101)
36 {
37 System.out.println("5");
38 }
39 if (binaryInput == 0110)
40 {
41 System.out.println("6");
42 }
43 if (binaryInput == 0111)
44 {
45 System.out.println("7");
46 }
47 if (binaryInput == 1000)
48 {
49 System.out.println("8");
50 }
51 if (binaryInput == 1001)
52 {
53 System.out.println("9");
54 }
55 if (binaryInput == 1010)
56 {
57 System.out.println("10");
58 }
59 if (binaryInput == 1011)
60 {
61 System.out.println("11");
62 }
63 if (binaryInput == 1100)
64 {
65 System.out.println("12");
66 }
67 if (binaryInput == 1101)
68 {
69 System.out.println("13");
70 }
71 if (binaryInput == 1110)
72 {
73 System.out.println("14");
74 }
75 if (binaryInput == 1111)
76 {
77 System.out.println("15");
78 }
79 else
80 {
81 System.out.println("Not a four digit binary number, exiting.");
82 }
83
84 }
85 }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// Screenshot of the code & output

// Code to copy

HomeworkTwoQ2.java

import java.util.Scanner;
public class HomeworkTwoQ2 {

   public static void main(String[] args) {
       System.out.println("Please enter a four bit binary number: ");
       Scanner binary = new Scanner(System.in);
       int binaryInput = binary.nextInt();
       if (binaryInput == 0000)
       {
           System.out.println("0");
       }else if(binaryInput == 0001) {
           System.out.println("1");
       }else if(binaryInput == 0010) {
           System.out.println("2");
       }else if(binaryInput == 0011) {
           System.out.println("3");
       }else if(binaryInput == 0100) {
           System.out.println("4");
       }else if(binaryInput == 0101) {
           System.out.println("5");          
       }else if(binaryInput == 0110) {
           System.out.println("6");
       }else if(binaryInput == 0111) {
           System.out.println("7");
       }else if(binaryInput == 1000) {
           System.out.println("8");
       }else if(binaryInput == 1001) {
           System.out.println("9");
       }else if(binaryInput == 1010) {
           System.out.println("10");
       }else if(binaryInput == 1011) {
           System.out.println("11");
       }else if(binaryInput == 1100) {
           System.out.println("12");
       }else if(binaryInput == 1101) {
           System.out.println("13");
       }else if(binaryInput == 1110) {
           System.out.println("14");
       }else if(binaryInput == 1111) {
           System.out.println("15");
       }else {
           System.out.println("Not a four digit binary number, exiting.");
       }
       binary.close();
   }

}

Add a comment
Know the answer?
Add Answer to:
I'm working on a java program where I'm supposed to convert 4 bit binary into decimal....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Generate the equivalent Sim68 assembly program for the following machine code assuming it originates at address...

    Generate the equivalent Sim68 assembly program for the following machine code assuming it originates at address dollar 0000: 1001 0000 0100 0000 0011 0000 0100 0000 0011 0010 0010 1000 0000 0000 0000 1100 0110 0111 0000 0110 0110 1101 1111 1000 1101 0000 0100 0001 0110 0000 1111 0110 0100 1110 0100 0000 0000 0000 0000 0000 0001 0110 1111 0000 1111 1111 1111 1111 1000 0000 0000 0000 0000 0000 0000 0001

  • 3. What is the hexadecimal representation of each of the following binary numbers in signed 2’s...

    3. What is the hexadecimal representation of each of the following binary numbers in signed 2’s complement? 0010 0101 0100 0011 0001 1011 0010 0100 1111 0110 1101 1001

  • We have learned a famous shift cipher called Caesar Cipher. Now if we are given a...

    We have learned a famous shift cipher called Caesar Cipher. Now if we are given a plain test: THE ART OF WARAnd key = 3 (a shift by 3 letters), please give the ciphertext Given an 8 bit block P = 10101111 and a key K = 01101011, please give the result of bitwise XOR between P and K Please give the left 2 shift of the 8 bit text 01100101 Use the given a permutation table 23614857 to define...

  • binary conversions. please help. thank you! Convert the following Binary number to Base 8 4. 1111...

    binary conversions. please help. thank you! Convert the following Binary number to Base 8 4. 1111 1001 0110 0001 1001 0101 1101 1010 1110 0010 0101 Convert the following Base 8 number to binary 5. 200076524, Convert the following Base 8 number to Base 16 6. 1177662231

  • Base no bit 0 0 Q-2 (25p): Assume that logical addresses are 16-bit long: 3 bits...

    Base no bit 0 0 Q-2 (25p): Assume that logical addresses are 16-bit long: 3 bits for segment no and 13 bits for offset. Segment table of the currently running process is given below: a) (5) How many segments can this Segment Present Modify Length process have at maximum? bit 0 00100 0111 0000 0100 0000 1000 0100 b) (15) Convert the following logical 10100 0100 0001 0010 0010 1100 1000 address to physical (real) address. 2 01100 0100 0000...

  • Please show work! 2. Now, give it a try by converting the binary number 01110110 to...

    Please show work! 2. Now, give it a try by converting the binary number 01110110 to decimal by filling in the same table in step 1 r of 2 Pov 128 64 32 16 Cumulative Amount 4. Now, you give it a try by converting the decimal number 131 to binary by filling in the table Power of 2 128 32 16 Bit Amount Remaining 6. Use the binary to hexadecimal table to convert the binary number 01101111 to hexadecimal...

  • Arduino. DEC HEX BIN(4-bits) Introducing ARDUINO 0 0 0000 1 1 0001 2 2 0010 3...

    Arduino. DEC HEX BIN(4-bits) Introducing ARDUINO 0 0 0000 1 1 0001 2 2 0010 3 3 0011 4 4 0100 5 5 0101 How many 1/0 of Port-D? How many usable 1/0 of Port-D, if Serial-Communication is in-used? What is the Arduino's pin assignment of ATMEL's PC5, PB3, & PD1*? What is the ATMEL's pin assignment of Arduino's D13*, D1, & D19? To complete the table about Number System Conversion (shown your step) 6 6 0110 7 7 0111...

  • DO bit 0 1 Q-2 (25p): Assume that logical addresses are 16-bit long: 3 bits for...

    DO bit 0 1 Q-2 (25p): Assume that logical addresses are 16-bit long: 3 bits for segment no and 13 bits for offset. Segment table of the currently running process is given below: a) (5) How many segments can this Segment Present Modify process have at maximum? Length bit Base 0 0 0 0100 0111 0000 0100 0000 1000 0100 b) (15) Convert the following logical 1 1 0100 0100 0001 0010 0010 1100 1000 address to physical (real) address...

  • I think you are actually doing binary to hex. please do this without loops and you...

    I think you are actually doing binary to hex. please do this without loops and you can use recursion. please write a working C code. Thanks Write a loop-less function to convert from Hex to Binary. (HINT: Use a helper function/recursion) binHex[16] [5] {"0000", "O001","0010","0011","0100" ,"0101", "0110", "0111", "1000", "1001" , "1010", "1011", "1100", "1101", "1110" 1 const char = s char hexToBinary ...) 7

  • Question 1.1. (TCO 1) Which number system has a radix of two? (Points : 4) Hexadecimal...

    Question 1.1. (TCO 1) Which number system has a radix of two? (Points : 4) Hexadecimal Binary Decimal Octal Question 2.2. (TCO 1) Convert 24 base 10 to hexadecimal. (Points : 4) 1A 18 20 30 Question 3.3. (TCO 1) If FF h is converted to decimal, the result is _____. (Points : 4) 100 200 255 256 Question 4.4. (TCO 1) Convert decimal 103 to an 8-bit binary number. (Points : 4) 1110 0100 0100 0000 0110 0111 0110...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT