In: Computer Science
(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.
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: