first commit
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=CalcEngine()
|
||||
comment0.text=\n\ Create\ a\ CalcEngine.\n
|
||||
comment1.params=
|
||||
comment1.target=int\ getDisplayValue()
|
||||
comment1.text=\n\ @return\ The\ value\ that\ should\ currently\ be\ displayed\n\ on\ the\ calculator\ display.\n
|
||||
comment2.params=number
|
||||
comment2.target=void\ numberPressed(int)
|
||||
comment2.text=\n\ A\ number\ button\ was\ pressed.\n\ Either\ start\ a\ new\ operand,\ or\ incorporate\ this\ number\ as\n\ the\ least\ significant\ digit\ of\ an\ existing\ one.\n\ @param\ number\ The\ number\ pressed\ on\ the\ calculator.\n
|
||||
comment3.params=
|
||||
comment3.target=void\ plus()
|
||||
comment3.text=\n\ The\ 'plus'\ button\ was\ pressed.\ \n
|
||||
comment4.params=
|
||||
comment4.target=void\ minus()
|
||||
comment4.text=\n\ The\ 'minus'\ button\ was\ pressed.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ equals()
|
||||
comment5.text=\n\ The\ '\='\ button\ was\ pressed.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ clear()
|
||||
comment6.text=\n\ The\ 'C'\ (clear)\ button\ was\ pressed.\n
|
||||
comment7.params=
|
||||
comment7.target=java.lang.String\ getTitle()
|
||||
comment7.text=\n\ @return\ The\ title\ of\ this\ calculation\ engine.\n
|
||||
comment8.params=
|
||||
comment8.target=java.lang.String\ getAuthor()
|
||||
comment8.text=\n\ @return\ The\ author\ of\ this\ engine.\ This\ string\ is\ displayed\ as\ it\ is,\n\ so\ it\ should\ say\ something\ like\ "Written\ by\ H.\ Simpson".\n
|
||||
comment9.params=
|
||||
comment9.target=java.lang.String\ getVersion()
|
||||
comment9.text=\n\ @return\ The\ version\ number\ of\ this\ engine.\ This\ string\ is\ displayed\ as\ \n\ it\ is,\ so\ it\ should\ say\ something\ like\ "Version\ 1.1".\n
|
||||
numComments=10
|
||||
@@ -0,0 +1,90 @@
|
||||
/**
|
||||
* The main part of the calculator doing the calculations.
|
||||
*
|
||||
* @author: (none yet)
|
||||
* @version 0.1 (incomplete)
|
||||
*/
|
||||
public class CalcEngine
|
||||
{
|
||||
// Put instance variables here.
|
||||
|
||||
/**
|
||||
* Create a CalcEngine.
|
||||
*/
|
||||
public CalcEngine()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The value that should currently be displayed
|
||||
* on the calculator display.
|
||||
*/
|
||||
public int getDisplayValue()
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* A number button was pressed.
|
||||
* Either start a new operand, or incorporate this number as
|
||||
* the least significant digit of an existing one.
|
||||
* @param number The number pressed on the calculator.
|
||||
*/
|
||||
public void numberPressed(int number)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'plus' button was pressed.
|
||||
*/
|
||||
public void plus()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'minus' button was pressed.
|
||||
*/
|
||||
public void minus()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The '=' button was pressed.
|
||||
*/
|
||||
public void equals()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* The 'C' (clear) button was pressed.
|
||||
*/
|
||||
public void clear()
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The title of this calculation engine.
|
||||
*/
|
||||
public String getTitle()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The author of this engine. This string is displayed as it is,
|
||||
* so it should say something like "Written by H. Simpson".
|
||||
*/
|
||||
public String getAuthor()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
|
||||
/**
|
||||
* @return The version number of this engine. This string is displayed as
|
||||
* it is, so it should say something like "Version 1.1".
|
||||
*/
|
||||
public String getVersion()
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
#BlueJ class context
|
||||
comment0.params=
|
||||
comment0.target=Calculator()
|
||||
comment0.text=\n\ Create\ a\ new\ calculator\ and\ show\ it.\n
|
||||
comment1.params=
|
||||
comment1.target=void\ show()
|
||||
comment1.text=\n\ In\ case\ the\ window\ was\ closed,\ show\ it\ again.\n
|
||||
numComments=2
|
||||
@@ -0,0 +1,30 @@
|
||||
|
||||
/**
|
||||
* The main class of a simple calculator. Create one of these and you'll
|
||||
* get the calculator on screen.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class Calculator
|
||||
{
|
||||
private CalcEngine engine;
|
||||
private UserInterface gui;
|
||||
|
||||
/**
|
||||
* Create a new calculator and show it.
|
||||
*/
|
||||
public Calculator()
|
||||
{
|
||||
engine = new CalcEngine();
|
||||
gui = new UserInterface(engine);
|
||||
}
|
||||
|
||||
/**
|
||||
* In case the window was closed, show it again.
|
||||
*/
|
||||
public void show()
|
||||
{
|
||||
gui.setVisible(true);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
Project: calculator-gui
|
||||
Authors: Michael Kölling and David J. Barnes
|
||||
|
||||
This project is part of the material for the book
|
||||
|
||||
Objects First with Java - A Practical Introduction using BlueJ
|
||||
Sixth edition
|
||||
David J. Barnes and Michael Kölling
|
||||
Pearson Education, 2016
|
||||
|
||||
|
||||
This program is meant to implement a simple pocket calculator.
|
||||
In its initial version, it is incomplete.
|
||||
|
||||
The Calculator and UserInterface classes can be used as they are, but the
|
||||
CalcEngine, the bit that does the real calculations, has not been implemented.
|
||||
|
||||
It is your task to finish the job. Have fun!
|
||||
|
||||
To start this application (once you have implemented the missing bits) just
|
||||
create an object of class Calculator.
|
||||
@@ -0,0 +1,23 @@
|
||||
#BlueJ class context
|
||||
comment0.params=engine
|
||||
comment0.target=UserInterface(CalcEngine)
|
||||
comment0.text=\n\ Create\ a\ user\ interface.\n\ @param\ engine\ The\ calculator\ engine.\n
|
||||
comment1.params=visible
|
||||
comment1.target=void\ setVisible(boolean)
|
||||
comment1.text=\n\ Set\ the\ visibility\ of\ the\ interface.\n\ @param\ visible\ true\ if\ the\ interface\ is\ to\ be\ made\ visible,\ false\ otherwise.\n
|
||||
comment2.params=
|
||||
comment2.target=void\ makeFrame()
|
||||
comment2.text=\n\ Make\ the\ frame\ for\ the\ user\ interface.\n
|
||||
comment3.params=panel\ buttonText\ action
|
||||
comment3.target=void\ addButton(java.awt.Container,\ java.lang.String,\ UserInterface.ButtonAction)
|
||||
comment3.text=\n\ Add\ a\ button\ to\ the\ button\ panel.\n\ @param\ panel\ The\ panel\ to\ receive\ the\ button.\n\ @param\ buttonText\ The\ text\ for\ the\ button.\n\ @param\ action\ Action\ to\ be\ taken\ by\ the\ button.\n
|
||||
comment4.params=panel\ digit
|
||||
comment4.target=void\ addNumberButton(java.awt.Container,\ int)
|
||||
comment4.text=\n\ Add\ a\ number\ button\ to\ the\ button\ panel.\n\ @param\ panel\ The\ panel\ to\ receive\ the\ button.\n\ @param\ digit\ The\ single\ digit\ on\ the\ button.\n
|
||||
comment5.params=
|
||||
comment5.target=void\ redisplay()
|
||||
comment5.text=\n\ Update\ the\ interface\ display\ to\ show\ the\ current\ value\ of\ the\ \n\ calculator.\n
|
||||
comment6.params=
|
||||
comment6.target=void\ showInfo()
|
||||
comment6.text=\n\ Toggle\ the\ info\ display\ in\ the\ calculator's\ status\ area\ between\ the\n\ author\ and\ version\ information.\n
|
||||
numComments=7
|
||||
@@ -0,0 +1,144 @@
|
||||
import java.awt.*;
|
||||
import java.awt.event.*;
|
||||
import javax.swing.*;
|
||||
import javax.swing.border.*;
|
||||
|
||||
/**
|
||||
* A graphical user interface for the calculator. No calculation is being
|
||||
* done here. This class is responsible just for putting up the display on
|
||||
* screen. It then refers to the "CalcEngine" to do all the real work.
|
||||
*
|
||||
* @author: Michael Kölling and David J. Barnes
|
||||
* @version 2016.02.29
|
||||
*/
|
||||
public class UserInterface
|
||||
{
|
||||
private CalcEngine calc;
|
||||
private boolean showingAuthor;
|
||||
|
||||
private JFrame frame;
|
||||
private JTextField display;
|
||||
private JLabel status;
|
||||
|
||||
/**
|
||||
* Create a user interface.
|
||||
* @param engine The calculator engine.
|
||||
*/
|
||||
public UserInterface(CalcEngine engine)
|
||||
{
|
||||
calc = engine;
|
||||
showingAuthor = true;
|
||||
makeFrame();
|
||||
frame.setVisible(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the visibility of the interface.
|
||||
* @param visible true if the interface is to be made visible, false otherwise.
|
||||
*/
|
||||
public void setVisible(boolean visible)
|
||||
{
|
||||
frame.setVisible(visible);
|
||||
}
|
||||
|
||||
/**
|
||||
* Make the frame for the user interface.
|
||||
*/
|
||||
private void makeFrame()
|
||||
{
|
||||
frame = new JFrame(calc.getTitle());
|
||||
|
||||
JPanel contentPane = (JPanel)frame.getContentPane();
|
||||
contentPane.setLayout(new BorderLayout(8, 8));
|
||||
contentPane.setBorder(new EmptyBorder( 10, 10, 10, 10));
|
||||
|
||||
display = new JTextField();
|
||||
contentPane.add(display, BorderLayout.NORTH);
|
||||
|
||||
JPanel buttonPanel = new JPanel(new GridLayout(4, 4));
|
||||
addNumberButton(buttonPanel, 7);
|
||||
addNumberButton(buttonPanel, 8);
|
||||
addNumberButton(buttonPanel, 9);
|
||||
addButton(buttonPanel, "C", () -> calc.clear());
|
||||
|
||||
addNumberButton(buttonPanel, 4);
|
||||
addNumberButton(buttonPanel, 5);
|
||||
addNumberButton(buttonPanel, 6);
|
||||
addButton(buttonPanel, "?", () -> showInfo());
|
||||
|
||||
addNumberButton(buttonPanel, 1);
|
||||
addNumberButton(buttonPanel, 2);
|
||||
addNumberButton(buttonPanel, 3);
|
||||
buttonPanel.add(new JLabel(" "));
|
||||
|
||||
addNumberButton(buttonPanel, 0);
|
||||
addButton(buttonPanel, "+", () -> calc.plus());
|
||||
addButton(buttonPanel, "-", () -> calc.minus());
|
||||
addButton(buttonPanel, "=", () -> calc.equals());
|
||||
|
||||
contentPane.add(buttonPanel, BorderLayout.CENTER);
|
||||
|
||||
status = new JLabel(calc.getAuthor());
|
||||
contentPane.add(status, BorderLayout.SOUTH);
|
||||
|
||||
frame.pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a button to the button panel.
|
||||
* @param panel The panel to receive the button.
|
||||
* @param buttonText The text for the button.
|
||||
* @param action Action to be taken by the button.
|
||||
*/
|
||||
private void addButton(Container panel, String buttonText, ButtonAction action)
|
||||
{
|
||||
JButton button = new JButton(buttonText);
|
||||
button.addActionListener(e -> { action.act(); redisplay(); });
|
||||
panel.add(button);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a number button to the button panel.
|
||||
* @param panel The panel to receive the button.
|
||||
* @param digit The single digit on the button.
|
||||
*/
|
||||
private void addNumberButton(Container panel, int digit)
|
||||
{
|
||||
addButton(panel, "" + digit, () -> calc.numberPressed(digit));
|
||||
}
|
||||
|
||||
/**
|
||||
* Update the interface display to show the current value of the
|
||||
* calculator.
|
||||
*/
|
||||
private void redisplay()
|
||||
{
|
||||
display.setText("" + calc.getDisplayValue());
|
||||
}
|
||||
|
||||
/**
|
||||
* Toggle the info display in the calculator's status area between the
|
||||
* author and version information.
|
||||
*/
|
||||
private void showInfo()
|
||||
{
|
||||
if(showingAuthor)
|
||||
status.setText(calc.getVersion());
|
||||
else
|
||||
status.setText(calc.getAuthor());
|
||||
|
||||
showingAuthor = !showingAuthor;
|
||||
}
|
||||
|
||||
/**
|
||||
* Functional interface for button actions.
|
||||
*/
|
||||
@FunctionalInterface
|
||||
private interface ButtonAction
|
||||
{
|
||||
/**
|
||||
* Act on a button press.
|
||||
*/
|
||||
public void act();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
#BlueJ package file
|
||||
dependency1.from=UserInterface
|
||||
dependency1.to=CalcEngine
|
||||
dependency1.type=UsesDependency
|
||||
dependency2.from=Calculator
|
||||
dependency2.to=CalcEngine
|
||||
dependency2.type=UsesDependency
|
||||
dependency3.from=Calculator
|
||||
dependency3.to=UserInterface
|
||||
dependency3.type=UsesDependency
|
||||
objectbench.height=76
|
||||
objectbench.width=760
|
||||
package.editor.height=435
|
||||
package.editor.width=652
|
||||
package.editor.x=70
|
||||
package.editor.y=80
|
||||
package.numDependencies=3
|
||||
package.numTargets=3
|
||||
package.showExtends=true
|
||||
package.showUses=true
|
||||
project.charset=UTF-8
|
||||
readme.editor.height=602
|
||||
readme.editor.width=814
|
||||
readme.editor.x=41
|
||||
readme.editor.y=23
|
||||
target1.editor.height=735
|
||||
target1.editor.width=843
|
||||
target1.editor.x=50
|
||||
target1.editor.y=60
|
||||
target1.height=60
|
||||
target1.name=Calculator
|
||||
target1.naviview.expanded=true
|
||||
target1.showInterface=false
|
||||
target1.type=ClassTarget
|
||||
target1.typeParameters=
|
||||
target1.width=100
|
||||
target1.x=80
|
||||
target1.y=70
|
||||
target2.editor.height=696
|
||||
target2.editor.width=822
|
||||
target2.editor.x=99
|
||||
target2.editor.y=49
|
||||
target2.height=60
|
||||
target2.name=UserInterface
|
||||
target2.naviview.expanded=false
|
||||
target2.showInterface=false
|
||||
target2.type=ClassTarget
|
||||
target2.typeParameters=
|
||||
target2.width=120
|
||||
target2.x=240
|
||||
target2.y=150
|
||||
target3.editor.height=721
|
||||
target3.editor.width=870
|
||||
target3.editor.x=50
|
||||
target3.editor.y=60
|
||||
target3.height=60
|
||||
target3.name=CalcEngine
|
||||
target3.naviview.expanded=true
|
||||
target3.showInterface=false
|
||||
target3.type=ClassTarget
|
||||
target3.typeParameters=
|
||||
target3.width=120
|
||||
target3.x=430
|
||||
target3.y=230
|
||||
Reference in New Issue
Block a user