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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:46:41+00:00 2026-05-30T15:46:41+00:00

I wrote some c# code, which implements the transfer of several functions. I used

  • 0

I wrote some c# code, which implements the transfer of several functions. I used delegates for it.

public delegate float Func(float x);
public Dictionary<string, Func> Functions = new Dictionary<string, Func>();

Fill in dictionary:

Functions.Add("y=x", delegate(float x) { return x; });
        Functions.Add("y=x^2", delegate(float x) { return x*x; });
        Functions.Add("y=x^3", delegate(float x) { return x*x*x; });
        Functions.Add("y=sin(x)", delegate(float x) { return (float)Math.Sin((double)x); });
        Functions.Add("y=cos(x)", delegate(float x) { return (float)Math.Cos((double)x); });

and I use these functions to find max of them and some other tasks.

private float maxY(params Func[] F)
    {
        float maxYi = 0;
        float maxY = 0;
        foreach (Func f in F)
        {
            for (float i = leftx; i < rightx; i += step)
            {
                maxYi = Math.Max(f(i), maxYi);
            }
            maxY = Math.Max(maxY, maxYi);
        }
        return maxY;
    }

How can I do this in Java?
I don`t know how to use delegates in Java.

  • 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-30T15:46:43+00:00Added an answer on May 30, 2026 at 3:46 pm

    You can use an interface like this:

    public static void main(String... args) throws Exception {
        Map<String, Computable> functions = new HashMap<String, Computable>();
        functions.put("y=x", new Computable() {public double compute(double x) {return x;}});
        functions.put("y=x^2", new Computable() {public double compute(double x) {return x*x;}});
    
        for(Map.Entry<String, Computable> e : functions.entrySet()) {
            System.out.println(e.getKey() + ": " + e.getValue().compute(5)); //prints: y=x: 5.0 then y=x^2: 25.0
        }
    }
    
    interface Computable {
        double compute(double x);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote some code which monitors the output of Stopwatch using a tight loop.
Some time ago, I wrote some code to decide which method of updating mutable
I have been refactoring throwaway code which I wrote some years ago in a
So, I wrote a code which uses some Microsoft Sql server dlls, these dlls
I wrote a simple plugin which sets some css code using wp_options. It all
i wrote a little addin, which does some formatting of my C# code. in
For a homework assignment I wrote some scala code in which I have the
I am trying to write some simple code which will read a text file
I'm trying to write some C code which is portable only so far as
I'm trying to write some Haskell code in which there are multiple data types,

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.