Skip to content

Instantly share code, notes, and snippets.

@JustAnotherJavaProgrammer
Last active April 1, 2022 19:55
Show Gist options
  • Save JustAnotherJavaProgrammer/cfdba0150855ea2178959c9a53141fb9 to your computer and use it in GitHub Desktop.
Save JustAnotherJavaProgrammer/cfdba0150855ea2178959c9a53141fb9 to your computer and use it in GitHub Desktop.
Arithmetic example (April Fools)

What is this?

This is a collection of examples illustrating how one could program some simple arithmetic functions in Java, ranging from ugly to bad to good. The order from ugly to perfect is as follows:

  1. ugly
  2. terrible
  3. bad
  4. good
  5. better
  6. great
  7. perfect

Or was it the other way around? Anyways, happy April Fools' Day!

These also serve as examples in this post on Medium.

package bad.arithmetic;
public class Arithmetic {
public static int A = 0;
public static int B = 0;
public static int RESULT = 0;
public static void add() {
RESULT = A + B;
}
public static void subtract() {
RESULT = A - B;
}
public static void multiply() {
RESULT = A * B;
}
public static void divide() {
RESULT = A / B;
}
}
package bad.arithmetic;
public class Example {
public static void main(String[] args) {
Arithmetic.A = 6;
Arithmetic.B = 2;
Arithmetic.add();
System.out.println("6 + 2 = " + Arithmetic.RESULT);
Arithmetic.subtract();
System.out.println("6 - 2 = " + Arithmetic.RESULT);
Arithmetic.multiply();
System.out.println("6 * 2 = " + Arithmetic.RESULT);
Arithmetic.divide();
System.out.println("6 / 2 = " + Arithmetic.RESULT);
}
}
package better.arithmetic;
public class Arithmetic {
public final int a;
public final int b;
private final Operation operation;
private int result = 0;
public Arithmetic(int a, int b, Operation operation) {
this.a = a;
this.b = b;
this.operation = operation;
}
private void add() {
result = a + b;
}
private void subtract() {
result = a - b;
}
private void multiply() {
result = a * b;
}
private void divide() {
result = a / b;
}
public void calculate() {
switch (operation) {
case ADDITION:
add();
break;
case SUBTRACTION:
subtract();
break;
case MULTIPLICATION:
multiply();
break;
case DIVISION:
divide();
break;
default:
throw new IllegalStateException("Operation not supported");
}
}
public int getResult() {
return result;
}
public enum Operation {
ADDITION, SUBTRACTION, MULTIPLICATION, DIVISION;
}
}
package better.arithmetic;
import better.arithmetic.Arithmetic.Operation;
public class Example {
public static void main(String[] args) {
Arithmetic addition = new Arithmetic(6, 2, Operation.ADDITION);
addition.calculate();
System.out.println("6 + 2 = " + addition.getResult());
Arithmetic subtraction = new Arithmetic(6, 2, Operation.SUBTRACTION);
subtraction.calculate();
System.out.println("6 - 2 = " + subtraction.getResult());
Arithmetic multiplication = new Arithmetic(6, 2, Operation.MULTIPLICATION);
multiplication.calculate();
System.out.println("6 * 2 = " + multiplication.getResult());
Arithmetic division = new Arithmetic(6, 2, Operation.DIVISION);
division.calculate();
System.out.println("6 / 2 = " + division.getResult());
}
}
package good.arithmetic;
public class Arithmetic {
public final int a;
public final int b;
private int result = 0;
public Arithmetic(int a, int b) {
this.a = a;
this.b = b;
}
public void add() {
result = a + b;
}
public void subtract() {
result = a - b;
}
public void multiply() {
result = a * b;
}
public void divide() {
result = a / b;
}
public int getResult() {
return result;
}
}
package good.arithmetic;
public class Example {
public static void main(String[] args) {
Arithmetic arithmetic = new Arithmetic(6, 2);
arithmetic.add();
System.out.println("6 + 2 = " + arithmetic.getResult());
arithmetic.subtract();
System.out.println("6 - 2 = " + arithmetic.getResult());
arithmetic.multiply();
System.out.println("6 * 2 = " + arithmetic.getResult());
arithmetic.divide();
System.out.println("6 / 2 = " + arithmetic.getResult());
}
}
package great.arithmetic;
public abstract class Arithmetic {
public final int a;
public final int b;
protected int result = 0;
public Arithmetic(int a, int b) {
this.a = a;
this.b = b;
}
public abstract void calculate();
public int getResult() {
return result;
}
public static class Addition extends Arithmetic {
public Addition(int a, int b) {
super(a, b);
}
@Override
public void calculate() {
result = a + b;
}
}
public static class Subtraction extends Arithmetic {
public Subtraction(int a, int b) {
super(a, b);
}
@Override
public void calculate() {
result = a - b;
}
}
public static class Multiplication extends Arithmetic {
public Multiplication(int a, int b) {
super(a, b);
}
@Override
public void calculate() {
result = a * b;
}
}
public static class Division extends Arithmetic {
public Division(int a, int b) {
super(a, b);
}
@Override
public void calculate() {
result = a / b;
}
}
}
package great.arithmetic;
public class Example {
public static void main(String[] args) {
Arithmetic addition = new Arithmetic.Addition(6, 2);
addition.calculate();
System.out.println("6 + 2 = " + addition.getResult());
Arithmetic subtraction = new Arithmetic.Subtraction(6, 2);
subtraction.calculate();
System.out.println("6 - 2 = " + subtraction.getResult());
Arithmetic multiplication = new Arithmetic.Multiplication(6, 2);
multiplication.calculate();
System.out.println("6 * 2 = " + multiplication.getResult());
Arithmetic division = new Arithmetic.Division(6, 2);
division.calculate();
System.out.println("6 / 2 = " + division.getResult());
}
}
package perfect.arithmetic;
public class Arithmetic {
public final int a;
public final int b;
public Arithmetic(int a, int b) {
this.a = a;
this.b = b;
}
public static interface Calculation {
public abstract int getResult();
}
public class Addition implements Calculation {
private final int result;
public Addition() {
this.result = a + b;
}
@Override
public int getResult() {
return result;
}
}
public class Subtraction implements Calculation {
private final int result;
public Subtraction() {
this.result = a - b;
}
@Override
public int getResult() {
return result;
}
}
public class Multiplication implements Calculation {
private final int result;
public Multiplication() {
this.result = a * b;
}
@Override
public int getResult() {
return result;
}
}
public class Division implements Calculation {
private final int result;
public Division() {
this.result = a + b;
}
@Override
public int getResult() {
return result;
}
}
}
package perfect.arithmetic;
import perfect.arithmetic.Arithmetic.Calculation;
public class Example {
public static void main(String[] args) {
Arithmetic arithmetic = new Arithmetic(6, 2);
Calculation addition = arithmetic.new Addition();
System.out.println("6 + 2 = " + addition.getResult());
Calculation subtraction = arithmetic.new Subtraction();
System.out.println("6 - 2 = " + subtraction.getResult());
Calculation multiplication = arithmetic.new Multiplication();
System.out.println("6 * 2 = " + multiplication.getResult());
Calculation division = arithmetic.new Division();
System.out.println("6 / 2 = " + division.getResult());
}
}
package terrible.arithmetic;
public class Arithmetic {
public static int A = 0;
public static int B = 0;
public static int add() {
return A + B;
}
public static int subtract() {
return A - B;
}
public static int multiply() {
return A * B;
}
public static int divide() {
return A / B;
}
}
package terrible.arithmetic;
public class Example {
public static void main(String[] args) {
Arithmetic.A = 6;
Arithmetic.B = 2;
System.out.println("6 + 2 = " + Arithmetic.add());
System.out.println("6 - 2 = " + Arithmetic.subtract());
System.out.println("6 * 2 = " + Arithmetic.multiply());
System.out.println("6 / 2 = " + Arithmetic.divide());
}
}
package ugly.arithmetic;
/**
* Basic arithmetic methods
*/
public class Arithmetic {
/**
* Adds two numbers
*
* @param a The first summand
* @param b The second summand
* @return The sum of the two numbers
*/
public static int add(int a, int b) {
return a + b;
}
/**
* Subtracts one number from the other
*
* @param a The minuend
* @param b The subtrahend
* @return The difference between the two numbers
*/
public static int subtract(int a, int b) {
return a - b;
}
/**
* Multiplies two numbers
*
* @param a The fist factor
* @param b The second factor
* @return The product of the two numbers
*/
public static int multiply(int a, int b) {
return a * b;
}
/**
* Divides one number by the other
*
* @param a The dividend
* @param b The divisor
* @return The quotient of the two numbers
*/
public static int divide(int a, int b) {
return a / b;
}
}
package ugly.arithmetic;
public class Example {
public static void main(String[] args) {
System.out.println("6 + 2 = " + Arithmetic.add(6, 2));
System.out.println("6 - 2 = " + Arithmetic.subtract(6, 2));
System.out.println("6 * 2 = " + Arithmetic.multiply(6, 2));
System.out.println("6 / 2 = " + Arithmetic.divide(6, 2));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment