Question

In: Computer Science

(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 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.

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.

import java.lang.Math.*;
class QuadraticExpression
{
int a,b,c;
QuadraticExpression(int a,int b,int c)
{
this.a=a;
this.b=b;
this.c=c;
}
public double smallerRoot() throws Exception
{
int x= b*b - 4*a*c;
if(a==0)
{
//Infinite roots
return -Double.MAX_VALUE;
}
  
if(x < 0)
{
//No real roots
throw new Exception("No Real Roots");
}
  
if(x==0)
{
// only one root
return -b / (double)(2*a) ;
}
else
{
//TWO ROOTS
  
double root1 = (-b + Math.sqrt(x) )/(double)(2*a);
double root2 = (-b - Math.sqrt(x) )/(double)(2*a);
return root1 > root2 ? root2 : root1;
  
}
}
  
public double largerRoot() throws Exception
{
int x= b*b - 4*a*c;
if(a==0)
{
//Infinite roots
return Double.MAX_VALUE;
}
  
if(x < 0)
{
//No real roots
throw new Exception("No Real Roots");
}
  
if(x==0)
{
// only one root
return -b / (double)(2*a) ;
}
else
{
//TWO ROOTS
  
double root1 = (-b + Math.sqrt(x) )/(double)(2*a);
double root2 = (-b - Math.sqrt(x) )/(double)(2*a);
return root1 < root2 ? root2 : root1;
  
}
}
  
// Override the equals method
public boolean equals(Object o)
{
QuadraticExpression obj = (QuadraticExpression)o;
return (a==obj.a && b==obj.b && c==obj.c);
}
  
//Test the program
   public static void main (String[] args) throws Exception
   {
   QuadraticExpression q=new QuadraticExpression(1,5,6);
       System.out.println("The Smaller root: "+q.smallerRoot());
   System.out.println("The Larger root: "+q.largerRoot());
  
   }
}

=====================================

SCREENSHOT:


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...
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
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
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...
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 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)
(x-1) is a factor of P(x) = x^3 - ax^2 + bx -10. When P(x) is...
(x-1) is a factor of P(x) = x^3 - ax^2 + bx -10. When P(x) is divided by (x+4) there is a remainder of 10. Determine the values of a and b.
(T or F)   C++ throws an exception if you try to refer to element 47 in...
(T or F)   C++ throws an exception if you try to refer to element 47 in an array with 20 elements. (T or F)   I can copy one array to another by simply using an assignment statement which assigns the one array to the other. (T or F) An exception is the occurrence of an erroneous or unexpected situation during the execution of a program. (T or F) The try / catch structure is used to test for and catch...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT