Results 1 to 3 of 3
  1. #1
    jackfletcher is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Question Running method on object problem

    Hi everyone,

    I've got a problem with my code, which consists of 2 classes.

    The error I'm being given at compile is <identifier> expected at the line:

    37. poly1.evaluate();

    Here is the rest of the code for classes Polynominal and Test:

    Java Code:
    // Import ArrayList
    import java.util.*;
    import java.lang.Math;
    public class Polynominal
    {
    
    // variable assignments
    
    public int x = 0;
    public int sum = 0;
    
    
    // Create a new empty ArrayList called 'coefficient'
    public ArrayList<Integer> coefficient = new ArrayList<Integer>();
    
    
    /**
    * Constructor for objects of class Polynominals
    */
    Polynominal(ArrayList<Integer> coefficient)
    {
    this.coefficient = coefficient;
    }
    
    // Method: evaluate using X placeholder
    
    public int evaluate () {
    
    int sum = 0;
    
    for (int v: coefficient)
    {
    int get = coefficient.get(v); // get the coefficient
    int eval = get*(int)Math.pow(x,v); // evaluate each block
    sum += eval; // add up running total of block addition
    }
    return sum;
    }
    
    // Method: toString
    
    public String toString() {
    String output = "";
    
    
    for (int i=0; i <coefficient.size(); i++) //TODO: change to <size
    {
    output = output + " " + this.coefficient.get(i);
    }
    
    return output;
    
    }
    }
    
    
    import java.util.*;
    public class Test
    {
    public static void main(String[] args) {
    
    // Create 2 ArrayLists
    ArrayList<Integer> array = new ArrayList<Integer>();
    ArrayList<Integer> array2 = new ArrayList<Integer>();
    
    // Add elements to 'array'
    array.add(7);
    array.add(0);
    array.add(0);
    array.add(1);
    array.add(3); // 70013
    
    // Add elements to 'array2'
    array2.add(8);
    array2.add(1);
    array2.add(0);
    array2.add(0);
    array2.add(2); // 81002
    
    // Create 2 Polynominal objects, taking the arraylists as parameters
    Polynominal poly1 = new Polynominal(array);
    Polynominal poly2 = new Polynominal(array2);
    
    // Display elements in array object
    // Uses toString class to print contents of the object
    System.out.println(poly1);
    System.out.println(poly2);
    
    
    }
    
    // Evaluate polynominal 1 using evaluate() in Polynominal class
    poly1.evaluate();
    
    // Evaluate polynominal 2 using evaluate() in Polynominal class
    poly2.evaluate();
    }

    Hope somebody can help me with this one!
    Thanks a lot.
    Last edited by jackfletcher; 10-09-2012 at 09:22 PM.

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default Re: Running method on object problem

    The last two ones are out of the main method :D move them a little bit higher ^^

  3. #3
    jackfletcher is offline Member
    Join Date
    Oct 2012
    Posts
    3
    Rep Power
    0

    Default Re: Running method on object problem

    Quote Originally Posted by eRaaaa View Post
    The last two ones are out of the main method :D move them a little bit higher ^^
    Great! Thanks so much I've sorted it although now it's giving me another error when I run the Test class :/

    Java Code:
    int get = coefficient.get(v);
    Gives the error:
    java.lang.IndexOutOfBoundsException:
    Index: 7, Size: 5 (in java.util.ArrayList)

    When the Test class is run.
    Thanks.

Similar Threads

  1. Replies: 3
    Last Post: 03-27-2012, 11:23 PM
  2. Replies: 7
    Last Post: 12-18-2011, 04:13 AM
  3. Replies: 1
    Last Post: 10-27-2011, 07:16 AM
  4. Problem Of Scanner Object with its method nextLine()
    By Cluster Storm in forum AWT / Swing
    Replies: 12
    Last Post: 06-17-2010, 05:40 PM
  5. Replies: 0
    Last Post: 08-26-2009, 01:03 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •