Lab 5 : Polynomials
Professor: Ronaldo Menezes
TA: Ivan Bogun
Department of Computer Science
Florida Institute of Technology
February 4, 2014
1
CSE 2010 Algorithms and Data Structures
Spring 2014
1 General description
Implement a data structure representing a polynomial. Use linked list in your implementation ( you have to implement your own linked list class, interface will be given). Each
node in the linked list should represent an exponent of the polynomial, it should have
two keys: integer value of the exponent and coefficient. Nodes in the linked list should
exist only for exponents with non-zero coefficients.
2 Implementation
In this part you will have to implement interface with generic class. If you are not familiar
with generics in Java consult tutorials available online 1 . 2
2.1 LinkedList.java
Create LinkedList.java which implements interface LinkedListInterface.
// LinkedListInterface.java
public interface LinkedListInterface<Key> {
public void
public void
denoted
public void
add(Key key);// add key into the beginning of the list
add(int index, Key key); // add key at specific position
by index
clear(); // delete elements from the list
public Key get(int index);// retrieve element at index
public Key getFirst(); // get first element
public Key getlast(); // get last element
public Key peek();
// return the last element in the list
without deleting it
public Key remove();
// remove the head of the list
public Key remove(int index); // remove element at index
public int size();
// return size of the list
public int indexOf(Key key); // find index of the element key, if the
element is not present in the list return -1
public Key[] toArray();
// return array representation of the list
}
LinkedList class should be in the form:
public class LinkedList<Key> implements LinkedListInterface<Key>{
1
2
http://docs.oracle.com/javase/tutorial/extra/generics/index.html
http://docs.oracle.com/javase/tutorial/java/generics/
2
CSE 2010 Algorithms and Data Structures
Spring 2014
private Node head;
private Node tail;
private int size;
private class Node{
Key key;
Node next;
}
}
Lastly, implement the class Polynomial.java
public class Polynomial {
private LinkedList<Monomial> list; // list representation of the Polynomial,
note that you have to use your own LinkedList, not the one provided in
java.util.LinkedList.
private class Monomial { // private class with information containing
exponent, coefficient
int exponent;
double coefficient;
}
public Polynomial(String string) {
// parse polynomial from the string
}
public Polynomial add(Polynomial poly) {
// return the sum of the two polynomials
}
public Polynomial subtract(Polynomial poly) {
// return this-poly polynomial
}
public Polynomial multiply(Polynomial poly) {
// multiply polynomial with poly and return the result
}
public double evaluate(double x) {
// evaluate polynomial at x
}
public String toString(){
// return polynomial in the printable format
}
}
3
CSE 2010 Algorithms and Data Structures
Spring 2014
Figure 1: Representation of p(x) = x4 + 5x2 − x − 3 via linked list.
2.2
String format
Strings to be fed into the polynomial constructor will have the following format:
• every non-zero coefficient is written explicitly
• every non-negative exponent is written explicitly
• exponent is denoted by symbol ^
Example: the polynomial p(x) = x4 + 5x2 − x − 3 would be represented as
str=1x^4+5x^2-1x^1-3x^0
3 Optional
3.1 Division
Implement the function which will perform division with reminder ( see http://en.
wikipedia.org/wiki/Polynomial_long_division). Name your function divide(). The
function should return two polynomials: the result and reminder.
4 Sample input-output
Testing class Driver.java.
public class Driver{
public static void main(String[] args) {
String strPoly1 = "-1x^4+5x^2-1x^1-3x^0";
String strPoly2 = "1x^3+7x^1-5x^0";
4
CSE 2010 Algorithms and Data Structures
Spring 2014
Polynomial poly1 = new Polynomial(strPoly1);
Polynomial poly2 = new Polynomial(strPoly2);
System.out.println(poly1.toString());
System.out.println(poly2.toString());
System.out.println("Sum");
Polynomial sum = poly1.add(poly2);
System.out.println(sum.toString());
System.out.println("Difference");
Polynomial difference = poly1.subtract(poly2);
System.out.println(difference.toString());
System.out.println("Multiplication");
Polynomial mult = poly1.multiply(poly2);
System.out.println(mult.toString());
// basic testing
double[] x = { 0.1, 0.2, -1, -5, 1, 5, 0 };
boolean isSumCorrect = true;
boolean isDifferenceCorrect = true;
boolean isMultiplicationCorrect = true;
double tol = 1e-3;
for (int i = 0; i < x.length; i++) {
if (Math.abs(poly1.evaluate(x[i]) + poly2.evaluate(x[i])
- sum.evaluate(x[i])) > tol) {
isSumCorrect = false;
}
if (Math.abs(poly1.evaluate(x[i]) - poly2.evaluate(x[i])
- difference.evaluate(x[i])) > tol) {
isSumCorrect = false;
}
if (Math.abs(poly1.evaluate(x[i]) * poly2.evaluate(x[i])
- mult.evaluate(x[i])) > tol) {
isSumCorrect = false;
}
}
System.out.println("Polynomial sum: " + isSumCorrect);
System.out.println("Polynomial difference: " +
isDifferenceCorrect);
System.out.println("Polynomial multiplication: "
+ isMultiplicationCorrect);
}
5
CSE 2010 Algorithms and Data Structures
}
6
Spring 2014
CSE 2010 Algorithms and Data Structures
Spring 2014
Output:
-1.0x^4+5.0x^2-1.0x^1-3.0x^0
1.0x^3+7.0x^1-5.0x^0
Sum
-1.0x^4+1.0x^3+5.0x^2+6.0x^1-8.0x^0
Difference
-1.0x^4-1.0x^3+5.0x^2-8.0x^1+2.0x^0
Multiplication
-1.0x^7-2.0x^5+4.0x^4+32.0x^3-32.0x^2-16.0x^1+15.0x^0
Polynomial sum: true
Polynomial difference: true
Polynomial multiplication: true
5 Grade breakdown
basis
Implementation
Linked List
Polynomial
Comments
Javadocs
General
Overall
Compiled
Style
Runtime
Total
7
grade
(60)
30
30
(20)
10
10
(20)
5
5
10
100
© Copyright 2026 Paperzz