Results 1 to 3 of 3
  1. #1
    ramathews is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Problem Creating/implementing Objects

    Hello, This is my first attempt at a "real" program and I think I have it all down except for constructing the object.

    The program is supposed to take two files specified by the user, check that they are sorted, parse each line into an array, and create an object "Polynomial" out of it. The objects are then merged on the fly while they are displayed on the screen. The only compiler errors I get are "cannot find symbol" errors that I think are related to the fact that I messed up the constructor. Here is a typical example of my errors:

    Error: cannot find symbol
    symbol : method toString(polynomial.Polynomial)
    location: class polynomial.Polynomial

    The code(note: i didn't include the exception class):
    Java Code:
    package polynomial;
    
    import polynomial.*;
    import java.util.*;
    import java.io.*;
    
    public class Project1 {
      public static void main (String[] args) {
        
        Scanner fileA = checkSorted();
        Scanner fileB = checkSorted();
        
        //perform merge operation
        
        //get value1 from the first file
        Polynomial value1 = new Polynomial(Polynomial.input(fileA));    
        //get value2 from the second file
        Polynomial value2 = new Polynomial(Polynomial.input(fileB));
        
        while (fileA.hasNextLine() && fileB.hasNextLine()) {
          if (value1.compareTo(value2) > 0){
            System.out.println(value1.toString(value1));
            value1 = value1.input(fileA);
          }  
          else {
            System.out.println(value2.toString(value2));
            value2 = value2.input(fileB);
          }
          
          while (fileA.hasNextLine());
          System.out.println(value1.toString(value1));
          value1 = value1.input(fileA);
          
          while (fileB.hasNextLine());
          System.out.println(value2.toString(value2));
          value1 = value1.input(fileB);
        }
      }   
      
      public static Scanner checkSorted() throws fileUnsorted {
        String file = "";
        String currentLine = "";
        String nextLine = "";
        int compRes = 0;
        
        Scanner keyboard = new Scanner(System.in);
        //Request and get file name from user
        System.out.println("Enter the name of of the file:");
        file = keyboard.next();
        Scanner fileA = new Scanner(new FileReader(file));
        currentLine = fileA.nextLine();
        
        try{
          //Read and verify that data is in ascending order.  If no, throw exception, and exit.  If yes, close and reopen.
          while (fileA.hasNextLine()){
            nextLine = fileA.nextLine();
            compRes = currentLine.compareTo(nextLine);
            
            if (compRes >= 0)
              currentLine = nextLine;
            
            else throw new fileUnsorted();
            
          }
        } 
        catch(fileUnsorted unsorted){
          System.out.println("File is not sorted.");
          System.exit(1);
        }
        
        fileA.close();
        Scanner fileB = new Scanner(new FileReader(file));
        
        return  fileB;                                         
      }
    }
    
    ******
    package polynomial;
    import polynomial.*;
    import java.util.*;
    import java.io.*;
    
    public class Polynomial implements Comparable {
        
     public static Polynomial input(Scanner file){
      int coeff = 0;
      int exp = 0;
      
      Scanner line= new Scanner(file.nextLine());
      
        //extract first exponent and coefficient
      coeff = line.nextInt();
      exp = line.nextInt();
        
        //allocate an array of integers whose size is one more than the exponent
      int[] polyArray = new int[exp+1];
      
        //initialize all the exponents in the array to zero
      for (int i = 0; i < polyArray.length; i++)
        polyArray[i] = 0;
      
      //put the first coefficient in the array using the exponent as the subscript
      polyArray[exp] = coeff;
      
       //while more coefficients and exponents
      while (line.hasNextInt()) {
         //extract another coefficient and exponent
        coeff = line.nextInt();
        exp = line.nextInt();
    
      //put the coefficient in the array using the exponent as the subscript
        polyArray[exp] = coeff;
    
      //make a new polynomial using that coefficient array and return it
        new Polynomial() = polyArray;
        return newPolynomial;
     }
     } 
     public String toString(Polynomial value){
        int index = 0;
        String polyString = "";
        
        for (index = value.length; index > 0; index--){
          if (value[index] != 0)
            polyString += value[index] + "x^" + index + "+";
        }
        
        polyString += value[0];
        return polyString;
     }
      
     public int compareTo(Object poly2){
        //compares the calling object to the object passed as a parameter
      int result = 0;
      
        //Takes care of the easy case of different exponents
      if (this.length > ((Polynomial)poly2).length)
        return result = 1;
      else if (this.length < poly2.length)
        return result = -1;
        
        //Compares to see if it is the same polynomial
      boolean res = Arrays.equals(this, poly2);
      if (res == true)
        return result = 0;
    
        //Drlls down the polynomial to find out which is bigger
      if (this.length == poly2.length)
        for (int index = this.length; index >= 0; index--)
          if (this[index] > poly2[index]){ 
            return result = 1;
            break;
          }  
          else if (this[index] < poly2[index]){
            return result = -1;
            break;
          }
     }
    }
    Thank you!
    Last edited by ramathews; 02-26-2010 at 12:48 AM. Reason: Updated code with my progressing changes

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    The error is telling you exactly what is wrong. It says it can't find a certain method (and gives you the name) that takes a certain parameter (and gives you the type of the parameter). You know that your parameters that you pass to a method must match the type of parameter that is declared by the method. For instance if I have a method foo:
    Java Code:
    public void foo(int i) {
      // some code in here
    }
    I can't call it with a String parameter:

    Java Code:
    String str = "hello";
    foo(str);  // won't work since parameter must be an int
    You have the same type of error above. I can't tell if you have other errors since your code does not show its formatting without code tags. Please look at my signature below and use this information to edit your post above, giving it code tags.

    Much luck.

  3. #3
    ramathews is offline Member
    Join Date
    Feb 2010
    Posts
    6
    Rep Power
    0

    Default Ah... Rookie Mistake. Thanks!

    Thank you, Fubarable for the catch on that parameter passing as well as setting me wise to the coding tag. Any other tips you can provide are greatly appreciated!
    Andy

Similar Threads

  1. Creating an Array of Objects
    By int80 in forum New To Java
    Replies: 4
    Last Post: 08-09-2011, 12:40 PM
  2. Creating custom objects
    By coltragon in forum New To Java
    Replies: 11
    Last Post: 12-29-2009, 10:50 PM
  3. Creating an array of objects
    By geowizard in forum New To Java
    Replies: 5
    Last Post: 11-16-2009, 01:25 AM
  4. Creating objects question
    By sergm in forum New To Java
    Replies: 2
    Last Post: 12-27-2007, 04:10 PM

Posting Permissions

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