Hello, I am attempting to enter two numbers via console in my test class and call a method in my main class to add the numbers and display the answer. Java doesn't like the Calculator.Calcuate(); code. It gives me an error of "cannot make a static reference to the non static method Calculate". Not sure what to do. Any suggestions? Thanks, David.

Here is what I have so far:
Main Class:

public class Calculator
{

    //Attributes
    private double num1;
    private double num2;
    private double answer;

    //Constructor
    public Calculator(double anum1, double anum2)
    {
        this.Setnum1(anum1);
        this.Setnum2(anum2);

    }//end constructor

    //Methods
    public void Setnum1( double anum1)
    {
        this.num1 = anum1;
    }//end set num1

    public void Setnum2( double anum2)
    {
        this.num2 = anum2;
    }//end set num2


    public double Getnum1()
    {
        return this.num1;
    }// end get num1

    public double Getnum2()
    {
        return this.num2;**
    }// end get num2


    public String Calculate()
    {   
        this.Setnum1(num1);
        this.Setnum2(num2);
        answer = num1 + num2;
        return "\nThe answer is " + num1 + " plus " + num2 + " = " + answer;
    }// end method Calculate  

}// end public class calculator

And the Test Class:

import java.io.*;
import java.util.Scanner;

public class TestCalculator
{
public static void main(String args[])
    {
     Scanner s = new Scanner(System.in);
     System.out.print("\nEnter your first number: ");
     double temp1 = s.nextDouble();
     System.out.print("\nEnter your second number: ");
     double temp2 = s.nextDouble();

     Calculator JDB = new Calculator(temp1, temp2);
     Calculator.Calculate();
     }
}

Edited 4 Years Ago by pyTony: Indent your code blocks

Use the jdb reference to an instance of the Calculator class to call the calculate() method.
The code is ignoring the value returned by the method???

Edited 4 Years Ago by NormR1

This is the error I am getting with the Calculator.Calculate(); code: Cannot make a static reference to the non-static method Calculate() from the type Calculator
Not sure what that means.

The syntax of your statement: CLASSNAME.METHODNAME() is that for when METHODNAME is a static method in the CLASSNAME class. Since calculate() is not a static method, the compiler gives you an error message.

one thing i didnt understand is why are u setting it agian in the Calculate(). Try getting the values from the already set
values into new variables and add them.

I spotted the error in my Test Class. Line 15 should be JDB.Calculate(). However, the program will take my two variables but then stops. How do I get the program to take the two variables and then call the method Calculate. Is there something that needs to be done in the Test Class to make this work?

I'm not sure what you are asking. The code you posted asks for and gets two numbers and then calls the calculate() method and then exits the main() method and the program.
What do you want it to do differently?

This article has been dead for over six months. Start a new discussion instead.