patternjavaModerate
Binary Calculator in Java
Viewed 0 times
calculatorbinaryjava
Problem
I'm a beginner in Java and I tried to create a program that will perform basic arithmetic operations on binary numbers.
```
package binaryoperations;
public class Operations {
public String GetBinarySum(String a, String b){
int left = Integer.parseInt(a, 2);
int right = Integer.parseInt(b, 2);
int sum = left + right;
return Integer.toBinaryString(sum);
}
public String GetBinaryDiff(String a, String b){
int left = Integer.parseInt(a, 2);
int right = Integer.parseInt(b, 2);
int diff = left - right;
return Integer.toBinaryString(diff);
}
public String GetBinaryProd(String a, String b){
int left = Integer.parseInt(a, 2);
int right = Integer.parseInt(b, 2);
int prod = left * right;
return Integer.toBinaryString(prod);
}
public String GetBinaryQu
BinaryOperations class:package binaryoperations;
import java.io.IOException;
import static java.lang.System.in;
import java.util.Scanner;
public class BinaryOperations {
public static void main(String[] args) throws IOException {
try(Scanner in = new Scanner(System.in)) {
Operations operation = new Operations();
System.out.print("First Binary: ");
String binOne = in.next();
System.out.print("Second Binary: ");
String binTwo = in.next();
System.out.println("Sum: " + operation.GetBinarySum(binOne, binTwo));
System.out.println("Difference: " + operation.GetBinaryDiff(binOne, binTwo));
System.out.println("Product: " + operation.GetBinaryProd(binOne, binTwo));
System.out.println("Quotient: " + operation.GetBinaryQuotient(binOne, binTwo));
} catch(NumberFormatException e) {
System.out.println("Looks like you entered a non Binary digit.");
} finally {
in.close();
}
}
}Operations class:```
package binaryoperations;
public class Operations {
public String GetBinarySum(String a, String b){
int left = Integer.parseInt(a, 2);
int right = Integer.parseInt(b, 2);
int sum = left + right;
return Integer.toBinaryString(sum);
}
public String GetBinaryDiff(String a, String b){
int left = Integer.parseInt(a, 2);
int right = Integer.parseInt(b, 2);
int diff = left - right;
return Integer.toBinaryString(diff);
}
public String GetBinaryProd(String a, String b){
int left = Integer.parseInt(a, 2);
int right = Integer.parseInt(b, 2);
int prod = left * right;
return Integer.toBinaryString(prod);
}
public String GetBinaryQu
Solution
Style
I'm sorry for making it personal here, camelCase, but the preferred styling for function names is that they're, well, camelCased. (Apologies for the bad pun). So not
Removing the Operations class
I think your approach for implementation of
You're taking two Strings, converting them to numbers, performing a simple operation and converting it to String again.
This by itself is not a problem, but...
Here you're doing the following:
If you instead converted the Strings directly after reading, you could do input validation per input ("hey, you entered a 2 for your second binary number!"), and you'd save on doing all this conversion. You'd convert once, do the math, convert back 4 times (4 different results), and done.
Something like this:
Here's an implementation without error handling:
And poof! The entire
Why have an Operations class
Now, if this were some super advanced calculator which has to do more than just do basic math, it'd help to have the Operations class.
For example, "count all the positive bits in the number" or "detect if this binary number is a square". Then you'd be better served by an operations class that takes
Consider why the Java creators made a
About use of Integer.parseInt with radix
Use built-ins when you can.
Built-ins apply to a larger populace than your custom code. Built-ins have been tested by the people who create the Java language. And above all, built-ins don't require you to implement them yourself.
When the resulting code is written faster, has a greater chance of being correct and uses components people are familiar with, why wouldn't you use built-in functions?
Oh, and in case you forgot what they do, they come with documentation too. So it's easy to remind yourself of how the built-ins work. Custom functions require you to re-read the code if you've forgotten and there's no documentation.
I'm sorry for making it personal here, camelCase, but the preferred styling for function names is that they're, well, camelCased. (Apologies for the bad pun). So not
GetBinarySum, but getBinarySum.Removing the Operations class
I think your approach for implementation of
Operations is flawed.You're taking two Strings, converting them to numbers, performing a simple operation and converting it to String again.
This by itself is not a problem, but...
System.out.println("Sum: " + operation.GetBinarySum(binOne, binTwo));
System.out.println("Difference: " + operation.GetBinaryDiff(binOne, binTwo));
System.out.println("Product: " + operation.GetBinaryProd(binOne, binTwo));
System.out.println("Quotient: " + operation.GetBinaryQuotient(binOne, binTwo));Here you're doing the following:
read two Strings
convert to ints
sum
convert to binary string
print
convert to ints
diff
convert to binary string
print
convert to ints
prod
convert to binary string
print
convert to ints
quotient
convert to binary string
printIf you instead converted the Strings directly after reading, you could do input validation per input ("hey, you entered a 2 for your second binary number!"), and you'd save on doing all this conversion. You'd convert once, do the math, convert back 4 times (4 different results), and done.
Something like this:
read two Strings
convert to ints
sum
convert to binary string
print
diff
convert to binary string
print
prod
convert to binary string
print
quotient
convert to binary string
printHere's an implementation without error handling:
int left = Integer.parseInt(binOne, 2);
int right = Integer.parseInt(binTwo, 2);
System.out.println("Sum: " + Integer.toBinaryString(left + right));
System.out.println("Difference: " + Integer.toBinaryString(left - right));
System.out.println("Product: " + Integer.toBinaryString(left * right));
System.out.println("Quotient: " + Integer.toBinaryString(left / right));And poof! The entire
Operations class is gone.Why have an Operations class
Now, if this were some super advanced calculator which has to do more than just do basic math, it'd help to have the Operations class.
For example, "count all the positive bits in the number" or "detect if this binary number is a square". Then you'd be better served by an operations class that takes
int(s) and returns int or boolean. You can later convert this to a String. If you write the implementations like that, your BinaryOperations (which needs a better name) class would be responsible for handling input and output (Like the screens and the buttons of a calculator) and the Operations class would be responsible for doing the actual calculating.Consider why the Java creators made a
java.lang.Math class that takes integers, not Strings and it should be easier to understand how to split responsibilities.About use of Integer.parseInt with radix
Use built-ins when you can.
Built-ins apply to a larger populace than your custom code. Built-ins have been tested by the people who create the Java language. And above all, built-ins don't require you to implement them yourself.
When the resulting code is written faster, has a greater chance of being correct and uses components people are familiar with, why wouldn't you use built-in functions?
Oh, and in case you forgot what they do, they come with documentation too. So it's easy to remind yourself of how the built-ins work. Custom functions require you to re-read the code if you've forgotten and there's no documentation.
Code Snippets
System.out.println("Sum: " + operation.GetBinarySum(binOne, binTwo));
System.out.println("Difference: " + operation.GetBinaryDiff(binOne, binTwo));
System.out.println("Product: " + operation.GetBinaryProd(binOne, binTwo));
System.out.println("Quotient: " + operation.GetBinaryQuotient(binOne, binTwo));read two Strings
convert to ints
sum
convert to binary string
print
convert to ints
diff
convert to binary string
print
convert to ints
prod
convert to binary string
print
convert to ints
quotient
convert to binary string
printread two Strings
convert to ints
sum
convert to binary string
print
diff
convert to binary string
print
prod
convert to binary string
print
quotient
convert to binary string
printint left = Integer.parseInt(binOne, 2);
int right = Integer.parseInt(binTwo, 2);
System.out.println("Sum: " + Integer.toBinaryString(left + right));
System.out.println("Difference: " + Integer.toBinaryString(left - right));
System.out.println("Product: " + Integer.toBinaryString(left * right));
System.out.println("Quotient: " + Integer.toBinaryString(left / right));Context
StackExchange Code Review Q#87830, answer score: 13
Revisions (0)
No revisions yet.