Question

In: Computer Science

(1) default constructor which initalizes all the coefficients to 0 (2) a constructor that takes three...

(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.

Solutions

Expert Solution

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;
          }
      }
   }
}
}


Related Solutions

Shirt inherits clothing. In the Shirt constructor, how is the Clothing default constructor called? (Select all...
Shirt inherits clothing. In the Shirt constructor, how is the Clothing default constructor called? (Select all that apply) Group of answer choices super(); Clothing(); Clothing.Clothing() Make no explicit call Shirt inherits Clothing. In the Shirt constructor, how is a Clothing constructor with one int parameter called? For the answer, assume that an int variable called "myInt" is in scope. Group of answer choices Clothing.Clothing(myInt); Clothing(myInt); super(myInt); Make no explicit call Shirt inherits Clothing. Both classes have their own version of...
This class has two constructors. The default constructor (the one that takes no arguments) should initialize the first and last names to "None", the seller ID to "ZZZ999", and the sales total to 0.
For this assignment, implement and use the methods for a class called Seller that represents information about a salesperson.The Seller classUse the following class definition:class Seller { public:   Seller();   Seller( const char [], const char[], const char [], double );        void print();   void setFirstName( const char [] );   void setLastName( const char [] );   void setID( const char [] );   void setSalesTotal( double );   double getSalesTotal(); private:   char firstName[20];   char lastName[30];   char ID[7];   double salesTotal; };Data MembersThe data members for the class are:firstName holds the Seller's first namelastName holds the Seller's last nameID holds the Seller's id numbersalesTotal holds the Seller's sales totalConstructorsThis class has two constructors. The default constructor (the one that takes...
Which of the following is true about a credit default swap? A. All three of these...
Which of the following is true about a credit default swap? A. All three of these potential answers here are true about a credit default swap. B. It is basically the same as a mortgage backed security C. It is insurance on an investment D. It helps to reduce the defualt rate on mortgages
Find the indicated coefficients of the power series solution about ?=0 of the differential equation (?2−?+1)?″−?′+5?=0,?(0)=0,?′(0)=7...
Find the indicated coefficients of the power series solution about ?=0 of the differential equation (?2−?+1)?″−?′+5?=0,?(0)=0,?′(0)=7 Answer: ?=7?+....
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
find all eigenvalues and eigenvectors of the given matrix A= [1 0 0 2 1 -2...
find all eigenvalues and eigenvectors of the given matrix A= [1 0 0 2 1 -2 3 2 1]
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2....
1. Add code to the constructor which instantiates a testArray that will hold 10 integers. 2. Add code to the generateRandomArray method which will fill testArray with random integers between 1 and 10 3. Add code to the printArray method which will print each value in testArray. 4. Write an accessor method, getArray which will return the testArray Here's the code starter code: import java.util.ArrayList; public class TestArrays { private int[] testArray; public TestArrays() { } public void generateRandomArray() {...
Let X be a discrete random variable that takes value -2, -1, 0, 1, 2 each...
Let X be a discrete random variable that takes value -2, -1, 0, 1, 2 each with probability 1/5. Let Y=X2 a) Find the possible values of Y. Construct a joint probability distribution table for X and Y. Include the marginal probabilities. b) Find E(X) and E(Y). c) Show that X and Y are not independent.
Is there a fourth degree polynomial that takes these values? x 1 -2 0 3 -1...
Is there a fourth degree polynomial that takes these values? x 1 -2 0 3 -1 7 y -2 -56 -2 4 -16 376
Ex 3.41 Suppose X takes on the values −2,−1, 0, 1, 2 with probability 1/5 each,...
Ex 3.41 Suppose X takes on the values −2,−1, 0, 1, 2 with probability 1/5 each, and let Y = X^^2. (a) Find Cov (X, Y). (b) Prove X and Y are independent and explain your steps.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT