You are on page 1of 9

CSC207H – Software Design

Fall 2009
Assignment 2
Due Date: Saturday 11:59pm Nov 14th

Introduction
This assignment is about Java GUI programming, program design and also class design. It is
meant as an exercise for you to consider how to design your packages, how should classes in
different packages communicate, how to design a good class, and also how to do all that in Java.

After reading the below description, don't start coding right away. Instead use a pen and paper,
and try to come up with the classes, the attributes and the methods in each class. This assignment
builds on A1 – so reuse parts of your A1 solution.

Spending two or three hours documenting the needed classes and the relationship between those
classes will enable you to 1) divide the work between team members, 2) produce better quality
design, and 3) break the problem into smaller manageable pieces.

Team Formation
You are going to do this assignments in teams of 4 (4 is ideal, 3 is acceptable, 2 is not – NO
EXCEPTIONS! If you have worked on your own in A1 or with a partner, there are other teams
with 2/3 members... Post a message on the discussion board asking to join a team. If you can’t
find one, contact the instructor ASAP).

CAUTION: Each team member will be handing in an individual form - in a closed envelope -
evaluating the performance of other team mates. If 2 or more team mates report that a team
member is not doing an effort in getting the assignment done, not attending meetings, etc... this
will result in loss of marks for that student.

Description
In this assignment you are going to write a GUI program which performs the same
functionalities of A1 – in addition to 2 new functionalities: verifying a floor layout and
displaying a floor layout.

Following are screen shots of the required GUI and functionalities:



Assuming your team-name is TorGirls, running your code should be through
java TorGirls.App

This should display a GUI application similar to the following:

Note that the above application has 3 menus File, Tools and Help. The big light gray area is a
Canvas (you are allowed to use a Panel or JPanel instead) on which a floor layout will be
displayed. The bottom white area is a textarea (you are allowed to use JTextArea instead) –
which you will use to display the outputs of your commands to the user.

In the file menu, you should have 3 menu items: Open, Close and Exit. Open will launch a
Filedialog so the user can load a text file with the commands (same input file approach as in A1).
Following is a screen shot of a typical Java Filedialog (you can use a JFileChooser instead):



In addition to the set of commands from A1, a new parameter which is the coordinates/location
of apartments and rooms. Following is the commands description (new parameters underlined):

add-building <name> <(Address)>


add-floors <building-name> <count>
add-apartment <building-name> <floor-number> <apartment-number> <x1><y1><x2><y2>
add-room <building-name> <floor-number>
<apartment-number> <room-type> <x1><y1><x2><y2>

<room-type> could be any one of the following: bedroom, livingroom, kitchen, washroom,

Once the objects are created, your program should output a message in the text area to the user:
File <file-path> loaded
where <file-path> is the full path to the file as selected by the user in the FileDialog.

Clicking Close will remove the objects from memory.

Clicking Exit will exit the application!



In the tools menu, there are 6 options/menu-items: the first 4 functionalities are from A1
(Display Building, List Missing Floors, List Missing apartments, List Missing Rooms). The
output of the first 4 should display in the bottom white textarea.

Verify Layout will display a dialog to the user asking him/her for the building name and floor
number (you will need to check for input validity or use a Choice control - aka dropdown or
JComboBox - to prevent the user from entering invalid input). Once the user presses the Enter
button (i.e. the dialog should have Enter and Cancel buttons), your program should check the
following
1) no 2 apartments over lap each other (i.e. if you plot them they won’t intersect!).
2) no 2 rooms over lap each other
3) no room is partially or completely outside it’s apartment

If the layout is valid, your program should display a message in the textarea informing the user
that the layout is valid.
Building <Y> Floor <X> has valid layout
where <X> is the floor number and <Y> is the building name

If the layout is invalid, your program should display a message in the textarea informing the user
that the layout is invalid;
Building <Y> Floor <X> has invalid layout
where <X> is the floor number and <Y> is the building name

Plot Floor will display a dialog to the user asking for the building name and floor number (you
will need to check for input validity or use a Choice control - aka dropdown or JComboBox - to
prevent the user from entering invalid input). Once the presses the Enter button (dialog should
have Enter and Cancel buttons), your program should display the layout of that floor according
to the coordinates specified in the commands file.



Clicking About will display a dialog to the user with your team name, application version, and an
OK button (to dismiss the dialog).

Program Design
Because this is your first ever application, guidelines are provided to help you in creating a better
design, you should make sure that you understand the reasoning behind the below choices and
don’t just implement them blindly!

1) Recall that a good characteristic of a program is that the constituent components


should be loosely coupled! (i.e. we can take one of them and reuse in another
application without being forced to drag along other unneeded components). How
many major components we have in this application? the GUI classes and the
Building hierarchy. To decrease the coupling between those two sets of classes, we
should create a package for each. Assuming your team name is tordudes, then create
one nested package for GUI (tordudes.gui) and another for the logic/class hierarchy –
call the second: tordudes.core So, your directory hierarchy should be
tordudes
App.java
gui
core

2) Now, the core has many classes; Building.java, Apartment.java, Floor.java etc… If
the GUI classes will access these classes directly, i.e. in the source code of a class
under the gui package, you will have a code like this:
package tordudes.gui;
……….
{
File file = new File( "C:\commands.txt" );
String strLine = readLine( file );
if( strLine.startsWith("add-building" );
{
Building building = new Building( blah, blah, blach, ….);
…..
}
}



Then, you have just increased the coupling between this GUI class and the core classes
because this GUI class knows now about the core classes. To separate the 2 packages, we
are going to add a Façade class to each package. Any class outside a package X who
wants to talk (query or modify) to a class in X should do that using the X’s façade. So,
your directory hierarchy and files should look like:
tordudes
App.java
gui
GUIFacade.java
….
core
CoreFacade.java
…..

Since we want to have one façade per nested package, the methods in the façade should
be static.

Visiting the above code again, it should look now as follows:


package tordudes.gui;
……….
{
…….
CoreFacade.buildObjects("C:\commands.txt");
…..

How the objects are built is not the GUI business, and so is what happens inside the core
package. All what the GUI should do is to say buildObjects and that’s it. The
construction/building of objects is done inside the core.

3) In order to decrease the coupling between classes, each class should contain all
methods that relates to what that class represents, and what it is used for and nothing
else. For example, if you consider the Plot Floor functionality; plotting a floor means
plotting the apartments in that floor and also plotting the rooms in each apartment. If
we put the code of drawing the corresponding images of floor and rooms in one
single paint method like this
package tordudes.gui;
public class T extends Canvas
{
public void paint( Graphics grx ){
Apartment apart;
…. /// somehow assign apart a proper object

grx.drawImage("C:\apartment.bmp",apart.getX(),apart.getY( ) );
}
}
Then the above code is coupled to Apartment.java class because if we modify
Apartment.java (e.g. change getX( ) to getXCoord( )), we will have to modify this code.
An alternative and better approach is make each class draw itself on the screen. Thus
Apartment.java will look like the following:



package tordudes.core;

public class Apartment


{
public void paint( Graphics grx ){
grx.drawImage("C:\apartment.bmp",X, Y );
}
}
And hence the above T class code will be:
package tordudes.gui;

public class T extends Canvas


{
public void paint( Graphics grx ){
Apartment apart;
…. /// somehow assign apart a proper object
apart.draw( grx );
}
}

4) If you look at the above piece of code, the apart.draw still creates coupling because it
means that class T knows about Apartment.java, so how can we achieve the same
functionality without mentioning Apartment in the code at all ?! The answer is to use
an interface, like this:
package tordudes.gui;

public interface Drawable{


public void paint( Graphics grx );

}
-----------------------------------------------------------
package tordudes.core;

import tordudes.gui.Drawable;

public class Apartment implements Drawable{


int X,Y;
….
public void paint( Graphics grx ){
grx.drawImage("C:\apartment.bmp",X, Y );
}
}
-----------------------------------------------------------
package tordudes.gui;

public class T extends Canvas


{
public void paint( Graphics grx ){
Drawable drawable;
drawable = CoreFacade.getDrawable( someid );
drawable.draw( grx );
}
}

Now class T knows nothing about Apartment.java, all what it knows is that CoreFacade
will return an object that implements the contract (aka interface) Drawable, so it can call
it’s draw method.



Notes and Tips
 You should start by reading lecture 5 notes which cover Java GUI programming and the
MyPaint.java example covered in lecture 6.
 When defining the coordinates of a room or apartment, remember that Java coordinate
system starts from top left corner as follows:

 GUI tutorials:
Menus: http://java.sun.com/docs/books/tutorial/uiswing/components/menu.html
Dialog:http://java.sun.com/docs/books/tutorial/uiswing/components/dialog.html
TextArea: http://java.sun.com/docs/books/tutorial/uiswing/components/textarea.html
FileChooser: http://java.sun.com/docs/books/tutorial/uiswing/components/filechooser.html
BorderLayout: http://java.sun.com/docs/books/tutorial/uiswing/layout/using.html
 The Tools menu items should be disabled if the user did not load a file yet or has closed
the file (using File -> Close).
 A set of images are provided to you in the Assignments folder on the portal for use with
the different room types. Do not worry about orientation of the rooms or door location!
 Here is a sample code to read an image from the hard disk and load it into memory for
display on screen;

import java.awt.*;
import javax.swing.*;
import javax.imageio.*;
import java.awt.image.*;

public class ImageApp {


public void loadAndDisplayImage(JFrame frame) {
// Load the img
BufferedImage loadImg = ImageUtil.loadImage("C:/building.bmp");
frame.setBounds(0, 0, loadImg.getWidth(), loadImg.getHeight());
// Set the panel visible and add it to the frame
frame.setVisible(true);
// Get the surfaces Graphics object
Graphics2D g = (Graphics2D)frame.getRootPane().getGraphics();
// Now draw the image
g.drawImage(loadImg, null, 0, 0);
}
public static void main(String[] args) {
ImageApp ia = new ImageApp();
JFrame frame = new JFrame("Demo");
ia.loadAndDisplayImage(frame);
}
}



 The values for parameters <x1><y1><x2><y2> should conform to the Java coordinate
system (and the units is in pixels, not feet nor meters!).
 The input commands file can contain instructions for more than 1 building.
 The program should immediately stop loading the file and (delete any created objects) if
there is a duplication in create-add commands - for example, adding a building with the
same name and address twice, adding the same floor twice to a building, etc.. Also, if
there is a typo in the commands or missing parameters, the program should also stop
loading the file.
 Check the sample input/output file for A1 if you have doubts about what is an acceptable
input.
 As mentioned in class, there are two Java GUI libraries available for you to use; AWT
and Swing. Although you are free to use either of them, it is recommended to use Swing.
However, make sure you don’t mix user interface controls from both libraries since they
don’t work together. Swing Classes are all in the package javax.swing and they all start
with a J !
 Since the user interface only has 2 components canvas and textarea, it is recommended to
use Border Layout.
 The class that will contain the main method must be named App.java. You should create
a top package with your team-name, for example if your team-name is TorGirls, running
your code should be through; java TorGirls.App

Submission
We are going to do an update from your repo to retrieve the assignment submission (any file
version after Nov 14th 11:59pm will be ignored). Do not commit .class files or any IDE (eclipse)
project files. Only commit the .java files

