Com S 362 --- Object-Oriented Analysis and Design HOMEWORK 4: IMPLEMENTING GUIS IN JAVA (File $Date: 2003/03/09 16:08:41 $) Due: February 24, 2003, at 11AM in class. The purpose of this homework is to have you learn how to implement graphical user interfaces in Java. User interface code also gives excellent examples of the uses of inheritance and polymorphism. This is an individual homework, and is not to be done in teams. READINGS Read Kathy Walrath and Mary Campione's book "The JFC Swing Tutorial: A Guide to Constructing GUIs" (Addison-Wesley, Reading, MA, 1999). This is available online from: http://java.sun.com/docs/books/tutorial/uiswing/index.html See also the javadocs for the packages javax.swing, java.awt, and java.awt.event. Also recommended are the chapters on Swing UIs in the book by Core Java 2 Volume I -- Fundamentals by Cay S. Horstmann and Gary Cornell (Sun Microsystems Press, Palo Alto, CA, 2001). 1. (150 points) [Calculator graphical UI] The user interface for the calculator example presented in class is intended to look something like the following... |-------------------------------------| | Formula-Register Calculator | |-------------------------------------| | [ 0.0 ] | |-------------------------------------| | Named Formulas | | | | [ add0 ][\/] [ r1 + r2] (Eval) | | | |-------------------------------------| | Registers | | | | r0 [ 0.0] r1 [ 0.1] | | r2 [ 2.2] r3 [ 0.3] | | r4 [ 0.4] r5 [ 0.5] | | r6 [ 60.6] r7 [ 0.7] | | r8 [ 0.8] r9 [ 0.9] | | | |-------------------------------------| | Formula Composition | | | | First Second | | Register Operator Register | | [ r0][\/] [ +][\/] [ r1][\/] | | | | Name [ ] ( Save ) | | ( Reset ) | |-------------------------------------| where (Eval) is a button, [...][\/] is an item choice, and [ ] is a text display or text box. The rest are labels. The horizontal lines in between the sections are just for illustrative purposes and don't need to appear in the actual calculator. Start by copying our code for the calculator. This can be done as follows (on the department linux machines): cd mkdir Calculator cd Calculator cp -r /home/course/cs362/public/homework/hw4/Calculator/* . If you import this into Eclipse, import all three packages underneath the Calculator directory. The code for the calculator example is found in three packages. The "calc" package has the application logic for the calculator. The "tests" package holds the JUnit tests for the application logic. You shouldn't have to worry too much about the "calc" package, and you don't need to use the "tests" package at all, unless you change something in the "calc" package. Where you will do your work is in the "gui" package. The "gui" package has 3 classes currently. (you may need to add more classes or interfaces.) The class gui.Calculator has the main method in it; thus you can start the calculator by running gui.Calculator as an application in Eclipse, or by executing: java gui.Calculator The gui.CalculatorFrame class is where the main graphical user interface code is. This is the class you will mostly need to change. The class gui.RegisterInputListener is an example of a listener. It extends the classes FocusListener and ActionListener, and its instances are stored in the frame. When focus is lost or when the user types the return (enter) key on the keyboard when the textbox has focus, then this a method of this listener is invoked, which parses the number in the associated JTextField, and stores the number in the appropriate register. Your task is to (a) understand the user interface code in the gui package and (b) flesh it out so that its other designed functions work as a useful interface for the underlying calculator code. To do this you will have to add item choices for the "Named Formulas" section, and the labels, item choices, and buttons in the "Formula Composition" section. You only have to deal with formulas that are of types Sum, Diff, or Mult (don't worry about Sum3 or Negate). To be able to test, however, you should start working on the "Formula Composition" section. Make item choices so that the user can select one of the 10 registers for the first register argument, and an operator, and then a second register. They should be able to enter the name for the formula in the text box, and then press the save button to have the formula be remembered, and available as a name that can be selected in the "Named Formulas" section. At any point they can press the reset button to undo all the unsaved work in this section. When the "Formula Composition" section works, move on to the in the "Named Formulas" section. Here the user should be able to first select a named formula from an item list, and then press the "Eval" button; which should show the result of the evaluation in the text box at the top. Consult the user-interface use cases in /home/course/cs362/public/homework/hw4/ui-use-cases.txt for more details on how the user interface is supposed to work. This description of the user interface is (intentionally) somewhat vague. You are responsible for making appropriate decisions that you need to make to implement it. (You can, of course, ask us what to do if you get stuck or if you can't see any reasonable alternative of how to do it.) You can change the layout from the current layout a bit to make it look nicer if you wish, but it should be basically similar. You are to do the coding by hand. This is a bit unrealistic for user interfaces, which are often laid out by automatic tools. However, we want you to: (a) understand what is going on under the covers in a Swing user interface, and (b) have the knowledge needed to understand and tweak or tune code generated by an automatic user interface tool. HINTS Use Eclipse. Read the books and other information about Java as needed. In Eclipse it's extraordinarily helpful to be able to see the javadocs displayed when your mouse hovers over a class or method. Also if you press control-space after typing the dot (.) in a method call, you'll get a list of methods, and you can see the javadocs for the ones you select. This makes browsing for the right method easier than hunting through the documentation via a separate browser window, although that is more handy for getting an overview of a type or more details than what Eclipse shows. You might want to use these hints first to read the code we have given you, to get familiar with the tools. Fussing with code like this takes a significant amount of time, much of which you'll spend learning how to do things. Don't start at the last minute. TESTING Your code should compile, and work sensibly under Java 1.4.1. (You can use Java 1.3.1, which you may have to use if you have a Mac. However, note that our code was developed under 1.4.1, and may use some features not found in 1.3.1.) You should do testing yourself, by playing systematically with the user interface. WHAT TO HAND IN Hand in a printout of all of the code you wrote. By doing this you certify that your code does what it is supposed to do. Also email a jar file containing your code to our TA, Tongjie Chen . This jar file will be used to see what your interface looks like and to make some rudimentary checks that it works. If your code does not work correctly you can still hand it in for partial credit, but attach an explanation of what about it does not work. (In that case you may want to still send us a jar file, but it's not necessary.) You can make a jar file from Eclipse as follows. In the Java perspective, click on the File menu (top left), and from the File menu select the Export menu item. Then follow the prompts. If you're not using Eclipse, then use the "jar" program that comes with the J2SDK. For example, do jar -cvf myHw4.jar gui/ calc/ GRADING You will be graded, in part, on how clear your code is. Code generated by user interface tools is not likely to be clear, and will be severely penalized. Excessively long code will be penalized: don't repeat code in multiple places, don't write the same expression in multiple places. Be sure your code has javadoc comments for all fields and methods that you add. Your code should be sensibly indented so it is easy to read (Eclipse can do this for you automatically.) Make all of the fields in the classes you write private. Be sure your name is in the comments in your code.