import java.io.File; import java.io.IOException; import java.util.*; public class Recap { /** * increment input param i * @param i: input int to be incremented * pre: true * post: i incremented * BUT: actual parameter of the caller not incremented */ public void incrInt(int i){ i++; System.out.println("Inside incrInt, i = " + i); } /** * * @param A: input array * @param i: index in A * pre: 0 <= i <= (A.length-1) * post: A[i] incremented * AND: array element of the actual array parameter of the caller incremented */ public void incrArrayEl(int[] A, int i){ A[i]++; } /** * * @param A array * @param i index in A * @param j index in A * * post: elements i and j in A are swapped */ public void swap(int[]A, int i, int j){ int t = A[i]; A[i]=A[j]; A[j]=t; } /** * * @param A array to be reversed * * post: A is reversed */ public void reverse(int[]A){ // reverse A by swapping for(int i=0;i<(A.length/2);i++){ swap(A,i,(A.length-1)-i); } } /** * * @param A input array * square A elements */ public void square1(int[] A){ for(int i=0;i0 * @return: true if n is prime, false otherwise * How does it work? */ public boolean isPrime(int n){ if (n==2 || n==3) return true; // How does it work? // Why going only to sqrt(n)? // Why test only (6*i+1) and (6*i-1)? if (n%2==0 || n%3==0) return false; else for(int i=1; (6*i-1) <= Math.sqrt(n); i++) if(n%(6*i-1)==0 || n%(6*i+1)==0) return false; return true; } /** * * @param n: bound * pre: n>=0 * post: all ordered pairs (i,j) * i in 0..(n-1), j in 0 .. (n-1) * printed one per line * in lexicographical order * format: open-parenthesis i comma j close-parenthesis * eg n=2: * (0,0) * (0,1) * (1,0) * (1,1) */ public void printOrderedPairs(int n){ for(int i = 0; i < n; i++){ // invariant: printed all pairs (0,j) to (i-1,j), j in {0,1,... n-1} for(int j = 0; j < n; j++){ // invariant: and printed all pairs // (i,0) to (i,j-1) System.out.println("("+i+","+j+")"); } } } /** * * @param n: bound * pre: n>=0 * post: all subsets size 2 {i,j} printed ONCE * i in 0..(n-1), j in 0..(n-1) * notice {i,j} = {j,i}, only one of these is printed * each subset is printed on one line * in lexicographical order * format: open-brace i comma j close-brace * eg n=2: * {0,1} */ public void printSubsets(int n){ //pre: n>=0 for(int i = 0; i < n; i++){ // printed all subsets {x,y} x in {0,1.. (i-1)} for(int j = i+1; j < n; j++) // and printed all subsets (i,y) // y in {0,1..(n-1)} System.out.println("{"+i+","+j+"}"); } } /** * * @param args program inputs * exercise cmd line; stdin; file IO */ public void doIO(String[] args ){ // first: "command line arguments" living in args String name, color; int age; double gpa; Scanner StIn = new Scanner(System.in); // IO 1 name = args[0]; age = Integer.parseInt(args[1]); gpa = Double.parseDouble(args[2]); System.out.println("Hello, my name is " + name); System.out.println("my age is " + age); System.out.println("my GPA is " + gpa); // IO 2 // now from Standard Input, eg the Eclipse console System.out.println("What is your name? "); name = StIn.next(); System.out.println("What is your favorite color? "); color = StIn.next(); System.out.println("Your name is " + name + " and your favorite color is " + color); System.out.println("What is the answer to the Universe?"); int answer = StIn.nextInt(); System.out.println("The answer is " + answer); // IO3 System.out.println("What is the input file name? "); //file Input String fName = StIn.next(); try { Scanner scan = new Scanner(new File(fName)); int sum = 0; int num; // echo file name System.out.println("reading from file: " + fName); // loop through numbers in input file and sum them while ( scan.hasNext()){ num = scan.nextInt(); System.out.print(" " + num ); sum = sum + num; } System.out.println("\n sum: " + sum); } catch(IOException E){ System.out.println(fName + " not found, movin' on"); } } /** * exercise Java scope */ public void doScope(){ // int i; final int k = 17; // constant k for array, problem sizes int[] kNumbers = new int[k]; // assign values to array elements for(int i=0;i