Exam is on Wednesday, seating as currently assigned. 

Please identify 16 problems (of the 33 posted) send them to me. 

You will be given two solve two random problems from the 16.

Study Guide: 

  https://www.cs.indiana.edu/classes/c212-dgerman/spr2014/sg.html

Let's imagine we play a game with the computer. 

Both I and the computer move by saying a number between 1 and 10. 

Every time this happens the number gets added to a sum that starts at zero. 

Whoever gets to 100 or over 100 wins. 

Let's implement this game.

Question 1. Is there a winning strategy if you get to choose if you move first or second?

Question 2. What does the program look like? 

import java.util.*; 

class Game {
  public static void main(String[] args) {
     int sum = 0; 
     Scanner in = new Scanner(System.in); 
     while (true) {
       System.out.print("(" + sum + ") Number: "); 
       String line = in.nextLine(); 
       if (line.equals("bye")) break;
       int number = Integer.parseInt(line); 
       sum += number; 
       if (sum >= 100) {
         System.out.println("You won."); 
         break; 
       }
       System.out.println("Sum becomes " + sum); 
       int computer = 1 + (int) (Math.random() * 10); 
       System.out.println("The computer chooses " + computer); 
       sum += computer; 
       if (sum >= 100) {
         System.out.println("Computer wins."); 
         break; 
       }
     }
  }
}

We then discussed this code:

class Something {
   String name; 
   void talk() {
     System.out.println("I am " + this + " and my name is " + this.name);  
   }
}

Here's how it works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Something
Static Error: This class does not have a static void main method accepting String[].
> Something a = new Something()
> a
Something@109bb90
> a.name = "Adrian"
"Adrian"
> a
Something@109bb90
> a.talk()
I am Something@109bb90 and my name is Adrian
> Something b = new Something()
> b.name = "Jon"
"Jon"
> b.talk()
I am Something@1539f96 and my name is Jon
> b
Something@1539f96


So we should try next time to write the game in this style. 

--


class Experiment {
  public static void main(String[] args) {
     int x = 0, y = 0;
     double max = 0; 
     int steps = Integer.parseInt( args[0] ); 
     for (int step = 0; step < steps; step = step + 1) {
        int direction = (int) (Math.random() * 4);
        if (direction == 0) y = y - 1;
        else if (direction == 1) x = x + 1;
        else if (direction == 2) y = y + 1;
        else x = x  - 1;
        double d = distance(x, y);
        if (d > max) max = d; 
        if (steps - step == 1) 
          System.out.println( step + ". Drunkard(" + x + ", " + y + ") " + d + " " + max ); 
     }
  }
  public static double distance(int x, int y) {
    return Math.sqrt( x * x + y * y );  
  }
}

Here's how this works:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Experiment 1000
999. Drunkard(24, 38) 44.94441010848846 51.088159097779204
> java Experiment 10000
9999. Drunkard(84, 116) 143.22011031974526 161.20794025109308
> java Experiment 100000
99999. Drunkard(22, 226) 227.06827167175956 321.50272160589867
> java Experiment 1000000
999999. Drunkard(-214, 1158) 1177.6077445397511 1299.720354537852
> java Experiment 1000000
999999. Drunkard(-26, 120) 122.78436382536663 830.822484048187
> java Experiment 1000000
999999. Drunkard(166, 374) 409.18455493823325 783.6019397627854


What limitation might this approach have? 


class Drunkard {
    int x, y;
    double max; 
    void talk() {
      System.out.println( this + "(" + this.x + ", " + this.y + ")" ); 
    }
    void report() {
      System.out.println( this + " " + this.distance() + " " + this.max );  
    }
    double distance() {
      return Math.sqrt( this.x * this.x + this.y * this.y ); 
    }
    void move() {
      int direction = (int) (Math.random() * 4); 
      if (direction == 0) this.y -= 1; 
      else if (direction == 1) this.x += 1;
      else if (direction == 2) this.y += 1;
      else this.x -= 1;
      double d = this.distance(); 
      if (this.max < d) 
        this.max = d;
    }
}

This models one Drunkard. 

class Program {
  public static void main(String[] args) {
    int steps = Integer.parseInt( args[0] ); 
    Drunkard[] a = new Drunkard[13];
    for (int i = 0; i < a.length; i++) {
      a[i] = new Drunkard();  
    }
    for (int i = 0; i < steps; i++) {
      for (Drunkard d : a)
        d.move();
    }
    for (Drunkard d : a)
      d.report();
  }
}

--