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
Test code
public class QuadracExpressionTest {
public static void main(String[] args)
{
QuadraticExpression f1 = new QuadraticExpression();
System.out.println("f1(x) = " + f1);
System.out.println("f1(2) = " + f1.evaluate(2));
System.out.println("f1(-2) = " + f1.evaluate(-2));
System.out.println("f1 = 0 has " + f1.numberOfRoots() + "
roots.");
try{
System.out.println("The smaller root of f1 :" +
f1.smallerRoot());
} catch (Exception e)
{
System.out.println(e);
}
System.out.println();
QuadraticExpression f2 = new QuadraticExpression(0, 0,
1);
System.out.println("f2(x) = " + f2);
System.out.println("f2(2) = " + f2.evaluate(2));
System.out.println("f2(-2) = " +
f2.evaluate(-2));
System.out.println("f2 = 0 has " + f2.numberOfRoots()
+ " roots.");
try{
System.out.println("The small root of f2 :" +
f2.smallerRoot());
} catch (Exception e)
{
System.out.println(e);
}
System.out.println();
f2.setB(0.5);
System.out.println("Now, f2(x) = " + f2);
System.out.println("f2(2) = " + f2.evaluate(2));
System.out.println("f2(-2)= " +
f2.evaluate(-2));
System.out.println("f2 = 0 has " + f2.numberOfRoots()
+ " roots.");
try{
System.out.println("The small root of f2 :" +
f2.smallerRoot());
} catch (Exception e)
{
System.out.println(e);
}
System.out.println();
QuadraticExpression f3 = new QuadraticExpression(1, 2,
1);
System.out.println("f3(x) = " + f3);
System.out.println("f3(3) = " + f3.evaluate(3));
System.out.println("f3(-3)= " +
f3.evaluate(-3));
System.out.println("f3 = 0 has " + f3.numberOfRoots()
+ " roots.");
try{
System.out.println("The smaller root of f3 :" +
f3.smallerRoot());
System.out.println("The larger root of f3 :" +
f3.largeRoot());
} catch (Exception e)
{
System.out.println(e);
}
System.out.println();
f3.setB(3);
System.out.println("Now, f3(x) = " + f3);
System.out.println("f3(2) = " + f3.evaluate(2));
System.out.println("f3(-2)= " +
f3.evaluate(-2));
System.out.println("f3 = 0 has " + f3.numberOfRoots()
+ " roots.");
try{
System.out.println("The smaller root of f3 :" +
f3.smallerRoot());
System.out.println("The larger root of f3 :" +
f3.largeRoot());
} catch (Exception e)
{
System.out.println(e);
}
System.out.println();
System.out.println("\t f2(x) = " + f2);
System.out.println("\t f3(x) = " + f3);
System.out.println(" QuadraticEexpression.add(f2, f3)
=" + QuadraticExpression.add(f2, f3));
System.out.println("After QuadraticExpression.add(f2,
f3)");
System.out.println("\t f2(x) = " + f2);
System.out.println("\t f3(x) = " + f3);
System.out.println();
f2. add( QuadraticExpression.scale(2, f3));
System.out.println("After f2. add(
QuadraticExpression.scale(2, f3))");
System.out.println("\t f2(x) = " + f2);
System.out.println("\t f3(x) = " + f3);
System.out.println();
QuadraticExpression f4 = f3.clone();
System.out.println("f4(x) = " + f4);
System.out.println("f3.equals(f4): " +
f3.equals(f4));
System.out.println("f3==f4: " + (f3==f4));
System.out.println();
f4.setB(0);
System.out.println("Now, f4(x) = " + f4);
try{
System.out.println("The smaller root of f4 :" +
f4.smallerRoot());
System.out.println("The larger root of f4 :" +
f4.largeRoot());
} catch (Exception e)
{
System.out.println(e);
}
}
}
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.
CODE: QuadraticExpression.Java
public class QuadraticExpression {
double a,b,c;
// 1. Constructor
public QuadraticExpression() {
this.a=0;
this.b=0;
this.c=0;
}
// 2. parametrized constructor
public QuadraticExpression(double a, double b, double c) {
this.a = a;
this.b = b;
this.c = c;
}
//3 to string
@Override
public String toString() {
return a +"x^2 +"+ b+"x +"+c;
}
//4.evaluate for given x
public double evaluate(double x)
{
return a*x*x + b*x + c;
}
//5 setters
public void setA(double newA) {
this.a = newA;
}
public void setB(double newB) {
this.b = newB;
}
public void setC(double newC) {
this.c = newC;
}
//6 sum
public static QuadraticExpression add( QuadraticExpression q1, QuadraticExpression q2)
{
return new QuadraticExpression(q1.a+q2.a, q1.b+q2.b, q1.c+q2.c);
}
//7. scaling
public static QuadraticExpression scale( double r, QuadraticExpression q)
{
return new QuadraticExpression(r*q.a, r*q.b, r*q.c);
}
// 8 number of roots
public int numberOfRoots()
{
double disc = this.b*this.b - 4*a*c ;
if(this.a==0)
return 3;
if(disc==0)
return 1;
return 1;
}
//9 sum
public void add( QuadraticExpression q)
{
this.a += q.a;
this.b += q.b;
this.c += q.c;
}
//10 smaller root
public double smallerRoot() throws Exception
{
if(numberOfRoots()==0)
{
throw new Exception("No Roots");
}
if(numberOfRoots()==1)
{
double root = -b / 2* a ;
return root;
}
if(numberOfRoots()==2)
{
double root1 = (-b + Math.sqrt(b*b - 4*a*c) )/ 2* a ;
double root2 = (-b - Math.sqrt(b*b - 4*a*c) )/ 2* a ;
return Math.min(root1,root2);
}
return -Double.MAX_VALUE;
}
//11 larger root
public double largeRoot() throws Exception
{
if(numberOfRoots()==0)
{
throw new Exception("No Roots");
}
if(numberOfRoots()==1)
{
double root = -b / 2* a ;
return root;
}
if(numberOfRoots()==2)
{
double root1 = (-b + Math.sqrt(b*b - 4*a*c) )/ 2* a ;
double root2 = (-b - Math.sqrt(b*b - 4*a*c) )/ 2* a ;
return Math.max(root1,root2);
}
return Double.MAX_VALUE;
}
//12. equals
@Override
public boolean equals(Object obj) {
QuadraticExpression q=(QuadraticExpression)obj;
return q.a == a && q.b==b && q.c==c;
}
//13 clone
@Override
protected QuadraticExpression clone() throws CloneNotSupportedException {
QuadraticExpression cloned = new QuadraticExpression(a,b,c);
return cloned;
}
}
======
SCREENSHOT FOR CODE:




OUTPUT:


OUTPUT:
f1(x) = 0.0x^2 +0.0x +0.0
f1(2) = 0.0
f1(-2) = 0.0
f1 = 0 has 3 roots.
The smaller root of f1 :-1.7976931348623157E308
f2(x) = 0.0x^2 +0.0x +1.0
f2(2) = 1.0
f2(-2) = 1.0
f2 = 0 has 3 roots.
The small root of f2 :-1.7976931348623157E308
Now, f2(x) = 0.0x^2 +0.5x +1.0
f2(2) = 2.0
f2(-2)= 0.0
f2 = 0 has 3 roots.
The small root of f2 :-1.7976931348623157E308
f3(x) = 1.0x^2 +2.0x +1.0
f3(3) = 16.0
f3(-3)= 4.0
f3 = 0 has 1 roots.
The smaller root of f3 :-1.0
The larger root of f3 :-1.0
Now, f3(x) = 1.0x^2 +3.0x +1.0
f3(2) = 11.0
f3(-2)= -1.0
f3 = 0 has 1 roots.
The smaller root of f3 :-1.5
The larger root of f3 :-1.5
f2(x) = 0.0x^2 +0.5x +1.0
f3(x) = 1.0x^2 +3.0x +1.0
QuadraticEexpression.add(f2, f3) =1.0x^2 +3.5x +2.0
After QuadraticExpression.add(f2, f3)
f2(x) = 0.0x^2 +0.5x +1.0
f3(x) = 1.0x^2 +3.0x +1.0
After f2. add( QuadraticExpression.scale(2, f3))
f2(x) = 2.0x^2 +6.5x +3.0
f3(x) = 1.0x^2 +3.0x +1.0
f4(x) = 1.0x^2 +3.0x +1.0
f3.equals(f4): true
f3==f4: false
Now, f4(x) = 1.0x^2 +0.0x +1.0
The smaller root of f4 :-0.0
The larger root of f4 :-0.0
Process finished with exit code 0
