Sam C.
Sam C.

Reputation: 31

Multi Class program

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.

Upvotes: 2

Views: 1426

Answers (3)

Sparker0i
Sparker0i

Reputation: 1861

Do not have any parameter (in this program, the String[] args parameter) for the function inside the four operator classes. Your program should work without them.

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

Same applies for other classes too.

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

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

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

Upvotes: 0

Jacob Tomaw
Jacob Tomaw

Reputation: 1391

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.");
}

Upvotes: 0

Thierry Roy
Thierry Roy

Reputation: 8512

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)

Upvotes: 3

Related Questions