Debugging Practice
Beware! This document needs cleaning up. Formatting may be messy or broken. Text may be inaccurate or in need of editing.
You can look at this preview, but be aware that the content will change before the instructor assigns this to the class.
This document is the result of an automated conversion. You can view the original version but again beware: that version too is not necessarily what will eventually be assigned.
This activity asks you to debug existing code. There are 4 separate files for this activity, each with a different type of bug.
Part 1: Macalester Lines
In this part, you will be debugging the MacalesterLines
class. This class should draw lines that look something like the back of a Macalester ID card, but it currently only draws one line.
Carefully read through the code to determine what is causing the issue. You might consider using the source debugger, print statements, or partial execution to narrow down the problem.
Please note that all the colors and coordinates in the code are already correct.
Then fix the code so that it draws the picture below:
Part 2: Drawing Stars
The Star
class is supposed to produce this image, but it doesn’t:
The Star
class draws stars by breaking the process down into two methods. The first method, getStarPoints
, makes the list for the points of a star that is centered at (0,0)
. The second method, translateStarPoints
, moves the points to be centered in the canvas window, so that the entire star is visible when drawn. However, it isn’t quite drawing the star correctly. Hint: The getStarPoints
method has no bugs.
- Find and fix this bug so that the code draws the star correctly.
- Take a closer look at where you found the bug. Is this the best means of carrying out this type of task, given that we are transforming each element of a List? If not, what would be a better way of doing this? Hint: There is a stream operation that applies some transformation to every element of a list and gives you a new list with the results. What is it? To use it, you will need to change the signature of the translateStarPoints() method and slightly change how you use the method. Note that if the code used that approach, it would have prevented the original bug entirely!
Part 3: Color Wall
The ColorWall
class is supposed to draw some simple mid-century modernist art. There are multiple bugs in this class, which you can solve in any order. Hint: The getColor
method has no bugs.
- The outline on the image above shows how large the window should be. Unfortunately, the window is much bigger. (NOTE: on most computers it will only go as big as your screen by default.) Figure out why the window is this size. Pay special attention to the variables the code uses to define the window’s size.
- Only one square seems to be drawing. Figure out why. Hint: Pay careful attention to the punctuation characters around the loops.
- The squares are likely not centered in the window. Figure out why and fix it.
Part 4: Shape Wall
The ShapeWall class is supposed to draw a series of shapes from simple to more complex, but there are multiple errors preventing this from working as intended.
- The first error is keeping the program from drawing any shapes on the canvas at all. Finding this error will require a very close read-through of all of the code. Pay careful attention to how variables are declared, initialized, and used within each scope.
- Once you have shapes being drawn to the canvas, you’ll notice that they aren’t the regular polygons you should be getting. Take a look at how the code generates the polygons’ points. You may need to look up the javadoc for the math functions the code uses.
- Now that your program is drawing regular polygons, note you might still get an error message in the console. If so, figure out what causes this error, and what changes you need to make so the error doesn’t occur.