Uploaded by Gysgzyh

Project 2 Calculator W2021

advertisement
Project 2
Calculator Application
Course:
Professor:
Assignment:
Due Date:
Student Name(s):
INFO3134, W2021
Bill Pulling, Madhavi Mohan, Janice Manning
Project #2: GUI Application and Exception Classes
Friday, April 16, 2021 by 11:59 pm
NOTE: you may work in pairs on this project (optional)
Description:
Write an application that simulates a calculator (see
example opposite). Your calculator must:
• accept multiple operands and operators and
performs multiple operations applying BEDMAS
• ensure that only one decimal point is clicked for
an operand
• allow an operand to be a negative value (or
toggles back to positive)
• perform additional math functions on the current
display as shown on the buttons (%, square,
square root)
• clear the current operation
• allows backspace of one key click at a time when
building an operand
• allow conversion of the current display to hex,
dec, bin, and oct numbers using a drop-down
menu
In this project, you will create four classes:
1. Calculator
• A class to be used to create a Calculator object
2. GUICalculator
• A GUI class which instantiates a Calculator object and performs calculator operations on
that object
3. LongOperandException
• An exception class which will throw an exception if an operand is too long for the text field
display area
4. EmptyOperandException
• An exception class which will throw an exception if no operand has been entered
UML for the Calculator class
Calculator
- operand : String
- operator : String
- decimalPressed : boolean
- list : ArrayList<String>
+ Calculator() {initialize all fields to default values}
+ getter methods for all fields, excluding list
+ setter methods for all fields, excluding list
+ clear() : void
+ backspace(value : String) : String
+ findPercentage(value : String) : String
+ togglePlusMinus(flag : boolean) : String
+ findSquared(value : String) : String
+ findSquareRoot(value : String) : String
+ buildOperand(value : String) : void
+ buildExpression(value : String) : void
+ calculate() : double
+ convertHex(value : String) : String
+ convertOct(value : String) : String
+ convertBin(value : String) : String
+ toString() : String
Specifications
1. Calculator Class
• For the no-arg constructor, initialize all fields to their default values
• Include getters and setters for all fields, excluding list
• void clear()
o re-set all fields to their default values
• String backspace(String value) throws EmptyOperandException
o Return value with the last number or decimal removed, if value is not empty
• String findPercentage(String value) throws EmptyOperandException
o Return the current value as a percentage in decimal format, if value is not empty
• String togglePlusMinus(boolean flag) throws EmptyOperandException
o Add “-“ to the beginning of operand or remove “-“ from the operand, depending on the
value of flag
o Return updated operand
• String findSquared(String value) throws EmptyOperandException
o Return the square of the current value
• String findSquareRoot(String value) throws EmptyOperandException
o Return the square root of the current value
•
•
•
•
•
void buildOperand(String value) throws LongOperandException
o Concatenate the current value to operand
o Ensure that an operand doesn’t become too long
void buildExpression(String value) throws EmptyOperandException
o assign the value to operator and add both the operand and operator to the list
o clear fields
double calculate() throws EmptyOperandException, ArithmeticException
o add the current operand to the list
o calculate the answer, using the values in the list applying the rules of BEDMAS
o clear fields
o return the answer to the expression
For each of the following Convert methods:
o These methods must be your own implementation, not a static method from the API
library
o Value could be an operand or an answer to an expression
o Value must be rounded to 0-decimal places to avoid truncating
▪ String convertHex(String value) throws EmptyOperandException,
LongOperandException
• Return value converted to its hexadecimal equivalent
▪ String convertOct(String value) throws EmptyOperandException,
LongOperandException
• Return value converted to its octal equivalent
▪ String convertBin(String value) throws EmptyOperandException,
LongOperandException
• Return value converted to its binary equivalent
o About the functionality:
▪ the convert methods should be able to convert from the current decimal value (the
current display) to any of the other three number systems
String toString()
o Return a String representation of a Calculator object
2. GUICalculator
• Declare a Calculator object
• Declare GUI references for components and any variables that will have class scope
• For the no-arg constructor:
o pass the title “Calculator” to the superclass constructor
o set an appropriate layout manager for the JFrame, assuming two components (JTextField
and JPanel)
o initialize any variables to default values
o create an instance of Calculator
o instantiate GUI components (or call private helper methods)
▪ you may wish to use to images for some of the buttons’ displays
o create a Font object for both the text field and the buttons’ display as SansSerif, plain, and
22-point size
o create the text field for the digit display to have the following properties:
▪ initial display of 0.0
▪ white background
▪ non-editable so that operands must be entered by clicking the buttons
▪ font display as SansSerif, plain, and 22-point size
▪ horizontal alignment is right aligned
o add two components to the JFrame: a JTextField and a JPanel
▪ the panel will act as a container for all the buttons of the keypad area
o set up the frame using the methods:
▪ setSize(300, 365);
//suggested size
▪ setResizable(false);
▪ setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
▪ setVisible(true);
•
Add a menu system including:
o File, Exit
o Convert, Hex, Dec, Oct,
Bin
o About, How to Use,
About
•
Add ActionListeners to the menu items to do the following:
o Exit the application
o Allow the user to convert the current display to Hex or Oct or Bin and then back to Dec, by
making a call to the corresponding Calculator method
▪ Note: the current display could be an operand or an answer to an expression
▪ Note: the Dec menu option restores any converted number back to the current
decimal number
o Display instructions for how to use the application
o Display information about the application (use your own information here as the
developer)
Add ActionListeners to each of the functional buttons and call its corresponding Calculator method
o Clear, backspace, find percentage, toggle between negative and positive, find square value,
find square root value
Add an ActionListener to each of the number buttons and the decimal button to call Calculator’s
buildOperand method
Add an ActionListener to each of the operator buttons to call Calculator’s buildExpression()
method
o Ensure that an operator can only be clicked once for a binary expression
Add an ActionListener to equals button to call Calculator’s calculate() method and allows the
result to be an operand in a new expression
•
•
•
•
3. Exception Classes
• Write your own exception classes for the following possible exception when entering an operand:
o LongOperandException
o EmptyOperandException
• Throw exceptions in the specified methods in Calculator class
• Write handling code in GUICalculator when calling Calculator methods that might throw
exceptions, reporting the exceptions in a JOptionPane dialog box
• Write exception handling code for any unchecked run-time exceptions that might occur, so that
your program doesn’t crash, for example:
o Division by zero
Submission Requirements:
1. In File Explorer, navigate to your Project1 folder. Zip the project for submission.
2. Submit your .zip file to the Submissions folder by the deadline indicated on page 1 of this document.
Submit your project on time!
3. Code submissions to the FOL drop box must be made on time! Late projects will be subject to late penalties
as follows:
• up to 24 hours late, 20% deduction
• over 24 hours late but less than 48 hours late, an additional 30% off
• over 48 hours late, mark of zero is given.
Submit your own work!
4. It is considered cheating to submit work done by another student (unless it is your partner!) or from another
source as your own work. This is called plagiarism. Helping other students cheat by letting them copy your
source code files is called abetting plagiarism. Both are considered to be academic offences.
5. YOU MUST WRITE YOUR OWN CODE!
Students are encouraged to share ideas and to work together on practice exercises, and you can help
someone else to fix a bug in their code, but any code or documentation prepared for your project must be
done by you. Penalties for committing plagiarism, or helping another student plagiarize by sharing source
code with them, include a zero grade and an academic offence being filed against the student or students
involved. All submissions will be analyzed using plagiarism detection software especially designed to analyze
Java code.
Marking Scheme
Item
Overall Coding Style
• Each host class and private inner classes have appropriate documentation
• All methods have appropriate documentation
• All variables, methods, and constructors have proper access modifiers assigned to them
Calculator
• Four fields declared
• No-arg constructor initializes fields to default values
• Getters and setters are present
• Method header and body correctly implemented:
o clear(), backspace(), findPercentage(), togglePlusMinus(), findSquared(),
findSquareRoot(), toString()
o buildOperand(), buildExpression()
o convertHex(), convertOct(), convertBin()
o calculate()
▪ correctly handles multiple operands and operators
▪ correctly calculates the answer applying the order of operations
GUICalculator Interface
• GUI components are declared in class scope
• No-arg constructor:
o initializes a Calculator object
o A Font object has been created: SansSerif, plain, and 22-point size
o Text field has appropriate settings and initial font display
o JFrame uses an appropriate layout manager and has a text area and button panel
added
o correctly builds JFrame object with its title, suggested size, centered, and can be
closed by the user
• JPanel object:
o Uses an appropriate layout manager
o JButtons appear in the same order as in the sample
o GUI components have been initialized with correct captions
o JButtons have new font or image icon display
• Menu system:
o Menus and menu items are correctly added to the menu bar
o Radio buttons are mutually exclusive
GUICalculator ActionListeners
• Listeners have been added to menu items and inner listener classes have been written,
including interface implementation:
o Exit, How To Use, About
o Hex, Oct, Bin correctly convert from current rounded value
o Current value is converted to any of the three number systems
o Current value could be an answer to an expression
o Dec converts back to current value
• Listeners have been added to button components and inner listener classes have been
written, including interface implementation
o Number buttons correctly build and display the current operand
o Operator buttons correctly build an expression
o Equals button correctly calculates and displays the answer in the text field
o Clear button resets all variables and the display text field
o Backspace clears the last digit entered
o Function buttons displays the current display according to captions:
▪ %, +/-, x2, square root
Marks
Awarded
3
3
1
1
1
7
4
6
4
1
1
1
1
1
1
1
1
1
1
1
1
3
3
1
1
1
10
Exception Handling
• Exception classes have been written for EmptyOperand and LongOperand
• Specified Calculator methods throw required exceptions
• GUICalculator uses handling code when calling Calculator methods, reporting exceptions in
a JOptionPane dialog box (or in the text field)
• Unchecked exceptions are handled and the program doesn’t crash
6
2
/65
Calculator Tests
-----------• initial display should be 0.0
• no function or operator buttons should work
before operand entered
• empty operand exception thrown
Test 1
2 - 6 + 3 * 4 - 35 / 10
2 - 6 + 12 - 3.5
-4 + 12 - 3.5
8 - 3.5
4.5
Test 2
4 as % = 0.04
4 as square root = 2.0
4 as squared = 16.0
Test 3
-6 + 2.3 = -3.7
1.2 * 6.5 = 7.8
Test that decimal can only be pressed once and
that is restored for next operand
Test 4
Clear restores display to 0.0
5.5 * 2 = 11.0
Test that an operator can only be clicked once for a
binary expression
Test 5
• division by zero; display 0.0 restored
• long operand exception thrown; display 0.0
restored
• backspace one digit at a time, if none left,
display 0.0 restored
• -/+ toggle
Test 6
Decimal of 2,587.0 converts to:
• Hex: A1B
• Oct: 5 033
• Bin: 1010 0001 1011
• Dec: 2587.0
Decimal of 1,111.9 rounds to 1,112 and converts
to:
• Hex: 458
• Oct: 2 130
• Bin: 0100 0101 1000
• Dec: 1111.9
Result of calculation 101.55*25 = 2538.75 rounds
to 2539 and converts to:
• Hex: 9EB
• Oct: 4753
• Bin: 1001 1110 1011
• Dec: 2538.75
•
•
•
Help, How To Use
Help, About
File, Exit
Download