N-DEV
N-DEV

Reputation: 37

Java: Failed test with Calculator sumTwoNumbers, subtractTwoNumbers, divideTwoNumbers and multiplyTwoNumbers

Task description: In a template below you have a simple calculator that subtracts, sums, divides, and multiplies the two numbers inside the switch statement. Now, we've decided to upgrade it to perform more complex tasks, such as logarithmic functions. For that, separate methods are a better solution. Let's start with decomposing what we have.

Take a look at the template and decompose operations of the calculator into four methods: subtractTwoNumbers(long a, long b) for subtraction, sumTwoNumbers(long a, long b) for adding, divideTwoNumbers(long a, long b) for division and multiplyTwoNumbers(long a, long b) for multiplication. Each method should print the result of calculations.

Note that you can't divide by zero. In case your second argument is zero, you should print the "Division by 0!" message.

I am trying to create a calculator and get the following errors:

FEEDBACK Failed test #1 of 6. Wrong answer

This is a sample test from the problem statement!

Test input: 1 + 2 Correct output: 3

Your code output:

import java.util.Scanner;

class Main {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        long num1 = scanner.nextLong();
        String operator = scanner.next();
        long num2 = scanner.nextLong();


        long result = 0;
        switch (operator) {
            case "-":
                result = subtractTwoNumbers(num1, num2);
                break;
            case "+":
                result = sumTwoNumbers(num1, num2);
                break;
            case "/":
                if (num2 == 0) {
                    System.out.println("Division by 0!");
                } else {
                    result = divideTwoNumbers(num1, num2);
                    break;
                }
            case "*":
                result = multiplyTwoNumbers(num1, num2);
                break;
            default:
                break;
        }
        System.out.println(result);
    }

    // Implement your methods here
    // Each method should print the result of calculations.
    //Note that you can't divide by zero.
    // In case your second argument is zero, you should print the "Division by 0!" message.
    public static long subtractTwoNumbers (long a, long b) { return a - b; }
    public static long sumTwoNumbers (long a, long b) {
        return a + b;
    }
    public static long divideTwoNumbers (long a, long b) {
        return a / b;
    }
    public static long multiplyTwoNumbers (long a, long b) {
        return a * b;
    }
}

Can you please tell me what I did wrong there? Thanks & BR enter image description here

Upvotes: 0

Views: 275

Answers (2)

N-DEV
N-DEV

Reputation: 37

i finally got the correct answer :-)

import java.util.Scanner;
class SimpleCalculator {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        long num1 = scanner.nextLong();
        String operator = scanner.next();
        long num2 = scanner.nextLong();

        //long result = 0;
        switch (operator) {
            case "-":
                subtractTwoNumbers(num1, num2);
                break;
            case "+":
                sumTwoNumbers(num1, num2);
                //System.out.println(sumTwoNumbers(num1, num2));
                break;
            case "/":
                divideTwoNumbers(num1, num2);
                 break;
            case "*":
                multiplyTwoNumbers(num1, num2);
                break;
            default:
                break;
        }
        //System.out.println(result);
    }

    // Implement your methods here
    // Each method should print the result of calculations.
    //Note that you can't divide by zero.
    // In case your second argument is zero, you should print the "Division by 0!" message.
    public static void subtractTwoNumbers (long num1, long num2){
        System.out.println(num1 - num2);
    }
    public static void sumTwoNumbers (long num1, long num2){
        System.out.println(num1 + num2);
    }
    public static void divideTwoNumbers (long num1, long num2){
        if (num2 <= 0) {
            System.out.println("Division by 0!");
        } else
        System.out.println(num1 / num2);
    }
    public static void multiplyTwoNumbers (long num1, long num2){
        System.out.println(num1 * num2);
    }
}

Thanks a lot!!

Upvotes: 1

Lev M.
Lev M.

Reputation: 6269

It looks like you are submitting your code to an automated system like leetcode.com or codeforces.com

When working with such sites, you need to pay extra close attention to the program description and requirements, and make sure your code outputs exactly what they demand when they demand it.

Your code as you posted here would run fine on your home computer and produce correct results. It even makes sense, based on common practices, to have the print statement outside the methods, in main.

Normally, this design would be acceptable.

Unfortunately, that is not what the task required:
Each method should print the result of calculations.

This sentence is very important because it tells you how to implement the methods:
They should print their results themselves instead of returning them.

That is why the test shows you have no output - the testing mechanism the site uses probably does not run your main function, but calls your methods directly instead, to test only them.

So it ignores the return value, and just checks the output of the method.

Move all your printing and also the test for division by zero in to the appropriate methods to make them completely self contain and comply with the task requirements.

Upvotes: 1

Related Questions