Course web page: Introduction to Computer Science for majors II James Tam Return to the course web page

CPSC 219: Full Assignment 5

Due Wed April 12 at 7 PM

Text only connections such as Putty won't allow you to run programs that employ graphical user interfaces. If you have been working at home to complete assignments but haven't installed JDK on your computer then you might want to either install a free file transfer program such as Filezilla (server name:, port: 22) or perhaps a cloud based storage system such as To run your programs you can run 'Java' locally using JRE (unlike the JDK which includes the compiler you don't have to worry about setting the 'path'). Open up a command line on your home computer 'cmd' and use it to navigate to where you have your GUI code on your CPSC account. (. When you type 'java Driver' (or whatever the name of starting class is) your GUI will be run locally on your computer.

New concepts to be applied for this assignment:


Implement a simple graphical user interface to simulate a shopping program. You have some flexibility in the exact content and layout, an example is shown in Figure 1.

Figure 1: Main shopping screen

At a minimum this window should consist of the following: a JFrame container, a JTextField and JTextArea for the name and address input areas (respectively), Jlabels to describe the input fields, two JButtons to handle most of the user interaction. The full version of the program will employ the GridBagLayout (and GridBagConstraints) for the main shopping window. Regardless of whether a layout 'manager class' is used the layout should be organized and logical so the user can quickly understand the interface at a glance. If you desire you can use other sub-containers with other layout manager classes. ImageIcons will be used to adorn the buttons (your choice of image but the visuals should complement the text descriptions).  Finally you should include another image somewhere in the container with an appropriate company logo ("TAMCO Enterprises Inc." in the example) contained with a Java GUI Component (a JLabel is a good choice). There should be an appropriate descriptive title in the window. If you use external data - something that you didn't create yourself - unless it's largely in the public domain (e.g., clipart that is made universally available for free) make sure that you cite the source. The program should be written so that the data files (order file and images) will reside in the same directory as your program (so your marker doesn't have to waste time determining the structure assumed...those few minutes adds up over the course of marking all the assignments in a tutorial).

How the interactive widgets work

  1. Save button: When pressed this button will take the current information in the name and address and save it to a file called 'order.txt'  in the same directory as your program. The name information should sit by itself on the first line of the file while the second line (and successive lines - if appropriate) will consist of the address. Each line of the address from the JTextArea will reside on a separate line in the order file (the example shown in Figure 1 will create a order file with 4 files: 1 for the name and 3 for the address). If you are still unsure then you should look at the sample order file 'order.txt' in the A5 directory. It's the result of saving the order shown in Figure 1. The program does not have to read this information from the file. (You only have implement file output not input). If the file already exists then the previous contents can be overwritten. During the save process the title bar of the main window should provide an appropriate status message (and the program should temporarily pause itself to provide the user with enough time to read it).

  2. Clear button: When pressed the two input fields (name and address should be cleared of text).

As previously indicated clicking on the close window control will just shut the application down. Because the address field can consist of multiple lines (i.e., the user can hit enter to separate lines) hitting enter in the name field should have no effect.

Data files

In the assignment directory (/home/219/assignments/assignment5/data) there will be sample images that you can use in your program, a sample 'order.txt' file. You may also find in this directory a short video illustrating how my sample solution works. Keep in mind that this year's version of the assignment has been reduced slightly in size so the video includes additional features such as" a login dialog and password encryption that you don't have implement. As well the sound effects that are triggered when the mouse 'enters' the label aren't necessary either.

Video (Windows Media player format): AGAIN: it contains a few extra features (such as sound) over and above what you need for this years version. If you have trouble viewing the video you can find versions (mp4 and WMV) in the UNIX file system under: /home/219/assignments/assignment5/data/
Video (MP4 format): . AGAIN: it contains a few extra features (such as sound) over and above what you need for this years version. If you have trouble viewing the video you can find versions (mp4 and WMV) in the UNIX file system under: /home/219/assignments/assignment5/data/

Using pre-written Java code

Unlike the other assignments you will make extensive use of the pre-created Java libraries developed by Sun/Oracle for graphical components (e.g., java.awt., javax.swing), event handling (e.g., java.awt.event) and the two classes for file output.


Points to keep in mind:

  1. Due time: All assignments are due at 4 PM on the due dates listed on the course web page.  Late assignments or components of assignments will not be accepted for marking without approval for an extension beforehand. The latest versions of the files that you have submitted in D2L as of the due date is what will be marked.
  2. Extensions may be granted for reasonable cases by the course instructor with the receipt of the appropriate documentation (e.g., a doctor's note). Typical examples of reasonable cases for an extension include: illness or a death in the family. Cases where extensions will not be granted include situations that are typical of student life: having multiple due dates, work commitments etc. Tutorial instructors (TA's) will not be able to provide extension on their own and must receive permission from the course instructor first. (Note: Forgetting to hand your assignment or a component of your assignment in does not constitute a sufficient reason for handing your assignment late).
  3. Method of submission: You are to submit your assignment using D2L [help link]. Make sure that you [check the contents of your submitted files] (e.g., is the file okay or was it corrupted, is it the correct version etc.). It's your responsibility to do this! (Make sure that you submit your assignment with enough time before it comes due for you to do a check).
  4. Identifying information: All assignments should include contact information (full name and student ID number) at the very top of your program in the class where the 'main()' method resides. (Note other documentation is also required for most assignments).
  5. Collaboration: Assignments must reflect individual work; group work is not allowed in this class nor can you copy the work of others.  For more detailed information as to what constitutes academic misconduct (i.e., cheating) for this course please read the following [link].
  6. Execution: programs must run on the computer science network running Java 8.x. If you write you code in the lab and work remotely using a remote login program such as Putty or SSH then should already be using the correct version. If you choose to install Java on your own computer, then it is your responsibility to ensure that your program will run properly on the CPSC Linux computers. It's not recommended that you use an IDE for writing your programs but if you use one then make sure that you submit your program in the form of text ".java" file or files. If you only submit your byte code files (e.g. Driver.class) then you will not be awarded any credit.
  7. Use of pre-created Java libraries: unless otherwise told you are to write the code yourself and not use any pre-created functions from the Java libraries. For this assignment the usual acceptable functions include: System.out.print(), System.out.println(), the methods of the Console class and for some assignments the methods of the Random class. Look at the particular assignment description for a list of other classes that you are allowed to use and still get credit in an assignment submission.
  8. Style conventions, programming decomposition: the marking points from the previous assignment also apply to this assignment. The one blanket exception is the use of a static debugging flag (or flags) if you choose to implement multiple flags.