Synthia
Generic and flexible data structure generator
examples.gui.Calculator Class Reference

Detailed Description

A simple calculator.

The code for this window is an adaptation of the Java Codex example.

Calculator window

The original code on Java Codex is interesting since it contains a genuine fault that can be detected using the Monkey: clicking on − followed by = throws a NumberFormatException. This error is not intentional and corresponds to a case that the source code does not properly take into account. For the purpose of the example, this fault can be disabled from the code by calling disableNumberFormatException().

The calculator has been modified so that other exceptions can be thrown:

  • Dividing by zero throws an IllegalArgumentException, which is expected
  • Any result producing a value greater than 100,000 produces an OverflowException. The calculator can obviously manipulate larger values; this limitation is artificial. It is enabled by calling hasOverflow().

Definition at line 62 of file Calculator.java.

Inheritance diagram for examples.gui.Calculator:

Classes

class  CalculatorPanel
 The actual panel containing the interface of the calculator. More...
 
class  OverflowException
 Exception that is thrown when the calculator produces an overflow.
 
class  UnderflowException
 Exception that is thrown when the calculator produces an underflow.
 

Public Member Functions

 Calculator ()
 Creates a new calculator window. More...
 
Calculator hasOverflow ()
 Instructs the calculator to throw an OverflowException when producing a number over 100,000. More...
 
Calculator disableNumberFormatException ()
 Instructs the calculator to check the format of numbers and ignore parsing errors. More...
 
JButton getButton (String label)
 Gets the button instance with given label. More...
 
void reset ()
 Puts the object back into its initial state. More...
 

Static Public Member Functions

static void main (String[] args)
 A main method allowing the calculator to be run as a stand-alone application. More...
 

Static Public Attributes

static final String[] BUTTON_LABELS
 The array of button labels. More...
 

Protected Attributes

Map< String, JButton > m_buttons
 A map associating each button to its label. More...
 
CalculatorPanel m_panel
 The calculator panel contained within the frame. More...
 
boolean m_checkFormat
 A flag specifying if the calculator checks the format of numbers. More...
 
boolean m_hasOverflow
 A flag specifying if the calculator produces an overflow exception. More...
 

Constructor & Destructor Documentation

◆ Calculator()

examples.gui.Calculator.Calculator ( )

Creates a new calculator window.

Definition at line 98 of file Calculator.java.

Member Function Documentation

◆ disableNumberFormatException()

Calculator examples.gui.Calculator.disableNumberFormatException ( )

Instructs the calculator to check the format of numbers and ignore parsing errors.

Returns

Definition at line 129 of file Calculator.java.

◆ getButton()

JButton examples.gui.Calculator.getButton ( String  label)

Gets the button instance with given label.

Parameters
labelThe label
Returns
The button instance

Definition at line 141 of file Calculator.java.

◆ hasOverflow()

Calculator examples.gui.Calculator.hasOverflow ( )

Instructs the calculator to throw an OverflowException when producing a number over 100,000.

Returns
This calculator

Definition at line 118 of file Calculator.java.

◆ main()

static void examples.gui.Calculator.main ( String[]  args)
static

A main method allowing the calculator to be run as a stand-alone application.

Parameters
argsCommand-line arguments

Definition at line 157 of file Calculator.java.

◆ reset()

void examples.gui.Calculator.reset ( )

Puts the object back into its initial state.

Implements ca.uqac.lif.synthia.Resettable.

Definition at line 147 of file Calculator.java.

Member Data Documentation

◆ BUTTON_LABELS

final String [] examples.gui.Calculator.BUTTON_LABELS
static
Initial value:
= {"7", "8", "9", "\u00f7",
"4", "5", "6", "\u00d7", "1", "2", "3", "\u2212", "0", ".", "=", "+"}

The array of button labels.

Definition at line 67 of file Calculator.java.

◆ m_buttons

Map<String,JButton> examples.gui.Calculator.m_buttons
protected

A map associating each button to its label.

Definition at line 78 of file Calculator.java.

◆ m_checkFormat

boolean examples.gui.Calculator.m_checkFormat
protected

A flag specifying if the calculator checks the format of numbers.

Definition at line 88 of file Calculator.java.

◆ m_hasOverflow

boolean examples.gui.Calculator.m_hasOverflow
protected

A flag specifying if the calculator produces an overflow exception.

Definition at line 93 of file Calculator.java.

◆ m_panel

CalculatorPanel examples.gui.Calculator.m_panel
protected

The calculator panel contained within the frame.

Definition at line 83 of file Calculator.java.


The documentation for this class was generated from the following file: