0

My job is to create a quadratic class containing four class methods.

One method being plusRoot() which returns the root when the quadratic formula uses plus sign (-b+squarerootof D) / 2a

Another being minusRoot() which returns the root when the quadratic formula uses minus sign (-b-squarerootof D) / 2a

A discriminant method which returns (b^2 - 4ac)

The program starts at the main method which asks user for constants a, b, and c then uses all the other methods to output the roots. This is what I have created.

package chapter3Codes;
import java.util.Scanner;
public class QuadraticClass {
    static double d;
    static double mroot;
    static double proot;
    public final static void main(String[] args) {
        System.out.println("Enter constants a, b, and c");
        Scanner scn = new Scanner (System.in);
        a = scn.nextDouble();
        Scanner scn1 = new Scanner (System.in);
        b = scn1.nextDouble();
        Scanner scn2 = new Scanner (System.in);
        c = scn2.nextDouble();
        plusRoot proot = new plusRoot();
        minusRoot mroot = new minusRoot();
        System.out.println ("The roots are" + proot + "and" mroot);
    }
        public static double plusRoot (double a, double b, double c){
            double proot;
            proot = -b + Math.sqrt(d)/ 2*a;
            return proot ;
        }
        public static double minusRoot (double a, double b, double c){
            double mroot;
            mroot = -b - Math.sqrt(d)/ 2*a;
            return mroot ;
        }
        public static double Disc (double a, double b, double c){

            d = Math.pow(b, 2) - 4*a*c;
            return d ;

        }       
}

I am getting errors such as a, b, and c cannot be resolved to a variable and plusRoot and minusRoot cannot be resolved to a type. What am I doing wrong?

Chris Martin
  • 30,334
  • 10
  • 78
  • 137
SethZiotic
  • 125
  • 2
  • 8

2 Answers2

0

In java you have to declare all variables with a type. a, b, c, plusRoot, and minusRoot aren't defined. Add,

double a, b, c, plusRoot, minusRoot; 

prior to their usage.

Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
0

You want to specify the type of the a, b, and c in the main method. This should work:

package chapter3Codes;
import java.util.Scanner;

    public class QuadraticClass {
        static double d;
        static double mroot;
        static double proot;
        public final static void main(String[] args) {
            System.out.println("Enter constants a, b, and c");
            Scanner scn = new Scanner (System.in);
            // specifying that type is double
            double a = scn.nextDouble();
            double b = scn.nextDouble();
            double c = scn.nextDouble();
            double proot = plusRoot(a,b,c);
            double mroot = minusRoot(a,b,c);
            System.out.println ("The roots are" + proot + "and" + mroot);
        }
            public static double plusRoot (double a, double b, double c){
                double proot;
                proot = -b + Math.sqrt(d)/ 2*a;
                return proot ;
            }
            public static double minusRoot (double a, double b, double c){
                double mroot;
                mroot = -b - Math.sqrt(d)/ 2*a;
                return mroot ;
            }
            public static double Disc (double a, double b, double c){

                d = Math.pow(b, 2) - 4*a*c;
                return d ;

            }       
    }

Second, you are trying to create new objects out of methods that you have created. You cannot create an object from a method, it must be created with a class. The new keyword indicates the creation of a new object, which is not what you want to do. I am not going to explain right now how to create a new class file (learn basics here), but the way that you want to call the method is like this:

// double because methods are returning a double
double proot = plusRoot(a, b, c); // calls the method and passes a,b,c as parameters
double mroot = minusRoot(a, b, c);

Also, you do not need to create a new scanner object every time you want to use the scanner. You need to initialize it once, Scanner scn = new Scanner (System.in); and every time you want to use the scanner you can call scn.nextDouble();. There is no need to create scn1 and scn2.

Zach P
  • 560
  • 10
  • 30
  • where would I put double proot = plusRoot(a, b, c); and double mroot = minusRoot(a, b, c);? – SethZiotic Nov 19 '15 at 00:01
  • Replace the lines that say plusRoot proot = new plusRoot(); and the same with minusRoot. That is the way to create an object, which is not what you want to do right now. – Zach P Nov 19 '15 at 00:06
  • *what you are doing now is the way to create an object, which is not what you want to do – Zach P Nov 19 '15 at 00:07