| CPSC 124 | Introduction to Programming | Spring 2007 |
In this lab, you'll learn how to write very basic object-oriented programs.
Create a lab09 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/s07/cs124/labs/lab09/ to your lab09 directory.
This lab covers Sections 5.1 through 5.4 of the book. If you don't have your book with you, the book can be found online at http://math.hws.edu/javanotes5/.
Here are the exercises for this week's lab, due next Thursday.
Within a program called CoinToss.java (you'll see why it's called CoinToss below), write a class called Counter that represents a counter that counts 0, 1, 2, 3, 4, .... The Counter class should have one private instance (i.e., non-static) variable representing the value of the counter. It also has two instance (i.e., non-static) methods: increment() adds one to the counter value, and getValue() returns the current counter value. (When you define the class, you should use "class Counter" rather than "public class Counter".)
You will then write another class called CoinToss (which will be declared as "public class CoinToss") that simulates 100 coin tosses and keeps track of the number of heads and tails using two counter objects: headCount and tailCount. Your class CoinToss will look like the following, although you will have to fill in the blanks (i.e., _____________________):
public class CoinToss {
public static void main(String[] args) {
Counter headCount, tailCount;
headCount = new Counter();
tailCount = new Counter();
for (int flip = 0; flip < 100; flip++) {
if (Math.random() < 0.5)
_____________________;
else
_____________________;
}
System.out.println("There were " + _____________________ + " heads.");
System.out.println("There were " + _____________________ + " tails.");
}
}
This exercise was borrowed from the University of Pennsylvania introductory programming course (CSE 110). In the file Game.java (in the lab09 directory), you will find a class Player that is a simplistic representation of a Player in a video game. The code is incomplete and you must fill in the details. At every point where you see the comment "add code here..." you will need to fill in the appropriate java code, based on the specification and sample output below. There is also a class called Game, which can be used for testing your Player class. To test your player, add code to the main() method in Game (it already has some code in it that will test your Player class, but you may want to add more). Then compile your program using "javac Game.java" and run it using "java Game".
Here are some details about a Player. Every Player has strength and points (both of which may vary). Every player is alive or dead. When a Player is created, the maximum amount of strength that it can attain is passed as an argument to the constructor. However the maximum strength can not be less than 50 or greater than 200. If there is an attempt to create a Player with maximum strength outside this range, its maximum strength is set to 100. Upon creation a Player has maximum strength and 0 points (and is alive). A Player can earn points by fighting, although it loses strength when doing so. For each level of strength that the Player is able to fight it gains 10 points. A Player's strength can not go below 0. When a player loses all strength, it dies. After that, it can not become alive again and neither its strength nor points may change. The following is the main() method from Game along with the output from running Game, that you should also see once your Player class is working.
Testing code:
public class Game {
public static void main(String[] args) {
Player p = new Player(80);
System.out.println("Max strength: " + p.getMaxStrength());
// Correct output: Max strength: 80
System.out.println("Current strength: " + p.getStrength());
// Correct output: Current strength: 80
System.out.println("Is alive? " + p.isAlive());
// Correct output: Is alive? true
System.out.println("Current points: " + p.getPoints());
// Correct output: Current points: 0
System.out.println("Able to win fight of 20? " + p.fight(20));
// Correct output: Able to win fight of 20? true
// Note: Player is able to win fight of 20 since Player has
// strength of 80 (>20)
System.out.println("Current strength: " + p.getStrength());
// Correct output: Current srength: 60
System.out.println("Current points: " + p.getPoints());
// Correct output: Current points: 200
// Note: Player got 10*20 points for the fight of 20 above
System.out.println("Max strength: " + p.getMaxStrength());
// Correct output: Max strength: 80
System.out.println("Able to revive 30? " + p.revive(30));
// Correct output: Able to revive 30? true
// Note: can't have more strength than max strength of 80
// so revived for only 20 of 30 points
System.out.println("Current strength: " + p.getStrength());
// Correct output: Current strength: 80
System.out.println("Able to win fight of 100? " + p.fight(100));
// Correct output: Able to win fight of 100? false
// Note: this fight uses up all Player's strength (80) so Player
// does not win, and is killed
System.out.println("Current strength: " + p.getStrength());
// Correct output: Current strength: 0
System.out.println("Is alive? " + p.isAlive());
// Correct output: Is alive? false
System.out.println("Current points: " + p.getPoints());
// Correct output: Current points: 1000
// Note: first fight gave Player 200 and second fight (which
// killed Player) gave it 800 for a total of 1000
System.out.println("Able to revive 50? " + p.revive(50));
// Correct output: Able to revive 50? false
// Note: can't revive a dead player
System.out.println("Current strength: " + p.getStrength());
// Correct output: Current strength: 0
}
}
Output:
bash$ java Game Max strength: 80 Current strength: 80 Is alive? true Current points: 0 Able to win fight of 20? true Current srength: 60 Current points: 200 Max strength: 80 Able to revive 30? true Current strength: 80 Able to win fight of 100? false Current strength: 0 Is alive? false Current points: 1000 Able to revive 50? false Current strength: 0
Verify that your lab09 folder contains all of the files you created or modified for this lab, then copy your entire lab09 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).