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

Solutions

Expert Solution

//QuadraticExpression.java(file name)

public class QuadraticExpression implements Cloneable 
{
    // coefficient of x^2,x and constant term
    double a,b,c;
    double expVal;
    double discriminant;
    QuadraticExpression qexp;

    // setter of a,b,c..
    public void setA(double newA) {
        this.a = newA;
    }

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

// default constructor..
    public QuadraticExpression() {
        super();
        this.a = 0;
        this.b = 0;
        this.c = 0;
    }

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

    // this returns the value of expression at x
    public double evaluate(double x) {
        this.expVal = a*x*x + b*x + c;
        return this.expVal;
    }
    // it adds two quadratic expressions
    public static QuadraticExpression sum(QuadraticExpression q1,QuadraticExpression q2) {
        double x2 = q1.a + q2.a;
        double x1 = q1.b + q2.b;
        double x0 = q1.c + q1.c;
        QuadraticExpression temp = new QuadraticExpression(x2,x1,x0);
        return temp;//to be rechecked..
    }
// it scales the coefficient of quadratic expression by r times
    public static QuadraticExpression scale(double r,QuadraticExpression q) {
        double A = q.a * r;
        double B = q.b * r;
        double C = q.c * r;
        QuadraticExpression temp = new QuadraticExpression(A,B,C);
        return temp;
    }
    
    // this returns the no of roots 
    public int numberOfRoots() {
        discriminant = Math.pow(b,2) - 4*a*c;
        if(discriminant<0)
            return 0;
        else {
            if(a==0 && b==0 && c==0) {
                return 3;
            }
            if(discriminant==0)
                return 1;
            else
                return 2;
        }
    }
    
    
    // add method: it add q to the calling expression object
    public void add(QuadraticExpression q) {
        this.a += q.a;
        this.b += q.b;
        this.c += q.c;
    }
    
    //it gives the smaller root of a expression
    public double smallerRoot(){
        try {
        if(numberOfRoots() ==0) {
            throw new Exception();
        }
        }catch(Exception e) {
            System.out.println(e);
        }
        if(numberOfRoots() == 1) {
            return -(b/2*a);
        }else if(numberOfRoots()==2) {
            return (-b - Math.sqrt(discriminant))/2*a;
        }else if(numberOfRoots()==3) {
            return -Double.MAX_VALUE;
        }
        return (-b - Math.sqrt(discriminant))/2*a;
    }
    
    // it returns the larger root of expression
    public double largerRoot() {
        try {
        if(numberOfRoots() ==0) {
        throw new Exception();
        }}catch(Exception e) {
            System.out.println(e);
        }
        if(numberOfRoots() == 1) {
            return -(b/2*a);
        }else if(numberOfRoots()==2) {
            return (-b + Math.sqrt(discriminant))/2*a;
        }else if(numberOfRoots()==3) {
            return Double.MAX_VALUE;
        }
        return (-b + Math.sqrt(discriminant))/2*a;
    }

    // It returns the copy of a calling object
    @Override
    protected Object clone() throws CloneNotSupportedException {
        // TODO Auto-generated method stub
        return super.clone();
    }

    // checks the value of coefficients of two expressions
    @Override
    public boolean equals(Object obj) {
        // TODO Auto-generated method stub
        return super.equals(obj);
    }

    
    // return the expression as a string.
    @Override
    public String toString() {
        return a+"x^2 + "+b+"x + "+c;
        //return "QuadraticExpression [a=" + a + ", b=" + b + ", c=" + c + "]";
    }
    
}

QuadraticExpressionTest.java(file name (contains main method))

public class QuadraticExpressionTest {

   public static void main(String[] args) throws
CloneNotSupportedException {
       QuadraticExpression qexp = new QuadraticExpression(2,9,3);
      
       QuadraticExpression qexp1 = new QuadraticExpression();
       qexp1.setA(3);
       qexp1.setB(9);
       qexp1.setC(6);
      
       System.out.println("qexp: "+qexp);
       System.out.println("qexp1: "+qexp1);
       System.out.println();
       QuadraticExpression newQexp = new QuadraticExpression();
       System.out.println("Sum of above two: "+newQexp.sum(qexp, qexp1));
       System.out.println("Scale by 4 to qexp1: "+newQexp.scale(4, qexp1));
       System.out.println();
       qexp.add(qexp1); // add qexp1 to qexp.
       System.out.println("New qexp: "+ qexp);
      
       System.out.println();
QuadraticExpression qf = (QuadraticExpression)qexp.clone();
System.out.println("Clone of qexp: "+qf);
       System.out.println("Smaller root: "+qexp1.smallerRoot());
       System.out.println("Larger root: "+qexp.largerRoot());
      
       System.out.println("checks if qexp == qexp1: "+qexp.equals(qexp1));
   }
}


Related Solutions

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...
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.
(10) public double smallerRoot() throws Exception Depending on the equation ax^2 + bx + c =...
(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...
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)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT