CSE1030 Lab 02

Tue Sep 24 and Thu Sep 26
Due: Mon Sep 30 before 11:59PM

Introduction

The goals of this lab are to implement a small immutable class, and then use the class in generating pictures of shapes produced by a famous toy called the Spirograph™.

You will need to implement the following features in your class:

Question 1: Implement a small immutable class

Recall that an immutable class is a class whose instances cannot modify their state after they have been constructed.

Implement the class named IPoint2D that represents immutable points in the real 2D plane. Every IPoint2D object has an x and a y coordinate.

In eclipse:

  1. Create a new Java Project (perhaps called lab2)
  2. In your project, create a new Package named cse1030.drawing
  3. In the package cse1030.drawing create a new Java class named IPoint2D.
  4. Complete the class IPoint2D so that it implements the following API: This means that you must create and complete the following fields, constructors, and methods:
    • a field for the x coordinate
    • a field for the y coordinate
    • IPoint2D()
    • IPoint2D(double x, double y)
    • getX()
    • getY()
    • distanceTo(IPoint2D other)
    • equals(Object obj)
    • toString()

Question 2: Use your class to draw hypotrochoids

Spirograph™ is a famous toy that lets the user draw shapes called hypotrochoids (and epitrochoids) using a colored pencil and special wheels (see the image below by Kungfuman)

The user spins the smaller wheel inside the larger wheel using a colored pencil. Usually, the smaller wheel must travel around the larger wheel many times before the pattern is complete. The curves generated by this process are called hypotrochoids. Here is a video showing a similar toy in action.

For our purposes, we will describe hypotrochoids using the following equations:


  1. Download this jar file and add it to your project:
    1. select the Project menu
    2. select Properties
    3. on the left side of the dialog that appears, select Java Build Path
    4. on the right, select the Libraries tab
    5. click the Add External JARs... button
    6. add the jar file you downloaded
  2. In the package cse1030.drawing create a new Java class named Spiro. The main method of this class will draw a hypotrochoid using values P, Q, and H that are supplied to the program using command line arguments. A partial implementation is here: The partial implementation runs, and shows you how to draw a single point. Try running the program; it should crash with an exception. Once it crashes, go to the Run menu in eclipse, and select Run Configurations.... Click in the Arguments tab and add three program arguments as shown below; press Run and a window titled Standard Draw should appear with a single black dot in the center of the window.

  3. Complete the main method so that it draws a hypotrochoid one point at a time. Your program should draw each point of the hypotrochoid for values of θ = 0, 1, 2, 3, ..., 360 * N degrees; note that N is computed for you in the starter code. Basically, you need to write a loop that counts from 0 to 360 * N degrees in steps of 1 degree. Each time in the loop you need to:
    1. compute the x coordinate
    2. compute the y coordinate
    3. create an IPoint2D instance having coordinates x and y
    4. draw the point using the SimpleDrawing.drawPoint method
    Beware that Math.cos and Math.sin expect angles in radians whereas we are using angles in degrees. There is a method in java.lang.Math that will convert degrees to radians.

Here are some sample solutions using different values for the command line arguments:

3 5 0.5


7 15 0.2


31 100 0.2

Submit

Submit your solution using the submit command. Remember that you first need to find your workspace directory, then you need to find your project directory. In your project directory, your files will be located in the directory src/cse1030/drawing

submit 1030 L2 IPoint2D.java Spiro.java