Java OOP functionality between objects

4146 views java
-3

I have a problem with solving exercise

There are two classes ready in the exercise: ObjectFunctionality and Calculator. Your task is to create the class Printer which asks for two integers from the user, calls the method Sum() of class Calculator relaying the inputted integers to the method. Method Sum() counts the sum of the numbers and returns the result. Finally Sum() prints the sum on screen.

import java.util.Scanner;


public class ObjectFunctionality {
    public static void main(String args[]) {
        Printer thing = new Printer();
        thing.Print();
    }
}

//  Write the missing class here
//  Class is written in the text box below.

class Calculator {
    static int Sum(int first, int second) {
        int sum = first + second;
        return sum;
    }
}

Here is my code:

class Printer {
        Scanner input = new Scanner(System.in);
    System.out.print("Type in the first integer: ");
    int first = reader.nextInt();
    System.out.print("Type in the second integer: ");
    int second = reader.nextInt();


    System.out.print("Sum of the numbers: " + sum.sum);


}

I know that something is very wrong, but I cant solve this.

PS. I need an output like this:

Type in the first integer: -3 Type in the second integer: -1 Sum of the numbers: -4

Thanks a lot!

answered question

What's your question? It isn't clear what specifically you're asking.

1 Answer

13

You need to create method inside the class like this:

class Printer {
   void Print() {
    //implementation
   }
}

And you are not calling the Sum() method correctly. Try:

System.out.print("Sum of the numbers: " + Calculator.Sum(first, second));

posted this

Have an answer?

JD

Please login first before posting an answer.