Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Below, I have provided 3 classes. The first class is where I am stuck and unsure of how to use constructors. There are instructions above

Below, I have provided 3 classes. The first class is where I am stuck and unsure of how to use constructors. There are instructions above each method explaining the purpose of each method. I do not understand how to utilize these constructors when different classes are being brought into another class and when a list is involved. There are more methods within the Polynomial.java class that I can only do once these constructors are complete. So my question is basically how to use these constructors that are using other classes and lists. I have already completed the term.java class myself because I understand how to apply constructors in this case, but the classes are used together which is where I'm stuck. I also require an explanation for private helper methods and there is one in the polynomial.java class. I have dealt with them before, but a clear and basic explanation for its purpose is what I need because I always find myself thinking too much and not entirely understanding what it is I am doing. Thank you and this is my fourth time posting this question because I think the other times I asked this question , my question and instructions were not clear

FIRST CLASS - Polynomial.java

package cp213;

import java.util.ArrayList; import java.util.Iterator; import java.util.List;

/** * This class defines polynomial objects. It contains a collection of Term * objects, each of which represents a polynomial monomial. The index in the * collection represents the exponent of the Term. Polynomials can be added, * subtracted, multiplied, differentiated, and copied. TODO: division. * * Polynomials can be evaluated for x using Horner's Rule. * */ public class Polynomial implements Iterable {

// Attributes // Stores the coefficients in a List to easily provide an iterator. private final List terms = new ArrayList<>();

/** * Empty constructor. */ public Polynomial() { }

/** * Coefficients constructor. * * @param coefficients List of coefficients * @throws Exception on bad Term construction */ public Polynomial(final Coefficients coefficients) throws Exception { }

/** * Copy constructor. The contents of that are unchanged. * * @param that The Polynomial to copy. * @throws Exception Exception on bad Term construction (should never be thrown * from this constructor assuming that is valid. */ public Polynomial(final Polynomial that) throws Exception { }

/** * Private helper method. Removes all Terms with a 0 coefficient after the last * non-0 coefficient with index > 0. (All Polynomials must have a least a 0 * exponent Term.) May be required after construction, addition, or subtraction. */ private void normalize() {

// your code here

return; }

SECOND CLASS - Term.java

package cp213;

/** * Represents an individual term of a polynomial, containing the coefficient * (double) and exponent (integer) of a term. Implements term addition, * subtraction, multiplication, negation, and differential. TODO: division * * The contents of a Term may not be not be changed once constructed, thus there * are no setters and methods that manipulate a Term must return a new Term. * * Some Term methods may throw an exception where two terms are used and their * exponents do not match as required. * */ public class Term {

// Attributes. private double coefficient = 0; private int exponent = 0;

/** * Constructs a new Term given a coefficient and exponent. * * @param coefficient The Term coefficient. * @param exponent The Term exponent. * @throws Exception If exponent is negative, throws Exception with message: * "negative exponents not allowed" */ public Term(final double coefficient, final int exponent) throws Exception { this.coefficient = coefficient; this.exponent = exponent; if (exponent < 0) { throw new Exception("negative exponents not allowed"); } }

/** * Copy constructor. The contents of that are unchanged. * * @param that The Term object to copy. */ public Term(final Term that) { coefficient = that.coefficient; exponent = that.exponent; }

/** * Adds two Term objects together and returns the resulting new Term. The two * Term objects must have the same exponent value. The contents of this and that * are unchanged. * * @param that The Term object to add to this Term object. * @return The Term that results from adding this and that. * @throws Exception If the exponents of this and that are not identical, throws * Exception with message: "exponents do not match" */ public Term add(final Term that) throws Exception { Term sumTerms = null; double sumCoefficients; if (this.exponent != that.exponent) { throw new Exception("exponents do not match"); } else { sumCoefficients = this.coefficient + that.coefficient; sumTerms = new Term(sumCoefficients, this.exponent); } return sumTerms; }

/** * Returns a new Term object that is the differential of this Term. If this * exponent is 0, sets the both the coefficient and exponent of the new Term to * 0. The contents of this are unchanged. * * @return The differential of this, or Term(0, 0) if the differentiation * reduces this exponent to less than zero. * @throws Exception from Term constructor */ public Term differential() throws Exception { Term differential = null; double productCoefficients; int differenceExponents; if (this.exponent < 0) { throw new Exception("negative exponents not allowed"); } else { productCoefficients = this.coefficient * this.exponent; differenceExponents = this.exponent - 1; differential = new Term(productCoefficients, differenceExponents); } return differential; } /** * Returns whether this Term object and another Term object are the same object, * or contain equivalent attributes. * * @param that The Term object to compare this Term object to. * @return true if this and that are the same object or contain equivalent * attributes, false otherwise. */ public boolean equals(final Term that) { return super.equals(that); }

/** * @return this coefficient. */ public double getCoefficient() { return this.coefficient; }

/** * @return this exponent. */ public int getExponent() { return this.exponent; }

/** * Multiplies this Term with another Term and returns the result in a new Term. * The contents of this and that are unchanged. * * @param that The other Term to multiply this Term with. * @return The result of multiplying this Term with that Term. * @throws Exception from Term constructor */ public Term multiply(final Term that) throws Exception { Term finalProduct = null; double product; int sumExponents; if (this.exponent < 0 || that.exponent < 0) { throw new Exception("negative exponents not allowes"); } else { product = this.coefficient * that.coefficient; sumExponents = this.exponent + that.exponent; finalProduct = new Term(product, sumExponents); } return finalProduct; }

/** * Negates this Term, i.e. the coefficient is multiplied by -1. The contents of * this are unchanged. * * @return a negated version of the current Term. * @throws Exception from Term constructor */ public Term negate() throws Exception { Term negation = null; double negationEquation; if (this.exponent < 0) { throw new Exception("negative exponents not allowed"); } else { negationEquation = this.coefficient * (-1); negation = new Term(negationEquation, this.exponent); } return negation; }

/** * Subtracts another Term from this Term. The two Term objects must have the * same exponent value. The contents of this and that are unchanged. * * @param that The Term to subtract from this Term. * @return The new Term resulting from subtracting that Term from this Term. * @throws Exception If the exponents of this and that are not identical, throws * Exception with message: "exponents do not match" */ public Term subtract(final Term that) throws Exception { Term differenceTerms = null; double differenceCoefficients; if (this.exponent != that.exponent) { throw new Exception("exponents do not match"); } else { differenceCoefficients = this.coefficient - that.coefficient; differenceTerms = new Term(differenceCoefficients, this.exponent); } return differenceTerms; }

/** * Returns a string of format: * * ax^n * * where a is the coefficient, and n is the exponent. * * Returns "x^n" if a == 1, "ax" if n == 1, "a" if n == 0, "0" if a == 0. */ @Override public String toString() { String theString = ""; if (this.coefficient == 1) { theString = ""; } else if (this.coefficient == 0) { theString = "0"; } else { theString = this.coefficient + ""; } if (!theString.equals("0")) { if (this.exponent == 1) { theString = theString + "x"; } else if (this.exponent == 0) { theString = theString + this.coefficient; } else { theString = theString + "x^" + this.exponent; } } return theString; } }

THIRD CLASS - Coefficients.java

package cp213;

import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.List; import java.util.Scanner;

/** * DO NOT CHANGE THE CONTENTS OF THIS CLASS. * * A utility class that defines a list of double coefficients for the terms of a * polynomial. The position determines the exponent, i.e. the coefficient at * index 0 is for a term with exponent 0, at index 1 for a term with exponent 1, * and so on. Parses a comma-delimited string of the form: "c0,c1,c2,c3,..." * where each 'c' is a double coefficient. * * Makes the coefficients available through an iterator, either: * * for(double coefficient: coefficients) { * * or * * while(coefficients.hasNext()) { * double coefficient = coefficients.next(); * ... * */ public class Coefficients implements Iterable {

// Stores the coefficients in a List to easily provide an iterator. private final List coefficients = new ArrayList<>();

/** * Generates a List of coefficients from a comma-delimited string of double * values. * * @param string The comma-delimited string to parse. */ public Coefficients(final String string) { final Scanner parser = new Scanner(string); parser.useDelimiter(",");

while (parser.hasNext()) { this.coefficients.add(parser.nextDouble()); } parser.close(); }

/** * @return the degree of coefficients */ public int degree() { return this.coefficients.size() - 1; }

@Override public Iterator iterator() { return this.coefficients.iterator(); }

/** * Returns a string of the format: * * "[a0, a1, a2, ... , an-1]" * * where the number of coefficients is n. */ @Override public String toString() { return Arrays.toString(this.coefficients.toArray()); } }

Step by Step Solution

3.42 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

Lets start by discussing constructors and how they can be used in the context of the provided classes Constructors In Java a constructor is a special ... 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

Income Tax Fundamentals 2013

Authors: Gerald E. Whittenburg, Martha Altus Buller, Steven L Gill

31st Edition

1111972516, 978-1285586618, 1285586611, 978-1285613109, 978-1111972516

More Books

Students also viewed these Programming questions