CS 161 Assignment #2

Due Monday, January 29th by 11:59pm
(Not accepted after 1/31)

Introduction

This week you'll get a chance to write some actual Java code, but we'll take it in small steps: You'll make a few modifications to the book's Circle class — the same one we've been using in class and in lab.

The Assignment

For full credit, the code you add to Circle should behave exactly as described, and the method names and printed output should match the assignment description. As part of the grading process I will run a program that creates instances of your class and tests them. If your names or other details differ, my testing code won't compile and I'll get sad and grumpy.
  1. Start by downloading the BetterCircle BlueJ project and extracting it from the .zip archive like you did with the first project in lab. Double-click on the package.bluej file within the project to open it in BlueJ. When it opens, you'll see that only the Canvas and Circle classes are included. All of your changes will be to the Circle class.
  2. Your first task is to implement a method called moveDiagonally, that takes a single input from the user — the distance to move — and moves that far both horizontally and vertically. I've written the outline of the method, but you need to add some code where it says to in order to make the circle move as it should. Think about which methods you'd call on a Circle object to make it do those two move operations. Then think about what the equivalent lines of code would be to perform those operations, and add them at line 35. Compile and test your method before moving on. (Note: Moving the same distance horizontally and vertically means it will only be able to move up and to the left, or down and to the right, but that's ok.)
  3. The next task is to add code to the class to keep track of how many times a Circle has been asked to move, and to report that information when the user requests it. We'll break this down into smaller steps so it's easier to manage:
    1. Add a new field to the class that will store the number of moves. You'll need to declare this field at the top of the class where diameter, xPosition, etc, are located. Use a meaningful name for the field so your code's easier to read, and make sure it's of the appropriate type.
    2. Make sure the field is properly initialized — it should start out at zero, since a circle object hasn't yet moved when it's created.
    3. Go through the existing methods and add lines of code where necessary that will increment the field's value (make it larger by one) when the various move methods are called. The moveDiagonally operation you just implemented should only count as one move since the user can't distinguish the horizontal and vertical moves being performed — it just looks like a single movement. Verify that your code works by creating a circle, moving it, and using the object inspector (double clicking the red Circle object) to verify that the new field's value is updating properly.
    4. Now add a method called printNumberOfMoves below moveDiagonally. It should print a line of output that looks exactly like the one below (assuming it has moved five times). The output will appear in the Terminal window when the method is called.

      Circle has moved 5 times
      

      You'll need to write the entire method yourself this time, since the project doesn't contain a starting point for it like it did for moveDiagonally. Test your code before proceeding.

Grading

This assignment will be graded out of a total of 50 points.

Extras

If you're up for an extra challenge, try writing a method called resetCount that sets the number of moves back to zero whenever it's called. Test it to make sure it works the way you expect it to.

Submitting

Before submitting, test your new code thoroughly and make sure it is commented properly. When you're convinced that it's ready to go, submit the assignment using BlueJ's submission tool as described here.


Brad Richards, 2024