CCSU Fall 1998 Project 4 Posting Date: November 30, 1998 Due Date: December 9, 1998 Relevant Demos: None. Instead, see the linked list code in chapter 5 of the text. You may find it useful to download a skeleton of the Applet you are required to write. The file is PolyArithmetic.java, and it is available from the CS152 Projects page. This project comes in only one part. It covers material on linked lists. Background: The purpose of this project is to give you practice with linked lists, and and practice with an important application of linked lists, the implementation of a polynomial arithmetic package which handles sparse polynomials well. You are familiar with polynomials from algebra. For example, P(x) = 1.0 - 2.3x + 3.5x^2 is a simple example of a polynomial. Here, we are using a mixture of mathematical and computer scientific notation: We have omitted the * operator as in mathematics, but we have used the ^ operator to indicate exponentiation, as in many programming languages (but NOT Java). We are also writing polynomials in order of ascending degree, with the lowest degree term first, rather than in the more common order of descending degree with highest degree term first. The simplest way to represent polynomials is to use arrays. For example, if we used arrays of length 5, we could represent all polynomials up to degree 4, and the polynomial P(x) above would be represented this way: ----- ------ ----- ----- ----- array P | 1.0 | -2.3 | 3.5 | 0.0 | 0.0 | ----- ------ ----- ----- ----- index 0 1 2 3 4 But what if we wanted to represent a polynomial like Q(x) = 1.0 - 3.5x^99 A polynomial like this is called a sparse polynomial because most of its coefficients are 0. We would have to use an array of length 100 to represent Q(x), and 98% of the space would be wasted because it would simply hold 0's. An alternative method of representing polynomials is to use linked lists. This method works effectively for all polynomials, of whatever degree, and it really shines, in comparison to the array method, for sparse polynomials. The linked list representation of P(x) would look something like this: ------- | --- | ------------- -------------- ------------- | | --|-|--> | 1.0 | 0 | --|--> | -2.3 | 1 | --|--> | 3.5 | 2 | --|--> null | --- | ------------- -------------- ------------- ------- Link Link Link LinkedList p The linked list representation of Q(x) is even more concise: ------- | --- | ------------- --------------- | | --|-|--> | 1.0 | 0 | --|--> | -3.5 | 99 | --|--> null | --- | ------------- --------------- ------- Link Link LinkedList q Description: Write a Java Applet so a user can do the following: (1) Enter coefficients and degrees and build polynomials term by term. (2) Add two polynomials (3) Subtract two polynomials (4) Multiply two polynomials (5) Evaluate polynomials. (Evaluate means plug in). Your Applet will use a Java class which represents polynomials using linked lists. You will have to write this class, and you will have to include methods to do polynomial arithmetic and evaluation in this class. Here is the skeleton of the basic Polynomial class definitions: (There may be some dumb errors in this code, such as unmatched braces. If so, part of the project is to straighten them out.) class Term { // a Term is a Link in a linked list representation of a // polynomial -- it corresponds to a term in a mathematical // polynomial double coefficient; int degree; Term next; // constructor Term( double c, int d ) { coefficient = c; degree = d; next = null; } you probably don't need any more methods in this class } // class Term class Polynomial { // a Polynomial is a LinkedList object. Term first; methods add, subtract, multiply, and evaluate go here. Methods add, subtract and multiply return Polynomials. Method evaluate returns a double. You might want to parametrize method add this way: // add Polynomial q to this polynomial Polynomial add(Polynomial q) You also might want a utility method to multiply a polynomial by a constant. Then you could subtract by multiplying by the constant -1 and then adding. Be efficient! Write a utility method to multiply a polynomial by a monomial. Then repeatedly use your monomialMultiply method and your add method in method multiply. Don't make work for yourself! Assume all polynomials are entered in order of increasing degree, lowest degree term at the front of the list. (For EXTRA CREDIT, you could swap in the LinkedList version of insertionSort in the text to sort polynomials entered in a random order.) methods insertAtFront and insertAfter go here. Method insertAtFront inserts a Term at the front of the list. Method insertAfter inserts a Term after a given Term. You can find code for these (as LinkedList methods) in the text and in the notes. You will use these methods to build polynomials term by term in your Applet, and you will use them in the polynomial arithmetic methods (where you also will have to build polynomials term by term). You probably won't need methods deleteFromFront and deleteAfter, but if you do, they would probably go here. Method deleteFromFront deletes the Term at the front of the list. Method deleteAfter deletes the Term after a given Term. } // class Polynomial Just to drive the point home, here is how our old friend, the polynomial Q(x) = 1.0 - 3.5x^99 would be represented using objects of the classes Polynomial and Term ------- | --- | ------------- --------------- | | --|-|--> | 1.0 | 0 | --|--> | -3.5 | 99 | --|--> null | --- | ------------- --------------- ------- Term Term Polynomial q Here is an outline of how the Applet is to work. The Applet Panel will look something like this after the user has entered polynomials P(x) and Q(x) by building them up term by term and then adding them together: ------------------------------------------------------- | | | --------------------------------------------- | | P(x) | 1.0 + -2.3x + 3.5x^2 | | | --------------------------------------------- | | | | --------------------------------------------- | | Q(x) | 1.0 + -3.5x^99 | | | --------------------------------------------- | | | | --------------------------------------------- | | R(x) | 2.0 + -2.3x + 3.5x^2 + -3.5x^99 | | | --------------------------------------------- | | | | --------------- ------- | | P(x) Term Coeff | 3.5 | Degree | 2 | | | --------------- ------- | | | | --------------- ------- | | Q(x) Term Coeff | 3.5 | Degree | 2 | | | --------------- ------- | | | | ---------------------- ---------------------- | | | Enter P(x) Term | | Enter Q(x) Term | | | ---------------------- ---------------------- | | | | -------------- -------------- ------------- | | | Clear P(x) | | Clear Q(x) | | Clear R(x) | | | -------------- -------------- ------------- | | | | ---------- --------------- --------------- | | | Add | | Subtract | | Multiply | | | ---------- --------------- --------------- | | | | ----------- ----------------- | | Evaluate At | | Value | | | | ----------- ----------------- | | | | -------------- -------------- -------------- | | | Eval P(x) | | Eval Q(x) | | Eval R(x) | | | -------------- -------------- -------------- | | | ------------------------------------------------------- Most of the TextFields and Buttons are self explanatory. The Clear Buttons set the corresponding Polynomials to null. The value of the null Polynomial will always be 0. Be sure that your Applet can evaluate Polynomials at doubles. To drive this point home, ANY STUDENT WHOSE APPLET ONLY ACCEPTS INTEGER VALUES OF x FOR EVALUATION automatically gets a grade of C or worse. General Instructions: (1) Hand in a FOLDER containing (1) a printed copy of the text of the PolyArithmetic.java Applet, and the Term.java and Polynomial.java classes, and (2) a 3.5" floppy disk with the .java and .class files for your classes. The folder should have a pocket, as described in project 1. (2) The text of each of your three classes must be contained in separate .java files. Your .java files MUST be named and PolyArithmetic.java, Term.java and Polynomial.java, and your .class files must be similarly named. There are no input or output files. (3) Your program must begin with a remark box, as described in project 1. Under files, list none. (4) Each method in your program must begin with a remark box as described in project 1. (5) Your program MAY NOT use a depreciated api, as described in project 1.