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.
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());
}
}
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);
}
}
