Write a program in JAVA to read in a 32-bit pattern and translate to a IEEE-754
ID: 2247869 • Letter: W
Question
Write a program in JAVA to read in a 32-bit pattern and translate to a IEEE-754 floating point number. Do not use library routines. Be sure to look for infinity, -infinity, zero and NaN patterns. ( This is what I NEED, please use the import: import javax.swing.JOptionPane; for the input and outputs, a string to turn the int to binary,then a loop to check and see if there are any special patterns, example: NaN, -infinity, infinity, if there isnt any special patterns check with something else.
Need help thank you
Explanation / Answer
Hi,
I have coded the required program.The code works as follows:-
The code will first get the number from user from Joption pane.Then it will convert the number to IEEE 754 format and will display through joption pane only.
Next after converting and displaying it will go for checks :-
Whether the number is infinite
whether the number is NaN.
BELOW IS THE CODE:-
=================================================================================
import java.util.*;
import java.lang.*;
import java.io.*;
import javax.swing.JOptionPane;
public class main
{
// change the decimal value to IEEE 754 FORMAT
private static String GetBinary32( float value )
{
int intValue = Float.floatToIntBits(value); //will convert to binary IEEE 754
String binaryValue = Integer.toBinaryString(intValue);
return binaryValue;
}
//program starts from here.
public static void main (String[] args) throws java.lang.Exception
{
String num_toconvert;
num_toconvert = JOptionPane.showInputDialog("Enter Number");
String string1 = GetBinary32((float) num_toconvert );
System.out.println( "Binary equivalent is" +string1);
OptionPane.showMessageDialog( null, string1,JOptionPane.PLAIN_MESSAGE);
// TO CHECK FOR NAN,INFINITE
float F1 = (float)num_toconvert;
OptionPane.showMessageDialog( null, "The number is infinite "+F1.isInfinite(),JOptionPane.PLAIN_MESSAGE );
OptionPane.showMessageDialog( null, "The number is Not a Number NaN "+F1.isNaN(),JOptionPane.PLAIN_MESSAGE );
System.exit(0);
}
}
=======================================================================================
Please let me know in case of any queries.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.