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 806993
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T00:20:01+00:00 2026-05-15T00:20:01+00:00

I have this Python code that I found online and would like to know

  • 0

I have this Python code that I found online and would like to know how to translate it to Java. My question is not about the algorithm but how to handle the function’s arguments.

Here is the code:

def ternarySearch(f, left, right, absolutePrecision):
    #left and right are the current bounds; the maximum is between them
    if (right - left) < absolutePrecision:
        return (left + right)/2
 
    leftThird = (2*left + right)/3
    rightThird = (left + 2*right)/3
 
    if f(leftThird) < f(rightThird):
        return ternarySearch(f, leftThird, right, absolutePrecision)

    return ternarySearch(f, left, rightThird, absolutePrecision)

I would like to know what the function definition would look like. For example, a function returning y=x^2+3 would look like:

public static int y(int x){
 return x*x+3;
}

but

 return ternarySearch(f, leftThird, right, absolutePrecision)

is not working for me and I’d like to know what to do.

Update:

so for example i have y=3*x+2 it will be like this?

interface MyFunctor {
 int myFunction(int x);
}

class MyFunctorImpl implements MyFunctor {
  int myFunction(int  x) {
      return 3*x+2
  }
}

like this?

  • 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-05-15T00:20:01+00:00Added an answer on May 15, 2026 at 12:20 am

    In Java, there are no higher-order functions. That is, you can’t pass a function as an argument to another function. What you can do is use the Command pattern; define an Interface supporting the method that you need, then pass an instance of that Interface implementing the method.

    For example:

    int ternarySearch(MyFunctor f, int left, int right, float absolutePrecision) {
      #left and right are the current bounds; the maximum is between them
      if (right - left) < absolutePrecision:
        return (left + right)/2
    
      leftThird = (2*left + right)/3
      rightThird = (left + 2*right)/3
    
      if (f.myFunction(leftThird) < f.myFunction(rightThird)) {
        return ternarySearch(f, leftThird, right, absolutePrecision)
      }
      return ternarySearch(f, left, rightThird, absolutePrecision)
    }
    

    and

    interface MyFunctor {
      int myFunction(int arg);
    }
    

    and

    class MyFunctorImpl implements MyFunctor {
      int myFunction(int arg) {
         // implementation
      }
    }
    

    Then you could call ternarySearch with an instance of MyFunctorImpl as the first argument.

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

Sidebar

Related Questions

I have a python ndarray temp in some code I'm reading that suffers this:
In Python I have this code: now = datetime.now().isoformat() if . not in now:
I have a python question, but i think any programer would know enough to
I have this code in Python inputted = input(Enter in something: ) print(Input is
I have this code: def display(self): print self.doc.toprettyxml(indent= ) strigName ='/Users/my_user/Desktop/python/' + str(datetime.datetime.now()) +
Say I have this sample code I'm writing for a Python lesson I'm holding:
I'm newbie in Python. I have this simple code a = 0 b =
I have a python script the runs this code: strpath = sudo svnadmin create
I have some data that I would like to save to a MAT file
I know this is probably a question that is asked a ton on here

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.