Computer Science 252
Problem Solving with Java

Fall 2014, The College of Saint Rose

Lab 3: Flowers
Due: 11:59 PM, Tuesday, September 23, 2014

In this lab, you will gain experience working with a custom object by writing a program that grows and displays some flowers.

When you press the mouse on the canvas, a small plant is drawn. As you drag the mouse, the plant grows slowly. When it reaches its full height, a flower blooms. If you then click on that flower's petals before beginning to work on the next flower, its color will change.

You may work alone or with a partner on this lab.

Design Document

To get started, you will develop a written design for this program, no later than our class meeting on Thursday, September 18. Before you write anything down, read this entire lab description carefully. Then develop a design that includes:

Do not start coding before completing this design and having it approved by your instructor. It is often more productive to design a program away from the computer, and you should get into the habit of working in this way. If you arrive at the keyboard with a written idea of how to approach the problem, you will be able to make progress much more quickly than you would otherwise.

To help you get started, we have provided a starter BlueJ project with the class header and some method stubs for the Flower class and a complete version of a WindowController extension named Spring that will manipulate the flowers on the screen using the constructor and methods of the Flower class.

Problem Description

The canvas should initially start empty. When you click on the canvas, a flower should start growing with its base centered at that point. Initially it will be just a sprout, the height of which you may choose to be either a constant value or a percentage of the ultimate height of the flower. Its final height is determined by the second formal parameter to the constructor. As you drag the mouse, the stem should grow (upward, obviously). When it reaches its full height, the stem should stop growing and petals should appear. The color for the petals should be chosen randomly. The flower should not grow any more, but if you click on the petals after it has bloomed, the flower will change color. When you are happy with the flower's color, you can grow another one by clicking somewhere else in the window. When the mouse moves out of and then back into the window, the flowers should be removed from the canvas. Only the most-recently-created flower needs to respond to mouse clicks by changing its color.

If the mouse is released before the stem reaches its full height, the stem's growth stops and this plant will never produce a flower.

A working solution for this program will appear below. Click inside the applet to interact with it.

Your program will be divided into two classes: a window controller called Spring and a Flower class. A complete Spring class, which you should not modify, is provided in the starter folder. Your task is to implement the Flower class so that it works with our Spring. In particular, the Flower class needs to provide:

  1. A constructor that takes three parameters: the Location where the Flower's stem should be planted, a double specifying the maximum height of the Flower, and the canvas.
  2. A mutator method public void changeColor(): sets the color of the flower petals to a random color. This may be completely random, or you may restrict to colors that you think look nice.
  3. An accessor method public boolean flowerContains(Location point): returns true if the petals or center of the flower contains the point.
  4. A mutator method public void grow(): make the flower grow a bit if it has not already reached its full size and sprouted petals. Growing by about 1 for each mouse drag event is an appropriate rate of growth.

Your flowers should be at least as realistic as those in the demo solution. But feel free to use more graphics primitives in your Flower class to make the flowers look more attractive. Up to 3 bonus points are available for especially nice flowers.


Before 11:59 PM, Tuesday, September 23, 2014, submit your lab for grading. There are two things you need to do to complete the submission: (i) Upload a copy of your lab (a .7z or .zip file containing your project directory) using Submission Box under assignment "Lab3". (ii) Demonstrate the execution of your programs for your instructor. (2 business day grace period for demos).


This assignment is worth 50 points, which are distributed as follows:

> FeatureValueScore
Initial Design
Sufficient detail to convey the basic design of Flower 4
Includes picture of flower layout with dimensions 2
Style, Design, and Efficiency
Appropriate comments 3
Good variable names 2
Appropriate variable declarations 3
Good use of constants 4
Appropriate formatting 1
Does not generate new objects unnecessarily 2
Good overall design and methods 3
Initial stem drawn properly 4
Stem grows while mouse is dragged 7
Growth stops at specified height 4
Flower appears when height is achieved 7
Color changes when most recent petals clicked 4
Extra Credit
Nicer flowers 3
Total 50