-1

I am new to Java. I would like to test the methods available in the Calculator. What could be the most efficient way you could test this?

package BeginnerLevel;

import java.util.Scanner;

public class Calculator {

This is the constructor:

public Calculator(){


  while (true){
      System.out.println("Personal Calculator\n"+
      "Addition---> 1 \n"+"Subtraction---> 2\n"+"Multiplication---> 3\n"+"Division---> 4\n"+"Exit--->0");
       Scanner input = new Scanner(System.in);

       int UsrIn= input.nextInt();

       input.close();


      switch (UsrIn){

      case 1: 
          Addition();
          break;
      case 2: 
          Subtraction();
          break;
      case 3:
          Multiplication();
          break;

      case 4:
          Division();
          break;

      case 0:
          break;  
      }


  }
}

These are the methods that are called in the switch case.

Addition method:

private void Addition(){

     Scanner input= new Scanner(System.in);

     double total=0;
     double firstnum=0;
     double secondnum=0;

     System.out.println("Enter Fist number: ");
     firstnum= input.nextDouble();

     System.out.println("Enter Fist number: ");
     secondnum=input.nextDouble();
    input.close();

     total= firstnum + secondnum;

    System.out.println("The result of Addition for "+ firstnum +" + "+secondnum+" is "+total);
}

Subtraction method:

private void Subtraction(){

    Scanner input= new Scanner(System.in);

    double total=0;
    double first=0;
    double second=0;

    System.out.println("Enter First number ");
    first=input.nextDouble();

    System.out.println("Enter Second number ");
    second=input.nextDouble();

    total= first-second;
    System.out.println("The result of Addition for "+ first +" + "+second+" is "+total);

    input.close();

}

Multiplication method:

private void Multiplication(){
Scanner input= new Scanner(System.in);

    double total=0;
    double first=0;
    double second=0;

    System.out.println("Enter First number ");
    first=input.nextDouble();

    System.out.println("Enter Second number ");
    second=input.nextDouble();

    total= first*second;
    System.out.println("The result of Addition for "+ first +" + "+second+" is "+total);

    input.close();
}

Division Method:

private void Division(){
    Scanner input= new Scanner(System.in);
    double total=0;
    double first=0;
    double second=0;

    System.out.println("Enter First number ");

    first=input.nextDouble();

    System.out.println("Enter Second number ");
    second=input.nextDouble();

    total= first*second;
    System.out.println("The result of Addition for "+ first +" + "+second+" is "+total);

    input.close();

} 

}

Any recommendation is appreciated !

Subash J
  • 1,901
  • 2
  • 11
  • 24
Klayd Pro
  • 1
  • 1
  • 2
  • 7
  • You should research into what types of testing is available in java, e.g jUnit testing. Showing your code won't help. What are you testing for etc. 'Here are my methods' aren't very helpful – someguy76 Apr 03 '18 at 10:53

2 Answers2

1

You should try Junit test suite in your program. Junit Or you can use debug model to find out what happends.

roomy
  • 27
  • 8
1

There is quite a bit wrong with your code. And much of the "wrongness" is related to things that will make it difficult to test in a systematic way.

In a good OO design, a method will do one task and one task only. But your methods are mixing multiple tasks:

  • requesting and reading parameters,
  • performing an arithmetic operation,
  • printing a result.

Your constructor the constructor is even worse. A constructor should construct / initialize an object. It should not contain the "business logic" of the object. Refactor the class so that the business logic is in methods ... then instantiate the class and call the methods.

Ad @roomy states, the correct way to do testing is to develop a suite of unit tests using a framework like JUnit. But your application needs to be designed to be testable ... by calling the methods and checking that they produce the correct results.

Testing that involves input and output is more difficult, but it is possible provided that you make the input and output streams parameters. The other alternative is to create a "system test" where you create an input file (or set of input files) and corresponding expected output files, run the program with each input and check that the output matches the expected output. Hint: use shell level file redirection ...


In addition to the points above, there are couple of mistakes that are liable bite you:

  1. You should never use a method name that starts with a capital letter. It goes against accepted Java style ... and people will complain at you, and dock your style marks.

  2. You need to be very careful about closing System.in, etcetera. Once they are closed, you won't be able to reopen them, or read from / write to them.

  3. Also beware of discarding a Scanner that may have read ahead data from the underlying input source.

Stephen C
  • 632,615
  • 86
  • 730
  • 1,096