Lab 8: Using a Java Collection to Add Polynomials
This is a two week lab. The final product will be due on November 27
but stages will be due earlier
- Before leaving lab today you must demonstrate that you have a class Term
written and working.
- In lab next week, November 20, you must demonstrate that you have the
class contining List<Term> able to input, store and output a
single polynomial.
- Your final product should be able to add any number of polynomials by adding
two at a time, saving the sum, then add another if the user desires.
- It should also contain a menu that lets the user decide what they want to do.
Background
This assignment is #2 under Programming projects on page 253 of your text, but
I will reproduce it here, along with a few hints.
The Programming Problem
- We can represent a polynomial as an ordered list of terms, where the terms are ordered by their exponents
- To add two polynomials, you traverse both lists and examine the two terms at the current iterator position.
- If the exponent of one is smaller than the exponent of the other, then insert this one into the result and advance that list's iterator
- If the exponents are equal, then create a new term with that exponent and the sum of the coefficients, and advance other iterators.
- For example 3x4 + 2x2 + 3x + 7 added to 2x3
+ 4x + 5 is equal to
3x4 + 2x3 + 2x2 + 7x + 12
What to do
- Write a program to read and add polynomials.
- You should define a class Term that contains the exponent and coefficient
- This class should implement the Comparable interface by comparing
the values of the exponents.
How to Start
- As usual, you should start this project by giving it some thought
instead of beginning to code right away.
- Draw a diagram showing how you intend to store the term 3x4
- Will your diagram work for all the other terms?
- What Java construct will you use to store a term?
- I assume you answered the above question with "a class". You now
need to figure out what data members will be needed, and what methods
will be needed by the class.
- You could now go ahead and implement the class, but when you reflect on
the rest of the problem, you may want to modify your class design.
- After the class is well thought out, you need to give some thought
to how you will store the polynomial
- A polynomial is made up of terms, along with the sign of
the term.
- This assignment is asking you to save the terms that make up a
single polynomial in a list.
- Use one of the lists available from the library to hold
the terms in your list.
- You should give some thought to which list will be the most efficient:
a linked list, or an array list.
- Start by making sure you can input, store, and output a single
polynomial, then worry about how to add them.