Spring Semester 2004


Due date
Tuesday April 20, 11:59pm

Late policy
Try to turn everything on time, no solutions will be accepted late.

Work policy
Working in groups is encouraged but please turn in your own version of the assignment.

Also, please make sure you

the Computer Science Department's Statement on Academic Integrity before turning in your assignment.

Task
Implement the addition game using Java servlets (hidden fields and sessions).

Here's the prototype for the game as developed in class on April 6, 2004.

Prototypes for Homework Assignments 5-7 developed on Tue in class:

burrowww.cs.indiana.edu% cat Five.java
import java.io.*; 

class Five {
  public static void main(String[] args) throws Exception {

    int attempts = 0;     
    int score = 0; 

    String message = null; // "Welcome to the game."; 

    while (true) {

      BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
      

      int a = (int)(Math.random() * 200 - 100), 
          b = (int)(Math.random() * 200 - 100); 

      System.out.print("What is " + a + " + " + b + " ?> "); 

      int result = Integer.parseInt(in.readLine());       

      attempts += 1; 
      if (result == (a + b)) {
        score += 1; 
      } else {
        System.out.println("That was wrong."); 
      } 
      if (attempts == 10) {
        attempts = 0; 
        score = 0; 
        message = "Welcome to the game.";
      } else {
        System.out.println(score + " / " + attempts); 
      } 
    }
  } 
}burrowww.cs.indiana.edu% 
This was OK to define the problem but not that usable and/or operational.

Here's how we changed it to match the pattern:

import java.io.*; 

class Six {
  public static void main(String[] args) throws Exception {
    // define the state and what not 
    int attempts = 0;     
    int score = 0; 
    int a = 0, b = 0, result = 0; 
    String message = null; // "Welcome to the game."; 
    // what not: that's how we read 
    String incoming = null; 
    while(true) {
      // another device (created with every single access) 
      BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); 
      // check the state 
      if (message == null) { // new user: initialize state 
        message = "How are you? Welcome to the game."; 
        attempts = 0; 
        score = 0; 
        a = (int)(Math.random() * 200 - 100);  
        b = (int)(Math.random() * 200 - 100); 
        result = a + b; 
        message += "\nWhat is " + a + " + " + b + " ?: "; 
      } else { // not a new user 
        // read the input you need 
        int user = Integer.parseInt(incoming); 
        // then process the state (update it with this input) 
        attempts += 1; 
        if (result == user) {
          score += 1; 
        } else {
          message = "Not good."; 
        } 
        if (attempts == 10) {
          attempts = 0; 
          score = 0; 
          message += "\nEnd of game.\nNew game has started.";
        } else {
          message += "\n" + score + " / " + attempts; 
        } 
        a = (int)(Math.random() * 200 - 100);  
        b = (int)(Math.random() * 200 - 100); 
        result = a + b;         
        message += "\nWhat is " + a + " + " + b + " ?: "; 
      }
      // store the state: no need for that here 
      // show the state: we put everything we want to show in the message 
      System.out.print(message); 
      // get ready for more input 
      incoming = in.readLine(); 
    } // on the web the process is user-driven, here we need this infinite loop 
  } 
}
Grading
Feedback will be provided within a week, grades will be posted on-line.