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

I am struggling with 12.7. Java I am struggling with 12.7. Java .12.6 (NumberFor

ID: 642143 • Letter: I

Question

I am struggling with 12.7. Java

I am struggling with 12.7. Java .12.6 (NumberFormatException) Listing 6.8 implements the hex2Dec(String hexString) method, which converts a hex string into a decimal number. Implement the hex2Dec method to throw a NumberFormatException if the is not a hex string. *12.7 (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. *12.8 (HexFormatException) Exercise 12.6 implements the hex2Dec method to throw a NumberFormatException if the string is not a hex string. Define a custom exception called HexFormatException. Implement the hex2Dec method to throw a Hex FormatExcept ion if the string is not a hex string. * 12.9 (BinaryFormatException) Exercise 12.7 implements the bin2Dec method to throw a BinaryFormatException it the string is not a binary string. Define a custom exception called BinaryFormatException. Implement the bin2Oec method to throw a BinaryFormatException ion if the string is not a binary string. 12.10 (OutOfMemoryErrOr) Write a program that causes the JVM to throw an OutOfMemoryErrOr and catches and handles this error.

Explanation / Answer


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import static java.lang.System.out;

/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

/**
*
* @author info
*/
public class JavaApplication1
{

/**
* @param args the command line arguments
* @throws java.io.IOException
*/
public static void main(String[] args) throws IOException
{
InputStreamReader istream = new InputStreamReader(System.in) ;
BufferedReader bufRead = new BufferedReader(istream) ;
System.out.println("Enter the binary string:");
String stringBinary=bufRead.readLine(); //reading binary string value
if(Binary(stringBinary))// checks whether the given number is binary or not
{
int length=stringBinary.length();// finding lebhth of the string
out.println("length of the string is"+length);
String check="1";
int decemal;
int final_decemal=0;
for(int i=0;i<length;i++)
//Loop to find the sum of position values
{
decemal=1;
if(stringBinary.charAt(i)=='1')
{
int numIterations=length-1-i;
for(int j=1;j<numIterations+1;j++)
//Loop to find the sum of individual position values
{
decemal=decemal*2;
}
final_decemal=final_decemal+decemal;
}
}
out.println("The decimal value corresponding to the string binary "+stringBinary+" is "+ final_decemal);
}
else
{
System.out.println("Invalid Input");
}
}

public static boolean Binary(String stringBinary)
{
  
int number = Integer.parseInt(stringBinary);
while (number != 0)
{
if (number % 10 > 1)
{
return false;
}
number = number / 10;
} return true;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote