Search code examples
javaeclipseclasscalculatorcls

Calculator.java Review And Opinions


Here's A Java Calculator Program I Just Made Recently, But It Doesn't Meet My Expectations! I Want It In A More Convenient Way Like It Has 6 Classes And Some Exclamation Marks, I Wanna Get A+ So Please Help Me!

1) Can I loop the codes so after displaying the answer, It runs the code again? 2) Can I somehow decrease the number of classes and the length of codes? 3) Can I clear screen in the console like in C++, So it should display a separate view for the Intro and the answer?

Here's The Code:

import java.util.Scanner;
public class javaCalc {

public static void welcome() {
    System.out.println("Welcome to Calculator.java v0.1");
    System.out.println("(Developed By RAZ0229)");
}

public static void main(String[] args) {

    welcome();

    System.out.flush();
    System.out.println("\n1) Addition");
    System.out.println("2) Substraction");
    System.out.println("3) Multiplication");
    System.out.println("4) Division");
    System.out.println("\nChoose A Basic Operator:");

    Scanner operandOne = new Scanner(System.in);
    int inpOperation = operandOne.nextInt();

    switch(inpOperation) {
    case 1: additionMethod();
        break;

    case 2: substractionMethod();
        break;

    case 3: multiplicationMethod();
        break;

    case 4: divisionMethod();
        break;

    default: System.out.println("\n(Invalid Argument)");
        return;

    }

}

public static void additionMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne + numTwo;
     System.out.println(numOne + " + " + numTwo + " = " + answer);
}

public static void substractionMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne - numTwo;
     System.out.println(numOne + " - " + numTwo + " = " + answer);
}

public static void multiplicationMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne * numTwo;
     System.out.println(numOne + " x " + numTwo + " = " + answer);
}

public static void divisionMethod()  {
    Scanner operandOne = new Scanner(System.in);
    System.out.println("Enter The First Quantity:");
    float numOne = operandOne.nextFloat();
    System.out.println("Enter The Second Quantity:");
    float numTwo = operandOne.nextFloat();
    float answer = numOne / numTwo;
     System.out.println(numOne + " / " + numTwo + " = " + answer);
             }
}

Solution

  • You are asking for two floats in every method and using the same prints many times, so you can just create some method such as this and call it inside your operation method to stop repeating code (constantly repeated blocks of code is a strong indicator that the block can probably be abstracted into its own method):

    public static float[] getValues(){
       float[] values;
    
       /*Implement your logic here asking user for floats, then put into above array
       and do calculations in your methods using float array*/   
    
       return values;
    }
    

    You can also loop your main by wrapping it in a while loop and adding an extra case to your switch statement like so (if you would like to exit program, enter 5):

    public static void main(String[] args) {
    
      welcome();
    
      while (true){
        System.out.flush();
        System.out.println("\n1) Addition");
        System.out.println("2) Substraction");
        System.out.println("3) Multiplication");
        System.out.println("4) Division");
        System.out.println("5) Quit");
        System.out.println("\nChoose A Basic Operator:");
    
        Scanner operandOne = new Scanner(System.in);
        int inpOperation = operandOne.nextInt();
    
        switch(inpOperation) {
          case 1: additionMethod();
            break;
    
          case 2: substractionMethod();
            break;
    
          case 3: multiplicationMethod();
            break;
    
          case 4: divisionMethod();
            break;
    
          case 5: System.exit(0);
    
          default: System.out.println("\n(Invalid Argument)");
            return;
        }
      }
    }