At this point raw scores for all exams have been posted. 

Please come and pick them up we can talk about:

(a) adjustments (get up to half of lost points back via oral defense)

(b) retake and score 100 (any number of times)

Today I want to give two examples:

(a) retake

(b) third exam

For the retake practice Homework Eight

Solve this problem now:

Write a program that asks the user for an integer and then prints 
out the prime factorization of the number. For example, if the user 
enters 300 program prints: 2 2 3 5 5. 

Five minutes. What we come up with:

       n         f        factor?    factorization     done?  (n == 1) 
-----------------------------------------------------------
     300         2         true        2               false 
     150         2         true        2 2             ...
      75         2         false 
                 3         true        2 2 3 
      25         3         false 
                 4         false 
                 5         true        2 2 3 5         ...
       5         5         true        2 2 3 5 5       false 
       1         5         false                       true 

My design: 

ArrayList<Integer> factorization = new ArrayList<Integer>();
for (int n = Integer.parseInt(args[0]), f = 2 ; n > 1;     ) {
  if (n % f == 0) {
    n /= f; 
    factorization.add( f ); // an array list of Integers
  } else {
    f += 1;
  }
}
     
Program: 

import java.util.*; 

class Program {
  public static void main(String[] args) {
    ArrayList<Integer> factorization = new ArrayList<Integer>();
    for (int n = Integer.parseInt(args[0]), f = 2 ; n > 1;     ) {
      if (n % f == 0) {
        n /= f; 
        factorization.add( f ); // an array list of Integers
      } else {
        f += 1;
      }
    }
    System.out.println( factorization ); 
  }
}

I compile and run and obtain: 

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> java Program 300
[2, 2, 3, 5, 5]
> java Program 1
[]
> java Program 2345
[5, 7, 67]


Next we start a problem of the kind we might see on the third exam:

1. Design a Dice abstraction such that 

class Dice {
  public int roll() {                         // [1] 
    return  1 + (int)(6 * Math.random()) ;    // [1] 
  }                                           // [1] 
  public static void main(String[] args) {
    Dice d = new Dice();
    for (int i = 0; i < 20; i=i+1)
      System.out.print( d.roll() + " " );
    System.out.println();
  }
}

Produces when run something like this:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Dice
4 3 2 3 1 2 4 3 4 2 4 3 3 1 6 3 5 1 5 2 
>

2. Design a Player abstraction 

import java.util.*; 

class Player extends ArrayList<Integer> {
  String name;
  Player(String name) {
    this.name = name; 
  }
  public String toString() {
    return this.name + " " + super.toString();  
  }
  public int score() {
    int sum = 0;
    for (Integer d : this) {
      sum += d;  
    }
    return sum;
  }
  public boolean isWinner() {
    return this.score() >= 21;  
  }
  
  public static void main(String[] args) {
    Player a = new Player("Adrian");
    System.out.println(a + " " + a.score() + " " + a.isWinner());
    a.add(6);
    System.out.println(a + " " + a.score() + " " + a.isWinner());
    a.add(2);
    System.out.println(a + " " + a.score() + " " + a.isWinner());
    a.add(5);
    System.out.println(a + " " + a.score() + " " + a.isWinner());
    a.add(6);
    System.out.println(a + " " + a.score() + " " + a.isWinner());
    a.add(3);
    System.out.println(a + " " + a.score() + " " + a.isWinner());
    a.add(1);
    System.out.println(a + " " + a.score() + " " + a.isWinner());
  }

}

Run this to get something like in the minute paper:

Welcome to DrJava.  Working directory is C:\Users\dgerman\Desktop
> run Player
Adrian [] 0 false
Adrian [6] 6 false
Adrian [6, 2] 8 false
Adrian [6, 2, 5] 13 false
Adrian [6, 2, 5, 6] 19 false
Adrian [6, 2, 5, 6, 3] 22 true
Adrian [6, 2, 5, 6, 3, 1] 23 true


3. Implement Game:

import java.util.*;

class Game {
  ArrayList<Player> players;
  Dice d = new Dice();
  Game(ArrayList<Player> players) {
    this.players = players;
  }
  void start() {
    while (true) {
      for (Player p : players) {
        p.add(d.roll());
        System.out.println(p + " " + p.score() + " " + p.isWinner());
        if (p.isWinner())
          return;
      }
    }
  }
  public static void main(String[] args) {
    ArrayList<Player> players = new ArrayList<Player>();
    for (int i = 0; i < args.length; i++)
      players.add(new Player(args[i]));
    Game a = new Game(players);
    a.start();
  }
}

4. Add a new kind of Player as indicated in the minute paper. 

--