views:

36

answers:

3

Hi I was attempting to create a calculator that can add subtract multiply and divide to challenge myself but find myself getting stuck around the switch part:(I will point out the errors within the switch message that say "The method addition etc(String[]) in the type addition etc is not applicable for the arguments ()." I believe the problem lies within the public void of the other classes.

Script:

public class ComputronCalc { public static void main(String args[]) {

    int mode;
    mode = 1;

    Addition ADD = new Addition();
    Subtraction SUB = new Subtraction();
    Multiplication MUL = new Multiplication();
    Division DIV = new Division();

    System.out.println("Hello welcome to the Computron fully functional calculator, coded by Samuel Cole, designed by Dwight Schrute.");

    switch(mode) {
    case 1:
        ADD.Addition();<-----------addition is underlined in red
        break;
    case 2:
        SUB.Subtraction();<-------------same
        break;
    case 3:
        MUL.Multiplication();<---------------same
        break;
    case 4:
        DIV.Division();<----------------same
        break;
    default:
        System.out.println("You have not selected a mode, do so by editing the mode variable in the source.");
    }
System.out.println("Thank you for choosing Computron.");
}

}

import java.util.Scanner;

public class Addition { public void Addition(String Args[]) { Scanner input = new Scanner(System.in);

    double fnum, snum, answer;
    System.out.println("Type the first number you desire to calculate.");
    fnum = input.nextDouble();
    System.out.println("Type the second number you desire to calculate.");
    snum = input.nextDouble();
    System.out.println("Calculating...");
    answer = fnum + snum;
    System.out.println(answer);

}

}

import java.util.Scanner;

public class Multiplication { public void Multiplication(String Args[]) { Scanner input = new Scanner(System.in);

    double fnum, snum, answer;
    System.out.println("Type the first number you desire to calculate.");
    fnum = input.nextDouble();
    System.out.println("Type the second number you desire to calculate.");
    snum = input.nextDouble();
    System.out.println("Calculating...");
    answer = fnum * snum;
    System.out.println(answer);

}

}

import java.util.Scanner;

public class Division { public void Division(String Args[]) { Scanner input = new Scanner(System.in);

    double fnum, snum, answer;
    System.out.println("Type the first number you desire to calculate.");
    fnum = input.nextDouble();
    System.out.println("Type the second number you desire to calculate.");
    snum = input.nextDouble();
    System.out.println("Calculating...");
    answer = fnum / snum;
    System.out.println(answer);

}

}

note: I'm using eclipse so each class is on like a different page.

+2  A: 

Your methods expect the argument "args", which you don't use. Remove it. For example:

public void Addition(String Args[]) {

becomes:

public void Addition() {

(by the way, your code does not follow Oracle Java Naming Convention)

Thierry-Dimitri Roy
A: 

You define a method which waits for a parameter, and calls it without any parameter. What result did you expect ?

Samuel_xL
A: 

While the acute problem is that you need to change your method signatures to not have any parameters or change the method invocation to send a parameter, I think there is a better solution you should concider.

Change the methods in your operation classes to be constructors:

public class Addition { 
    public Addition() {
    //...
    }
}

Then you do not need to instantiate all the operations for each run and the switch becomes:

switch(mode) {
case 1:
    Addition();
    break;
case 2:
    Subtraction();
    break;
case 3:
    Multiplication();
    break;
case 4:
    Division();
    break;
default:
    System.out.println("You have not selected a mode, do so by editing the mode variable in the source.");
}
Jacob Tomaw