CSSE 120 -- Intro. to Software Development

Homework 14 (due at the beginning of Session 15)

Through step 6f due at the regular time.

Remainder due together with Homework 15.

  1. (5 pts) Complete the Team Preference Survey for the Team Project. You'll find the survey on the course Angel page, under Lessons → Team Preference Survey -- Section X (Instructor) where X is your section number and Instructor is your class instructor.
  2. Complete the assigned reading for the next session: Zelle Chapter 10.6–10.7, 11.1–11.3.
  3. (30 points ) Complete the Angel quiz over the reading assignment. You'll find the quiz on the course Angel page, under Lessons → Homework → Homework 14 → Widgets and Data Collections
  4. Check-out the BallSim project from your Subversion repository into your Eclipse workspace by using the SVN Repository Exploring perspective.
  5. (120 points total) An ideal gas can be simulated by balls moving on a frictionless surface. For this homework you'll be developing the first part of such a simulation. You'll be working through several small steps, committing your work after each step. This will help us give you partial credit if you don't quite make it through all the steps.

    1. (2 points) Open the ballSim module in Eclipse. You should find a few constants and a couple of functions already defined. Add your name to the initial comment, then commit your work to your repository.
    2. (10 points) Add the definition of a Ball class to your program (before main(), so other functions can use Balls). Your class must be named Ball. It should have a constructor that takes one non-self argument, representing the window in which the ball will (eventually) be displayed. Once you have done this, you should be able to run the ballSim module, though it won't yet do anything interesting.

      Ask for help if you get an error when trying to run the program!

      Once the program runs without error, commit your work to your repository.

    3. (10 points) Edit the constructor of your Ball class so that it picks a random initial x and y position for the ball within the given window. Save the coordinates in instance variables of your class. Once this runs without error, commit your work to your repository.

      (Recall that you can use the randrange() function to get a random value in a particular range. For example, randrange(300) returns a random number between 0 and 299 inclusive.)

    4. (10 points) Now you're ready to start making your ball object do some real work. First, add a method named __str__ to your class that returns a string giving the ball's x and y coordinates. Recall that the __str__() method must take a self parameter, but doesn't need any other parameters.
    5. (10 points) Next, add a method named timePasses to your Ball class. The method should take one non-self parameter giving the amount of time that has passed in seconds. For now, you'll ignore the formal parameter. Instead the method body should just contain the single line:

          print self

      Look in the simulate() function. You should find a commented-out line that calls the timePasses() method. Uncomment that line and run the program. The program should print the ball's x and y position to the Eclipse console view.

      Ask for help if this doesn't work!

      Once the program works correctly to this point, commit your work to your repository.

    6. (15 points) Edit the constructor of your Ball class to store an initial x and y velocity for the ball. For now you can just pick a constant velocity. Edit your timePasses() method to update the position of the ball. Recall that the new x position should be the old x position plus the x velocity multiplied the time that has elapsed, and similarly in the y direction.

      Run your program again. You should see the ball's coordinates changing in the Eclipse console window.

      Ask for help if this doesn't work!

      Once the program works correctly to this point, commit your work to your repository.

    7. (15 points) Edit your Ball class to display the ball on the graphics window. Your constructor should create a Circle object to represent the ball in the window. As time passes, your Ball class should use Circle's move() method to update the position of the ball. Once this works, you can remove the print self statement from your timePasses() method. Commit your code to your repository.
    8. (10 points) Give the ball a random initial velocity. You should create a constant at the top of your program that indicates the maximum initial speed allowed. Use the random() function to calculate an initial velocity in both the x and y directions.
    9. (5 points) Does your ball always start moving down and to the right? If so, you probably aren't allowing negative initial velocities. Change your program so the ball can initially move in any direction.
    10. (5 points) Make your ball start with a random initial color. You may find it helpful create a constant like this:

      COLORS = ['red', 'orange', 'yellow', 'green', 'blue', 'violet' ]

      You can then use the randrange() function to choose from the list.

    11. (10 points) Add code to your Ball class so that the ball detects when it has reached an edge of the window. When this happens, print which edge was reached. You should just use a print statement to print to the console.
    12. (5 points) Change your code from the previous step so that instead of printing to the console, the ball bounces off the edge. Do you see how you can change the ball's velocity to simulate bouncing? Did you remember to commit your code to your repository?
    13. (3 points) Edit the code for your Ball class to make your ball change to a new color each time it bounces. Commit your code to your repository.
    14. (10 points) Change your program to create multiple balls. You should create a constant at the top of your program that indicates the number of balls to create. Edit the simulate() function so it first builds a list of the right number of balls, then animates all of the balls in the list. You should be able to do this step without changing your Ball class. Do you see why?

      After testing your code, commit it to your repository.

    15. (5 points) For this last step you do not have to write the code, just think about the problem and write down your thoughts. Add a comment to your program after the simulate() function, answering the following question:

      Suppose you wanted to make the balls in your program bounce off each other. How would you need to change the simulate() function, and what method or methods would you add to your Ball class to implement this?

    16. BONUS CHALLENGE: (15 points) Actually write the code to make the balls bounce off each other. Not for the faint of heart, but if the rest was easy for you ...
    17. Commit the final version of your code to your repository.