pastebin - collaborative debugging

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

This site is developed to XHTML and CSS2 W3C standards. If you see this paragraph, your browser does not support those standards and you need to upgrade. Visit WaSP for a variety of options.

Java pastebin - collaborative debugging tool View Help


Posted by caiobm on Sat 24 Jan 01:35
report abuse | download | new post

  1. /*
  2.  * MatGraph is a class that implements the interface Graph<Double,Double>.
  3.  * It was not intended to make a good oriented-object design.
  4.  * Only for test.
  5.  *
  6.  * Creates an adjacency matrix graph from a file with the follow example format:
  7.  *
  8.  *  
  9.         8
  10.         0 * * * * * * *
  11.         30 0 * * * * * *
  12.         100 80 0 * * * * *
  13.         * * 120 0 * * * *
  14.         * * * 150 0 25 * *
  15.         * * * 100 * 0 90 140
  16.         * * * * * * 0 100
  17.         170 * * * * * * 0
  18.        
  19.         The first line must contains the number "n" of vertices. The other "n" lines must
  20.         have "n" elements. If there is no conexion between two vertices a single char "*" must be typed.
  21.  */
  22.  
  23. /**
  24.  *
  25.  * @author Caio Bomfim Martins
  26.  * @version 1.0
  27.  */
  28. public class MatGraph implements Graph<Double,Double> {
  29.    
  30.     private double[][] cost;
  31.    
  32.     public MatGraph(String fileName) throws java.io.IOException {
  33.         java.io.BufferedReader br = new java.io.BufferedReader(
  34.                            new java.io.FileReader(new java.io.File(fileName)));
  35.        
  36.         int length = Integer.parseInt(br.readLine());
  37.        
  38.         cost = new double[length][length];
  39.        
  40.         String[] line;
  41.        
  42.         for ( int i = 0; i < cost.length; i++ ) {
  43.             line = br.readLine().split(" ");
  44.            
  45.             for ( int j = 0; j < cost.length; j++ ) {
  46.                 if (line[j].compareTo("*") == 0) {
  47.                     cost[i][j] = Float.MAX_VALUE;
  48.                    
  49.                 } else {
  50.                     cost[i][j] = Float.parseFloat(line[j]);
  51.                   }              
  52.             }
  53.         }
  54.        
  55.         br.close();
  56.     }
  57.    
  58.     public Double get(int i, int j) {
  59.         return Double.valueOf(cost[i][j]);
  60.     }
  61.    
  62.     public int size() {
  63.         return cost.length;
  64.     }
  65. }

Submit a correction or amendment below (click here to make a fresh posting)
After submitting an amendment, you'll be able to view the differences between the old and new posts easily.

Syntax highlighting:

To highlight particular lines, prefix each line with @@


Remember me so that I can delete my post