CS 161 - Intro to Computer Science
Lab: Circle Drawer
This assignment is all about conditionals and object references! We’ll work with the CircleDrawer
class that is given to you. Along the way you’ll write code that uses and manipulates object references, and requires more complex boolean expressions than you’ve written previously.
Student Outcomes
- Practice object interaction (puppeteering)
- Practice using object references
- Practice returning values across objects
- Working with null reference testing
Required Files
The following file(s) have been provided for this homework.
Instructions
-
Download the
CircleDrawer
project from the link above and extract its contents, then start BlueJ and open the project. Take a moment to familiarize yourself with the code in theCircleDrawer
class. ACircleDrawer
contains two instance variables as references to a pair of Circles (namedfirst
andsecond
). -
Notice that the
CircleDrawer
class has two constructors. The default constructor is not given references to the two Circles to manage, it just sets both fields tonull
. The second constructor is given references to twoCircles
, and it sets both instance variables appropriately.- Using the menus in BlueJ, instantiate two
Circle
objects. You can name themc1
andc2
. - Change
c1
to green, change size to50
. - Change
c2
to red, change size to75
. Then move it down by50
pixels. -
Now create a new
CircleDrawer
, called drawer with its second constructor (the one that requires references to two Circles). Inputc1
andc2
as your inputs. The newCircleDrawer
will now “control” the two circles. -
Inspect the
CircleDrawer
object on the workbench, and you see thatfirst
andsecond
store references (arrows). Follow the arrows by double-clicking on them. It opens the inspector to the Circles you created! -
Call the
drawCircles()
method on the drawer, and notice that both Circles appear. (If you calleraseCircles()
both disappear!) - So, this gives us some idea about what a
CircleDrawer
object does. It’s basically a “puppet master” that bosses around one or two Circles.
- Using the menus in BlueJ, instantiate two
-
Now go back in the
CircleDrawer
code and take a look atdrawCircles()
anderaseCircles()
. Their code looks pretty straightforward. Calling them will make both circles visible or invisible, but… that’s assuming that ourfirst
andsecond
fields actually refer to actualCircle
objects, and notnull
.- Create a
CircleDrawer
object using its default constructor (that’s the one with no inputs). - Inspect it, and see that
first
andsecond
both point tonull
(no arrows to circle objects as before). - Let’s see what happens if you tried to call
drawCircles()
oreraseCircles()
. - The program should crash with a
"NullPointerException."
That’s bad news – it means our code isn’t all that robust.
- Create a
-
Back in the
CircleDrawer
code, add necessary changes todrawCircles()
anderaseCircles()
so that no action is taken if either field refers tonull
. To do this, use an if-statement to check if each field is==
or!=
to the keywordnull
. Make sure you test these out after you’re done. -
Now modify the
drawCircles()
method so that it only draws circles with radii strictly greater than20
and strictly less than50
.- But how do you get a
Circle
’s radius? One way is to go intoCircle
and making thediameter
public instead of private. However this is a practice considered highly dangerous because it exposesdiameter
to unwanted changes. You should instead add a method in theCircle
class that returns its diameter. - Again, test this out. Ahem – if your Circle’s diameter is say, 41, it should be drawn.
- But how do you get a
-
Go back in the
CircleDrawer
class. Add a method calleddrawLarger()
that draws only the larger of the two circles. The smaller one should be hidden. If there’s a tie, then draw both of them. -
Add a method in
CircleDrawer
calledsizeRatio()
that returns the ratio of the two circle’s diameters as adouble
. If one circle has a diameter of 10 and another has a diameter of 3, then this method returns 0.333… You should always divide the smaller diameter by the larger.- If either circle is
null
, simply return 0.
- If either circle is
-
Add a method in
CircleDrawer
calledaddCircle()
that inputs aCircle
object, and it doesn’t return anything. This method will first erase bothCircle
s. If either of the two instance variables (first
,second
) are pointing tonull
, then simply assign the newCircle
to that field. If neither reference is pointing tonull
, then promote the secondCircle
to thefirst
, then sets the newly-inputCircle
to be thesecond
. After you’re done, make both Circles visible.- To test, I would create 3 circles in BlueJ. Then create a CircleDrawer using the default constructor so that both
first
andsecond
arenull
. Then calladdCircle()
3 consecutive times (on the 3 circles you created).
- To test, I would create 3 circles in BlueJ. Then create a CircleDrawer using the default constructor so that both
-
Add a method called
swapCircles()
that inputs and returns nothing. Calling this method will causefirst
to now point to whateversecond
held, and vice versa. -
Add a method called
replaceSmallest()
that takes aCircle
as input. InCircleDrawer
, if either of the two instance variables arenull
, simply add the new circle into the empty slot. If both instance variables are non-null, this method must replace the smaller circle with the new one. We need to respect seniority, though:- If
first
is smaller, we’ll first promotesecond
to take its place, and assign the new circle insecond
’s place. - If
second
is smaller, we’ll just replace it with the new circle. In the case of a tie, replace second. - This method seems to have quite a bit of similarities to
addCircle()
. You don’t need to, but kudos if you can figure out how to utilizeaddCircle()
andswapCircles()
to simplify writing this method.
- If
Grading
This assignment will be graded out of 2 points, provided that:
- You were in attendance and on-time.
- Completed all required methods.
Submitting Your Assignment
Follow these instructions to submit your work. You may submit as often as you’d like before the deadline. I will grade the most recent copy.
-
Navigate to our course page on Canvas and click on the assignment to which you are submitting. Click on “Submit Assignment.”
-
Upload all files ending in
.java
from your project folder. -
Click “Submit Assignment” again to upload it.
Credits
Written by Brad Richards with modifications.
Lab Attendance Policies
Attendance is required for lab. Unexcused absence = no credit even if you turned in the lab. Unexcused tardiness = half credit.