R6.1

-bash-4.1$ cat R61.java
import java.util.*;

class R61 {
  public static void main(String[] args) {
    int[] a = new int[10];

    System.out.println(" (d) " + Arrays.toString(a));

    for (int i = 0; i < a.length; i++) a[i] = i + 1;
    System.out.println(" (a) " + Arrays.toString(a));

    for (int i = 0; i < a.length; i++) a[i] = 2 * i;
    System.out.println(" (b) " + Arrays.toString(a));

    for (int i = 0; i < a.length; i++) a[i] = (i + 1) * (i + 1);
    System.out.println(" (c) " + Arrays.toString(a));

    int[] b = {1, 4, 9, 16, 9, 7, 4, 9, 11}; // see http://oeis.org/
    System.out.println(" (e) " + Arrays.toString(b));

    for (int i = 0; i < a.length; i++) a[i] = i % 2;
    System.out.println(" (f) " + Arrays.toString(a));

    for (int i = 0; i < a.length; i++) a[i] = i % 5;
    System.out.println(" (g) " + Arrays.toString(a));

  }
}
-bash-4.1$ javac R61.java
-bash-4.1$ java R61
 (d) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 (a) [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 (b) [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
 (c) [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
 (e) [1, 4, 9, 16, 9, 7, 4, 9, 11]
 (f) [0, 1, 0, 1, 0, 1, 0, 1, 0, 1]
 (g) [0, 1, 2, 3, 4, 0, 1, 2, 3, 4]
-bash-4.1$

------------------------------------------------------------------(R6.1)---------------------

R6.2 


-bash-4.1$ cat R62.java
class R62 {
  public static void main(String[] args) {

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 0; i < 10; i++) { total = total + a[i]; }
      System.out.println( "(a) " + total );
    }
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 0; i < 10; i = i + 2) { total = total + a[i]; }
      System.out.println( "(b) " + total );
    }
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 1; i < 10; i = i + 2) { total = total + a[i]; }
      System.out.println( "(c) " + total );
    }
try{
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 2; i <= 10; i++) { total = total + a[i]; }
      System.out.println( "(d) " + total );
    }
} catch (Exception e) {
  System.out.println( "(d) throws a java.lang.ArrayIndexOutOfBoundsException." );
}
/*****
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 0; i < 10; i = 2 * i) { total = total + a[i]; } // this is an infinite loop
      System.out.println( "(e) " + total );
    }
 *****/
try {
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 0; i < 10; i--) { total = total + a[i]; }
      System.out.println( "(f) " + total );
    }
} catch (Exception e) {
  System.out.println( "(f) throws an Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: -1" );
}
try {
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 0; i < 10; i = i +-2) { total = total + a[i]; }
      System.out.println( "(g) " + total );
    }
} catch (Exception e) {
  System.out.println( "(g) throws an Exception in thread \"main\" java.lang.ArrayIndexOutOfBoundsException: -2" );
}
    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      int total = 0;
      for (int i = 0; i < 10; i++) { total = a[i] - total; }
      System.out.println( "(h) " + total );
    }

  }
}
-bash-4.1$ javac R62.java
-bash-4.1$ java R62
(a) 25
(b) 13
(c) 12
(d) throws a java.lang.ArrayIndexOutOfBoundsException.
(f) throws an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
(g) throws an Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -2
(h) -1
-bash-4.1$

------------------------------------------------------------------(R6.2)---------------------

-bash-4.1$ cat R63.java
import java.util.*;

class R63 {
  public static void main(String[] args) {

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 1; i < 10; i++) { a[i] = a[i - 1]; }
      System.out.println( " (a) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 9; i > 0; i--) { a[i] = a[i - 1]; }
      System.out.println( " (b) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 0; i < 9; i++) { a[i] = a[i + 1]; }
      System.out.println( " (c) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 8; i >= 0; i--) { a[i] = a[i + 1]; }
      System.out.println( " (d) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 1; i < 10; i++) { a[i] = a[i] + a[i - 1]; }
      System.out.println( " (e) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 1; i < 10; i = i + 2) { a[i] = 0; }
      System.out.println( " (f) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 0; i < 5; i++) { a[i + 5] = a[i]; }
      System.out.println( " (g) " + Arrays.toString( a ) );
    }

    { int[] a = {1, 2, 3, 4, 5, 4, 3, 2, 1, 0};
      for (int i = 1; i < 5; i++) { a[i] = a[9 - 1]; }
      System.out.println( " (h) " + Arrays.toString( a ) );
    }


  }
}
-bash-4.1$ javac R63.java
-bash-4.1$ java R63
 (a) [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
 (b) [1, 1, 2, 3, 4, 5, 4, 3, 2, 1]
 (c) [2, 3, 4, 5, 4, 3, 2, 1, 0, 0]
 (d) [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
 (e) [1, 3, 6, 10, 15, 19, 22, 24, 25, 25]
 (f) [1, 0, 3, 0, 5, 0, 3, 0, 1, 0]
 (g) [1, 2, 3, 4, 5, 1, 2, 3, 4, 5]
 (h) [1, 1, 1, 1, 1, 4, 3, 2, 1, 0]
-bash-4.1$

------------------------------------------------------------------(R6.3)---------------------

-bash-4.1$ cat R64.java
import java.util.*;

class R64 {
  public static void main(String[] args) {
    int[] a = new int[10];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100) + 1;
      System.out.println( Arrays.toString( a ) );
    }
    a = new int[10];
    for (int i = 0; i < a.length; i++) {
      a[i] = (int) (Math.random() * 100) + 1;
      System.out.println(i + ". Trying " + a[i]);
      boolean found = false;
      for (int j = 0; j < i; j++) {
        if (a[i] == a[j])
          found = true;
      }
      if (found) i--;
      System.out.println( Arrays.toString( a ) );
    }
  }
}
-bash-4.1$ javac R64.java
-bash-4.1$ java R64
[49, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[49, 32, 0, 0, 0, 0, 0, 0, 0, 0]
[49, 32, 98, 0, 0, 0, 0, 0, 0, 0]
[49, 32, 98, 88, 0, 0, 0, 0, 0, 0]
[49, 32, 98, 88, 79, 0, 0, 0, 0, 0]
[49, 32, 98, 88, 79, 22, 0, 0, 0, 0]
[49, 32, 98, 88, 79, 22, 14, 0, 0, 0]
[49, 32, 98, 88, 79, 22, 14, 27, 0, 0]
[49, 32, 98, 88, 79, 22, 14, 27, 97, 0]
[49, 32, 98, 88, 79, 22, 14, 27, 97, 47]
0. Trying 2
[2, 0, 0, 0, 0, 0, 0, 0, 0, 0]
1. Trying 73
[2, 73, 0, 0, 0, 0, 0, 0, 0, 0]
2. Trying 95
[2, 73, 95, 0, 0, 0, 0, 0, 0, 0]
3. Trying 78
[2, 73, 95, 78, 0, 0, 0, 0, 0, 0]
4. Trying 55
[2, 73, 95, 78, 55, 0, 0, 0, 0, 0]
5. Trying 43
[2, 73, 95, 78, 55, 43, 0, 0, 0, 0]
6. Trying 73
[2, 73, 95, 78, 55, 43, 73, 0, 0, 0] <------(73 already in)----
6. Trying 60
[2, 73, 95, 78, 55, 43, 60, 0, 0, 0]
7. Trying 42
[2, 73, 95, 78, 55, 43, 60, 42, 0, 0]
8. Trying 78
[2, 73, 95, 78, 55, 43, 60, 42, 78, 0] <----(78 already in)----
8. Trying 33
[2, 73, 95, 78, 55, 43, 60, 42, 33, 0]
9. Trying 68
[2, 73, 95, 78, 55, 43, 60, 42, 33, 68]
-bash-4.1$ 

------------------------------------------------------------------(R6.4)---------------------

-bash-4.1$ cat R65.java
import java.util.*;

class R65 {
  public static void main(String[] args) {
    int[] a = new int[args.length];
    for (int i = 0; i < a.length; i++)
      a[i] = Integer.parseInt( args[i] );
    if (a.length == 0) {

    } else {
      int min = a[0], max = a[0];
      for (int i = 1; i < a.length; i++) {
        if (a[i] < min) min = a[i];
        if (a[i] > max) max = a[i];
      }
      System.out.println( "min: " + min + ", max: " + max );
    }
  }
}
-bash-4.1$ javac R65.java
-bash-4.1$ java R65 1 3 2 -1 8
min: -1, max: 8
-bash-4.1$ java R65 -6
min: -6, max: -6
-bash-4.1$ java R65
-bash-4.1$ java R65 -29 15 42 -51
min: -51, max: 42
-bash-4.1$

------------------------------------------------------------------(R6.5)---------------------

Assume int[] a is a given array. 

for (int elem : a) 
  System.out.print(a[i] + " ")
System.out.println()

int product = 1; 
for (int elem : a) 
  product *= elem;
System.out.println( product );

int count = 0; 
for (int elem : a) 
  if (elem < 0)
    count += 1
System.out.println( count ); 

------------------------------------------------------------------(R6.7)---------------------

(a) for (int i = 0; i < values.length; i++) 
      total = total + values[i]; 

(b) for (int i = 0; i < values.length; i++) {
      if (values[i] == target) {
        return true; 
      }
    }

------------------------------------------------------------------(R6.8)---------------------

------------------------------------------------------------------(R6.9)---------------------

------------------------------------------------------------------(R6.30)--------------------

(a) true 

(b) false 

(c) false 

(d) false 

(e) false 

(f) true 

(g) false 


-bash-4.1$ javac Example.java
-bash-4.1$ java Example
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
[0, 0, 0, 0, 0, 0]
-----
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
[0, 0, 0]
-----
-bash-4.1$ cat Example.java
import java.util.*;

class Example {
  public static void main(String[] args) {
    int[][] a = new int[6][6];
    for (int i = 0; i < a.length; i++) {
      System.out.println(Arrays.toString(a[i]));
    }
    System.out.println("-----");
    for (int i = 0; i < a.length; i++) {
      a[i] = new int[3];
    }
    for (int i = 0; i < a.length; i++) {
      System.out.println(Arrays.toString(a[i]));
    }
    System.out.println("-----");
  }
}
-bash-4.1$


------------------------------------------------------------------(R6.33)--------------------

(a) 

(b) 

(c) 

(d) 


------------------------------------------------------------------(R6.34)--------------------

(a) true 

(b) true 

(c) false 

(d) true 

(e) false 

(f) false 


------------------------------------------------------------------(R6.35)--------------------

(a) int larger(int a, int b)

(b) float smallest(float a, float b, float c)

(c) boolean isPrime(int n)

(d) 

(e) 

(f) 

(g) 

(h) 

(i) 


------------------------------------------------------------------(R5.2)---------------------

------------------------------------------------------------------(R5.10)--------------------

When a method is called the values are copied in the parameters.

Any change in the param value does not affect the argument. 

Like this: 

  int i = 3; 

  int j = i; // a second 3 is in j 

  j = j + 1; // i is not modified 

------------------------------------------------------------------(R5.14)--------------------

------------------------------------------------------------------(R4.15)--------------------

------------------------------------------------------------------(R4.16)--------------------

for (int i = 1; i <= height * width; i++) 
{
  if (i % height == 0) 
    System.out.println( ); 
  else 
    System.out.print("*");   
}

------------------------------------------------------------------(R4.27)--------------------

------------------------------------------------------------------(P4.1)---------------------

------------------------------------------------------------------(P4.2)---------------------

Here's how I code correctly in my obsessive-compulsive way (all curly braces, all branches): 

    if (gpa >= 1.5) {
      if (gpa < 2) {
        System.out.println("Probation."); 
      } else { 

      } 
    } else { 
      System.out.println("Failing."); 
    } 

The rules are: else is optional and blocks of one statement don't need curly braces. 

If I apply this blindly I get: 

    if (gpa >= 1.5) 
      if (gpa < 2) 
        System.out.println("Probation."); 
    else  
      System.out.println("Failing."); 
     
But this is wrong, because it runs like this: 

    if (gpa >= 1.5) 
      if (gpa < 2) 
        System.out.println("Probation."); 
      else  
        System.out.println("Failing."); 

We discussed this in class and notes. So the "moving" else they say "dangles" without the curly braces around the inner if. 

------------------------------------------------------------------(R3.25)--------------------

Complete the following truth table by finding the truth values of the Boolean expressions 
for all combinations of the Boolean inputs p, q, and r.

   p        q        r            (p && q) || !r       !(p && (q || !r))
 false    false    false             true               true
 false    false    true              false              true
 false    true     false             true               true
 false    true     true              false              true
 true     false    false             true               false
 true     false    true              false              true
 true     true     false             true               false
 true     true     true              true               false

How do we check if we are right or wrong? 

We ask the computer:

frilled.cs.indiana.edu%cat Boole.java
public class Boole {
    public static void main(String[] args) {
    boolean p, q, r; 
    p = false; q = false; r = false; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = false; q = false; r = true; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = false; q = true;  r = false; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = false; q = true;  r = true; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = true;  q = false; r = false; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = true;  q = false; r = true; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = true;  q = true;  r = false; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 
    p = true;  q = true;  r = true; 
    System.out.println( ((p && q) || !r) + " " + (!(p && (q || !r))) ); 

    } 

frilled.cs.indiana.edu%javac Boole.java
frilled.cs.indiana.edu%java Boole
true true
false true
true true
false true
true false
false true
true false
true false
frilled.cs.indiana.edu%

------------------------------------------------------------------(R3.26)--------------------

Not always, recall the example of yesterday. Assume  

  int i = 1; 

Evaluate this: 

  (i-- > 0) || (--i > 0)   

Then switch the order of operators:

-bash-4.1$ cat R327.java
class R327 {
  public static void main(String[] args) {
    { int i = 1;

      System.out.println( (i-- > 0) || (--i > 0) );
    }
    { int i = 1;

      System.out.println( (--i > 0) || (i-- > 0) );
    }

  }
}
-bash-4.1$ javac R327.java
-bash-4.1$ java R327
true
false
-bash-4.1$

------------------------------------------------------------------(R3.27)--------------------

(a)   b

(b)  !b 

(c)  !b 

(d)   b 

------------------------------------------------------------------(R3.30)--------------------

(a)   b = ( n == 0 ); 

(b)   b = ( n != 0 ); 

(c)   b = (1 < n && n < 2); 

    which can be further simplified to 

      b = false; 

    (assuming that n is an int). 

(d)   b = ( n < 1 ) || ( n > 2 ); 

    This can be simplified to 

      b != 1 && b != 2

    (assuming that n is an int). 

------------------------------------------------------------------(R3.31)--------------------

0

------------------------------------------------------------------(R2.1)--------------------

mystery is declared twice

------------------------------------------------------------------(R2.2)--------------------

double s = s0 + v0 * t + g * t * t / 2.0;
 
double G = 4 * Math.PI * Math.PI * Math.pow(a, 3) / ( P * P * (m1 + m2));
 
double FV = PV * Math.pow(1 + INT / 100, YRS); 
 
double c = Math.sqrt(a * a + b * b - 2 * a * b * Math.cos(gamma))

------------------------------------------------------------------(R2.3)--------------------

http://www.cs.indiana.edu/classes/a201-dger/sum2000/quizzes/images/img1.gif

http://www.cs.indiana.edu/classes/a201-dger/sum2000/quizzes/images/img2.gif

http://www.cs.indiana.edu/classes/a201-dger/sum2000/quizzes/images/img3.gif

... 

------------------------------------------------------------------(R2.4)--------------------