CSE 115 - Spring 2008 - Banner
   CSE 115 - Spring 2008 - Introduction to Computer Science for Majors I
CSE 115 - Spring 2008 - Navigation CSE 115 - Spring 2008 - Lab 4

Lab 5

Introduction

The training wheels are off (partially) and in this lab you will be creating a graphical program from scratch. You will use the bouncing ball again and use buttons to manipulate its properties, but this time, the user can select to create a new ball and the buttons will then stop controlling the old ball and start controlling the new one.


Objectives

The high-level objectives of this lab are to have you,
  • create a graphical program from scratch
  • add containers, a drawing canvas and a button onto your window
  • create classes that implment the ActionListener interface so that your buttons will react to events
  • implement and use your first design pattern
The following are the new concepts covered in this lab:
  • composition
  • implementing an interface
  • creating graphical containers
  • proxy design pattern

Assignment Specifications

Your program must look like the screen shot below when you are finished. The functionality of the components is explained below, but the first goal of the assignment is to create a window that looks like the one presented here (with the changes described below):

Your buttons should indicate what properties of the ball you will be modifying. Note that there is no ball bouncing around when the program begins. This is an important feature of your finished assignment.

The functionality of the program should be that when the user clicks the new ball button, a new ball should appear and begin bouncing around the screen. The two buttons on the bottom of the screen should control one property of the ball (color, size, or movement using vector - starting and stopping the ball is not allowed for this assignment). When the user clicks on one of the buttons, the ball should take on the change in properties. When the user selects to create a new ball, the new ball should appear and subsequent clicks on the property buttons should change the new ball (and the new ball only).


Helpful Hints

Read through the entire lab before you start working, so that you know what to expect. Make sure you save your work often, and keep track of what you are expected to submit.

Do not be afraid to refer to earlier labs to recall what things mean or what commands are available for you to use.


Reading

Make sure you have read chapter 5 of the book before coming to lab. Also make sure you have reviewed your lecture notes.


Lab set-up tasks

At your lab session your teaching assistant will briefly discuss how to carry out each of the set-up tasks below. She or he will also be available to answer questions you might have.

Check out the Lab5 Skeleton from the repository. If you don't remember how to do this, refer to earlier lab assignments.


Take-Home Lab Assignment (To be turned in next week)

You are to complete this assignment and turn it in at the beginning of your lab section next week. The completion of this assignment will be included in your Lab 5 grade. You need to print out a copy of the class definition found here and circle and identify using the number from the list below, each of the following elements from that code (one example ONLY).

  1. package declaration
  2. class definition begin
  3. class definition end
  4. class header
  5. name of class
  6. access control modifier
  7. constructor definition
  8. parameter list
  9. method call
  10. code that creates a new instance of an object
  11. local variable name
  12. assignment statement
  13. non-constructor method definition
  14. instance variable declaration
  15. interface name

Lab Attendance

In order to receive credit for attendance for this lab, you must show your TA your program running before you leave the lab. At minimum, you should have a window appearing and some of the graphical components visible before you leave. If you complete the assignment and show the entire working project, that is fine, but you only need to show how far you got in lab today to receive credit.


Lab Assignment (Electronic portion to be turned in by due date listed below)

You will first need to create a package in your project to begin your work. This package should be named lab5. We created a package for our project in Lab 2, so please refer back to thos instructions if you do not remember how to create a package. Inside this package, you should create the classes that you need to finish the assignment.

You should begin by thinking about how to create the look of the window that appears on the screen. What combination of rows and columns are needed to make the window appear as in the picture above. The drawing canvas should have the dimension (500, 500) and be white. The background of the window will be black.

You should create a top level object that will once again be in charge of putting all the pieces of this lab together. This object should create the graphical components and place them properly as well as setting up the action listeners for the buttons. As a good design habit, it makes sense to push off the work of organizing the look and feel of a frame to another method. That is, it should not be done in the constructor of the top-level object. Therefore, you should create a method called init that is called from the constructor of your top level object to set up the graphical components.

Once the graphics are set, you should begin by creating an action listener for the button that will create new instances of the bouncing ball. This button, when clicked, should create an instance of the bouncing ball and place it on your drawing canvas. However, the listener can not have direct access to the drawing canvas and thus will need to talk to your top-level object through a method to put the new ball in its proper place. Thus, the listener will need to "know" about your top level object in order to make this happen. A method will be needed in the top level object to finish this job.

Lastly, you must complete the action listeners for the buttons to manipulate the current ball. Note that in the last lab, you could simply pass a reference to the ball to each of the listeners so that they could control the ball. This time, however, the ball that they will control may change, so the listeners can not talk to a specific ball, but instead need to talk to the last ball that was created. What we need to accomplish this is an object to keep track of the current ball so that the buttons can talk to it and not have to worry about talking to the ball. It would be even nicer if that object implemented the same interface as the ball so that the listeners would know that the object could respond to the same methods as the ball.

Therefore, you need an object that implements the lab3lib.IBounceChanger interface and holds a reference as an instance variable to a lab3lib.BouncingBall. This object will also have a mutator that when called changes which ball is the current ball. A good name for this object would be Proxy because it implements the Proxy design pattern.

Design patterns

The proxy design pattern is just one example of a design pattern. Design patterns are "best practices" solutions to recurring software development problems. Design patterns are mined from existing code, written by software developers, and therefore represent examples of high-quality designs that practicing software developers actually use.

The idea of design patterns actually come from the field of architecture. The architect Christopher Alexander 's work on patterns in architecture inspired the so-called "Gang of Four" , Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides ( who sadly passed away recently ), to catalog a set of design patterns in a book, called Design Patterns: Elements of Reusable Object-Oriented Software . This book catalogs several design patterns; since then (it was published in 1995) many design patterns have been catalogued.

In this course we introduce you to several very useful design patterns. This lab, you will look at the Proxy Pattern. Throughout the rest of the semester you will be introduced to a selection of fundamental patterns which will help you understand the design of pieces of the Java class libraries (such as the graphical library). The proxy pattern is summarized by the UML diagram below:

The important pieces of the pattern are that there is a client that communicates with the Proxy. The proxy implements the same interface as the subjects it is standing in for and the client does not actually know it's not talking to the subjects. The TAs will discuss this more during lab this week.


What you hand in

When you are finished, you need to export your solution from the Eclipse environment so that you can submit it.  It should be named Lab5.jar and contain a package named lab5, all your Java source files and a UML diagram named Lab5.grn. Submit the file using the electronic submission command.


Due dates

Due dates are summarized in the table below.

To check that your lab was submitted, you can always refer back to the Submit Inspector on the Resources page of the website. After you have entered your user name, your submissions will be shown.

Date of lab Due date for electronic submission
Tuesday, February 19 Monday, February 25
Wednesday, February 20 Tuesday, February 26
Thursday, February 21 Wednesday, February 27
Friday, February 22 Thursday, February 28

 

CSE 115 - Spring 2008 - Footer

 

 
Page Maintained by: Adrienne Decker