Results 1 to 4 of 4
  1. #1
    ToastyBainey is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

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

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

    Default

    constructors don't have a return type (yours has one -- void).

  3. #3
    ToastyBainey is offline Member
    Join Date
    Jan 2009
    Posts
    6
    Rep Power
    0

    Default

    OMG D: it compiled. Thank youu. I feel like a dork.

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

    Default

    OMG D: it compiled. Thank youu. I feel like a dork.
    Please don't be too hard on yourself. That's our job. :)

Similar Threads

  1. [SOLVED] Constructor problem
    By sfe23 in forum New To Java
    Replies: 10
    Last Post: 02-21-2009, 09:22 PM
  2. Constructor
    By Sarinam in forum AWT / Swing
    Replies: 1
    Last Post: 06-19-2008, 09:03 AM
  3. Replies: 0
    Last Post: 12-19-2007, 10:10 AM
  4. Replies: 0
    Last Post: 12-19-2007, 10:01 AM
  5. Constructor Help
    By bluegreen7hi in forum New To Java
    Replies: 2
    Last Post: 11-15-2007, 06:44 AM

Posting Permissions

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