CS 1713 Introduction to Programming II Spring 2012 Daily Answers


Class 25:

  1. public static int posInteger(Stirng s) {
       try {
          int n = Integer.parseInt(s);
          if (n > 0)
             return n;
          return -1;
       } catch (Exception e) {
          return -1;
       }
    }
    
  2. public static boolean rowWin(char[] list) {
       if (list.length == 0)
          return false;   /* or true */
       if ((list[0] != 'X') && (list[0] != 'O'))
          return false;
       for (int i=1;i<list.length;i++)
          if (list[i] != list[0])
             return false;
       return true;
    }
    

  3. public static boolean rowWin(char[][] board, int n, char c) {
       for (int i=0;i<board[n].length;i++)
          if (board[n][i] != c)
             return false;
       return true;
    }
    
  4. public static boolean colWin(char[][] board, int n, char c) {
       for (int i=0;i<board.length;i++)
          if (board[i][n] != c)
             return false;
       return true;
    }