Tue Sep 24 and Thu Sep 26
Due: Mon Sep 30 before 11:59PM
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:
equals
methodtoString
methodRecall 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:
lab2
)cse1030.drawing
cse1030.drawing
create a new Java class named IPoint2D
.
IPoint2D
so that it implements the following
API:
This means that you must create and complete the following fields, constructors, and methods:
IPoint2D()
IPoint2D(double x, double y)
getX()
getY()
distanceTo(IPoint2D other)
equals(Object obj)
toString()
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:
Project
menuProperties
Java Build Path
Libraries
tabAdd External JARs...
buttoncse1030.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.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:
IPoint2D
instance having coordinates x and ySimpleDrawing.drawPoint
methodMath.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 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