user3240125
user3240125

Reputation: 31

Need help understanding how to connect an object from different class to main class in Java

I'm working on a project that has two classes. The main class outputs the data while the other class does all the calculations. Quick summary: The user inputs a lot of numbers (0-200) and then has to type a letter to stop input and calculate the numbers inputted.

The project that I'm working on says that the other class will have a method

public boolean inputInformation

that takes in a Scanner object passed in from main(). For some reason I'm having a brain fart and am having trouble understanding what that means (translating to code).

Extra Q: my other method public boolean addNumber takes in an integer grade <--- what does that mean in code?

Upvotes: 0

Views: 163

Answers (3)

Asim Mittal
Asim Mittal

Reputation: 101

Class A{

    public boolean method(int x){
        //do something to x
        return true;
    }

};

Class MainClass{

    public static void Main(){
        A someObject = new A();                     // create an object of class A
        boolean result = someObject.method(5);      // call the method of that object 
    }
};

Upvotes: 0

Asim Mittal
Asim Mittal

Reputation: 101

Your question isn't exactly clear, but I'll take a stab at answering it anyway. I'm guessing you're working on organizing your code through classes in Java. What you've mentioned is very common practice.

"The other class will have a Scanner object passed into it from main". What its asking you to do is create a class 'Scanner' which handles the input related operations in a function, 'inputInformation'. In your main function, create an object of this class and call the method to take in the input.

The 'boolean' return type probably indicates that you have to return true/false based on whether input has ended or not (by checking for that special character). The query about addNumber refers to the fact that you will be sending an integer to that function as a parameter when calling it.

I think you should try to explain your question more clearly, for anyone to give you a little more advice.

Upvotes: 1

NRaf
NRaf

Reputation: 7549

What it's saying is that you should create a Scanner object in the main() method. Your inputInformation method should take a Scanner object as a parameter, so you'd have to call that method from main() passing your Scanner object to it.

As for your second question, it's saying that it should have a parameter of type 'int'.

Upvotes: 0

Related Questions