All the raw scores for all exams are in. 

Starting tomorrow please come and pick up your exams:

(a) adjustments (lab reports) 

(b) retake (get 100)

C212 four exams:

(a) read Java

(b) write C211 type of problems in Java 

(c) more complex problems leading into the project (4/15)

the project happens here 

(d) variation on the project 

Today I would like to show you an example of (b) and (c). 

Midterm is (b) and I show a retake. 

Homework Eight defines all the retakes, but it will finalized tonight. 

Problem One: 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.

             Phoebe 
Shikun   Nick Vinayak Nick Joe    Kevin Griffin Shane 
   Minh Grant Daniel    Chen Mahamat Taylor Connor
   Haleigh Erin    Ben Michael   Danielle 

We want a basic approach expressed in C211 terms if possible.

n > 1     n       f      n % f == 0     factorization 
-------------------------------------------------------
true    300                             [] 
true              2      true           [2]
        150       2      true           [2 2]
         75       2      false   
                  3      true           [2 2 3]
         25       3      false 
                  4      false 
                  5      true           [2 2 3 5]
true      5       5      true           [2 2 3 5 5]
false     1 

So I write the core of what I described above as: 

ArrayList<Integer> factorization = new ArrayList<Integer>(); 
for (int n = Integer.parseInt( args[0] ) , f = 2; n > 1;          ) {
  if (n % f == 0) {
    n = n / f; // that's my next n 
    factorization.add(f); 
  } else {
    f = f + 1; // that's my next f 
  } 
}
System.out.println( factorization ); 

I can now write the code:

import java.util.*;

public class Program {
  public static void main(String[] args) {
    if (args[0].equals("0")) System.out.println( 0 ); 
    if (args[0].equals("1")) System.out.println( 1 ); 
    ArrayList<Integer> factorization = new ArrayList<Integer>(); 
    for (int n = Integer.parseInt( args[0] ) , f = 2; n > 1;          ) {
      if (n % f == 0) {
        n = n / f; // that's my next n 
        factorization.add(f); 
      } else {
        f = f + 1; // that's my next f 
      } 
    }
    System.out.println( factorization ); 
  }
}


import java.util.*;

public class Program {
  public static void main(String[] args) {
    if (args[0].equals("0")) System.out.println( 0 );
    else if (args[0].equals("1")) System.out.println( 1 );
    else {
      ArrayList<Integer> factorization = new ArrayList<Integer>();
      for (int n = Integer.parseInt( args[0] ) , f = 2; n > 1;          ) {
        if (n % f == 0) {
          n = n / f; // that's my next n
          factorization.add(f);
        } else {
          f = f + 1; // that's my next f
        }
      }
      System.out.println( factorization );
    }
  }
}

Let's implement a Dice abstraction.

class Dice {
  // your code here 
  public static void main(String[] args) {
    Dice d = new Dice(); 
    for (int i = 0; i < 20; i++) {
      System.out.println( d.roll() ); 
    } 
  } 
}

// 1 2 6 3 5 4 1 6 3 6 5 1 4 2 3 1 6 2 3 4 

import java.util.*; 

class Player {
  String name;
  ArrayList<Integer> scores;
  Player(String name) {
    this.name = name; 
    this.scores = new ArrayList<Integer>();
  }
  boolean isWinner() {
    return this.score() >= 21;  
  }
  int score() {
    int sum = 0;
    for (Integer n : this.scores) {
      sum += n;  
    }
    return sum;  
  }
  void add(int number) {
     this.scores.add( number ); 
  }
  public String toString() {
    return this.name + " " + this.scores + " " + this.score() + " " + this.isWinner(); 
  }
  public static void main(String[] args) {
    Player p = new Player("Chen"); 
    System.out.println( p ); // Chen [] 0 false 
    p.add( 5 ); // player p rolls a 5
    System.out.println( p ); // Chen [5] 5 false 
    p.add( 6 ); // player p rolls a 6
    System.out.println( p ); // Chen [5 6] 11 false 
    p.add( 6 ); // player p rolls a 6
    System.out.println( p + " " + p.isWinner()); // Chen [5 6 6] 17 false false 
    p.add( 1 ); // player p rolls a 1
    System.out.println( p + " " + p.score()); // Chen [5 6 6 1] 18 false 18  
    p.add( 3 ); // player p rolls a 3
    System.out.println( p ); // Chen [5 6 6 1 3] 21 true     
  }
}

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);
        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();
  }
}