Write a program in JAVA to read in a 32-bit pattern and translate to a IEEE-754
ID: 2247044 • 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, My SPECIFIED NEED is please use the import: import javax.swing.JOptionPane; for the input and output, then a loop to check and see if there are any special patterns, example: NaN, -infinity, infinity, ect... If not check it with something like this ..............
sign=(inputString.CharAt(0)=='0'?1:-1);
String expString=inputString,substring(1,8);
String mainString=inputSting.substring(9)
Relatively new to coding, so the simpler the better. That way I understand what I am doing , thanks
Explanation / Answer
JAVA CODE:
========================
package COOKBOOK;
import javax.swing.JOptionPane;
public class IEEE_754_2 {
public static void main(String[] args) {
String inputString,ieee_754;
inputString = JOptionPane.showInputDialog("Enter the 32bit number");
int sign=(inputString.charAt(0)=='0'?1:-1);
String expString=inputString.substring(1,9);
String mainString=inputString.substring(9);
int intExp = Integer.parseInt(expString, 2);
intExp = intExp -127;
//JOptionPane.showMessageDialog(null, ""+intExp);
int decimal_part=0;
double fraction =0.0;
for(int index=0; index <mainString.length();index++){
fraction = fraction + Integer.parseInt(""+mainString.charAt(index))*Math.pow(2,-1*(index+1));
}
//JOptionPane.showMessageDialog(null, ""+fraction);
ieee_754=""+(fraction+1)*sign*Math.pow(2,intExp);
JOptionPane.showMessageDialog(null,ieee_754);
}
}
============================
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.