Lab's purpose:

  (a) to answer questions (if any) about Homework 04

  (b) to provide opportunities for practice with static methods and arrays (also array lists etc.)

1. 

  (d) public static boolean inside(String a, String b) {
        // checks if b is inside a or not 
        // ...
      }

  (g) public static void calendar(int year, int month) {
        // prints the calendar for given month and year 

      }

  (i) public static int random(int n) {
        // returns random integer between 0 and n (inclusive) 
        double random = Math.random(); // random number in [0, 1)
        double adjusted = random * (n + 1); // random number in [0, (n+1)) 
        int result = (int) adjusted; // random integer in [0, n+1) which is [0, n]
        return result; 
      } 

  (b) public static double smallest(double a, double , double c) {
        return Math.min(a, Math.min(b, c)); 
      }

2. 

  (a) already discussed 

  (c) http://docs.oracle.com/javase/8/docs/api/java/lang/Double.html#parseDouble-java.lang.String-

3. 

  http://silo.cs.indiana.edu:8346/c212/spr2013/eeesols.phps has the answers, for example

  (g) False, for example Math.random()

  (f) if you have a void return value and you are useful you have a result to communicate what do you do? 

      1. print it (so the answer would be True) 

      2. leave it somewhere 

         public static void random(double[] number) {
           number[0] = Math.random(); 
         } 

         This argument (from 2) would make the answer False. 

         import java.util.Arrays; 

         class One {
           public static void main(String[] args) {
             double[] result = new double[1]; 
             System.out.println( Arrays.toString( result ) ); 
             One.random(result); 
             System.out.println( Arrays.toString( result ) ); 
             One.random(result); 
           }

           public static void random(double[] number) {
             number[0] = Math.random(); 
           } 

         } 

         If I were to choose between the two I would favor the False answer with the example. 

4. The reason is that falseSwap receives copies and works on those and it can't modify the originals. 

class One {
  public static void falseSwap(int a, int b) {
    System.out.println("a starts as: " + a); 
    a = a + 1; 
    System.out.println("a is now: " + a); 
  }
  public static void main(String[] args) {
    int x = 3, y = 4;
    System.out.println( x + " " + y); 
    One.falseSwap(x, y); 
    System.out.println( x + " " + y); 
  }
}

So this is the simplest example that explains it. 

Here's the original code:

class One {
  public static void falseSwap(int a, int b) {
    System.out.println("report: " + a + " " + b ); 
    int temp = a; 
    a = b; 
    b = temp; 
    System.out.println("report: " + a + " " + b ); 
  }
  public static void main(String[] args) {
    int x = 3, y = 4;
    System.out.println( x + " " + y); 
    One.falseSwap(x, y); 
    System.out.println( x + " " + y); 
  }
}

5. Here's (e) 

import java.util.Arrays; 

class One {
  public static void main(String[] args) {
    int[] a = { 1, 4, 9, 16, 1, 7, 4, 9, 11 }; 
    System.out.println( Arrays.toString( a ) ); 
  }
}

6. First part is easy:

import java.util.Arrays; 

class One {
  public static void main(String[] args) {
    int[] a = new int[10];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int)(Math.random() * 15);  
    }
    System.out.println(Arrays.toString( a )); 
    
  }
}

Here's what it might produce:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run One
[7, 2, 14, 6, 3, 7, 9, 10, 13, 10]


So the second part says: I don't want numbers to repeat (like 7 here). 

7. This problem is too simple!

8. Here (a) is almost the 

   public static String toString(int[] a) {
     String result = ""; 
     for (int element : a) // Thanks, Noor!
       result += element + " ";
     return result; 
   }

method you have to write. 

9. 

Don't forget there is a Help Session every Sunday in LH102 @2-4pm. 

Attendance today: Noor, Andrew, Jack, Shea, Chia-Hsuan, Jing, Adrian. 

--

Jing 1/2 more on 37.