I'm writing a program for my java class that has to solve quadratic equations. I have separate methods that calls the private variables a, b, and c. I also have separate methods for the discriminant and both the positive and negative roots. My code is as follows:
import java.util.Scanner; public class QuadraticEquation { private double a; private double b; private double c; public static void main(String[] args){ Scanner numberInput = new Scanner(System.in); System.out.print("Enter a, b, c: "); double a = numberInput.nextDouble(); double b = numberInput.nextDouble(); double c = numberInput.nextDouble(); QuadraticEquation q1 = new QuadraticEquation(a, b, c); System.out.println(getRoot1()); }//end main method public QuadraticEquation(double a, double b, double c){ this.a = a; this.b = b; this.c = c; }//end constructor public double getDiscriminant(){ double discriminant = Math.sqrt((getB()*getB()) - (4*getA()*getC())); return discriminant; }//end getDiscriminant() public double getA(){ this.a = a; return this.a; }//end getA() public double getB(){ this.b = b; return this.b; }//end getB() public double getC(){ this.c = c; return this.c; }//end getC public double getRoot1(){ double root1 = (-getB() + getDiscriminant()) / (2*a); return root1; }//end getRoot1() public double getRoot2(){ double root2 = (-getB() - getDiscriminant()) / (2*a); return root2; }//end getRoot2() }
My problem is that I get an error that says "Cannot make a static reference to the non-static method getRoot1() from the type QuadraticEquation". I know this means that it is not working because my method is not a static method but when I try to change it to a static method I get errors in all my other methods. I have no idea what to do any help would be appreciated.