CS 261 Lab B - Graphical Calculator
Due Wed Jan 30 at 9:00am
Overview
In this lab you will graphic a Graphical User Interface (GUI) for a simple 4-function calculator like the one you made in the previous lab.
This lab will be completed in pairs. Review the pair programming guidelines if you don't remember them!
Objectives
- To review GUI and event-driven programming techniques
- To practice reading and searching Java documentation
- To practice integrating a simple ADT within a larger program
- To continue practicing with Eclipse
Necessary Files
You will need to download the copy of the zipped
source files.
and import them into Eclipse (see previous lab for instructions). These files contain a Calculator
class for you to use, as well as an example ButtonFrame
that demonstrates the use of JButtons, JFrames, and ActionListeners. DO NOT MODIFY THE Calculator
CLASS!
Details
- I have provided you a simple ADT for a Calculator (which should closely resemble the one you created last lab). Download this an import it into Eclipse.
-
Your task is to create a new class,
CalculatorGUI
, that provides a graphical front-end to this Calculator class, such as in the screenshot below: There are a number of aspects you'll need to consider, which are described in more detail. Try to complete one step at a time, and be sure to switch drivers after each step! -
In order to run and test your program, make a
CalculatorTester
class with a main method that can instantiate your CalculatorGUI class. -
First you'll need to construct the window and the buttons. You can use code from the
ButtonFrame
class to get you started. I recommend using a GridLayout to arrange the buttons- Note that you can use a different arrangement than in the above picture; making a button span multiple rows or columns is non-trivial--only try it after you've completed the rest of the lab.
- You can use a JTextField to display the result. Look through the documentation for ways you can specify whether the box is editable and its alignment
-
Add functionality so that when the user presses one of the number buttons, that number is appended to the displayed value. So if the user presses '2' and then '6', they should see '26' on the display.
- You might consider making an
addDigit()
helper method that modifies the text being displayed, and then have pressing each button call that method with an appropriate argument. - Remember that you'll need to add an ActionListener to each button so that the buttons can react!
- Optional: get rid of any leading 0s the user may press!
- You might consider making an
- Add functionality so that when the user pressed the decimal button, a decimal point is added to the display. Remember that a number can only have a single decimal point!
-
Add functionality so that when the user hits one of the operation buttons ('+', '/', etc.), the interface remembers that operations and allows the user to start entering a new number.
- You'll likely want some kind of variable to keep track of whether the user is starting a new number--if you're starting a new number, you don't want to append a new digit but overwrite what was previously there!
- And of course, you might want to remember the previous value they put in. Should this value be stored in the Calculator class or the CalculatorGUI class? What is the appropriate way to structure this information?
- Consider using an
enum
or some CONSTANTS when storing the operation choosen to make your code more readable.
- Finally, add functionality so that when the user hits the equals ('=') button, the most recently selected operation and entered number are applied to the value currently stored in the Calculator. So if I had stored a 161 in the calculator, and the hit '+' and then '1' '0' '0', I should get 261 as a result (which should then be stored in my Calculator for future operations, as well as displayed on the GUI). This should give you a basic working calculator. In other words, I should be able to put in another operation and another number and continue manipulating my previous result.
- Be sure to include in-line comments explaining your code. Also include a comment describing each method you write (though you will not be marked down for not including full javadoc tags).
- Check and double-check that your program works flawlessly before submitting it to the submission folder.
- Fill out the lab partner evaluation survey after you turn in your work!
Extensions
No extra credit for these extensions, but they are interesting exercises in organizing your program. Be sure to list any extra features you include in the class comment of your CalculatorGUI.- Make it so that the user can simply hit an operation without hitting 'equals' in order to continue using the calculator.
- Make it so that successively hitting 'equals' will apply the previous operation. So if I type in '3' '+' '3' and then hit equals over and over, I will get '6' '9' '12', etc. This is harder than it seems
- Format your Calculator so that the add button takes up multiple rows, and so is easier to press!
- You can also add further operations to your calculator: memory management, percentages, trig functions, etc. These will likely take you well beyond the time allotted for the lab, but you are welcome to include them!
Submitting
Submit the following files to the LabB submission folder on hedwig: CalculatorGUI.java, CalculatorTester.java, and Calculator.java (which should not have been modified). Make sure you upload your work to the correct folder! The lab is due at the start of class on the day after the lab.
Remember to fill out your lab partner evaluation!
Notes
It is easy to get caught adding extra features and polish in this lab--try to get the core functionality in before you add extensions!
Grading
This assignment will be graded based on the following criteria:
- Program has an appropriately-layed out Calculator GUI (25%)
- Number buttons can be used to enter multi-digit numbers (20%)
- Decimal button works as expected (5%)
- Operation buttons (combined with 'equals') functions correctly (30%)
- Program integrates Calculator class; the GUI does not contain math logic (10%)
- Program is documented with inline comments and comments describing each method (5%)
- You completed your lab partner evaluation (5%)