SE-0010 Introduction to Java
Lab 4: Spirographer

Outcomes

Assignment

If you ever played with the Spirograph toy as a kid, you are already familiar with the shapes it can draw, like so:

Your assignment is to write a Java application that draws similar shapes using the WinPlotter class that you used in the first Java programming lab of SE1010. Your program will consist of main class called Lab4App. Within this class, you'll create an instance of the WinPlotter class, and call various methods (see this documentation) such that you draw a Spirograph image within the window it displays. This is actually not too difficult; the main trick is programming the equations that describe the shape.

The coordinates (x,y) of any point on the shape are given by the following equations:

where R, r, and d are parameters that govern the overall shape of the curve. The parameters n and t govern the precision at which the curve is drawn. To draw a particular curve, you choose fixed arbitrary values for R, r, and d (for example, 51, 99, and 75 for the curve shown above). You also choose a value for n (for example, 100) The equations are evaluated to give values for x and y as t varies from, say, 0 to 10000, and a line is drawn from each successive coordinate to the next.

For this assignment, this will be the only class needed. Within this class, you'll use several methods, as follows:

main method - this is the usual method where your program begins to execute when it is run, from within this method, you'll first call the following methods:

getR - this is a method that prompts the user for the value of R, converts that input to a numerical value, and returns that value to main(). Incorporate similar checks for empty strings or null strings as in the previous lab.

getr - this is a method that prompts the user for the value of r, converts that input to a numerical value, and returns that value to main().

getd - this is a method that prompts the user for the value of d, converts that input to a numerical value, and returns that value to main()

getn - this is a method that prompts the user for the value of n, converts that input to a numerical value, and returns that value to main()

display - this is the method that does the drawing. This method expects four arguments (R, r, d, and n). Within this method, you need to:

  1. Create an instance of the WinPlotter class.
  2. Call the method to set the size of the WinPlotter window to 500,500.
  3. Set the plot boundaries to -300,-300,300,300.
  4. Set the background to a color of your choice.
  5. Set the pen color to a color of your choice.
  6. Set the title to “SE0010 - lab 4".
  7. Using the above equations, compute the values for x and y when t=0. Use the moveTo() method of WinPlotter to move the pen to this x,y location.
  8. Execute a for() loop that varies t from 0 to 10000. Within the for() loop, evaluate x and y for each value of t. For each set of computed values, use the drawTo() method of WinPlotter to draw to each successive coordinate.

    After calling the display() method, the main() method should present a prompt to the user asking whether the program should run again.

Lab Submission (due 8:00am, Wednesday following lab)

Submit your completed Java program via via WebCT (assignment "Lab 4: Spirograph").. For this program, your program will consist of a single .java file.

Be sure to keep a copy of your work, in case something gets lost.

Your grade will be based on the following criteria: