Results 1 to 4 of 4
Thread: Problem with Constructor
- 03-09-2009, 01:25 AM #1
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
Problem with Constructor
Hi
I am working on an assignment for a class and it is supposed to read in a file that is a text file representing a graph and traverse it. And the teacher gave us the .class file as well as the traversal file, and I cannot get it to work with my Graph File. I get the error:
@MyProjects\GraphTraversals.java:15: cannot find symbol
symbol : constructor Graph(int,int,int)
location: class Graph
H = new Graph( G.numberOfNodes(), G.DIRECTED, G.UNWEIGHTED );
^
1 error
I thought I made the constructor, but apparently I did it wrong. I am sure there are other problems as well with the open and the openToRead methods but I can't really tell because it wont compile with that error.
It probably wont compile because I did not include the code for the BasicGraph class. But maybe it is a simple problem and if someone could help, thank you :) If needed, I will include the class file but I havent typed up the code.
Java Code:import java.io.*; import java.util.*; public class Graph extends BasicGraph{ private Graph G; private File diskFile; public static RandomAccessFile testFile; public StreamTokenizer reader; public PrintWriter fw; public void Graph(){ G = new Graph(); } public void Graph( int NumberOfNodes, int DIRECTED, int UNWEIGHTED){ //G = new Graph(); G.setNumberOfNodes(NumberOfNodes); //G.setDirected(DIRECTED); //G.setWeighted(UNWEIGHTED); } public void open(String fileName){ diskFile = new File( fileName ); } public void saveAs(String newFileName) throws IOException{//saves the file as something try{ File testFile = new File( G.fileName ); FileReader reader = new FileReader( testFile ); PrintWriter fw = new PrintWriter(new FileWriter(newFileName), true); int data = reader.read(); while( data != 0 ){ char c = (char) data; System.out.print( c ); fw.print(c); data = reader.read(); } fw.close(); reader.close(); } catch(IOException e){ System.out.println("Error in saveAs: " + e.toString()); } } public void openForReading(){ try{ if( !diskFile.exists() ) throw new IOException( "The File " + diskFile.getName() + " does not exist\n" ); testFile = new RandomAccessFile( diskFile, "r" );//makes the file testFile FileReader inStreamReader = new FileReader( diskFile );//streamreader BufferedReader r = new BufferedReader( inStreamReader );//bufferedreader reader = new StreamTokenizer( r );//streamtokenizer } catch(IOException e){ System.out.println("Error in openForReading(): " + e.getMessage() ); } } public void openForWriting(){ try{ if(!diskFile.exists() ) throw new IOException( "The File " + diskFile.getName() + " does not exist\n"); testFile = new RandomAccessFile( diskFile, "rw" ); fw = new PrintWriter(new FileWriter( diskFile ), true);//makes the printwriter object } catch(IOException e){ System.out.println("Error in openForWriting(): " + e.getMessage() ); } } public int nextInt() throws IOException{ int data = 0; int nextInt = 0; while(reader.nextToken() != StreamTokenizer.TT_NUMBER){ reader.nextToken(); } data = (int)reader.nval; nextInt = data; return nextInt; } public double nextDouble() throws IOException{ double data = 0.0; double nextDouble = 0.0; nextDouble = reader.nextToken(); return nextDouble; } public static void main( String[] args ) throws IOException{ Graph H = new Graph(); String userInput = ""; Scanner input = new Scanner(System.in); System.out.println("Enter the name of the file to open"); userInput = input.next(); H.open( userInput ); H.openForReading(); System.out.println(H.nextInt()); System.out.println(H.nextInt()); System.out.println(H.nextInt()); System.out.println(H.nextInt()); } }Java Code:import java.io.*; import java.util.*; public class GraphTraversals{ public void depthFirstTraversal( String fileName, int s){ //Graph G = new Graph(); //Graph H = new Graph(); Graph G; Graph H; G = new Graph(); G.open( fileName ); H = new Graph( G.numberOfNodes(), G.DIRECTED, G.UNWEIGHTED ); DFT( G, H, s ); H.print(); try{ H.saveAs( "dft.grf" ); } catch(IOException e){ System.out.println("Error in depthFirstTraversal: " + e.toString() ); } } private void DFT(Graph G, Graph H, int v){ G.setMark( v, G.MARKED ); for( int w = G.getFirstEdge( v ); G.isEdge( v, w ); w = G.getNextEdge( v, w)){ if( G.getMark( w ) != G.MARKED ){ H.setEdge( v, w ); DFT( G, H, w ); } } } public void breadthFirstTraversal( String fileName, int s){ Graph G, H; IntQueue Q; int v; G = new Graph(); G.open( fileName ); H = new Graph();// G.numberOfNodes(), G.DIRECTED, G.UNWEIGHTED ); Q = new IntQueue(); G.setMark( s, G.MARKED ); Q.enqueue( s ); while( ! Q.isEmpty() ){ v = Q.dequeue(); for( int w = G.getFirstEdge( v ); G.isEdge( v, w ); w = G.getNextEdge( v, w )){ if( G.getMark( w ) != G.MARKED){ G.setMark( w, G.MARKED ); H.setEdge( v, w ); Q.enqueue( w ); } } } H.print(); //H.saveAs( "bft.grf" ); } public static void main( String[] args) { GraphTraversals test = new GraphTraversals(); test.depthFirstTraversal("dijkstra.grf", 1); } }
-
constructors don't have a return type (yours has one -- void).
- 03-09-2009, 02:29 AM #3
Member
- Join Date
- Jan 2009
- Posts
- 6
- Rep Power
- 0
OMG D: it compiled. Thank youu. I feel like a dork.
-
Similar Threads
-
[SOLVED] Constructor problem
By sfe23 in forum New To JavaReplies: 10Last Post: 02-21-2009, 08:22 PM -
Constructor
By Sarinam in forum AWT / SwingReplies: 1Last Post: 06-19-2008, 08:03 AM -
Calling constructor of parent class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:10 AM -
Calling constructor of same class from a constructor
By Java Tip in forum Java TipReplies: 0Last Post: 12-19-2007, 09:01 AM -
Constructor Help
By bluegreen7hi in forum New To JavaReplies: 2Last Post: 11-15-2007, 05:44 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks