Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task The calculator application in the demomvc project could use more buttons for various functions. Expand the current 4x4 grid into a 5x5 grid. This

Task

The calculator application in the demomvc project could use more buttons for various functions. Expand the current 4x4 grid into a 5x5 grid. This means you will need to create code for 9 more buttons.

If you need some ideas for buttons, here are some:

-Clear

-Binary Operators: exponentiation, mod

-Unary Operators: exponential (base 2, base e, base 10), factorial, logarithm (base 2, base e, base 10), plus/minus, reciprocal, square root, squared, trigonometric (sine, cosine, etc.)

-Memory store, memory recall

Note that the current calculator only has binary operators and the logic to perform them. Unary operators will likely require their own logic.

You might like the behavior of the calculator with this modification. The first if condition (start) in CalculatorModel's update method could also check that the user has entered a digit or a period.

demomvc project

package calculator;

import javax.swing.JFrame;

/**

* The Calculator class along with CalculatorController,

* CalculatorModel and CalculatorView implements a

* simple-minded calculator.

* The implementation is based on the MVC design pattern.

*

* @author Tom Bylander

*/

public class Calculator {

/**

* Create the model, view and controller objects,

* and launch the application.

*/

public static void main(String[] args) {

/* create new model, view and controller */

CalculatorModel model = new CalculatorModel();

CalculatorView view = new CalculatorView();

CalculatorController controller = new CalculatorController(model, view);

/* register controller as listener */

view.registerListener(controller);

/* start it up */

view.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

view.setSize(400, 300);

view.setVisible(true);

}

}

package calculator;

import java.awt.event.ActionEvent; import java.awt.event.ActionListener;

/** * The CalculatorController class is the controller in this * MVC implementation of a calculator. Its sole function is * to interpret events from the GUI and update instances of * CalculatorModel and CalculatorView as appropriate. * * @author Tom Bylander */ public class CalculatorController implements ActionListener { /** * The model of this MVC implementation of a calculator. */ private CalculatorModel model; /** * The view of this MVC implementation of a calculator. */ private CalculatorView view; /** * This saves the model and and view. * * @param model a CalculatorModel for the functions of the calculator and the values * entered by the user * @param view a CalculatorView for what should be displayed in the GUI */ public CalculatorController(CalculatorModel model, CalculatorView view) { this.model = model; this.view = view; } /** * Determines what to do with the event generated by the GUI. * The event could correspond to a menu item or a button. * The model and the view are updated as appropriate. * * @param e an event from the GUI */ public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); if (command.equals("Exit")) { System.exit(0); } else if (command.equals("Two Decimal Digits")) { view.setDigits(2); view.update(model.getValue()); } else if (command.equals("Any Decimal Digits")) { view.setDigits(-1); view.update(model.getValue()); } else { model.update(command); view.update(model.getValue()); } } }

package calculator;

/**

* This is the model of this MVC implementation of a calculator.

* It performs the functions of the calculator and keeps track

* of what the user has entered.

*

* @author Tom Bylander

*/

public class CalculatorModel {

/**

* This is the numeric value of the number the user is entering,

* or the number that was just calculated.

*/

private double displayValue;

/**

* This is the previous value entered or calculated.

*/

private double internalValue;

/**

* This is the String corresponding to what the user.

* is entering

*/

private String displayString;

/**

* This is the last operation entered by the user.

*/

private String operation;

/**

* This is true if the next digit entered starts a new value.

*/

private boolean start;

/**

* This is true if a decimal dot has been entered for the current value.

*/

private boolean dot;

/**

* Initializes the instance variables.

*/

public CalculatorModel() {

displayValue = 0.0;

displayString = "" + displayValue;

internalValue = 0;

dot = false;

start = true;

operation = "";

}

/**

* @return the String value of what was just calculated

* or what the user is entering

*/

public String getValue() {

return displayString;

}

/**

* Updates the values maintained by the calculator based on the

* button that the user has just clicked.

*

* @param text is the name of the button that the user has just clicked

*/

public void update(String text) {

if (start) {

internalValue = displayValue;

displayValue = 0;

displayString = "";

start = false;

dot = false;

}

if (text.length() == 1 && "0123456789".indexOf(text) >= 0) {

displayString += text;

displayValue = Double.valueOf(displayString);

} else if (text.equals(".")) {

if (! dot) {

dot = true;

if (displayString.equals("")) {

displayString = "0";

}

displayString += ".";

}

} else {

if (operation.equals("+")) {

displayValue = internalValue + displayValue;

} else if (operation.equals("-")) {

displayValue = internalValue - displayValue;

} else if (operation.equals("*")) {

displayValue = internalValue * displayValue;

} else if (operation.equals("/")) {

displayValue = internalValue / displayValue;

}

displayString = "" + displayValue;

// internalValue = displayValue;

operation = text;

start = true;

}

}

}

package calculator;

import java.awt.*; import javax.swing.*;

/** * This is the view part of my MVC implementation of a calculator. * It creates the panels and the components of the window. * The current value is displayed in a JLabel. * * @author Tom Bylander */ public class CalculatorView extends JFrame { /** * the display of the calculator */ private JLabel display; /** * the buttons of the calculator */ private JPanel buttonsPanel; /** * the menu of the calculator */ private JMenu exampleMenu; /** * the number of fractional digits to show or -1 */ private int digits; private Font font; /** * Creates the panels and components for the JFrame */ public CalculatorView() { super("Simple Calculator"); font = new Font("SanSerif", Font.BOLD, 30); // create the menu JMenuBar menuBar = new JMenuBar(); setJMenuBar(menuBar);

exampleMenu = new JMenu("Menu"); menuBar.add(exampleMenu);

JMenuItem twoDigitsButton = new JMenuItem("Two Decimal Digits"); exampleMenu.add(twoDigitsButton);

JMenuItem anyDigitsButton = new JMenuItem("Any Decimal Digits"); exampleMenu.add(anyDigitsButton);

JMenuItem exitButton = new JMenuItem("Exit"); exampleMenu.add(exitButton);

// create the display JPanel displayPanel = new JPanel(); add(displayPanel, BorderLayout.NORTH);

display = new JLabel("0.0"); display.setFont(font); displayPanel.add(display); digits = -1;

// create the buttons buttonsPanel = new JPanel(); add(buttonsPanel, BorderLayout.CENTER); buttonsPanel.setLayout(new GridLayout(4, 4, 0, 0));

String[] buttonStrings = { "1", "2", "3", "+", "4", "5", "6", "-", "7", "8", "9", "*", "0", ".", "=", "/" };

for (String s : buttonStrings) { buttonsPanel.add(new JButton(s)); } } /** * Register the controller as the listener to the menu items * and the buttons. * @param controller The event handler for the calculator */ public void registerListener(CalculatorController controller) { Component[] components = buttonsPanel.getComponents(); for (Component component : components) { if (component instanceof AbstractButton) { AbstractButton button = (AbstractButton) component; button.addActionListener(controller); button.setFont(font); } }

components = exampleMenu.getMenuComponents(); for (Component component : components) { if (component instanceof AbstractButton) { AbstractButton button = (AbstractButton) component; button.addActionListener(controller); button.setFont(font); } } } /** * Display the value in the JLabel of the calculator. * Round off the number of digits if needed. * * @param value the value to be displayed */ public void update(String value) { if (digits < 0) { display.setText(value); } else { String format = "%." + digits + "f"; String text = String.format(format, Double.valueOf(value)); display.setText(text); } } /** * Set the number of fractional digits to display. * -1 means display them all. * * @param digits the number of fractional digits to display or -1 */ public void setDigits(int digits) { this.digits = digits; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Joseph J. Adamski

7th edition

978-1111825911, 1111825912, 978-1133684374, 1133684378, 978-111182591

More Books

Students also viewed these Databases questions

Question

What is meant by organisational theory ?

Answered: 1 week ago

Question

What is meant by decentralisation of authority ?

Answered: 1 week ago

Question

Briefly explain the qualities of an able supervisor

Answered: 1 week ago

Question

Define policy making?

Answered: 1 week ago