Back to the faculty page for James Tam |
Due at 4 PM. For assignment due dates see the main grid on the course webpage.
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 four classes for file input and output.
You need to write two programs for this assignment: a simple shopping program and a program that error checks user input using recursion.
Note: it is not sufficient to just implement a working program and expect full credit. Even if your program is fully working and the design is not as specified in this document (E.g. your program implements static methods other than the main method or only a single class is used) then your marks will suffer greatly. (Your raw grade point is quartered if you do these two things i.e. an 'A' becomes a 'D' grade). There are other crucial design requirements but I used two extreme examples to make a point. You are to learn and practice good Object-Oriented principles in CPSC 233 and you are expected to be able to apply these principles in higher level courses. Note: the prohibition on the use of static methods does not necessarily apply to static class constants (e.g. public static final double PI from class Math is an example of good style, the appropriate use of class constants.) These restrictions apply on to the code that you write and not prewritten code that you may or must for some assignments as they may represent an exception to these rules.
There is no starting code for this part of the assignment.
Program 1: Functionality of the login dialog
When the program is first run a login dialog will be presented to the user (use a JPasswordField to store the input). The password to login to the program will be stored in an 'encrypted' format in a file called "password.txt". The contents of this file consists of the string "password" after the Caesar cipher has shifted each character backward one place in the alphabet ('s' becomes 'r', 'p' becomes 'o' and 'a' wraps around to 'z'). The encrypted version thus becomes "ozrrvnqc". Your program must be able to use any arbitrary password file. However you can assume that the file will only consist of lower case alphabetic characters. [The following tutorial example] which 'scrambles' character information may be helpful when you implement your encryption feature.
Your program must read the encrypted password and convert it to the original form ("ozrrvnqc" read from file becomes "password"). It will then compare the unencrypted string vs. the current value entered in the password field when either: (1) the user hits the login button or (2) the cursor focus is in the password field (as shown in Figure 1A) and the user hits enter. If there is no match then the program should display a suitable error message as well as the current number of attempts and the maximum number allowed. If three unsuccessful attempts have been made then a suitable error message should be provided (e.g., Figure 1B for an example) and the program will end. Note that the program will need to 'pause' so the user actually has time to see the error message before it shuts down. (Two methods of pausing a program have been demonstrated in the 'GUI' lecture notes). [Link to video showing 3 unsuccessful logins]
The container for the login dialog need not be a JFrame, indeed in such a case something like a JDialog is probably more appropriate. (If you use the latter control then you may need to end the program with the call to the 'exit()' method via System.exit(<int>). Clicking on the close window of the login dialog (or the main shopping window (shown in Figure 2) will also end the program although without an error message).
Figure 1A: Login dialog when incorrect credentials entered Figure 1B: Max attempts reached If the password has been correctly entered then the login window is banished and the main shopping screen comes up. You have some flexibility in the exact content and layout, an example is shown in Figure 2.
Figure 2: Main shopping screen Program 1: Functionality of the 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 specify what is to be entered into the input fields, two JButtons to handle most of the user interaction. The full version of the program will employ the GridBagLayout (and the GridBagConstraints) for the main shopping window (it's optional for the login dialog). 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 it 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 (password 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). If there are problems then your grade will be affected.
Save button: When pressed this button will take the current information in the name and address and save it to a plain text file (unencrypted) called 'order.txt' which should be saved 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 file containing the order information (the example shown in Figure 2 will create a order file with 4 lines: 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'. It's the result of saving the order shown in Figure 2. The program does not have to read this information from the file. 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). Again you can refer to the approaches for pausing a program which were taught in 'GUIs' lecture.
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 (unlike the password field).
[Link to video showing a correct login & the shopping program in operation]
Modify the [starting program] so that it employs a simple recursive implementation to error check user input. (No credit will be awarded if the error checking uses iteration/looping rather than recursion). To simplify your solution Part 2 of the assignment *can* be completed by the use of a static method. Write the body of the promptForAge() method that will recursively prompt the user to enter an age so long as the value entered is outside the valid range of 1 - 144. After getting a valid age the program will display in main the value entered by the user. This means that the final value returned by this method must be within the valid range. [Example execution]
Submission received:
On time
Hours late : >0 and <=24
Hours late: >24 and <=48
Hours late: >48 and <=72
Hours late: >72 and <=96
Hours late: >96
Penalty:
None
-1 GPA
-2 GPA
-3 GPA
-4 GPA
Submission not graded.