| CPSC 124 | Introduction to Programming | Fall 2006 |
In this lab, you'll learn how to write very basic object-oriented programs.
Create a lab07 directory in your cs124 directory to hold the files for this lab. Copy the files from /classes/f06/cs124/labs/lab07/ to your lab07 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/.
As discussed briefly in class, you can use your own computer to work on assignments outside of the lab using VNC. If you have a Windows or a Macintosh computer, you'll need to install a free VNC program, which will allow you to connect to a computer in the Lansing lab. See the VNC section of Professor Eck's Using Linux at HWS document for information on how to obtain and use the software.
Here are the exercises for this week's lab, due next Thursday.
This exercise was borrowed from the University of Pennsylvania introductory programming course (CSE 110). In the file Game.java (in the lab07 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
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.");
}
}
In this assignment, you will modify Nim.java from lab 5 to use objects. I've provided my copy of Nim.java from the lab 5 solutions (which you can find in the lab07 directory) for you to modify. Your modified program will contain the class Nim along with the class Beans, which will contain state and methods associated with picking beans from the bag.
First, you will create a class called Beans (place it within Nim.java and define it using "class Beans" rather than "public class Beans"). The Beans class will contain an integer variable called totalBeans representing the total remaining beans. This variable should not be directly accessible from other classes, instead it should only be accessible via the methods in the class. You will also create three methods: beansRemaining(), beansLeft(), and pickBeans(). These should be accessible from other classes. beansRemaining() simply returns the remaining beans (i.e., totalBeans). beansLeft() returns true if there are beans remaining and false if there are not beans remaining. pickBeans() takes as input the number of beans picked, and prints out an error if this is an invalid number of beans (see lab 5 for the details), and otherwise, updates its beans remaining (i.e., totalBeans) by subtracting the picked beans from it. pickBeans() should also return true if the beans picked was valid and false otherwise. Finally, you will create one constructor, which takes as input the number of starting beans, which you will use to initialize totalBeans. Your constructor should print out an error message and exit the program if the number of beans is less than or equal to 0.
You will then modify the Nim class to use this new Beans class. Rather than keeping the total beans in an integer, both main() and turn() should instead use a Beans object. The main() method will initialize this object. It will read the number of starting beans from the user (making sure that this is greater than 0) and pass this in the constructor. Rather than passing the remaining beans as an argument to the turn() method, it will pass the bean object. main() will continuously call turn() while the method beansLeft() in the Beans class returns true. JOtherwise, it will stop and print a winner. turn() will then read the picked beans from the user, and call the pickBeans() method in the Beans class. Notice turn() does not have to do any error checking since pickBeans() is already doing the error checking. It just needs to make sure that pickBeans() returned true rather than false.
Verify that your lab07 folder contains all of the files you created or modified for this lab, then copy your entire lab07 folder to the handin directory ~mcorliss/handin/124/username (where username is replaced with your username).