In: Computer Science
(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 scale( double r, QuadraticExpression q) returns a new expression that is r times q
(7) public int numberOfRoots() returns number of roots, where 3 means infite number of roots
(8) public static QuadraticExpression add( QuadraticExpression q1, QuadraticExpression q2): returns a new expression that is the sum of the q1 and q2
(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.
Author : Rishabh Jain Date:07/09/2019
import java.util.*;
import java.lang.*;
import java.io.*;
class QuadraticExpressionTest
{
public static void main (String[] args) throws
java.lang.Exception
{
QuadraticExpression q=new
QuadraticExpression();
}
public static class QuadraticExpression{
double coeffA;
double coeffB;
double coeffC;
public QuadraticExpression()
{
coeffA = 0;
coeffB = 0;
coeffC = 0;
}
/**
Constructs a quadratic equation and get 2 solutions
@param coefficientA coefficient a of quadratic equation
@param coefficientB coefficient b of quadratic equation
@param coefficientC coefficient c of quadratic equation
*/
public QuadraticExpression(double coefficientA, double
coefficientB, double coefficientC)
{
coeffA = coefficientA;
coeffB = coefficientB;
coeffC = coefficientC;
}
public String toString(){
return (coeffA + " x^2 " +coeffB +"
x " +coeffC);
}
public double evaluate(double x){
return (coeffA * Math.pow(x,2) + coeffB *
x + coeffC);
}
public void setA(double newA){
coeffA=newA;
}
public void setB(double newB){
coeffB=newB;
}
public void setC(double newC){
coeffC=newC;
}
public static QuadraticExpression sum(
QuadraticExpression q1, QuadraticExpression q2){
QuadraticExpression q=new
QuadraticExpression();
q.setA(q1.coeffA + q2.coeffA);
q.setB(q1.coeffB + q2.coeffB);
q.setC(q1.coeffC + q2.coeffC);
return q;
}
public static QuadraticExpression scale( double r,
QuadraticExpression q){
QuadraticExpression qnew=new
QuadraticExpression();
qnew.setA(r * q.coeffA);
qnew.setB(r * q.coeffB);
qnew.setC(r * q.coeffC);
return q;
}
public int numberOfRoots(){
double d=(Math.pow(coeffB, 2) - 4 *
coeffA * coeffC);
if(coeffA==0 && coeffB==0){
return 3;
}else if(d==0){
return 1;
}else if(d>0){
return 2;
}else if(d<0){
return 0;
}
return 0;
}
public void add( QuadraticExpression q){
coeffA+=q.coeffA;
coeffB+=q.coeffB;
coeffC+=q.coeffC;
}
public double smallerRoot() throws Exception{
int num=numberOfRoots();
if(num==0){
throw new
Exception();
}
else if(num==3){
return
-Double.MAX_VALUE;
}
else{
double discriminant =
(Math.pow(coeffB, 2) - 4 * coeffA * coeffC);
double root1=(-coeffB
+ Math.sqrt(discriminant) / 2 * coeffA);
double root2=(-coeffB
- Math.sqrt(discriminant) / 2 * coeffA);
if(num==1){
return root1;
}else{
return root1<root2?root1:root2;
}
}
}
public double largerRoot() throws Exception{
int num=numberOfRoots();
if(num==0){
throw new
Exception();
}
else if(num==3){
return
Double.MAX_VALUE;
}
else{
double discriminant =
(Math.pow(coeffB, 2) - 4 * coeffA * coeffC);
double root1=(-coeffB
+ Math.sqrt(discriminant) / 2 * coeffA);
double root2=(-coeffB
- Math.sqrt(discriminant) / 2 * coeffA);
if(num==1){
return root1;
}else{
return root1>root2?root1:root2;
}
}
}
}
}