QuestionQuestion

Objective
To practice writing a complete Java class based on a design spec, to practice writing and calling methods of all kinds, and to practice writing JUnit tests to help check that your code is implementing the tasks correctly.

Introduction
In this project you will each implement a standalone Java class that represents a cubic polynomial. We imagine that the specs have been written by a design team and that they have determined their exact needs, so be sure to read and follow this spec they've sent us carefully. They have request the class be "immutable" similar to other data types in Java, meaning that once you have an instance of a Cubic Polynomial that has been created with a specific set of coefficients, it's contents cannot be changed.

What you Must Implement
For this project you will be implementing one data type class; CubicPoly. However, you will also be adding tests to a file of JUnit test class to demonstrate that you know how to test that your new data structure follows the specs. Our client has not provided us with an example of a program that uses this new class, so the specs and the JUnit tests you write are all you have to go from before you submit your work, so follow the instructions below very carefully.

Of course, the submit server will be doing rigourous testing as well but you only have a limited number of release test tokens to use, so you'll want to test things out on your local machine thoroughly.

CubicPoly class
You must implement all of the data members and methods described below. You may NOT add any instance variables or static variables to this class other than those described below. You may add methods of your own, as long as they are private. The data type class you are writing is a very general class that could be of use in a wide variety of projects at our client's site, so take care in your work!

Private Instance Variables
The class will have exactly four instance variables. These variables MUST be declared both private and final. By marking it as final your code will be allowed to assign a value to it once, which can then never be changed. The assignments to these final variables need to be be done in the constructor(s). The CubicPoly class you are implementing is an immutable class, and needs to have the following instance members:

private final DoubleWithAppx a;
private final DoubleWithAppx b;
private final DoubleWithAppx c;
private final DoubleWithAppx d;
These variables represent the coefficients of the CubicPoly. That is, your class can represent any cubic polynomial of the form "a*x^3 + b*x^2 + c*x + d" given those coefficients. For example, if an instance is supposed to represent the cubic polynomial 42x^3 - 17.2x^2 + 3.7x - 5, then the value of a would be 42.0, the value of b would be -17.2, the value of c would be 3.7, and the value of d would be -5.0. Note that you do not actually store things like the strings "x^3" or "x^2" or "x" as part of the state of CubicPoly since those are implicitly there due to the semantics of the class. The variables will actually be made final because once they are set in the constructors, there is no reason to ever change them. (Instance variables that are final can be initialized from within a constructor, but nowhere else.)

Public Constructors
A constructor that takes four parameters of type DoubleWithAppx representing coefficients a, b, c, d (in that order) for the CubicPoly being constructed. The data members a, b, c, and d are to be initialized with these values resulting in the cubic polynomial: "a*x^3 + b*x^2 + c*x + d". It is strongly suggested that to make your code more efficient to write and test, that you think about how all of the other constructors can make use of this one as a helper.
A constructor that takes three parameters of type DoubleWithAppx representing coefficients b, c, and d (in that order) for the CubicPoly being constructed. The data members b, c, and d are to be initialized with these values and the data member a should be initialized to 0, resulting in the cubic polynomial: "b*x^2 + c*x + d".
A constructor that takes two parameters of type DoubleWithAppx representing coefficients c, and d (in that order) for the CubicPoly being constructed. The data members a and b should both be set to 0 in this case, and c, and d are to be initialized with the arguments resulting in the cubic polynomial: "c*x + d".
A constructor that takes one parameter of type DoubleWithAppx representing coefficient d for the CubicPoly being constructed. The data members a and b and c should all be set to 0, and d is to be initialized with the argument resulting in the cubic polynomial: "d".
A constructor that takes no parameters for the CubicPoly being constructed. The data members a, b, c, and d should all be set to 0 resulting in the cubic polynomial: "0".
A copy constructor.

Public Instance Getter Methods
getA -- A simple "getter" for the value of the a data member.
getB -- A simple "getter" for the value of the b data member.
getC -- A simple "getter" for the value of the c data member.
getD -- A simple "getter" for the value of the d data member.

Public Instance Mathematical Methods
Remember, you do not modify the current object in any of these.
eval -- this method takes one parameter (DoubleWithAppx), evaluates the cubic polynomial at the point represented by the parameter and returns a DoubleWithAppx representing the result of that evaluation. (i.e., if your cubic polynomial is 5x^3-3x^2+2x+4, and you call eval(5.0), it should return 564.0)
add -- this method takes one parameter (CubicPoly). It will return a new CubicPoly that is equal to the sum of the current object and the parameter. The way this is done is by adding corresponding coefficients from the object that invokes the method and from the parameter. So, if the parameter were named cubicPolyIn, then the new CubicPoly's a value would be this.a plus cubicPolyIn.a
subtract -- this method takes one parameter (CubicPoly). It will return a CubicPoly that is computed by subtracting the value of the parameter from the current object. So, if the parameter were named cubicPolyIn, then the new CubicPoly's a value would be this.a minus cubicPolyIn.a
mult -- this method takes one parameter (CubicPoly). This is a bit involved and our client was quite clear that the details were critical to follow. It is not up to us to argue their logic in this method - our job is to code to their spec. If you want to mult the cubic polynomial a*x^3 + b*x^2 + c*x + d and the cubic polynomial e*x^3 + f*x^2 + g*x + h you end up with the new cubic polynomial of (ae)x^6 + (af+be)x^5 + (ag+bf+ce)x^4 + (ah+bg+cf+de)x^3 + (bh+cg+df)x^2 + (ch+dg)x + dh as the result. This method will return a CubicPoly which is the mult of the current object and the parameter as long as the result would NOT end up having a fourth, fifth, or sixth order term. If it would (ie: those coefficients are non-zero) then the method must return null instead. Note that you can return null by including the statement in mult: return null;
Note that you can "chain" method calls. For example, a*b+c could be evaluated using (a.multiply(b)).add(c) by making use of the multiply and add methods in DoubleWithAppx.
deriv -- this method takes no parameters and returns a new CubicPoly that is computed by taking the derivative of the current object. The formula for the derivative of a cubic polynomial ax^3 + bx^2 + cx + d is 3ax^2 + 2bx + c (Remember, you do not modify the current object.)
compareTo -- this method takes one parameter (CubicPoly) and returns an int. The client specs say that:
if all of the coefficients are equal, this method returns 0;
otherwise, we will look for the highest-order term whose coefficients differ and then if in that position the current object's coefficent is less than the corresponding coefficient of the parameter, this method returns -1 but if in that position the current object's coefficent is greater than the corresponding coefficient of the parameter, this method returns +1
Read the spec on how compareTo in DoubleWithAppx works to try to make your method here as simple to read and write as possible.

Public Instance Utility Method "Challenge Problem" (save for after the exam probably)
toString -- this method takes no parameters and returns a new String representing the CubicPoly. The string you create must have exactly the form specified here. See examples below, which illustrate correct return values for the toString() method in each case. Note that you are allowed to use the DoubleWithAppx.toString() method for this one method. These are the requirements:
There are never any spaces in the string.
Do not include an "*" in the "x^3" and "x^2" and "x" terms. (ie: create "23x^2", not "23*x^2").
If there are negative terms, they should be represented by subtraction, not addition of a negative term (i.e., create "2x^2+4x-6", not "2x^2+4x+-6".
If there are any terms with a 0 coefficient, then skip that term (i.e., create "2x^2-6", not "2x^2+0x-6"). If all 4 coefficients are 0, then the string should be "0"
If there are any terms with a 1 coefficient, then display that coefficient (i.e., create "2x^2+1x+3", not "2x^2+x+3").
If the decimal component of a coefficient is 0, then do not include a decimal portion for that coefficient (i.e., create "23x", not "23.0x"). Note that the DoubleWithAppx.toString() method does this for you automatically.

Examples:
41.7x^3+23.3x^2+37x+8
41.7x^3-23.3x^2+37x
-41.7x^3-23.3x^2+37x+8
23.3x^2+37x+8
-23.3x^2-37x+8
-23.3x^2-37x
-23.3x^2+1x+8
37x+8
23.3x^2+8
8
0

Solution PreviewSolution Preview

These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice. Unethical use is strictly forbidden.

public class CubicPoly {
    private final DoubleWithAppx a;
    private final DoubleWithAppx b;
    private final DoubleWithAppx c;
    private final DoubleWithAppx d;

public CubicPoly() {
//throw new RuntimeException("You need to implement this!");

       a = new DoubleWithAppx(0);
       b = new DoubleWithAppx(0);
       c = new DoubleWithAppx(0);
       d = new DoubleWithAppx(0);
}

public CubicPoly(DoubleWithAppx dIn) {
//throw new RuntimeException("You need to implement this!");

       a = new DoubleWithAppx(0);
       b = new DoubleWithAppx(0);
       c = new DoubleWithAppx(0);
       d = dIn;
}

public CubicPoly(DoubleWithAppx cIn, DoubleWithAppx dIn) {
//throw new RuntimeException("You need to implement this!");

       a = new DoubleWithAppx(0);
       b = new DoubleWithAppx(0);
       c = cIn;
       d = dIn;
}

public CubicPoly(DoubleWithAppx bIn, DoubleWithAppx cIn, DoubleWithAppx dIn) {
//throw new RuntimeException("You need to implement this!");

       a = new DoubleWithAppx(0);
       b = bIn;
       c = cIn;
       d = dIn;

}...

By purchasing this solution you'll be able to access the following files:
Solution.java and SolutionTests.java.

$36.00
for this solution

or FREE if you
register a new account!

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats