Table of Contents

This is not a current assignment. If you are currently enrolled in SE1010, do not do this assignment expecting to receive credit for it.

SE-1010 Software Development 1

Lab 2: Adding structure to a Java program

Objectives

Assignment

In Lab 1, you created a working Java program with a main class with a main() method. Within this main() method, you inserted a number of Java instructions that created three objects of the WinPlotter class and directed each WinPlotter object (class instance) to perform a specific task. In fact, your main method contains quite a few instructions, and it's beginning to get a little long - meaning that it's probably doing too many different things. This violates one of the important principles of Object Oriented Programming; that is: “Each method of a class should do one thing well”. Therefore, you're going to restructure your main class into smaller methods so that the code is a little more manageable.

Your current assignment is to modify your Java application so that your main class consists of additional static methods besides main called drawRectangle, drawTriangle, and printName, where each method performs a specific task. Rather than doing all the WinPlotter object creation and drawing within the main method, these additional methods will be responsible for creating WinPlotter objects and using the created objects to draw their respective images. Thus, each additional method will perform a subtask, and the main method will “manage” these subtasks by calling these additional “worker” methods. The first worker method will draw a triangle, the second will draw a rectangle, and the third will display your name.

One slight wrinkle: Each worker method must implement two integer formal parameters that represent the size of the window they are to create. The main method must supply the size arguments when it calls each worker method. Another wrinkle: each worker method must display the current time (in “HH:mm:ss SSS” format) in the lower left hand corner of its window by calling a fourth worker method named displayDate. When called this method must be passed the reference to the WinPlotter object to draw on, as well as the pen colors used in the window, such that the displayed date appears as a different color than the other elements within that window. Within displayDate, you'll have to create and use a Date object and a SimpleDateFormat object to generate the correct results. Review section 2.4.3 in your textbook for details.

Before your main method starts calling the worker methods, have it display a JOptionPane-based dialog containing the message ”<your name>'s Lab 2 program.”. Before writing your program, study the UML class and sequence diagrams below that illustrates the additional methods, method parameters, and method return datatype and sequence of method calls. You must adhere to these UML diagrams when creating the methods so that they have the specified names and parameters. Follow the sequence diagram's order in which the method calls are made.

Within Eclipse, create a new Java project called “Lab 2”, with a package called ”<yourMSOEusername>”, and with a main class that contains a main method. Add the four additional methods to this class.

You should use logical-sounding identifiers for any variables you use within your worker methods.

Put comments inside your Java program so that I know that you understand what you're doing.

Test your program and make certain that it works correctly.

lab2_class.jpg

lab2.jpg