Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8487707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:19:28+00:00 2026-06-10T21:19:28+00:00

Just out of curiosity! If I got a method that takes user input and

  • 0

Just out of curiosity! If I got a method that takes user input and stores it as an int variable. Is there a way I could pass those methods on to another method? I know about return values, but that is only (as far as I know) for one single number,object etc…

The easy way would of course to just to do calculate the result within this method, but is it possible somehow to pass these ints: a,b. To another method, without using global variables? I am just wondering.

private void takeInput(int valueA, int valueB){
    println("Enter a number: (a) and (b), to calculate the Phytagoeran Theorem: (c)\n");
    int a = readInt("a:");
    println("you set the value of a to: "+a +"\n\nNext! set the value of b");
    int b = readInt("b:");
    println("you set the value of b to:"+b);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T21:19:30+00:00Added an answer on June 10, 2026 at 9:19 pm

    The question is unclear, but I wonder whether you’ve invented “dependency injection”?

    The first part of the puzzle is that, yes, a method can return more than one value, by wrapping them in an object.

    public class ValuePair {
        public int a;
        public int b;
    }
    
    public ValuePair takeInput(){
      ValuePair pair = new ValuePair();
      println("Enter a number: (a) and (b)\n");
      pair.a = readInt("a:");
      pair.b = readInt("b:");
      return pair;
    }
    

    And with that you can do:

    ValuePair p = takeInput();
    System.out.println("Hypotenuse is " + calculateHypotenuse(p.a, p.b) + "\n";
    

    (Note that public fields are not usually considered good style – I use them here because the code is shorter. In real code use getters and setters).


    That might be all the answer you need. But you ask:

    Is there a way I could pass those methods on to another method?

    Well, yes you can if you wrap that method in a class dedicated the purpose. For reasons that I hope will become apparent, we’ll define an interface first.

     public interface ValuePairSource {
        public ValuePair getPair();
    }
    

    … and a class that implements it:

    public class UserInputValuePairSource implements ValuePairSource {
        @Override
        public ValuePair getPair() {
             ValuePair pair = new ValuePair();
             println("Enter a number: (a) and (b)\n");
             pair.a = readInt("a:");
             pair.b = readInt("b:");
             return pair;
        }
    }
    

    Now you could create a Pythagoras class that can be told where to get its values.

    public class Pythagoras {
         private ValuePairSource valuePairSource;
    
         public Pythagoras(ValuePairSource source) {
             this.valuePairSource = source;
         }
    
         public double hypotenuse() {
             ValuePair pair = valuePairSource.getPair();
             return Math.sqrt(pair.a ^ 2 + pair.b ^ 2)
         }
    }
    

    In the constructor, we tell each Pythagoras object we create, how to get a pair of values. So we can use it like this:

    Pythagoras p = new Pythagoras(new UserInputValuePairSource());
    double hypotenuse = p.hypotenuse();
    

    What use is this? Well, it means we can invent other kinds of ValuePairSource. For example, a ShapeValuePairSource, which gets the values from a Shape object we set it up with.

    (These are hypothetical classes for the purpose of the question)

    So you might use it like this:

    Triangle t = canvas.getLastTriangle(); // or whatever
    ValuePairSource vps = new ShapeValuePairSource(triangle);
    Pythagoras p = new Pythagoras(vps);
    double hypotenuse = p.hypotenuse();
    

    So without changing the Pythagoras class at all, we now have it reading the values from a shape object, rather than user input.

    OK, it’s a bit of a contrived example, but it does answer the question “could I pass those methods to another method” — and this is the basis of things like Spring.

    Note that some languages allow you to pass methods directly to other methods, without wrapping them in a class as we have here. This is called “functional programming”, and usually the “methods” are called “functions”.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way (just out of curiosity because I came across multiple same-value
Just out of curiosity - is there another way to obtain the platform's path
Just out of curiosity... Is there a way to add using to an ASPX/ASCX
Just out of curiosity I wanted to know if is there a way to
This is just out of curiosity. Is there any way we can make an
Just out of curiosity, how does iostream access the input-output system. (I have a
I know this is a stupid question. But just out of curiosity, is there
Just out of curiosity I tried overriding a abstract method in base class, and
Just out of curiosity, for applications that have a fairly complicated module tree, would
Just out of curiosity, does anyone know a better way of building the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.