The code we wrote in class this morning: 

  class Two {
    public static void main(String[] args) {
      System.out.println( "1 + 2 + ... + 100 = " + Two.sum( 100 ) );
    }

    public static int sum(int n) {
      int result = n * (n + 1) / 2;

      return result;
    }
  }

This was an example of a class that has a static method defined in it. 

The method is called sum and receives one argument. 

This is the second problem in the posted minute paper. 

There are two more ways to solve this problem: 

  (a) recursively
 
  (b) iteratively 

The method above uses a formula which one needs to prove first in order to trust it. 

Here's how the program above works when run, for example, on silo:

  bash-4.1$ ls -ld Two.java
  -rw-r--r-- 1 dgerman www 220 Jan 23 12:32 Two.java
  bash-4.1$ cat Two.java
  class Two {
    public static void main(String[] args) {
      System.out.println( "1 + 2 + ... + 100 = " + Two.sum( 100 ) );
    }

    public static int sum(int n) {
      int result = n * (n + 1) / 2;

      return result;
    }
  }
  bash-4.1$ javac Two.java
  bash-4.1$ java Two
  1 + 2 + ... + 100 = 5050
  bash-4.1$ 

The problem below is the first problem in the minute paper. 

It is the first problem we discussed in class this morning. 

It uses an infinite loop that's controlled by the user loop. 

Notice the use of "break" and the mention of related keyword "continue" in class. 

(Where are these described in your book? What pages and chapters?)

The code: 

  import java.util.Scanner;

  class One {
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int randomNum = (int) (Math.random() * 101);
      System.out.println(randomNum);
      int guesses = 0;
      while (true) {
        System.out.print("Enter a number: ");
        int num = in.nextInt(); // count this!
        guesses = guesses + 1;
        if (num == randomNum) {
          System.out.println("Congrats! You got it.");
          break;
        } else if (num > randomNum) {
          System.out.println("Try lower.");
        } else { // num < randomNum
          System.out.println("Try higher.");
        }
        System.out.println(num);
      }
       System.out.println("Yes, in only " + guesses + " attempts.");
    }
  }

Here's how the code runs:

  bash-4.1$ ls -ld One.java
  -rw-r--r-- 1 dgerman www 737 Jan 23 12:31 One.java
  bash-4.1$ cat One.java
  import java.util.Scanner;

  class One {
    public static void main(String[] args) {
      Scanner in = new Scanner(System.in);
      int randomNum = (int) (Math.random() * 101);
      System.out.println(randomNum);
      int guesses = 0;
      while (true) {
        System.out.print("Enter a number: ");
        int num = in.nextInt(); // count this!
        guesses = guesses + 1;
        if (num == randomNum) {
          System.out.println("Congrats! You got it.");
          break;
        } else if (num > randomNum) {
          System.out.println("Try lower.");
        } else { // num < randomNum
          System.out.println("Try higher.");
        }
        System.out.println(num);
      }
      System.out.println("Yes, in only " + guesses + " attempts.");
    }
  }


  bash-4.1$ javac One.java
  bash-4.1$ java One
  98
  Enter a number: 50
  Try higher.
  50
  Enter a number: 75
  Try higher.
  75
  Enter a number: 87
  Try higher.
  87
  Enter a number: 93
  Try higher.
  93
  Enter a number: 97
  Try higher.
  97
  Enter a number: 99
  Try lower.
  99
  Enter a number: 98
  Congrats! You got it.
  Yes, in only 7 attempts.
  bash-4.1$

--