CSCI 136
Fundamentals of Computer Science II
Spring 2018

Montana Tech of The University of Montana
Computer Science & Software Engineering



ASSIGNMENT #2 - Starfield - Dynamic Arrays

In this assignment, you will be creating an animated Starfield Display. Your job is to write the support class and the main program for the display. You will work with dynamic arrays in Java as well as practice some of the things you learned last semester.

Starfield. An animation loop continually displays the stars that are still within view. At each iteration of the animation loop, 20 new stars are added and any stars that have gone out of bounds of the display window are removed. The screen is redrawn each time with all stars shown at their current location (as updated according to they x and y velocities). If done correctly, this should be seen as stars being generated at the center and going in a straight line out to the edge of the field and then disappearing. You will need to use StdDraw.java to do the display and animation work for you. All of the slides from last semester are available to you if you've forgotten something (here), and in particular, the slides for using StdDraw graphics are here.

Since you don't know the number of stars at any one time, you will need to use a dynamic array to hold your list of stars. The size of the array will change as you add new stars and remove any that go out of view. Be careful in removing stars - remember the example in class where there were two "Bob"s in the ArrayList and only one got removed.

Star. The Star class represents a single star in the program. It knows things like its x and y position and its x and y velocities.

Here is the API you will implement for the Star class:
public class Star
-----------------------------------------------------------------------------------------
         Star()      // create a star at screen position 0.5, 0.5 and random  x and y 
                     //     velocities between -0.005 and +0.005
    void update()    // update the x, y position of the star by its velocity
    void draw()      // draw the star as a white circle with radius 0.002
 boolean gone()      // return true if the star has gone off the screen (x or y
                     //     position less than 0 or greater than 1

Do I need to follow the prescribed API? Yes. You may not add public methods to the API; however, you may add private methods (which are only accessible in the class in which they are declared) if you need them. The public methods you write should return the data type shown in the API, and take input parameters (if any) as specified.

Can I use Java's built in ArrayList class? Absolutely! Please do! That's kind of the point of this assignment. There will be assignments coming soon where I will require you to write your own data structures rather than using the Java library though.


Grading
Grade ItemPoints PossiblePoints Earned
Program Compiles
1
Program Runs
1
javadoc comments on all classes and methods
4
Implemented Star() constructor correctly
3
Implemented Star update) correctly
3
Implemented Star draw() correctly
3
Implemented Star gone() correctly
3
Implemented ArrayList additions and removals correctly
6
Implemented Starfield animation loop correctly
4
Draws correctly
2
Total
30

Extra credit idea 1. Create a black hole somewhere on the display where, if a star enters that area, it gets sucked in and disappears.

Extra credit idea 2. Same as above, but make it so that as a star gets closer to a black hole, its velocity changes so that it is drawn toward it.

Extra credit idea 3. Instead of random velocities, make it so that individual stars tend to have similar velocities to their neighbors in the array. This should create "swarm behavior" in the stars.

For the extra credit ideas, you may have to change the API to the Star class. Be sure that the one you submit as your lab assignment follows the API. The extra credit work does not have to.


Submission. Submit your code, Starfield.java and Star.java, using Moodle. Be sure each submitted source file has the required header with your name and a description of the program. Remember! All your comments must be in javadoc format! For extra-credit, please submit a single zip file containing all your programs/graphics/sounds via the special Extra Credit drop box at the top of the Moodle page.

Page last updated: December 27, 2018