Haris Mahmood
Haris Mahmood

Reputation: 21

Adding a log function to the java calculator program

I am building a personal scientific calculator. I need help to add the long function into the program with the compiler giving only one answer throughout program. At which part of the if-statement should I add the method?

I am a beginner by the way and this is one of the first projects I am working on.

public void OperatorIndicator(double x, double y, String op){
        if (op == "*") {
            multiply(x, y);
        } else if (op == "/") { 
            divide(x, y); 
        } else if (op == "+") {
            sum(x, y);
        } else if (op == "-") {
            difference(x, y);
        } else if (op == ""){
            if (y == 0) {
                squareroot(x);
            } else {
                exponent(x,y);
            }
        } else {
            System.out.println("OPERATOR NOT VALID");
        }
    }

I want the results to show only one value at all times

Upvotes: 2

Views: 412

Answers (2)

Yuri Chervonyi
Yuri Chervonyi

Reputation: 166

I don't know if you already know about it, but in your case, you can use pretty well statement like switch. It would look like:


public void OperatorIndicator(double x, double y, String op){

    switch(op) {
        case "*":   multiply(x, y);   break;
        case "/":   divide(x, y);     break;
        case "+":   sum(x, y);        break;
        case "-":   difference(x, y); break;
        case "^":   pow(x, y);        break;
        case "log": log(x, y);        break;
        // ...
        default: 
            System.out.println("OPERATOR NOT VALID");
    }
}

Upvotes: 1

Christian Westbrook
Christian Westbrook

Reputation: 339

You may add another condition to check at any point in this control structure.

For example, assuming that you are wanting to add a logarithmic function to your program, and assuming the method signature of that function contains log(double value, double base), you could add the statements...

else if (op == "log") {
    log(x, y);
}

...anywhere after your initial if statement and before the else statement. You could even replace your initial if statement with checking this condition and push your current if statement down into the series of else if statements if you so desired.

Given this level of freedom, your ultimate goal in designing this program should be to manage complexity such that someone else trying to read your code will easily understand your intentions. What I mean by this is that, although you may add this condition anywhere, you should place it where it will make the most sense.

In my opinion, seeing as your control structure first checks for the multiplication and division operators, and then for the addition and subtraction operators, followed by a check for the exponential/square-root operator, I would place the check for a logarithmic operator just after your check for the exponential/square-root operator, and just before your else statement.

I'd also advise separating the exponential/square-root operation into two separate else if statements, and defining an op code for both of these operations rather than using an empty op code to target those operations. I advise this not only for the sake of clarity and readability in your code; there is another reason.

For someone to understand that they should pass in the String "" as an op code to this program, they would need to understand its inner workings. A big part of object-oriented programming is 'information hiding', or attempting to make your methods and classes understandable without needing to know how they are implemented. Assume that someone wants to use this program without reading its inner workings. If no one else is going to be using this program, assume that you want to use this program a year from now, and you don't want to have to read through the program again to understand what it's doing. You just want to pass in two values and an op code and receive the value you want.

In this scenario a more user-friendly implementation would be to give each possible inner operation a clearly defined op code. You could provide a list of op codes and a description of their functions as documentation for reference by your users, even if that just means yourself.

As a small justification, imagine a user accidentally attempting to pass the String "" into this method as the op code. The ideal response in this scenario would be to inform the user that an invalid op code has been passed in to the method, but in your current implementation the method would either perform a square-root or exponential operation, depending on the values passed in. If this behavior is unexpected by the user, they will likely be confused and will probably waste some time trying to understand why this method isn't behaving like they would expect it to behave.

Another small suggestion would be to use the String comparison function equals() to compare the passed in op code to each value you're checking. The documentation for this function can be found here, and a revised version of your program would look like this:

public void OperatorIndicator(double x, double y, String op){
    if (op.equals("+")) {
        multiply(x, y);
    } else if (op.equals("/")) { 
        divide(x, y); 
    } else if (op.equals("+")) {
        sum(x, y);
    } else if (op.equals("-")) {
        difference(x, y);
    } else if (op.equals("")){
        if (y == 0) {
            squareroot(x);
        } else {
            exponent(x,y);
        }
    } else {
        System.out.println("OPERATOR NOT VALID");
    }
}

Upvotes: 2

Related Questions