In: Computer Science
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
//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));
}
}
