Duke Computer Science Shield

CompSci 201: Data Structures & Algorithms

Recitation

Fall 2012

 

Recitation 1:
Practicing Java syntax with loops and arrays

Make sure you have snarfed the project DrawCircles from the class snarf cite, http://www.cs.duke.edu/courses/fall12/compsci201/snarf/. The code has three files, DrawCircles.java, DrawHelper.java, and Circle.java. Today you will only be modifying DrawCircles.java. Run DrawCircles.java. You should see a picture like this:

 

 

Part 1

Look at the "draw" method in DrawCircles.java. The method is returning an array of Circles (you know this because before the name of the method you can see Circle[]). Change the code to draw 5 circles instead of 3.

Part 2

Now, what if we wanted to make 100 circles? 1000 circles? 10000 circles? You probably want to write a loop to do this. Change the code to create 5 circles using a loop. Start by making an array of 5 circles that are all the same color, the same size, and are in the same location. Once you get this working, modify your code so that you have a line of 50 circles, evenly spaced from the top right corner to the bottom left corner of the screen.

Part 3

Add a method to your code named drawMoreCircles that can draw n circles that have different colors, locations, and sizes, and are not in a line.

 

Can you get something like this?

 

 

Submit your code

Go to Web Submit to submit your code. Click on "Select File", select your DrawCircles.java file (Make sure your name is on it). Select Compsci201, and recitation_1. Enter your net id and password to submit your work.

 

Before you leave class, make sure you are comfortable writing loops and arrays in Java. If you are still having problems please come see us or write some practice code over the weekend.