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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:13:03+00:00 2026-05-13T23:13:03+00:00

Hi I have the following interfaces that I need to create. Can you just

  • 0

Hi I have the following interfaces that I need to create. Can you just answer one question. Am I supposed to create a variable to hold the value of the operator and the values of the two operands? If so, should these be created inside the interfaces or inside the class with my main method?

interface ArithmeticOperator {

    // Returns the result of applying the operator to operands a and b.
    double operate (double a, double b);

    // Return a String that is the name of this operator.
    String printName ();
}


interface OperatorIterator {

    // Apply the operator op repeatedly to the startValue, for numIterations
    // times, e.g., for numIterations=4 you would return
    //    op (startValue, op (startValue, op (startValue, startValue)))
    double iterate (ArithmeticOperator op, double startValue, int numIterations);

}


public class Exam1 implements ArithmeticOperator, OperatorIterator{

    public double operate(double a, double b) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public String printName() {
        String operator ="";
        if(operator.equals("+"))
            return "Add";
        else if(operator.equals("-"))
            return "Sub";
        else if(operator.equals("/"))
            return "Div";
        else if(operator.equals("*"))
            return "Mult";
        else
            return "Unknown Operator";
    }

    public double iterate(ArithmeticOperator op, double startValue, int numIterations) {
        throw new UnsupportedOperationException("Not supported yet.");
    }



    public static void main (String[] argv)
    {
        // Test 1:
        System.out.println ("TEST 1:");
        ArithmeticOperator add = OperatorFactory.get ("add");
        System.out.println ("  1 " + add.printName() + " 2 = " + add.operate (1,2));
        ArithmeticOperator sub = OperatorFactory.get ("sub");
        System.out.println ("  3 " + sub.printName() + " 2 = " + sub.operate (3,2));
        ArithmeticOperator mult = OperatorFactory.get ("mult");
        System.out.println ("  3 " + mult.printName() + " 2 = " + mult.operate (3,2));
        ArithmeticOperator div = OperatorFactory.get ("div");
        System.out.println ("  3 " + div.printName() + " 2 = " + div.operate (3,2));

        // Test 2:
        System.out.println ("TEST 2:");
        ArithmeticOperator add2 = OperatorFactory.get ("add");
        ArithmeticOperator sub2 = OperatorFactory.get ("sub");
        System.out.println ("  Does add2==add? " + add2.equals(add));
        System.out.println ("  Does add2==sub2? " + add2.equals(sub2));

        // Test 3:
        System.out.println ("TEST 3:");
        OperatorIterator opIter = new OpIterator ();
        System.out.println ("  3 * 8 = " + opIter.iterate(add, 3, 8));
        System.out.println ("  3 ^ 4 = " + opIter.iterate(mult, 3, 4));
    }


}
  • 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-13T23:13:03+00:00Added an answer on May 13, 2026 at 11:13 pm

    You use interfaces to define a contract. The variable is handled by the concrete implementation, e.g.

    public class Add implements ArithmeticOperator(){ 
       @Override double operate (double a, double b){ return a+b;}
    }
    

    If you want something wit a bit more behavior as a base you can use an abstract class.

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

Sidebar

Related Questions

I have the following interfaces. I'm not sure how I can use Moq to
I need to create a configuration section, that is able to store key-value pairs
I have a third-party (Win32) DLL, written in C, that exposes the following interface:
I have something that looks like the following: [CoolnessFactor] interface IThing {} class Tester
I have the following code that gets the MAC address of an interface: static
I have the following code that processes an INotification (custom interface) var mgr =
I have an interface (called Subject ) that has the following method: public void
I have the following Interfaces: public interface ITemplateItem { int Id { get; set;
I have the following two interfaces: public interface IMembershipProvider { object Login(ILoginProviderParameters loginParameters); void
In my project I have the following three interfaces, which are implemented by classes

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.