Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Design a class named QuadraticEquation

kikibkikib Member Posts: 1
We just began discussing classes and OOP in class and I am trying to begin y homework assignment. I don't fully grasp the concept yet so I could use some help.
My assignment is:
[color=Orange]Design a class named QuadraticEquation for a quadratic equation ax^2+bx+x=0. The class contains:

**private data fields a, b, and c that represents three coefficients.

**a constructor for the arguments for a, b, and c.

**three get methods for a, b, and c.

**a method named getDiscriminant() that returns the discriminant, which is b2-4ac

**the methods named getRoot1() and getRoot2() for returning two roots of the equation, or returning 0 if the discriminant is negative.[/color]

Write a test program that prompts the user to enter values for a, b, and c and displays the roots. If no roots for the equation, display "The equation has no roots."

Here is my code so far. I am getting the error: [color=Blue]"getDiscriminant() in QuadraticEquation cannot be applied to (double,double,double)
double discrim = getDiscriminant(a, b, c);
^
QuadraticEquation.java:36: getDiscriminant() in QuadraticEquation cannot be applied to (double,double,double)
double discrim = getDiscriminant(a, b, c);
" [/color]
when I go to compile.

Here is my code:
[code]import java.util.Scanner;
public class TestQuad {
public static void main(String[] args) {
//Create object
QuadraticEquation quad = new QuadraticEquation();
//Ask user for coefficients
Scanner input = new(System.in);
System.out.println("Enter the value of coefficient a: ");
double a = input.nextDouble();
System.out.println("Enter the value of coefficient b: ");
double b = input.nextDouble();
System.out.println("Enter the value of coefficient c: ");
double c = input.nextDouble();
//Display the roots
System.out.println("The roots are " + quad.getRoot1 + " and " + quad.getRoot2);
}
}
class QuadraticEquation {
//Data Fields
private double a, b, c;

//Constructor
public QuadraticEquation() {
}

//Get methods for each coeff
double getA() {
return a;
}
double getB() {
return b;
}
double getC() {
return c;
}

//Method to return discriminant (b^2 - 4ac)
double getDiscriminant() {
return (Math.pow(b, 2) - 4 * a * c);
}

//Method to get root 1
double getRoot1() {
double discrim = getDiscriminant(a, b, c);
if (discrim > 0)
return (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
else
return 0;
}

//Method to get root 2
double getRoot2() {
double discrim = getDiscriminant(a, b, c);
if (discrim > 0)
return (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
else
return 0;
}
}
[/code]
Any help would be greatly appreciated!

Comments

  • Josh CodeJosh Code Member Posts: 675

    [code]
    import java.util.Scanner;
    public class TestQuad {
    public static void main(String[] args) {
    //Ask user for coefficients
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the value of coefficient a: ");
    double a = input.nextDouble();
    System.out.println("Enter the value of coefficient b: ");
    double b = input.nextDouble();
    System.out.println("Enter the value of coefficient c: ");
    double c = input.nextDouble();
    //Create object
    QuadraticEquation quad = new QuadraticEquation(a,b,c);

    //Display the roots
    System.out.println("The roots are " + quad.getRoot1() + " and " + quad.getRoot2());
    System.out.println("a=" + quad.getA() + ", b= " + quad.getB()+", c="+quad.getC());
    System.out.println("equation is "+quad.toString());
    System.out.println("x function at root 1("
    +quad.getRoot1()+") is "+quad.xFunctionAt(quad.getRoot1()));
    System.out.println("x function at root 2("
    +quad.getRoot2()+") is "+quad.xFunctionAt(quad.getRoot2()));
    }
    }

    class QuadraticEquation
    {
    private double a,b,c;

    public QuadraticEquation(double a,double b,double c)
    {
    this.a=a;
    this.b=b;
    this.c=c;
    }

    public double getDiscriminant()
    {
    return b*b-4*a*c;
    }

    public double getRoot1()
    {
    double discriminant = getDiscriminant();
    if (discriminant<0)
    return 0;
    else
    return (-b-Math.sqrt(discriminant))/(2*a);
    }

    public double getRoot2()
    {
    double discriminant = getDiscriminant();
    if (discriminant<0)
    return 0;
    else
    return (-b+Math.sqrt(discriminant))/(2*a);
    }

    public double getA()
    {
    return a;
    }

    public double getB()
    {
    return b;
    }

    public double getC()
    {
    return c;
    }

    public String toString()
    {
    return a+"x^2+"+b+"x+"+c+"=0";
    }

    public double xFunctionAt(double x)
    {
    return a*x*x+b*x+c;
    }

    }
    [/code]
Sign In or Register to comment.