import java.io.*; import java.util.*; public class Graph extends Object { private int graphSize; private String[] locations; private int[][] adjacency; /** Construct a new graph with the given information. **/ public Graph(int howMany) { this.adjacency = new int[howMany][howMany]; this.locations = new String[howMany]; this.graphSize = howMany; } /******************************************************************************/ /* Accessors */ /******************************************************************************/ public int getAdjacency(int row, int column) { return this.adjacency[row][column]; } /******************************************************************************/ public int getGraphSize() { return this.graphSize; } /******************************************************************************/ public String getLocations(int which) { return this.locations[which]; } /******************************************************************************/ /* Mutators */ /******************************************************************************/ public void setAdjacency(int row, int column, int theValue) { this.adjacency[row][column] = theValue; } /******************************************************************************/ public void setLocations(int which, String theValue) { this.locations[which] = theValue; } /******************************************************************************/ /* Utilities */ /******************************************************************************/ /******************************************************************************/ public void getTheGraph(Scanner dataFile) { int inputInt; String inputString; // // Note that we do no checking here for having enough data or the right // kind of data. Very bad form. // System.out.println("File input processing beginning"); for(int i = 0; i < this.getGraphSize(); ++i) { inputString = dataFile.next(); this.setLocations(i,inputString); } for(int i = 0; i < this.getGraphSize(); ++i) { for(int j = 0; j < this.getGraphSize(); ++j) { inputInt = dataFile.nextInt(); this.setAdjacency(i, j, inputInt); } } System.out.println("File input processing ended"); } // public void getTheArray(Scanner dataFile) /******************************************************************************/ public void printTheGraph() { String printString = ""; System.out.printf(" "); for(int i = 0; i < this.getGraphSize(); ++i) { printString = this.getLocations(i) + " "; printString = printString.substring(0,5); System.out.printf(" %-5s",printString); } System.out.printf("%n"); for(int i = 0; i < this.getGraphSize(); ++i) { printString = this.getLocations(i) + " "; printString = printString.substring(0,5); System.out.printf("%5s",printString); for(int j = 0; j < this.getGraphSize(); ++j) { System.out.printf(" %5d",this.getAdjacency(i, j)); } System.out.printf("%n"); } } // public void printTheGraph() } // public class Graph extends Object