Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Polynomial The assignment: You have already written a class called Term, which holds a Mathematical Term. For this Minilab, you are to write a class

Polynomial

The assignment:

You have

already

written a class called Term, which holds a Mathematical

Term. For this Minilab, you are to write a class called Polynomial, which uses an ArrayList

(or Vec

tor if you want) to hol

d a number of Terms, which for

m

a polynomial.

Changing your Term class:

Please be sure that your Term class has data that is

private

since other classes should not be able to access the data directly. To make the Polynomial

class work correctly, your

Term class should also contain a

nother

method called:

//isZero

returns true if this instance of a Term is 0 (has a 0 coefficient)

public boolean isZero()

You should

understand

:

how your Term class works, especially the following methods:

isZero

degre

e

addIn

multiplyBy

evaluate

how an ArrayList works, especially the following methods:

add (both versions)

get

remove

isEmpty

size

contains

toString

Using the Term class in your Polynomial

class

(has

-

a vs. is

-

a):

Your Polynomial

class should

use

an

A

rrayList

(or Vector)

of Terms to hold its data.

It can either:

1)

Have an ArrayList

>

as its data

, similar to our Queue demo. In that case, it

would

be

a has

-

a relationship since the Polynomial has a ArrayList. The

data

and

relationship are sho

wn below:

public class Polynomial

{

//

-------

data

private ArrayList theAL;

And every time

a method wanted to manipulate the

data, it would access the

ArrayLists methods with a call such as

:

theAL.add(i, aTerm);

(Polynomial class)

(

ArrayList

class

)

2)

Or...have an ArrayList)

as

parent class

, so it would be an ArrayList itself and

inherit ArrayLists methods. In that case, it would be an is

-

a relationship since the

Polynomial

is

a ArrayList. The data and relationship are shown below:

public class Polynomial extends ArrayLi

st

{

//

-------

data

(nothing, since it already is an ArrayList)

And every time a method wanted to manipulate the data, it would access its own

(inherited)

methods

with a call such as:

this.add(i, aTerm); //calls its

own

method that was inher

ited from ArrayList

However, do not use both has

-

a and is

-

a at the same time. Java allows it, but it is

very easy to mix up which ArrayList is being manipulated.

Implementing Polynomial

Interface

:

Your Polynomial class should implement

PolynomialIn

terface. If you choose to use both implements and extends then they are

written like this:

public class Polynomial extends ArrayList implements PolynomialInterface

You can have additional methods besides those specifically listed in the Polyno

mialInterface.

You will have to do this to add the extra credit.

Inserting a Term into the ArrayList:

The insert(Term aTerm) method is the most

challenging, as the

Terms should be in de

scending order by degree. The insert

method will

have to traverse

the ArrayList and decide when and where to tell the ArrayList to add it or tell

an existing Term to do an addIn. The methods of individual elements can be accessed like

this:

for (int i=0; i

(for example) theAL.get(i).

addIn(...

O

r,

if

you decide to use a variable, like this:

{

Term

my

Term = theAL.get(i);

my

Term.addIn(...

}

changing

my

Term will also change the element in the ArrayList, since they are both

references to the same Term.

Exact requirements

and extra credit

:

The re

quirements

for both the Minilab and the extra

credit

are

described in the PolynomialInterface comments. Please do not change the

PolynomialInterface, even if you do the extra credit.

Please submit:

your Polynomial.java and updated Term.java

(updated wit

h isZero() method)

via HyperGrade.

// This interface will define the methods that must be present in any class that implements it // The comments will serve as the requirements for the methods.

// The requirements for the extra credit are also listed in the comments, but the actual // interface for those methods have been commented out.

public interface PolynomialInterface { //insert - updates itself by inserting the Term that is received so that Terms in the resulting // Polynomial are in descending order. // if the received Term is null, it should // throw new IllegalArgumentException("your String"); // If the received Term isZero(), it should not be inserted // If the received Term's degree is already present, // then existing Term with the same degree just adds in the received Term. // Otherwise, the received Term should be put into the correct place in the Polynomial // so that the degrees are in descending order. (just like a Math Polynomial). // If the insertion results in a 0 Term in the Polynomial then the 0 Term should be removed. public void insert(Term aTerm);

//toString - returns its representation as a String // Each term should use its own toString() when building up the String to return // Terms in the toString() should be separated by " + " (OK to have 4x^3 + -2x^2) // public String toString(); //EXTRA CREDIT

//toALString - returns its representation as a String by returning it's ArrayList's toString(). public String toALString();

//multiplyBy - receives another Polynomial and finds the product of itself and the Polynomial that is received. // It the Polynomial that is received is null, it should // throw new IllegalArgumentException("your String"); // Creates and returns a new Polynomial that holds the answer. Neither itself nor the // Polynomial that is received are changed. // public Polynomial multiplyBy(Polynomial anotherPoly); //EXTRA CREDIT

//contains - returns true if it contains the Term that is received. // If the received Term is null, it should // throw new IllegalArgumentException("your String"); // (Term must have a correct .equals for it to work) public boolean contains(Term aTerm);

//numTerms - returns the number of Terms in this Polynomial public int numTerms();

//evaluate - returns what the entire Polynomial evaluates to, using the received value of x public double evaluate(double x);

}

public class Term implements TermInterface { //Data private int coefficient; private int power; public Term() { //Constructor this.coefficient = 2; this.power = 3; } // parameterized constructor public Term(int theCoefficient, int thePower) { this.coefficient = theCoefficient; this.power = thePower; } // copy constructor public Term(Term c) { if(c == null) throw new IllegalArgumentException("your descriptive String here"); coefficient = c.coefficient; power = c.power; } public int getCoefficient() { return coefficient; } public int getPower() { return power; } // toString() - returns its representation as a String 3x^2 for example, @Override public String toString() { // - if coefficient is 0, return "0" if(coefficient == 0) return "0"; // - else if the power is 0, return the coefficient else if(power == 0) return coefficient + ""; // - else if the coefficient is 1 and the power is 1, return "x" else if(coefficient == 1 && power == 1) return "x"; // - else if the coefficient is -1 and the power is 1, return "-x" else if(coefficient == -1 && power == 1) return "-x"; // - else if the power is 1, return coefficient and "x" else if(power == 1) return coefficient + "x"; // - else if coefficient is 1, return "x^" and the power else if (coefficient == 1) return "x^" + power; // - else if the coefficient is -1, return "-x^" and the power else if(coefficient == -1) return "-x^" + power; // - else return coefficient and "x^" and power else return coefficient + "x^" + power; } //evaluate - evalutes with whatever double is received public double evaluate(double value) { return coefficient * Math.pow(value, power); } //degree - if coefficient is 0, then (it is a constant so) returns 0. // else returns the power public int degree() { if(coefficient == 0) return 0; else return power; } //derivative - return a new Term that is the derivative. The derivative // is calculated by: // the coefficient of the derivative is the original coefficient times the power // the power of the derivative is the original power minus 1 public Term derivative() { return new Term(coefficient * (power-1), power-1); } //addIn: add another Term to itself. //The Term that is received is not changed. // if the Term that is received is null, throw a new IllegalArgumentException() // if the powers are not the same, throw a new IllegalArgumentException() public void addIn(Term anotherTerm) { if(anotherTerm == null) throw new IllegalArgumentException("Term to be added should not be null"); if(power != anotherTerm.power) throw new IllegalArgumentException("Terms to be added should have same power"); coefficient += anotherTerm.coefficient; } //multiplyBy: multiply itself by anotherTerm - result is a new Term that is created and returned. //The original Term and the Term that is received are not changed. // if the Term that is received is null, throw a new IllegalArgumentException() public Term multiplyBy(Term anotherTerm) { if(anotherTerm == null) throw new IllegalArgumentException("Term to be mulitiplied should not be null"); Term t = new Term(coefficient * anotherTerm.coefficient, power + anotherTerm.power); return t; } //equals: returns true if it is the same as what is received. // If both coefficients are 0, they are automatically equal // otherwise, both coefficients AND both exponents must be the public boolean equals(Object obj) { if(obj instanceof Term) { Term t = (Term)obj; if(coefficient == t.coefficient) { if(coefficient == 0 || power == t.power ) return true; } } //all other cases return false; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Seven NoSQL Databases In A Week Get Up And Running With The Fundamentals And Functionalities Of Seven Of The Most Popular NoSQL Databases

Authors: Aaron Ploetz ,Devram Kandhare ,Sudarshan Kadambi ,Xun Wu

1st Edition

1787288862, 978-1787288867

More Books

Students also viewed these Databases questions

Question

3. What preparations should you make before an interview? (LO 14-3)

Answered: 1 week ago

Question

Who receives responsibility reports? What do the reports include?

Answered: 1 week ago