Problem:
Design an algorithm that accepts as input a decimal fraction and converts it to the corresponding binary fraction of a fixed accuracy.
Solution:
package com.myprograms;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DecimalFractionToBinaryFraction {
static double number;
List<Integer> integralPart = new ArrayList<Integer>();
List<Integer> fractionalPart = new ArrayList<Integer>();
int precesionLimit;
public static void main(String[] args) {
DecimalFractionToBinaryFraction decimalFractionToBinaryFraction = new DecimalFractionToBinaryFraction();
decimalFractionToBinaryFraction.getTheNumber();
decimalFractionToBinaryFraction.convertToBinaryFraction();
String result = decimalFractionToBinaryFraction.getTheIntegralPartResult() + "." + decimalFractionToBinaryFraction.getTheFractionalPartResult();
System.out.println("the converted number is: " + result);
}
public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("enter the decimal fraction: ");
number = s.nextDouble();
System.out.println("enter the precesion limit value: ");
precesionLimit = s.nextInt();
s.close();
}
public void convertToBinaryFraction(){
String[] numbers = new Double(number).toString().split("\\.");
int q = Integer.parseInt(numbers[0]);
for(int i = 0; q!= 0; i++){
integralPart.add(q%2);
q=q/2;
}
int fractionalNumber = Integer.parseInt(numbers[1]);
double fractionalNumber2 = Double.parseDouble("0." + fractionalNumber);
String[] fractionalNumbers;
for(int k = 1; k <= precesionLimit; k++){
fractionalNumber2 = fractionalNumber2* 2;
fractionalNumbers = new Double(fractionalNumber2).toString().split("\\.");
fractionalPart.add(Integer.parseInt(fractionalNumbers[0]));
fractionalNumber2 = Double.parseDouble("0." + fractionalNumbers[1]);
}
}
public int getTheIntegralPartResult(){
int number = 0;
for(int i = integralPart.size() - 1; i >= 0; i--){
number = number * 10 + (integralPart.get(i));
}
return number;
}
public int getTheFractionalPartResult(){
int number = 0;
for(int i = 0; i <fractionalPart.size(); i++){
number = number * 10 + (fractionalPart.get(i));
}
return number;
}
}
Output:
enter the decimal fraction:
6.986
enter the precesion limit value:
8
the converted number is: 110.11111100
Design an algorithm that accepts as input a decimal fraction and converts it to the corresponding binary fraction of a fixed accuracy.
Solution:
package com.myprograms;
import java.io.ObjectInputStream.GetField;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class DecimalFractionToBinaryFraction {
static double number;
List<Integer> integralPart = new ArrayList<Integer>();
List<Integer> fractionalPart = new ArrayList<Integer>();
int precesionLimit;
public static void main(String[] args) {
DecimalFractionToBinaryFraction decimalFractionToBinaryFraction = new DecimalFractionToBinaryFraction();
decimalFractionToBinaryFraction.getTheNumber();
decimalFractionToBinaryFraction.convertToBinaryFraction();
String result = decimalFractionToBinaryFraction.getTheIntegralPartResult() + "." + decimalFractionToBinaryFraction.getTheFractionalPartResult();
System.out.println("the converted number is: " + result);
}
public void getTheNumber(){
Scanner s = new Scanner(System.in);
System.out.println("enter the decimal fraction: ");
number = s.nextDouble();
System.out.println("enter the precesion limit value: ");
precesionLimit = s.nextInt();
s.close();
}
public void convertToBinaryFraction(){
String[] numbers = new Double(number).toString().split("\\.");
int q = Integer.parseInt(numbers[0]);
for(int i = 0; q!= 0; i++){
integralPart.add(q%2);
q=q/2;
}
int fractionalNumber = Integer.parseInt(numbers[1]);
double fractionalNumber2 = Double.parseDouble("0." + fractionalNumber);
String[] fractionalNumbers;
for(int k = 1; k <= precesionLimit; k++){
fractionalNumber2 = fractionalNumber2* 2;
fractionalNumbers = new Double(fractionalNumber2).toString().split("\\.");
fractionalPart.add(Integer.parseInt(fractionalNumbers[0]));
fractionalNumber2 = Double.parseDouble("0." + fractionalNumbers[1]);
}
}
public int getTheIntegralPartResult(){
int number = 0;
for(int i = integralPart.size() - 1; i >= 0; i--){
number = number * 10 + (integralPart.get(i));
}
return number;
}
public int getTheFractionalPartResult(){
int number = 0;
for(int i = 0; i <fractionalPart.size(); i++){
number = number * 10 + (fractionalPart.get(i));
}
return number;
}
}
Output:
enter the decimal fraction:
6.986
enter the precesion limit value:
8
the converted number is: 110.11111100
No comments:
Post a Comment