CS 140 - Final exam Review Question - Closest To Center

Thursday April 30

You may use your notes, the computer, and any previous programs we have developed this semester. Make sure you save your program in a folder named ClosestToCenter in your student folder.

Write a program that populates a screen with twenty particles in random locations. Each particle should be 20 pixels wide. Make all of the particles black. This program should color the particle that is closest to the center of the canvas red.

Hint one: Recall how you find the smallest item in an array. Hint two: write a function in the particle class named distanceToCenter that returns how far the particle is from the center of the canvas.

Start with Particle.java and Program.java file in the folder ClosestToCenter.

Enhancements

  1. Have the program animate finding the closest particle. When a particle has been processed turn it blue if it is not the closest. If the particle is the new closest particle then make it red and make the particle that was the closest blue. Have your program take one second to look at each particle.