patternjavaMinor
Converting a number to binary and back as a test in Java
Viewed 0 times
numberjavabackbinarytestandconverting
Problem
This is one of my first takes on Java, coming mainly from Matlab I found I had many things to learn.
The code will try to convert a number into an array of ones and zeros and then do the opposite returning you the original number (or at least something very close to it, as it is hard to return the exact number). It is not tested very well, so I can only tell that it is symmetric and not that it is correct.
It would be nice to know any errors I have made, or things that I could improve. An idea I had was using BigInt, I have read about it but not yet used it.
If you could focus on the language specific issues and kindly point them out I would be grateful.
The code will try to convert a number into an array of ones and zeros and then do the opposite returning you the original number (or at least something very close to it, as it is hard to return the exact number). It is not tested very well, so I can only tell that it is symmetric and not that it is correct.
It would be nice to know any errors I have made, or things that I could improve. An idea I had was using BigInt, I have read about it but not yet used it.
If you could focus on the language specific issues and kindly point them out I would be grateful.
package baseconversion;
import java.lang.Math;
import java.util.Arrays;
public class FloatToBin{
public static double pow2(double a, double b) {
return a * (java.lang.Math.pow(2, b));
}
public static double rem(double a, double b) {
return a % b;
}
public static double fix(double val) {
if (val = -m; ii--) {
temp[revCount] = pow2(1, (double)ii);
count--;
revCount++;
}
origVal = vConv(out, temp);
System.out.println("Original number is:");
System.out.println(a);
System.out.println("Binary representation is:");
System.out.println(Arrays.toString(out));
System.out.println("Inverting we get:");
System.out.println(origVal);
}
}Solution
Cryptic names
Please give longer and more descriptive names to n and m
Overworked main
I would define outside of main
So that main can contain the IO only.
int n = 10;
int m = 20;Please give longer and more descriptive names to n and m
Overworked main
I would define outside of main
public static double FloatToBin(float n) {
}
public static double BinToFloat(bin n) {
}So that main can contain the IO only.
Code Snippets
int n = 10;
int m = 20;public static double FloatToBin(float n) {
}
public static double BinToFloat(bin n) {
}Context
StackExchange Code Review Q#87952, answer score: 4
Revisions (0)
No revisions yet.