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

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

  1. 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.
  2. 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!
  3. In order to run and test your program, make a CalculatorTester class with a main method that can instantiate your CalculatorGUI class.
  4. 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
    Be careful not to get bogged down in formatting or making your calculator look perfect--add the functionality, and then polish up the appearance afterwards!
  5. 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!
  6. 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!
  7. 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.
  8. 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.
  9. 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).
  10. Check and double-check that your program works flawlessly before submitting it to the submission folder.
  11. 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.

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: