Hi There!

I'm Dan Schlegel, an Associate Professor in the Computer Science Department at SUNY Oswego

Programming Challenge 6: The Colorful Garden

In this assignment you will model two classes – flowers and gardens, allowing you to practice your class modeling, good programming techniques, and your use of constructs like ArrayLists.

Why do it?

This assignment glues together nearly everything we’ve done this semester, and will provide excellent practice for the final exam!

The Flower Class

  1. Create a new Java class called Flower in the garden package.
  2. Create the instance variables and constructor for Flower based on the fact that flowers have a name, a size, and a number of petals. Think about what types make sense to use for those. You can add additional constraints (ours only works for petal multiples of 4). Remember that the purpose of a constructor is to initialize the instance variables.
  3. Add a toString method to your Flower class which returns a string with the flower’s name, size, and number of petals formatted in a nice way.
  4. Remember that classes are developed, not written, so create a Java main class file to test your Flower class. We’ll be adding some painting mechanisms to the Flower class in a minute, so you might want to use the skeleton at the bottom of this page.
  5. We now wish to paint our flowers. Add another instance variable to your Flower class for the color used, and initialize it with a random color at the appropriate point in your code.
  6. Add a method called paint to your Flower class which takes as an argument an SPainter. In this method, paint your flower. Make use of (at least) the defined size and number if petals. You may use circles and squares for drawing your flowers. Use your imagination – yours doesn’t have to look like ours! Be sure to use good practices as far as problem decomposition and invariance.
  7. Test your Flower class by enhancing your tests from before. You’ll need to make an SPainter to pass to the Flower‘s paint method. Iterate on your design until you are happy with it.

The Garden Class

  1. Create a class called Garden in your garden package.
  2. Your class should have a single instance variable – an ArrayList of flowers.
  3. Your constructor will take one argument, the number of flowers in the garden. The goal of your constructor will be to initialize the ArrayList instance variable as a new empty list, and to populate it with flowers according to user input.
  4. Use the techniques we’ve used before this semester to create popup windows to ask the user for the information required to make the flowers, then add them to the ArrayList. Be sure you make as many as specified by the argument to the constructor!
  5. Add a toString method which returns a string containing the details of each flower in the garden. You should make use of the Flower‘s toString method.
  6. As before, create a tester class to make sure your class is working as it should.
  7. Add a method, called paint, to the Garden class. It will take one argument, an SPainter. This method should make calls to the paint method in each of the flowers stored in the ArrayList. Paint each flower side-by-side.
  8. Test your Garden class by enhancing your tests from before. You’ll need to make an SPainter to pass to the Garden‘s paint method. If it doesn’t work as you expect, go back and fix it!

The Testing Skeleton

The Exit

  1. The assignment is due May 2nd.
  2. The last day to demo will be May 5th, the last day of classes. It must be demoed on or before May 5th to receive any credit.
  3. You must post your work to you work site and demo to a TA to receive credit.
  4. Reflect on this assignment, your solution, and their relationships with the course content. Think on your engagement with the assignment and the course.