first commit

This commit is contained in:
Boris
2024-01-15 20:14:10 +00:00
commit 8c81ee28b7
3106 changed files with 474415 additions and 0 deletions

View File

@@ -0,0 +1,41 @@
#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
comment10.params=
comment10.target=void\ calculateResult()
comment10.text=\n\ Combine\ leftOperand,\ lastOperator,\ and\ the\n\ current\ display\ value.\n\ The\ result\ becomes\ both\ the\ leftOperand\ and\n\ the\ new\ display\ value.\n
comment11.params=operator
comment11.target=void\ applyOperator(char)
comment11.text=\n\ Apply\ an\ operator.\n\ @param\ operator\ The\ operator\ to\ apply.\n
comment12.params=
comment12.target=void\ keySequenceError()
comment12.text=\n\ Report\ an\ error\ in\ the\ sequence\ of\ keys\ that\ was\ pressed.\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\ Reset\ everything\ to\ a\ starting\ state.\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.\n
comment9.params=
comment9.target=java.lang.String\ getVersion()
comment9.text=\n\ @return\ The\ version\ number\ of\ this\ engine.\n
numComments=13

View File

@@ -0,0 +1,196 @@
/**
* The main part of the calculator doing the calculations.
*
* @author David J. Barnes and Michael Kölling
* @version 2016.02.29
*/
public class CalcEngine
{
// The calculator's state is maintained in three fields:
// buildingDisplayValue, haveLeftOperand, and lastOperator.
// Are we already building a value in the display, or will the
// next digit be the first of a new one?
private boolean buildingDisplayValue;
// Has a left operand already been entered (or calculated)?
private boolean haveLeftOperand;
// The most recent operator that was entered.
private char lastOperator;
// The current value (to be) shown in the display.
private int displayValue;
// The value of an existing left operand.
private int leftOperand;
/**
* Create a CalcEngine.
*/
public CalcEngine()
{
clear();
}
/**
* @return The value that should currently be displayed
* on the calculator display.
*/
public int getDisplayValue()
{
return displayValue;
}
/**
* 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)
{
if(buildingDisplayValue) {
// Incorporate this digit.
displayValue = displayValue*10 + number;
}
else {
// Start building a new number.
displayValue = number;
buildingDisplayValue = true;
}
}
/**
* The 'plus' button was pressed.
*/
public void plus()
{
applyOperator('+');
}
/**
* The 'minus' button was pressed.
*/
public void minus()
{
applyOperator('-');
}
/**
* The '=' button was pressed.
*/
public void equals()
{
// This should completes the building of a second operand,
// so ensure that we really have a left operand, an operator
// and a right operand.
if(haveLeftOperand &&
lastOperator != '?' &&
buildingDisplayValue) {
calculateResult();
lastOperator = '?';
buildingDisplayValue = false;
}
else {
keySequenceError();
}
}
/**
* The 'C' (clear) button was pressed.
* Reset everything to a starting state.
*/
public void clear()
{
lastOperator = '?';
haveLeftOperand = false;
buildingDisplayValue = false;
displayValue = 0;
}
/**
* @return The title of this calculation engine.
*/
public String getTitle()
{
return "Java Calculator";
}
/**
* @return The author of this engine.
*/
public String getAuthor()
{
return "David J. Barnes and Michael Kölling";
}
/**
* @return The version number of this engine.
*/
public String getVersion()
{
return "Version 1.0";
}
/**
* Combine leftOperand, lastOperator, and the
* current display value.
* The result becomes both the leftOperand and
* the new display value.
*/
private void calculateResult()
{
switch(lastOperator) {
case '+':
displayValue = leftOperand + displayValue;
haveLeftOperand = true;
leftOperand = displayValue;
break;
case '-':
displayValue = leftOperand - displayValue;
haveLeftOperand = true;
leftOperand = displayValue;
break;
default:
keySequenceError();
break;
}
}
/**
* Apply an operator.
* @param operator The operator to apply.
*/
private void applyOperator(char operator)
{
// If we are not in the process of building a new operand
// then it is an error, unless we have just calculated a
// result using '='.
if(!buildingDisplayValue &&
!(haveLeftOperand && lastOperator == '?')) {
keySequenceError();
return;
}
if(lastOperator != '?') {
// First apply the previous operator.
calculateResult();
}
else {
// The displayValue now becomes the left operand of this
// new operator.
haveLeftOperand = true;
leftOperand = displayValue;
}
lastOperator = operator;
buildingDisplayValue = false;
}
/**
* Report an error in the sequence of keys that was pressed.
*/
private void keySequenceError()
{
System.out.println("A key sequence error has occurred.");
// Reset everything.
clear();
}
}

View File

@@ -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

View File

@@ -0,0 +1,29 @@
/**
* 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);
}
}

View File

@@ -0,0 +1,14 @@
Project: calculator-full-solution
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 version of the project implements a complete
solution to the simple calculator discussed in
Chapter 9.

View File

@@ -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

View File

@@ -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();
}
}

View File

@@ -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=775
package.editor.height=435
package.editor.width=667
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=521
readme.editor.width=840
readme.editor.x=30
readme.editor.y=23
target1.editor.height=723
target1.editor.width=824
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=140
target1.x=110
target1.y=80
target2.editor.height=664
target2.editor.width=848
target2.editor.x=37
target2.editor.y=23
target2.height=60
target2.name=UserInterface
target2.naviview.expanded=true
target2.showInterface=false
target2.type=ClassTarget
target2.typeParameters=
target2.width=140
target2.x=280
target2.y=170
target3.editor.height=732
target3.editor.width=681
target3.editor.x=103
target3.editor.y=39
target3.height=60
target3.name=CalcEngine
target3.naviview.expanded=false
target3.showInterface=false
target3.type=ClassTarget
target3.typeParameters=
target3.width=140
target3.x=450
target3.y=260