Mini-Assignment A: Counters
Due Friday, February 8th at 7:00 PM

Java concepts I want you to learn from this assignment:

Other concepts that will prepare you for Course Assignment 1:

Files:
miniA.jar
titlescreen.PNG

Intro:
If an action happens repeatably, it's natural to ask how many times it has happened.  Whether it is times we've left the country, goals in a soccer game, movies from a  director, or hours playing World of Warcraft, it can be very useful information.  For this assignment we will write a class that can help us count and demonstrate how to use it.  For this assignment you will be responsible for writing three classes: Counter.java, CounterApp.java, and RunAway.java

You may ask for help from TAs during their consulting hours, but they may not have seen the assignment before.  They can help you as they would on a course assignment.  Show them this page if they have questions.

Counter.java

The Counter class will be an instantiable class useful for keeping track of how many times something has occurred.  There are three steps that we need to do this properly.  We must be able to construct a Counter that starts at zero, we must be able to increase the count by one whenever we want, and we must be able to check the count when we want.  Here's how we will define them:

A Constructor that takes no input and returns a constructor with a count of zero
A mutator method named increment() that takes no input and no output that increases the count by one.
An accessor method named getCount() that takes no input and return the count as an int.

We will also define another constructor and another two methods to make it even more convenient:

A constructor that takes an int as input and uses that as the start of the count.
A mutator method named reset that takes no input and gives no output that returns the count to 0.
A method named createEquivalentCounter() that takes no output and returns a brand new counter that has the same count as the one we started with.

The names, parameter types and returns types will need to be exactly correct in order for RunAway.java to work when you run it.  To make it even more clear, you may refer to Counter.html which has everything spelled out exactly for you.

CounterApp.java

Next we will use the Counter class.  This is in part to show what the counter is used for, but it is mostly a testing class to make sure the counter actually works. Create an application.  Here's what it should do when it is run, in order:

  1. Create a default Counter.  Print it's count to the screen.  (It should be 0)
  2. Create a second Counter that starts its count at 2.  Print out its count to the screen.    (It should be 2)
  3. Increment the first Counter three times and print the count.   (It should be 3)
  4. Increment the second Counter twice and print its contents to the screen.  (It should be 4)
  5. Create a third Counter by calling createEquivalentCounter() on the first Counter.  Print out its count.  (It should be 3)
  6. Reset the first Counter to zero and increment it once.  Print its count.   (It should be 1)
  7. Finally, print the count of the third Counter again.  (It should be 3)

When I run your application it should do all those actions and the console should have this output: expected.txt

Notice that every method is called at least one and the values is printed before and after the method calls.  We also involve several objects.  Otherwise, we may have a very poor tester.

RunAway.java

This will be an very simple class for you to write.   It will just be an application class that calls the function begin() on the class ChaseApp. Optionally, you may pass in your name as a parameter.  This will launch code that I have written. In order for it to compile, you will need to download miniA.jar to the folder where your project is.  Then go to your project properties and add the .jar to your project. If you want the game to look a little nicer, also put titlescreen.PNG  in your project directory.

The code in the jar is for a complete game, except there is one piece missing:  a Counter with the exact description I gave you.  If it's not there (or things have the wrong method names, return types, or parameters), the program will crash.  If the Counter class has all the right names but the code is wrong, you may get a very easy, very hard, or very strange game.  If it is written based exactly as described and written correctly, it is a puzzle piece that should fit exactly, and the game will work.

How do I know if it is working properly?
If a window pops up and lets you play a game, you are doing well.  Watch the score and see if the Counter is doing it's job right.  There is an older version of the game here you can use for a comparison, but there will be large differences between the two.

How is my Counter used?
Your Counter will be used in several ways.  When the game starts, a default Counter is created.  Every frame (remember how movies show 24 frames each second?) the increment() method is called.  When you restart or change levels, the reset() method() is called.  The getCount() method is used to display the score on the screen, to see if it is the high score, and to release opponents onto the screen.

If the current score is the high score, a new Counter is created through the createEquivalentCounter() method.  The new Counter is saved in each Level Object.  Then getCount() is called to display the best score at the top in the level and to determine the color and score for the High Score list.

This is cool!  How can I learn more from this assignment?
Save a copy of your working code and play around with it.  The game is much easier if the counter increases by 10 each turn.  The game is easier still if it increases by 9 (since opponents aren't always released).  Some later levels unleash hordes of enemies if getCount() always return 150.  What happens if getEquivalentCounter() return a count that's twice as high?  What if the reset() does nothing?  What if the default constructor creates a Counter that starts at 4242?

Turning this in

Remember that TUSSG applies, as well as the style and commenting guides for this class.  If you work in pairs, follow the pair programming guidelines and have both partners send me a readme.txt.  Do not use the course handin program, since it isn't set up for my miniassignments.

Recall that the course policy is not to accept late work.

The files I want are:
Counter.java
CounterApp.java
RunAway.java

Pair partners:
README.txt from both partners

Some extra credit if you generate good javadoc (with your own comments) for the Counter class and send me Counter.html.  Possible extra credit if you send me something that shows me you did something above and beyond what is required.

Please email the files to Tim Bahls.  Send them to bahls AT wisc.edu.  If you know how, please put them in a .zip file.

Last updated 2-4-2008