Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA (Data Structures): Define a class QuadraticExpression that represents the q

ID: 3874404 • Letter: J

Question

JAVA (Data Structures):

Define a class QuadraticExpression that represents the quadratic expression ax^2 + bx + c:

You should provide the following methods

(1) default constructor which initalize 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

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

return true if two expressions have same a, same b and same c

(13) clone

return a copy of the calling object

(13) put your name at the top of your source file

/**

@author Your Name

*/

(14) use javadoc style comments for the class, and the methods

(15) test your class:

you can write your own main to test your code; but you have to pass the test in QuadraticExpressionTest.java

(16) use javadoc to generate document for your class

Explanation / Answer

See here is the code. comments are also added in code.

// QuadraticExpression

public class QuadraticExpression {

private double a,b,c;

// default contructor: all values set to 0

public QuadraticExpression() {

a = 0;

b = 0;

c = 0;

}

//parametrized constructor

public QuadraticExpression(double a, double b, double c) {

a= A;

b= B;

c= C;

}

// a toString() method that returns the expression as a string.

public String toString (){

String quadEquation= "The quadratic equation is "+a+"x^2 + "+b+"x + "+c;

return quadEquation;

}

// evaluate method that returns the value of the expression at x

public double evaluate(double x){

return (a * Math.pow(x,2)) + (b*x)+ c ;

}

//set method of a, b, c

public void setA(double newA){

a = newA;

}

public void setB(double newB){

b = newB;

}

public void setC(double newC){

c = newC;

}

  

// returns a new expression that is the sum of the q1 and q2

public static QuadraticExpression sum( QuadraticExpression q1, QuadraticExpression q2){

return new QuadraticExpression( q1.a + q2.a, q1.b + q2.b, q1.c+q2.c);

}

// returns a new expression that is r times q

public static QuadraticExpression scale( double r, QuadraticExpression q){

return new QuadraticExpression(r*q1.a, r*q1.b, r*q3.c );

}

//returns number of roots, where 3 means infite number of roots

public int numberOfRoots(){

double d;

int i=0;

d = b * b - 4 * a * c;

if(d > 0)

{

System.out.println("Roots are real and unequal");

root1 = ( - b + Math.sqrt(d))/(2*a);

root2 = (-b - Math.sqrt(d))/(2*a);

System.out.println("First root is:"+root1);

System.out.println("Second root is:"+root2);

}

if (root1 != 0){

i = i + 1;

}

if (root2 != 0){

i = i + 1;

}

return i;

}

// add q to the calling expression object

public void add( QuadraticExpression q){

a = q1.a;

b = q1.b;

c = q1.c;

}

  

  

public double smallerRoot() throws Exception

{

int number;

number = numberOfRoots();

if (number == 0){

throws Exception;

}

if (number ==1){

return number;

}

if (number ==2){

d = b * b - 4 * a * c;

if(d > 0)

{

System.out.println("Roots are real and unequal");

root1 = ( - b + Math.sqrt(d))/(2*a);

root2 = (-b - Math.sqrt(d))/(2*a);

System.out.println("First root is:"+root1);

System.out.println("Second root is:"+root2);

}

if (root1 < root2){

return root1;

}

else {

return root2;

}

}

}

public QuadraticExpression clone(){

QuadraticExpression temp = new QuadraticExpression(4,3,2);

return temp;

}

}

// QuadraticExpressionTest.java

public class quadraticExpressionTest {

public static void main(String args[]) {

QuadraticExpression q1 = new QuadraticExpression(2,3,5);

QuadraticExpression q2 = new QuadraticExpression(6,4,3);

QuadraticExpression q3 = new QuadraticExpression(6,3 7);

QuadraticExpression q4 = new QuadraticExpression();

System.out.println(" quardraticexpression 1 " + q1.toString());

System.out.println(" quardraticexpression 2 " + q2.toString());

System.out.println(" quardraticexpression 3 " + q3.toString());

System.out.println(" quardraticexpression 4 " + q4.toString());

System.out.println(" evaluate quardraticexpression 1 with x= 3 " + q1.evaluate(3));

System.out.println(" set value of A in quardratic expression 1 " + q1.setA(4));

System.out.println(" set value of B in quardratic expression 1 " + q1.setB(8));

System.out.println(" set value of C in quardratic expression 1 " + q1.setC(9));

System.out.println(" sum of 2 quardratic expression " + q3.sum(q2));

System.out.println(" scale quardratic expression by 3" + q3.scale(3));

}}