Use java to write an application to convert decimal number to IEEE-754 Single Pr
ID: 3671817 • Letter: U
Question
Use java to write an application to convert decimal number to IEEE-754 Single Precision Floating-Point Representation (32-bit) and IEEE-754 Double Precision Floating-Point Representation (64-bit) and convert back to decimal. Use GUI as user interface asking the user for decimal number and the program will output the result.
Specification The IEEE-754 single precision floating point standard uses an 8-bit exponent (with a bias of 127) and a 23-bit significand. The IEEE-754 double precision standard uses an 11-bit exponent (with a bias of 1023) and a 52-bit significand.
This is what I have so far. I am new to using GUI so I don't really know how to do it.
import java.util.Scanner;
public class conversionS
{
private static Scanner scanInput;
public static void main(String[]args)
{
scanInput = new Scanner(System.in);
double decimalNum;
int valueBit;
String stringOfBits;
System.out.println("Please enter the decimal that needs to convert in single precision floating point");
decimalNum = scanInput.nextInt();
float f = (float)decimalNum;
valueBit = Float.floatToIntBits(f);
stringOfBits = Integer.toBinaryString(valueBit);
System.out.println("Single precision floating point:" + stringOfBits);
System.out.println("Please enter the decimal that needs to convert in double precision floating point");
decimalNum = scanInput.nextInt();
stringOfBits = Long.toString(Double.doubleToLongBits(decimalNum), 2);
System.out.println("double precision floating point:" + stringOfBits);
}
}
Explanation / Answer
import java.lang.Math; // header stuff MUST go above the first class
// our main class becomes a file but the main method is still found
import java.util.Scanner;
public class conversionS
{
private static Scanner scanInput;
public static void main(String[]args)
{
scanInput = new Scanner(System.in);
double decimalNum;
int valueBit;
String stringOfBits;
System.out.println("Please enter the decimal that needs to convert in single precision floating point");
decimalNum = scanInput.nextInt();
float f = (float)decimalNum;
valueBit = Float.floatToIntBits(f);
stringOfBits = Integer.toString(valueBit, 2);
System.out.println("Single precision floating point:" + stringOfBits);
System.out.println("Please enter the decimal that needs to convert in double precision floating point");
decimalNum = scanInput.nextInt();
stringOfBits = Double.toString(Double.doubleToLongBits(decimalNum), 2);
System.out.println("double precision floating point:" + stringOfBits);
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.