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();
}
}