CSCI Homework Assignment 4 - Bouncing, Bouncing, Everywhere!
Due Tues Sep 25 at 11:59pm
Introduction
This assignment will give you further chances to practice using loops and collections, as well as to practice working with object interactions. Loops are all about doing the same thing over and over again, and one of the best uses of that is animation! In an animation, you move the animated object a small amount over and over again, which gives the appearance of movement. In this assignment, you will expand the Bouncing Ball
program you wrote in lab this week to create an animation of many balls bouncing and changing color!
Note that this assignment should be completed with your lab partner, using pair programming (just like you do in lab)!
The Assignment
UPDATE: There were a few bugs in the back-end and project submitter (pink also now shows as pink). You can download a fixed version of the code here: Homework4_fixed.zip. Note that you will need to copy your existing Ball and BallCourt classes over, either by copying the source code or by moving the .java files from the project folder.
For this program, you and your partner will continue developing your BallCourt
class you started in lab. You will make it so your BallCount can have multiple balls bouncing in random directions. In addition, every time two balls touch, they should exchange colors. So if a red ball touches a blue ball, the red ball becomes Blue and the blue ball becomes red.
-
Start by giving your BallCourt the ability to support multiple balls bouncing in random direcions. Your BallCourt should have an
addBall()
method that adds another ball to the court.-
Hint: You'll want to use an
ArrayList
to create and store multiple balls, and then iterate through that list (using aforeach
loop) to move each ball on each time through the while loop. Make sure to implement and test this capability before you move on. - Your
addBall()
method should simply add another ball to the ArrayList. The method does not need to take a parameter. - Making a descriptive
toString()
method and printing out the Balls status at each step can help with getting this to work. Test your code with one or two balls at a time, and once those are working, add more and make sure it keeps working!
-
Hint: You'll want to use an
-
You'll also need to assign each ball a Random color. You should do this by first making an ArrayList
of all the possible colors. You can then use the ArrayList's get()
method to pick the color that is at a Random index of the list! Assigning a color should be done in the Ball class (remember, the color of a Ball is an attribute of the Ball object), inside the constructor.- This project has the following available colors: "red", "blue", "green", "cyan", "magenta", "yellow", "pink", and "orange".
-
Once you have multiple balls moving, let's make it so they change color when they touch.
-
If you haven't already, you'll need to write a method (e.g.,
isTouching(Ball otherBall)
for the Ball object) to determine whether two balls are touching or not. Hint: You can determine if two circles are intersecting by checking if the distance between their centers (as you might figure out using the Pythagorean Theorem) is less than the sum of their radii. Try drawing it out on paper if you don't believe me!-
Note: you can get the square root of a number using the
Math.sqrt()
method. For example, you can get the square root of 3 usingMath.sqrt(3)
.
-
Note: you can get the square root of a number using the
-
If you haven't already, you'll need to write a method (e.g.,
- Then, after you move all the balls in the BallCourt's while loop, you'll want to iterate through the balls again. This is called a nested loop--a loop inside of another loop. For each ball, you'll want to loop through all the other balls to see if they are touching the first ball. If they are, you want to change the color of the first ball to be the color of the ball it is touching.
-
However, Balls will likely be touching for more than one run through the loop. We want to make sure they don't keep changing color (so the red ball becomes blue, but then is still touching the now red ball so becomes red again, and then is still touching the now blue ball so becomes blue again, etc.)
- To avoid this occuring, we'll need to keep track of what was the LAST ball we touched, and make sure we don't change color when we touch that same ball. We can do this by making a new field (e.g.,
lastTouched
) of type Ball, and updating that field whenever we touch something. Then we just check whether theotherBall
that we touched is different from the reference in our lastTouched field.
- To avoid this occuring, we'll need to keep track of what was the LAST ball we touched, and make sure we don't change color when we touch that same ball. We can do this by making a new field (e.g.,
- In they end, you should be able to have a psychadelic series of bouncing spheres! (Remember to test your code!)
Be sure to document your code carefully, using Javadoc syntax (e.g., @param, @return) for each method and class you create. Also include comments inside methods to explain how your code works! You can view your code's documentation by selecting "Documentation" from the selector box in the upper right hand corner of the code window, or by selecting "Tools > Project Documentation".
Submitting
Before submitting, test each of your methods and classes thoroughly, and double check for comments above each method and make sure both your and your partner's name is in each class. When you're convinced it's ready to go, submit the project through BlueJ just like you did in the previous Assignments (selecting Hwk4 of course). Be sure to upload to the Hwk4 folder, not the Lab D folder!
Submitter will look for the Ball.java
and BallCourt.java
files.
Grading
This assignment will be graded on the following criteria:
- New Balls can be added to and show up in your BallCourt using the addBall() method [10%]
- New Balls are given a Random color [10%]
- All of the Balls in your Ballcourt move in random directions [15%]
- When two Balls touch, they exchange colors [20%]
- Two balls do not repeatedly change color if they continue touching [20%]
- Your code is carefully documented, with proper Javadoc methods [20%]
- Your code uses good style. This means that variables are given descriptive names, tabs and braces are lined up, local variables aren't specified in the fields, etc. If you have any questions about good style, don't hesitate to ask! [5%]