(NumberFormatException)Write the bin2Dec(String binaryString) method to convert a binary string into a decimal number. Implement the bin2Dec method to throw a NumberFormatException if the string is not a binary string. Write a test program that prompts the user to enter a binary number as a string and displays decimal equivalent of the string. If the method throws an exception, display "Not a binary number".Sample Run 1Enter a binary number: 1015Sample Run 2Enter a binary number: 41Not a binary number: 41Class Name: Exercise12_07

Respuesta :

The following program will be used that prompts the user to enter a binary number as a string and displays decimal equivalent of the string.

Explanation:

NumberFormat.java

import java.util.Scanner;

public class NumberFormat{

public static void main(String[] args) {

      Scanner scan = new Scanner(System.in);

      try{

      System.out.println("Enter a binary string:");

      String s = scan.next();

      int integerValue = bin2Dec(Integer.parseInt(s));

      System.out.println("The decimal value of "+s+" is "+integerValue);

      }

      catch(NumberFormatException e){

          System.out.println(e);

      }

  }  

  public static int bin2Dec(int binaryNumber) throws NumberFormatException{

  int decimal = 0;

  int p = 0;

  try{

  while(true){

  if(binaryNumber == 0){

  break;

  } else {

  int temp = binaryNumber%10;

  decimal += temp*Math.pow(2, p);

  binaryNumber = binaryNumber/10;

  p++;

  }

  }

  }

  catch(NumberFormatException e){

      throw new NumberFormatException("Invalid binary number");

  }

  return decimal;

  }

}

Output:

Enter a binary string:

1011000101

The decimal value of 1011000101 is 709

Following are the program to the given question:

Program Explanation:

  • Import package.
  • Defining a class "Exercise12_07".
  • Inside a class defining a method "bin2Dec" that takes a string variable as a parameter.
  • In the method, it converts binary digits to decimal number with the help of exception and returns its value.
  • Defining a main method, inside the method string variable is define that inputs value.
  • After input try and catch block are used that checks value and print its value.

Program:

// package

import java.util.*;

public class  Exercise12_07 // class Exercise12_07

{

   //defining method

   public static int bin2Dec(String binaryString) throws NumberFormatException //method bin2Dec that takes a one String parameter

   {

       int t = 0,i;//defining Integer variable

       for(i = 0; i < binaryString.length(); ++i)//defining loop that converts binary to decimal convert  

       {

           if(binaryString.charAt(i) != '0' && binaryString.charAt(i) != '1')//using if that checks binary digit with Exception

           {

               throw new NumberFormatException();//calling Exception

           }

           t += Math.pow(2, binaryString.length() - i - 1) * (binaryString.charAt(i) - '0');//converting and holding Decimal number

       }

       return t;//return value

   }

   public static void main(String[] a) //main method

   {

       Scanner inxv = new Scanner(System.in);//defining Scanner class Object

       System.out.print("Enter a binary number: ");//print message

       String str = inxv.nextLine();//defining String variable and input value

       try //defining try block

       {

           System.out.println("Decimal value of " + str + " is " + bin2Dec(str));//using print method to print method return value  

       }  

       catch (NumberFormatException e)//defining catch block

       {

           System.out.println("Not a binary number: " + str);//print message

       }

   }

}

Output:

Please find the attachment file.

Learn more:

brainly.com/question/19755688

Ver imagen codiepienagoya
ACCESS MORE