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

  • Home
  • SEARCH
  • 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 802605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:37:52+00:00 2026-05-14T23:37:52+00:00

Right now I’m going to have to write a method that looks like this:

  • 0

Right now I’m going to have to write a method that looks like this:

public String Calculate(String operator, double operand1, double operand2)
{

        if (operator.equals("+"))
        {
            return String.valueOf(operand1 + operand2);
        }
        else if (operator.equals("-"))
        {
            return String.valueOf(operand1 - operand2);
        }
        else if (operator.equals("*"))
        {
            return String.valueOf(operand1 * operand2);
        }
        else
        {
            return "error...";
        }
}

It would be nice if I could write the code more like this:

public String Calculate(String Operator, Double Operand1, Double Operand2)
{
       return String.valueOf(Operand1 Operator Operand2);
}

So Operator would replace the Arithmetic Operators (+, -, *, /…)

Does anyone know if something like this is possible 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-14T23:37:53+00:00Added an answer on May 14, 2026 at 11:37 pm

    No, you can’t do that in Java. The compiler needs to know what your operator is doing. What you could do instead is an enum:

    public enum Operator
    {
        ADDITION("+") {
            @Override public double apply(double x1, double x2) {
                return x1 + x2;
            }
        },
        SUBTRACTION("-") {
            @Override public double apply(double x1, double x2) {
                return x1 - x2;
            }
        };
        // You'd include other operators too...
    
        private final String text;
    
        private Operator(String text) {
            this.text = text;
        }
    
        // Yes, enums *can* have abstract methods. This code compiles...
        public abstract double apply(double x1, double x2);
    
        @Override public String toString() {
            return text;
        }
    }
    

    You can then write a method like this:

    public String calculate(Operator op, double x1, double x2)
    {
        return String.valueOf(op.apply(x1, x2));
    }
    

    And call it like this:

    String foo = calculate(Operator.ADDITION, 3.5, 2);
    // Or just
    String bar = String.valueOf(Operator.ADDITION.apply(3.5, 2));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now, I have code that looks something like this: Private Sub ShowReport(ByVal reportName
Right now my ant task looks like. <javadoc sourcepath=${source} destdir=${doc}> <link href=http://java.sun.com/j2se/1.5.0/docs/api/ /> </javadoc>
Right now I'm doing something like this: RewriteRule ^/?logout(/)?$ logout.php RewriteRule ^/?config(/)?$ config.php I
Right now I have an SSIS package that runs every morning and gives me
Right now, I have: public <T> T[] toArray(T[] old) { T[] arr = Arrays.copyOf(old,
Right now, I keep all of my projects on my laptop. I'm thinking that
Right now I have a database (about 2-3 GB) in PostgreSQL, which serves as
Right now, I have two Eclipse projects - they both use Maven 2 for
Right now I'm having an issue with a Singleton that I just wrote for
Right now I clarify this may be a duplicated question from: Right-aligned labels in

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.