Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write an application that inputs an integer between 00000 and 11111 and print

ID: 3544684 • Letter: 1

Question

1.      Write an application that inputs an integer between 00000 and 11111 and prints its decimal equivalent (between 0 and 63). If the number entered is out of range then the program must print an error message and allow the user to enter a new value.

Sample Run:

Enter a Binary Number between 00000 and 11111 (-1 to stop): 12343

WRONG VALUE! PLEASE ENTER A NUMBER AGAIN: 111111

WRONG VALUE! PLEASE ENTER A NUMBER AGAIN: 11000

The equivalent decimal number is: 24

Enter a Binary Number between 00000 and 11111 (-1 to stop): -1

Thank you !!!

Explanation / Answer

import java.util.*;

public class BinaryRep {

public static void main(String[] args) {

System.out.println("Enter a Binary Number between 00000 and 11111 (-1 to stop):");

while(true)

{

int flag=0;

Scanner input = new Scanner(System.in);

String number = input.nextLine();

String t="-1";

if((number.charAt(0)==t.charAt(0))&&(number.charAt(0)==t.charAt(0)))

break;

int len=number.length();

if(len!=5)

flag=1;

for(int i=0;i<5;i++)

if((number.charAt(i)!='1')&&(number.charAt(i)!='0'))

flag=1;

if(flag==0)

{int num=(int)((number.charAt(0)-'0')*16)+(int)((number.charAt(1)-'0')*8)+(int)((number.charAt(2)-'0')*4)+(int)((number.charAt(3)-'0')*2)+(int)(number.charAt(0)-'0');

System.out.println("the decimal equvalent is : "+num);

System.out.println("Enter a Binary Number between 00000 and 11111 (-1 to stop):");

}

else

{

System.out.println("WRONG VALUE! PLEASE ENTER A NUMBER AGAIN:");

}

}

}


}