Question

In: Computer Science

FOR JAVA Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:...

FOR JAVA

Define  a class  QuadraticExpression that represents the quadratic expression ax^2 + bx + c:
You should provide the following methods

  (1) default constructor which initalizes all the coefficients to 0

  (2) a constructor that takes three parameters
          public QuadraticExpression(double a, double b, double c) 
  
  (3) a toString() method that returns the expression as a string. 
   
  (4) evaluate method that returns the value of the expression at x 
    public double evaluate(double x)
    
  (5) set method of a, b, c  
       public void setA(double newA)
       public void setB(double newB)
       public void setC(double newC)
     
  (6) public static QuadraticExpression sum( QuadraticExpression q1, QuadraticExpression q2): 
      returns a new expression that is the sum of the q1 and q2

  (7) public static QuadraticExpression scale( double r, QuadraticExpression q)
      returns a new expression that is r times q

  (8) public int numberOfRoots()
      returns number of roots, where 3 means infite number of roots
     
  (9) public void add( QuadraticExpression q)
      add q to the calling expression object

  (10) public double smallerRoot() throws Exception
   Depending on the equation ax^2 + bx + c = 0: 
      if no roots, throw exception
      if single root, return it
      if two roots, return  the smaller root
      if infinite root, return -Double.MAX_VALUE

  (11) public double largerRoot() throws Exception
      if no roots, throw exception
      if single root, return it
      if two roots, return  the larger root
      if infinite root, return Double.MAX_VALUE
  
  (12) equals method
      This should OVERRIDE equals method from Object class
  
      return true if two expressions have same a, same b and same c

  (13) clone 
       return a copy of the calling object
   

  (14) use javadoc style comments for the class, and the methods
  
       At minimum, include the author,  parameters and return types for each method. 
       
  (15) use javadoc to generate document for your class
  
  (16) test your class:
       you can write your own main to test your code;
    
        but you have to pass the test in QuadraticExpressionTest.java 

   (17) submit  
     a. QuadraticExpression.java
     b. QuadraticExpression.html
      on blackboard.

   (18) turn in printout of
  a. QuadraticExpression.java
 b. the output of your program

Please!! test before posting it

Test code

public class QuadracExpressionTest {


   public static void main(String[] args)
   {
QuadraticExpression f1 = new QuadraticExpression();
System.out.println("f1(x) = " + f1);
   System.out.println("f1(2) = " + f1.evaluate(2));
System.out.println("f1(-2) = " + f1.evaluate(-2));

System.out.println("f1 = 0 has " + f1.numberOfRoots() + " roots.");
try{
System.out.println("The smaller root of f1 :" + f1.smallerRoot());
   } catch (Exception e)
{
System.out.println(e);
}
  


System.out.println();
     
   QuadraticExpression f2 = new QuadraticExpression(0, 0, 1);
   System.out.println("f2(x) = " + f2);
   System.out.println("f2(2) = " + f2.evaluate(2));
   System.out.println("f2(-2) = " + f2.evaluate(-2));
     
   System.out.println("f2 = 0 has " + f2.numberOfRoots() + " roots.");   
   try{
   System.out.println("The small root of f2 :" + f2.smallerRoot());
   } catch (Exception e)
   {
   System.out.println(e);
   }
  
     
   System.out.println();
     
   f2.setB(0.5);
   System.out.println("Now, f2(x) = " + f2);
   System.out.println("f2(2) = " + f2.evaluate(2));
   System.out.println("f2(-2)= " + f2.evaluate(-2));
     
   System.out.println("f2 = 0 has " + f2.numberOfRoots() + " roots.");
   try{
   System.out.println("The small root of f2 :" + f2.smallerRoot());
   } catch (Exception e)
   {
   System.out.println(e);
   }
     
   System.out.println();
     
     
   QuadraticExpression f3 = new QuadraticExpression(1, 2, 1);
   System.out.println("f3(x) = " + f3);
   System.out.println("f3(3) = " + f3.evaluate(3));
   System.out.println("f3(-3)= " + f3.evaluate(-3));
     
   System.out.println("f3 = 0 has " + f3.numberOfRoots() + " roots.");
   try{
   System.out.println("The smaller root of f3 :" + f3.smallerRoot());
   System.out.println("The larger root of f3 :" + f3.largeRoot());
   } catch (Exception e)
   {
   System.out.println(e);
   }

   System.out.println();
     
   f3.setB(3);
     
   System.out.println("Now, f3(x) = " + f3);
   System.out.println("f3(2) = " + f3.evaluate(2));
   System.out.println("f3(-2)= " + f3.evaluate(-2));
     
   System.out.println("f3 = 0 has " + f3.numberOfRoots() + " roots.");
   try{
   System.out.println("The smaller root of f3 :" + f3.smallerRoot());
   System.out.println("The larger root of f3 :" + f3.largeRoot());
   } catch (Exception e)
   {
   System.out.println(e);
   }

     
   System.out.println();
     
     
   System.out.println("\t f2(x) = " + f2);
   System.out.println("\t f3(x) = " + f3);
   System.out.println(" QuadraticEexpression.add(f2, f3) =" + QuadraticExpression.add(f2, f3));
   System.out.println("After QuadraticExpression.add(f2, f3)");
   System.out.println("\t f2(x) = " + f2);
   System.out.println("\t f3(x) = " + f3);

   System.out.println();
     
   f2. add( QuadraticExpression.scale(2, f3));
   System.out.println("After f2. add( QuadraticExpression.scale(2, f3))");
   System.out.println("\t f2(x) = " + f2);
   System.out.println("\t f3(x) = " + f3);
   System.out.println();
     
     

     
   QuadraticExpression f4 = f3.clone();
   System.out.println("f4(x) = " + f4);
   System.out.println("f3.equals(f4): " + f3.equals(f4));
   System.out.println("f3==f4: " + (f3==f4));
     
   System.out.println();
   f4.setB(0);
   System.out.println("Now, f4(x) = " + f4);
   try{
   System.out.println("The smaller root of f4 :" + f4.smallerRoot());
   System.out.println("The larger root of f4 :" + f4.largeRoot());
   } catch (Exception e)
   {
   System.out.println(e);
   }
     
     
     
   }
   }

Solutions

Expert Solution

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.


CODE: QuadraticExpression.Java

public class QuadraticExpression {
    double a,b,c;

    // 1. Constructor
    public QuadraticExpression() {
        this.a=0;
        this.b=0;
        this.c=0;
    }

    // 2. parametrized constructor
    public QuadraticExpression(double a, double b, double c) {
        this.a = a;
        this.b = b;
        this.c = c;
    }

    //3 to string
    @Override
    public String toString() {
        return a +"x^2 +"+ b+"x +"+c;
    }

    //4.evaluate for given x
    public double evaluate(double x)
    {
        return a*x*x + b*x + c;
    }

    //5 setters
    public void setA(double newA) {
        this.a = newA;
    }

    public void setB(double newB) {
        this.b = newB;
    }

    public void setC(double newC) {
        this.c = newC;
    }

    //6 sum
    public static QuadraticExpression add( QuadraticExpression q1, QuadraticExpression q2)
    {
        return new QuadraticExpression(q1.a+q2.a, q1.b+q2.b, q1.c+q2.c);
    }

    //7.  scaling
    public static QuadraticExpression scale( double r, QuadraticExpression q)
    {
        return new QuadraticExpression(r*q.a, r*q.b, r*q.c);
    }

    // 8  number of roots
    public int numberOfRoots()
    {
        double disc = this.b*this.b - 4*a*c ;
        if(this.a==0)
            return 3;
        if(disc==0)
            return 1;

        return 1;
    }

    //9 sum
    public void add( QuadraticExpression q)
    {
        this.a += q.a;
        this.b += q.b;
        this.c += q.c;
    }

    //10 smaller root
    public double smallerRoot() throws Exception
    {
        if(numberOfRoots()==0)
        {
            throw new Exception("No Roots");
        }
        if(numberOfRoots()==1)
        {
            double root = -b / 2* a ;
            return root;
        }
        if(numberOfRoots()==2)
        {
            double root1 = (-b + Math.sqrt(b*b - 4*a*c) )/ 2* a ;
            double root2 = (-b - Math.sqrt(b*b - 4*a*c) )/ 2* a ;
            return Math.min(root1,root2);
        }
        return -Double.MAX_VALUE;
    }

    //11 larger root
    public double largeRoot() throws Exception
    {
        if(numberOfRoots()==0)
        {
            throw new Exception("No Roots");
        }
        if(numberOfRoots()==1)
        {
            double root = -b / 2* a ;
            return root;
        }
        if(numberOfRoots()==2)
        {
            double root1 = (-b + Math.sqrt(b*b - 4*a*c) )/ 2* a ;
            double root2 = (-b - Math.sqrt(b*b - 4*a*c) )/ 2* a ;
            return Math.max(root1,root2);
        }
        return Double.MAX_VALUE;
    }

    //12. equals
    @Override
    public boolean equals(Object obj) {
        QuadraticExpression q=(QuadraticExpression)obj;
        return q.a == a && q.b==b && q.c==c;
    }

    //13 clone
    @Override
    protected QuadraticExpression clone() throws CloneNotSupportedException {
        QuadraticExpression cloned = new QuadraticExpression(a,b,c);
        return cloned;
    }
}

======

SCREENSHOT FOR CODE:

OUTPUT:

OUTPUT:

f1(x) = 0.0x^2 +0.0x +0.0
f1(2) = 0.0
f1(-2) = 0.0
f1 = 0 has 3 roots.
The smaller root of f1 :-1.7976931348623157E308

f2(x) = 0.0x^2 +0.0x +1.0
f2(2) = 1.0
f2(-2) = 1.0
f2 = 0 has 3 roots.
The small root of f2 :-1.7976931348623157E308

Now, f2(x) = 0.0x^2 +0.5x +1.0
f2(2) = 2.0
f2(-2)= 0.0
f2 = 0 has 3 roots.
The small root of f2 :-1.7976931348623157E308

f3(x) = 1.0x^2 +2.0x +1.0
f3(3) = 16.0
f3(-3)= 4.0
f3 = 0 has 1 roots.
The smaller root of f3 :-1.0
The larger root of f3 :-1.0

Now, f3(x) = 1.0x^2 +3.0x +1.0
f3(2) = 11.0
f3(-2)= -1.0
f3 = 0 has 1 roots.
The smaller root of f3 :-1.5
The larger root of f3 :-1.5

   f2(x) = 0.0x^2 +0.5x +1.0
   f3(x) = 1.0x^2 +3.0x +1.0
QuadraticEexpression.add(f2, f3) =1.0x^2 +3.5x +2.0
After QuadraticExpression.add(f2, f3)
   f2(x) = 0.0x^2 +0.5x +1.0
   f3(x) = 1.0x^2 +3.0x +1.0

After f2. add( QuadraticExpression.scale(2, f3))
   f2(x) = 2.0x^2 +6.5x +3.0
   f3(x) = 1.0x^2 +3.0x +1.0

f4(x) = 1.0x^2 +3.0x +1.0
f3.equals(f4): true
f3==f4: false

Now, f4(x) = 1.0x^2 +0.0x +1.0
The smaller root of f4 :-0.0
The larger root of f4 :-0.0

Process finished with exit code 0


Related Solutions

use Java The two roots of a quadratic equation ax^2 + bx + c = 0...
use Java The two roots of a quadratic equation ax^2 + bx + c = 0 can be obtained using the following formula: r1 = (-b + sqrt(b^2 - 4ac)) / (2a) and r2 = (-b - sqrt(b^2 - 4ac)) / (2a) b^2 - 4ac is called the discriminant of the quadratic equation. If it is positive, the equation has two real roots. If it is zero, the equation has one root. If it is negative, the equation has no...
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) /...
1. Solve quadratic equation Ax^2+Bx+C=0 using the quadratic formula x = (-B+ and - sqrt(B^2-4ac)) / 2a) and output the two solution with clear explanation could you please do it in MATLAB
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a,...
Using excel UserForm construct a Flowchart that solves a quadratic equation ax^2+bx+c=0 for changingvalues of a, b and c. Please also display the code you have used. Please use excel UserForm Thanks
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
Write a program usingif-elseif-else statements to calculate the real roots of a quadratic equation ax^2+bx+c=0
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d ER, intersect at 2 points P and Q. These points are also two points of tangency for the two tangent lines drawn from point A(2,9) upon the parobala. The graph of the cubic function has a y-intercept at (0,-1) and an x intercept at (-4,0). What is the standard equation of the tangent line AP.
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d...
The curves of the quadratic and cubic functions are f(x)=2x-x^2 and g(x)= ax^3 +bx^2+cx+d. where a,b,c,d ER, intersect at 2 points P and Q. These points are also two points of tangency for the two tangent lines drawn from point A(2,9) upon the parobala. The graph of the cubic function has a y-intercept at (0,-1) and an x intercept at (-4,0). What is the value of the coefficient "b" in the equation of the given cubic function.
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 +...
Draw a Flow chart and write a C++ program to solve the quadratic equation ax^2 + bx + c = 0 where coefficient a is not equal to 0. The equation has two real roots if its discriminator d = b2 – 4ac is greater or equal to zero. The program gets the three coefficients a, b, and c, computes and displays the two real roots (if any). It first gets and tests a value for the coefficient a. if...
Use a system of equations to find the parabola of the form y=ax^2+bx+c that goes through...
Use a system of equations to find the parabola of the form y=ax^2+bx+c that goes through the three given points. (2,-12)(-4,-60)(-3,-37)
1. Consider the cubic function f ( x ) = ax^3 + bx^2 + cx +...
1. Consider the cubic function f ( x ) = ax^3 + bx^2 + cx + d where a ≠ 0. Show that f can have zero, one, or two critical numbers and give an example of each case. 2. Use Rolle's Theorem to prove that if f ′ ( x ) = 0 for all xin an interval ( a , b ), then f is constant on ( a , b ). 3.True or False. The product of...
Given a general cubic function y = ax^3 + bx^2 + cx + d prove the...
Given a general cubic function y = ax^3 + bx^2 + cx + d prove the following, a) That a cubic must change at a quadratic rate.          I believe this to be the derivative of the general cubic function, yielding dy/dx = 3ax^2 + 3bx + c b) That there are only 6 basic forms (shapes) for a cubic.         This question is where I get lost. Please help, Thanks!
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT