Methods, Drawing and Style


  • Practice writing and calling void methods that take parameters.

  • Following coding standards.

  • Using Checkstyle to check coding standards.

Key Terms

parameter (or formal parameter)
A variable declared inside the parentheses of a method header.
argument (or actual parameter)
A value passed to a method at the time the method is called. The value will be assigned to the parameter variable.

Part 1: Drawing Pictures

For this activity we will make use of the StdDraw class developed by Robert Sedgewick and Kevin Wayne at Princeton University. This class provides a set of methods that make it possible to create simple drawings in Java.

You can find the complete documentation online, but today we will only be working with the following methods:

Modifier and Type Method and Description
static void circle(double x, double y, double radius)
Draws a circle of the specified radius, centered at (xy).
static void filledCircle(double x, double y, double radius)
Draws a filled circle of the specified radius, centered at (xy).
static void rectangle(double x, double y, double halfWidth, double halfHeight)
Draws a rectangle of the specified size, centered at (xy).
static void filledRectangle(double x, double y, double halfWidth, double halfHeight)
Draws a filled rectangle of the specified size, centered at (xy).
static void line(double x0, double y0, double x1, double y1)
Draws a line segment between (x0y0) and (x1y1).
  1. Create a folder to contain your work for today's activity.
  2. Copy the following files into your folder:
  3. Open in jGRASP or Eclipse. Take a minute to read over the code, then compile and execute it.
  4. By default, (0, 0) is located in the lower-left corner of the StdDraw drawing window and (1, 1) is in the upper-right. Modify to place a small filled rectangle in the upper-left corner of the drawing window. Test your modifications.

Part 2: Drawing Houses

  1. Open using jGRASP or Eclipse. Take a minute to read over the provided code, then try running it.
  2. Add a new method named drawHouse that satisfies the following requirements:
    • It should take two double parameters named x and y.
    • When executed, it should draw a house that looks like the following: house
    • The total width of the house should be .2.
    • The total height of the house should be .3. The rectangle at the bottom should have a height of .2 and the roof should have a height of .1.
    • The rectangle that forms the bottom part of the house should be centered at (x, y). I.e. the location of the house should be determined by the parameter values.
    • The method should include a call to drawDoor. YOU SHOULD NOT COPY THE CONTENTS OF drawDoor INTO drawHouse!
  3. Modify your main so that in includes three calls to drawHouse. Houses should be drawn at (.2, .5), (.5, .5) and (.8, .5). The result should look like the following:

  4. For your submission do a File - Save and save the drawing as houses.jpg

Part 3: Style

Make any necessary modifications to ensure that your finished program conforms to the style guide from Lab3.

Part 4: Checkstyle

Follow the instructions on the Checkstyle lab page to run Checkstyle on Fix any style issues that are flagged.

Part 6: Submitting

In order to complete this lab you need to:

  • Save a copy of the image generated by your completed You can save the image through the file menu of the StdDraw drawing window. The file name should be "houses.jpg".
  • Upload a zip file called containing your  and houses.jpg file through Autolab is configured to run Checkstyle. In order to get full credit, your submission will need to pass all of the formatting tests along with compiling and checking the diff on your output.

If You Have Extra Time

  • Copy your code to an file and add a drawWindow method to and use it to improve your drawHouse method.
  • Create a drawStreet method that draws three houses with a road in front of them. Modify the main for this class to include two calls to drawStreet.

Acknowledgements: This lab was originally developed by Nathan Sprague.

Back to Top