001package examination3;
002
003/**
004 * Add Javadoc comments to each of the methods
005 * (f0 to f6) in this class.
006 * 
007 * <p>
008 * This code is related to the SurfingSafari program
009 * and the code in section 1.6 of our textbook.
010 * <p>
011 * 
012 * @author Your Name
013 * @version 30 January 2015
014 */
015public class Matrix {
016
017  public static final int DIM = 3;
018
019  public Matrix() {
020    double[] vector = f2(DIM);
021
022    f0(vector);
023    System.out.println();
024
025    double[][] matrix = f3(DIM);
026
027    f1(matrix);
028    System.out.println();
029
030    f4(matrix);
031
032    f1(matrix);
033    System.out.println();
034
035    int n = 1;
036    for (int i = 0; i < n; i++) {
037      vector = f6(vector, matrix);
038    } // for
039
040    f0(vector);
041    System.out.println();
042
043    f5(matrix);
044
045    f1(matrix);
046    System.out.println();
047
048    int hops = 12;
049    int page = 0;
050    for (int i = 0; i < hops; i++) {
051      System.out.println("Visiting page #" + page);
052      double r = Math.random();
053      int j = 0;
054      while (r > matrix[page][j]) {
055        j++;
056      } // while
057      page = j;
058    } // for
059
060  } // Matrix()
061
062  public final void f0(double[] v) {
063    for (double x : v) {
064      System.out.printf("%6.2f ", x);
065    } // for
066    System.out.println();
067  } // f0( double [] )
068
069  public final void f1(double[][] m) {
070    for (double[] row : m) {
071      f0(row);
072    } // for
073  } // f1( double [][] )
074
075  public final double[] f2(int n) {
076    double[] vector = new double[DIM];
077    vector[0] = 1.0;
078    for (int i = 1; i < DIM; i++) {
079      vector[i] = 0.0;
080    } // for
081    return vector;
082  } // f2( int )
083
084  public final double[][] f3(int n) {
085    double[][] matrix = new double[DIM][DIM];
086    for (int i = 0; i < DIM; i++) {
087      for (int j = 0; j < DIM; j++) {
088        matrix[i][j] = Math.random();
089      } // for
090    } // for
091    return matrix;
092  } // f3( int )
093
094  public final void f4(double[][] matrix) {
095    for (int i = 0; i < DIM; i++) {
096
097      double sum = 0.0;
098      for (int j = 0; j < DIM; j++) {
099        sum += matrix[i][j];
100      } // for
101
102      for (int j = 0; j < DIM; j++) {
103        matrix[i][j] /= sum;
104      } // for
105    } // for
106  } // f4( double [][] )
107
108  public final void f5(double[][] matrix) {
109    for (int i = 0; i < DIM; i++) {
110      for (int j = 1; j < DIM; j++) {
111        matrix[i][j] = matrix[i][j] + matrix[i][j - 1];
112      } // for
113    } // for
114  } // f5( double [][] )
115
116  public final double[] f6(double[] vector,
117          double[][] matrix) {
118    double[] product = new double[DIM];
119
120    for (int i = 0; i < DIM; i++) {
121      double sumOfProducts = 0.0;
122      for (int j = 0; j < DIM; j++) {
123        sumOfProducts += vector[j] * matrix[j][i];
124      } // for
125      product[i] = sumOfProducts;
126    } // for
127    return product;
128  } // f6( double [], double [][] )
129
130} // Matrix