Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 20 of 20

Thread: Polynomial Array List

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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

    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.


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Polynomial Array List

    What are you stuck on?

  3. #3
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default 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.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Polynomial Array List

    So here is my latest insert method:
    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

    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?

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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
    }

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    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.
    If you don't understand my answer, don't ignore it, ask a question.

  11. #11
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  12. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    an infinite loop.
    What variable's value controls the looping? Why doesn't that variable's value change so the loop will end?
    If you don't understand my answer, don't ignore it, ask a question.

  13. #13
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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?

  14. #14
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    To exit the loop, the variable: input has to be given a null value inside the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #15
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Polynomial Array List

    It does not give me a chance to put in another input.

  16. #16
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    If the reading of the input were inside the loop, then it would.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #17
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default Re: Polynomial Array List

    Okay, I fixed the output loop problem, now I'm still having trouble with my insert method:

     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:

    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:

    terms.add(null);

    but there's a few .add() methods to the polynomial class and none of them seem to work.

  18. #18
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

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

    .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.
    If you don't understand my answer, don't ignore it, ask a question.

  19. #19
    Member
    Join Date
    Jan 2013
    Posts
    32
    Thanks
    5
    Thanked 0 Times in 0 Posts

    Default 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.

  20. #20
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Polynomial Array List

    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.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Urgent Polynomial Assignment !!!!!!!!!
    By thesoulpatchofBruce in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 9th, 2012, 01:38 PM
  2. Converting Two dimensional array into an Array list
    By NewbieJavaProgrammer in forum Object Oriented Programming
    Replies: 11
    Last Post: September 29th, 2012, 04:23 PM
  3. Solving a polynomial
    By arvindbis in forum Java Theory & Questions
    Replies: 9
    Last Post: March 8th, 2012, 12:16 AM
  4. Array List of Array Lists working for first item but not for second.
    By javapenguin in forum Collections and Generics
    Replies: 6
    Last Post: February 15th, 2012, 05:12 PM
  5. API polynomial java
    By mcherkao in forum Java SE APIs
    Replies: 1
    Last Post: September 10th, 2010, 08:39 AM