# Polynomial Array List

• April 16th, 2013, 02:52 PM
Pinares
Polynomial Array List
Here's the assignment:

I. The Term Class

Create a class to represent a term in an algebraic expression. As defined here, a term consists of an integer coefficient and a nonnegative integer exponent. E.g.

• in the term 4x2, the coefficient is 4 and the exponent 2
• in -6x8, the coefficient is -6 and the exponent 8

Your class will have a constructor that creates a Term object with a coefficient and exponent passed as parameters, and accessor methods that return the coefficient and the exponent

II. The Polynomial Class

Now create a class to represent a polynomial. As defined here, a polynomial is a sequence of terms. E.g.

1. 3x2 + 4x4 + x6
2. 2 + 5x2 + 6x3 + 2x7
3. 4x10

The terms of polynomial 1 are (3,2), (4,4) and (1,6). The terms of polynomial 2 are (2,0), (5,2), (6,3) and (2,7). Polynomial 3 has only one term (4,10)

 To receive credit for this assignment, your class must use a generic “ArrayList of Term” to store the terms of a Polynomial object

Your class will have a constructor that creates an empty list and additional methods to do each of the following:

1. insert a new term in its proper place in a polynomial (see “Additional Specifications,” below)

2. return all the terms of a polynomial as a single line string, as shown here:

3x^2 + 4x^4 + x^6

3. delete a term from a polynomial (see “Additional Specifications,” below)

4. compute and return the product of all the terms of a polynomial

5. reverse the order of the terms in a polynomial (see “Additional Specifications,” below)

III. The Test Class

The main method of your test class will create a Polynomial object and then read and process a series of operations until end of file.

The operations are:

1. INSERT X Y

Insert a new term with coefficient X and exponent Y into its proper place in the polynomial

2. DELETE X Y

Remove the term with coefficient X and exponent Y from the polynomial

3. REVERSE

Reverse the order of the terms of the polynomial

4. PRODUCT

Compute and return the product of all the terms

Each operation is to be carried out by calling a method of the Polynomial class

Each operation read must be “echo printed” to the screen

After each operation, print the updated polynomial by calling the toString() method

For the PRODUCT operation, print the string returned
-----------------------------------------------------------------------------
My professor said I should get started on the test class first and he gave this algorithm:
---------------------------
while (! end of file)

get the next operation

call the method that handles it

call toString to print the updated list

Although the operations are to be read from a file, get started now by having the user enter them. Input files will be covered next class. You only need to enter one of each (INSERT X Y, DELETE X Y, REVERSE, PRODUCT) to verify that you are doing this correctly. Then, you can start implementing the methods of the Polynomial class, and implementing the Term class.
---------------------------------------
This is what I have so far

Code Java:

```import java.util.Scanner ; import javax.swing.JOptionPane;       /* * To change this template, choose Tools | Templates * and open the template in the editor. */   /** * * @author John */ public class PolynomialTest { public static void main(String[] args) { Polynomial Poly = new Polynomial();   String input = JOptionPane.showInputDialog("Enter coefficient and exponent" + "\nseparated by spaces.");   while(input != null) { Scanner scan = new Scanner(input); int coefficient = scan.nextInt(); int exponent = scan.nextInt();   Term next = new Term(coefficient, exponent);   Poly.insert(next); input = JOptionPane.showInputDialog("Enter next coefficent and exponent"); }   }   }```

This assignment completely has me stuck. Any help or nudge in the right direction is much apreciated.
• April 16th, 2013, 03:16 PM
jps
Re: Polynomial Array List
What are you stuck on?
• April 16th, 2013, 03:27 PM
Pinares
Re: Polynomial Array List
As of now, the test class, this is what is has to do:

III. The Test Class

The main method of your test class will create a Polynomial object and then read and process a series of operations until end of file.

The operations are:

1. INSERT X Y

Insert a new term with coefficient X and exponent Y into its proper place in the polynomial

2. DELETE X Y

Remove the term with coefficient X and exponent Y from the polynomial

3. REVERSE

Reverse the order of the terms of the polynomial

4. PRODUCT

Compute and return the product of all the terms

Each operation is to be carried out by calling a method of the Polynomial class

Each operation read must be “echo printed” to the screen

After each operation, print the updated polynomial by calling the toString() method

For the PRODUCT operation, print the string returned

my professor gave an algorithm, but its pretty vague and I'm just not sure how to get started.
Here is the algorithm:

while (! end of file)

get the next operation

call the method that handles it

call toString to print the updated list

Although the operations are to be read from a file, get started now by having the user enter them. Input files will be covered next class. You only need to enter one of each (INSERT X Y, DELETE X Y, REVERSE, PRODUCT) to verify that you are doing this correctly. Then, you can start implementing the methods of the Polynomial class, and implementing the Term class.
• April 16th, 2013, 06:23 PM
Norm
Re: Polynomial Array List
Try to do the assignment one step at a time. When you have problems with a step, post the code and ask your questions about the problems you are having.
• April 16th, 2013, 06:33 PM
Pinares
Re: Polynomial Array List
Okay. Sorry about that, I was getting a bit frustrated. Right now I am working on my insert method that adds terms to the list in descending order of power, I'm having trouble with the descending order part. How can I compare the exponents if they are part of the array list?
• April 16th, 2013, 06:38 PM
Norm
Re: Polynomial Array List
Quote:

How can I compare the exponents if they are part of the array list?
Get them out of the arraylist so they can be compared.
• April 16th, 2013, 07:32 PM
Pinares
Re: Polynomial Array List
So here is my latest insert method:
Code Java:

```public void insert(int coeff, int expo) { if(terms.isEmpty()) { terms.add(null); }   Term biggest = terms.get(0);   for(int i = 1; i < terms.size(); i++) { Term current = terms.get(i); if(current.getExponent() > biggest.getExponent()) { terms.add(i, current); }   else { terms.add(current); } }       System.out.println("insert method called for " + coeff + " " + expo); }```

and this is my test class

Code Java:

```public class PolynomialTest { public static void main(String[] args) throws IOException { Polynomial Poly = new Polynomial();   String input = JOptionPane.showInputDialog("Enter operation, coefficient" + " and exponent, separated by spaces");   while(input != null) { Scanner scan = new Scanner(input) ; String operation = scan.next(); int coefficient = scan.nextInt(); int exponent = scan.nextInt();   if(operation.equalsIgnoreCase("insert")) { Poly.insert(coefficient, exponent); }   if(operation.equalsIgnoreCase("delete")) { Poly.delete(coefficient, exponent); }   if(operation.equalsIgnoreCase("reverse")) { Poly.reverse(); }   if(operation.equalsIgnoreCase("product")) { Poly.product(); } }   Poly.toString();   }   }```

I'm not getting any output though, any thoughts?
• April 16th, 2013, 07:34 PM
Norm
Re: Polynomial Array List
Quote:

I'm not getting any output though, any thoughts?
Where are the println() statements that would create output?
Add some more println() statements to print out messages when methods execute and to show the values of variables as they are changed and used.
• April 16th, 2013, 07:52 PM
Pinares
Re: Polynomial Array List
There is a println() statement at the end of the insert method that was printing earlier, but now not even that shows up. it gives me this message:

Exception in thread "main" java.lang.NullPointerException
at Polynomial.insert(Polynomial.java:25)
at PolynomialTest.main(PolynomialTest.java:32)
Java Result: 1

which are:

public void insert(int coeff, int expo)
{
for(int i = 1; i < terms.size(); i++)// line 25
{

if(operation.equalsIgnoreCase("insert"))
{
Poly.insert(coefficient, exponent);// line 32
}
• April 16th, 2013, 08:07 PM
Norm
Re: Polynomial Array List
Quote:

Exception in thread "main" java.lang.NullPointerException
at Polynomial.insert(Polynomial.java:25)
There is a variable with a null value on line 25. Look at line 25 in the your source and see what variable is null. Then backtrack in the code to see why that variable does not have a valid value.
If you can not tell which variable it is, add a println just before line 25 and print out the values of all the variables on that line.
• April 16th, 2013, 09:19 PM
Pinares
Re: Polynomial Array List
Still nothing for any variable on that line, but when I delete everything and just keep this line:

System.out.println("insert method called for " + coeff + " " + expo);

it prints in an infinite loop.
• April 17th, 2013, 05:55 AM
Norm
Re: Polynomial Array List
Quote:

an infinite loop.
What variable's value controls the looping? Why doesn't that variable's value change so the loop will end?
• April 17th, 2013, 05:42 PM
Pinares
Re: Polynomial Array List
I guess it would be the input variable that controls the loop, i have:

while(input !=null)

but even after just one input, the input window closes and it starts printing. Maybe its the way I set up my scanner object?
• April 17th, 2013, 06:23 PM
Norm
Re: Polynomial Array List
To exit the loop, the variable: input has to be given a null value inside the loop.
• April 17th, 2013, 08:39 PM
Pinares
Re: Polynomial Array List
It does not give me a chance to put in another input.
• April 17th, 2013, 09:05 PM
Norm
Re: Polynomial Array List
If the reading of the input were inside the loop, then it would.
• April 17th, 2013, 10:26 PM
Pinares
Re: Polynomial Array List
Okay, I fixed the output loop problem, now I'm still having trouble with my insert method:

Code Java:

``` public void insert(int coeff, int expo) { for(int i = 0; i < terms.size(); i++) { if(terms.isEmpty()) { terms.add(null); }   Term biggest = terms.get(0);     Term current = terms.get(i); if(current.getExponent() > biggest.getExponent()) { terms.add(i, current); }   else { terms.add(current); } } System.out.println(terms);     System.out.println("insert method called for " + coeff + " " + expo); }```

here is my test class again:

Code Java:

```public class PolynomialTest { public static void main(String[] args) throws IOException { Polynomial Poly = new Polynomial();   String input = JOptionPane.showInputDialog("Enter operation, coefficient" + " and exponent, separated by spaces");   while(input != null) {     Scanner scan = new Scanner(input) ; String operation = scan.next(); int coefficient = scan.nextInt(); int exponent = scan.nextInt();   Term number = new Term(coefficient, exponent);   if(operation.equalsIgnoreCase("insert")) { Poly.insert(coefficient, exponent); }   if(operation.equalsIgnoreCase("delete")) { Poly.delete(coefficient, exponent); }   if(operation.equalsIgnoreCase("reverse")) { Poly.reverse(); }   if(operation.equalsIgnoreCase("product")) { Poly.product(); }   input = JOptionPane.showInputDialog("Enter next operation, coefficient, and exponent"); }   Poly.toString();   }   }```

when I call the insert method, it says that there is a null pointer exception at line 25 in the method which is:

for(int i = 0; i < terms.size(); i++)

I'm guessing the only two variables that can be null are i and term.size(), but it gives me the same message when I try to print either variable.

--- Update ---

Now I fixed the output, but the variable 'terms' is not getting the right values. it returns '[]' when it should return a coefficient and an exponent. I think it has to do with the statement in the insert method:

but there's a few .add() methods to the polynomial class and none of them seem to work.
• April 18th, 2013, 06:30 AM
Norm
Re: Polynomial Array List
Quote:

it returns '[]' when it should return a coefficient and an exponent.
What method are you talking about?

Quote:

.add() methods to the polynomial class and none of them seem to work.
Add some println methods that print out the contents of the class being added to after each call to the add() methdod to show what is in the collection.
• April 18th, 2013, 10:28 AM
Pinares
Re: Polynomial Array List
at first I had terms.add(null); which I thought was the problem, then I replaced it with terms.add(coeff); terms.add(expo); and after each time I used Sop(terms); and it has the same result.
• April 18th, 2013, 10:30 AM
Norm
Re: Polynomial Array List
Quote:

and it has the same result.
Please post the program's output, explain what the problem is and show what the output should be.

Also post the current version of the code that can be compiled and executed for testing.