Results 1 to 3 of 3
 02252010, 03:40 AM #1Member
 Join Date
 Feb 2010
 Posts
 6
 Rep Power
 0
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; } } }
Last edited by ramathews; 02262010 at 01:48 AM. Reason: Updated code with my progressing changes

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 }
Java Code:String str = "hello"; foo(str); // won't work since parameter must be an int
Much luck.
 02252010, 04:42 AM #3Member
 Join Date
 Feb 2010
 Posts
 6
 Rep Power
 0
Similar Threads

Creating an Array of Objects
By int80 in forum New To JavaReplies: 4Last Post: 08092011, 12:40 PM 
Creating custom objects
By coltragon in forum New To JavaReplies: 11Last Post: 12292009, 11:50 PM 
Creating an array of objects
By geowizard in forum New To JavaReplies: 5Last Post: 11162009, 02:25 AM 
Creating objects question
By sergm in forum New To JavaReplies: 2Last Post: 12272007, 05:10 PM
Bookmarks