-3

I have an assignment that I have no idea how to complete. Tried google and everything, still no idea.

Assignment is:

create a class Calculator witch contains following:

  • constant pi (done)
  • constructor that accepts two following integers: operand1 and operand2 (done)
  • method to print out values of operand1 and operand2 (done)
  • instance methods for adding, subtracting, multiplying and division of operand1 and operand2 (done)
  • static methods for these same operations that accepts operand1 and operand2 as parameters (not done)
  • instance method for calculating area of circle with pi and operand1 (done)

In main method create object of class Calculator and call all methods and write result of all methods in console.

I don't want anyone writing a code for me, I just need guidelines on how to get operand1 and operand2 as parameters inside static method, because I have no idea how to start.

I attempted the following code:

public static int add(operand1, operand2)
{
    return operand1 + operand2;
}

and got the following error:

Identifier expected, an object reference is required for non-static field, method or property.

Community
  • 1
  • 1
Dwight
  • 52
  • 1
  • 6
  • 2
    "_how to get operand1 and operand2 as parameters inside static method_" ~ err...just pass them as parameters? What exactly are you asking? – cbr Jan 07 '17 at 17:50
  • 2
    That assignment is really poorly written. I don't blame you for feeling lost. – BoltClock Jan 07 '17 at 17:52
  • I do this: public static int add(operand1, operand2){ return operand1+operand2; } and get following errors: Identifier expected, an object reference is required for non-static field, method or property. how to bypass these errors? – Dwight Jan 07 '17 at 17:55
  • @Dwight In the future, please include the code in the question itself along with the error message from the beginning so that no one needs to ask you for it. Stack Overflow works much better when you show your attempt at the problem. It illustrates where your misunderstanding is and gives people something to base their answer off of. – mason Jan 07 '17 at 17:57
  • 1
    @Dwight you forget the parameter types in the code snippet – UnholySheep Jan 07 '17 at 17:58
  • @mason, I will, I'm sorry for not doing it right away. – Dwight Jan 07 '17 at 17:59
  • @Dwight see my answer – CodingYoshi Jan 07 '17 at 18:05
  • Your specific error is due to not having parameter types in front of operand1 and 2. Should they be int? – Rabid Penguin Jan 07 '17 at 18:09

4 Answers4

2

Static methods cannot access member vaiables. They behave as functions under the namespace of the class and not as instance methods. The assignment specification states that you should accept two parameters. Therefore, they need to be provided when making a call to the static method. In other words, put more consideration into what your method signature would look like.

Max Sindwani
  • 1,267
  • 7
  • 15
0

static methods for these same operations that accepts operand1 and operand2 as parameters (not done)

The static methods will look like this:

public static void Add(int operand1, int operand2) { … }

Note: for int substitute whatever numeric type you are using.

What are these methods supposed to do? Most likely instantiate Calculator itself, perform addition, and print the result.

Why such an assumption? Using static methods to represent the same operation otherwise implementated as a class method is a common shortcut for one-time use, encapsulating instatiation and execution of that operation.

The actual implementation is left as an excersise to the OP.

Community
  • 1
  • 1
Ondrej Tucny
  • 27,626
  • 6
  • 70
  • 90
0

Your calculator class should have a static method

public static return_type Method(parm_type parm1, parm_type parm2)

in that method you'll have access to parm1 and parm2 which is what you'll do your operations on.

Rabid Penguin
  • 1,084
  • 9
  • 22
-1

A static method is created like this:

public class Class1
{
    // For example: 
    // public static string GetFullName(string first, string last)
    // In example above return type is string and it has 2 string parameters

    public static <return type> NameOfMethod(parameterType parameterValue, ...)
    {
        // Code here
        // This code can only access other static fields, properties and 
        // methods. It cannot access anything non static (instance).
    }
}

To call that method, you call it using the class name because static methods are only accessible through the class name, not through an object instance:

Class1.NameOfMethod(arg1, ...);

I will not write the code for you since you said you do not want the code, but in your static method you will have 2 parameters: operand1 and operand2.

CodingYoshi
  • 25,467
  • 4
  • 62
  • 64
  • "The OP said, he does not want code and your answer is the exact opposite, i.e. it has code" — same useless comment as yours on my answer. – Ondrej Tucny Jan 07 '17 at 18:07
  • Thank you for answering. I get that, but, from what can understand, I think my teacher wants me to pass defined class values (operand1 and operand2) to static method, and I don't know how to do that... – Dwight Jan 07 '17 at 18:09
  • @Dwight Most likely he doesn't, because static methods just *cannot* access instance fields without being passed an instance of that class, which they are not in your case. – Ondrej Tucny Jan 07 '17 at 18:16
  • I'll try with that. Thank you for answering. – Dwight Jan 07 '17 at 18:18
  • I would love to know why my answer was downvoted... – CodingYoshi Jan 07 '17 at 18:18
  • I didn't downvoted it, I upwoted it but it doesn't change :/ – Dwight Jan 07 '17 at 18:19
  • @Dwight I am not saying you did but someone did and I have absolutely no idea why. If it answers your question, you can accept it so others know it answered your question. If it does not answer your question, then don't accept it. – CodingYoshi Jan 07 '17 at 18:21