Wrox Programmer Forums

Need to download code?

View our list of code downloads.

| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Java Programming 24-Hour Trainer by Yakov Fain
This is the forum to discuss the Wrox book Java Programming 24-Hour Trainer by Yakov Fain; ISBN: 978-0-470-88964-0
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Java Programming 24-Hour Trainer by Yakov Fain section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 28th, 2011, 03:34 PM
Authorized User
Points: 51, Level: 1
Points: 51, Level: 1 Points: 51, Level: 1 Points: 51, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2006
Location: Webster, NY, .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 10 Calculator Applet

Yakov et. al.,
I took my working calculator (two classes - Calculator.java and TestCalcEngine.java) from lesson 9 and attempted to follow through with the steps outlined in the TRY IT section at the end of the Lesson 10 Calculator Applet (Step-by-Step) items 1-9. I have commented where in the Calculator class each of the steps was taken. When I run the program now I get only a gray box with no buttons. Am I missing something? Will the applet still work with two classes or does the code to perform the calculations have to be combined into a sigle class?

Calculator.java:
import
javax.swing.*;

import
java.awt.Graphics;

import
java.awt.GridLayout;

import
java.awt.BorderLayout;

import
java.awt.event.WindowAdapter;

import
java.awt.event.WindowEvent;

import
javax.swing.JApplet;


 
// 2. Give it an ancestor...
<B>
public</B>
class Calculator extends JApplet {

// Declare all calculator's components...

private JPanel windowContent;
public JTextField displayField;
private JButton button0;
private JButton button1;
private JButton button2;
private JButton button3;
private JButton button4;
private JButton button5;
private JButton button6;
private JButton button7;
private JButton button8;
private JButton button9;
private JButton buttonPoint;
private JButton buttonEqual;
private JButton buttonAdd;
private JButton buttonSubtract;
private JButton buttonMultiply;
private JButton buttonDivide;
private JPanel p1;

// Public access to the display fields so that the engine

// class can update the field...

publicvoid setDisplayValue(String val){
displayField.setText(val);
}

public String getDisplayValue() {
returndisplayField.getText();
}

public String result ="";
public String operand1="";
public String operand2="";
public String operator="";

 
// Constructor method creates the components

// and adds them to the frame using combination of

// Borderlayout and Gridlayout...


// 4. Rename the constructor method into the applet's init() method...
publicvoid init(){
initComponents();
}

privatevoid initComponents(){

// Calculator(){
windowContent= new JPanel();

// Set the layout manager for this panel...

BorderLayout bl =
new BorderLayout();
windowContent.setLayout(bl);

// Create the display field and place it in the

// North area of the window...

displayField = new JTextField(30);
windowContent.add("North",displayField);

// Create buttons using constructor of the

// class JButton that takes the label of the

// button as a parameter...


button0=new JButton("0");
button1=new JButton("1");
button2=new JButton("2");
button3=new JButton("3");
button4=new JButton("4");
button5=new JButton("5");
button6=new JButton("6");
button7=new JButton("7");
button8=new JButton("8");
button9=new JButton("9");
buttonPoint = new JButton(".");
buttonEqual=new JButton("=");
buttonAdd=new JButton("+");
buttonSubtract=new JButton("-");
buttonMultiply=new JButton("x");
buttonDivide=new JButton("/");

// Create the panel with the GridLayout with 16 buttons,

// ten numeric ones (0-9) plus six operators (+,-,.,=,x,/)...


p1 = new JPanel();
GridLayout gl =
new GridLayout(4,4);
p1.setLayout(gl);

// Add window controls to the panel p1

p1.add(button1);
p1.add(button2);
p1.add(button3);
p1.add(buttonAdd);
p1.add(button4);
p1.add(button5);
p1.add(button6);
p1.add(buttonSubtract);
p1.add(button7);
p1.add(button8);
p1.add(button9);
p1.add(buttonMultiply);
p1.add(button0);
p1.add(buttonPoint);
p1.add(buttonEqual);
p1.add(buttonDivide);

// 7. Add the code to perform calculations...
TestCalcEngine calcEngine =
new TestCalcEngine(this);

button0.addActionListener(calcEngine);
button1.addActionListener(calcEngine);
button2.addActionListener(calcEngine);
button3.addActionListener(calcEngine);
button4.addActionListener(calcEngine);
button5.addActionListener(calcEngine);
button6.addActionListener(calcEngine);
button7.addActionListener(calcEngine);
button8.addActionListener(calcEngine);
button9.addActionListener(calcEngine);
buttonPoint.addActionListener(calcEngine);
buttonEqual.addActionListener(calcEngine);
buttonAdd.addActionListener(calcEngine);
buttonSubtract.addActionListener(calcEngine);
buttonMultiply.addActionListener(calcEngine);
buttonDivide.addActionListener(calcEngine);

// Add the panel p1 to the center of the window

windowContent.add("Center",p1);

//Create the frame and set its content pane


// 5. Do not control size of calculator by calling pack...
// 6. Do not instantiate JFrame...
// 8. Remove WindowListener functionality...
// JFrame frame = new JFrame("Calculator");

// frame.setContentPane(windowContent);

// frame.addWindowListener(new FrameListener());

// frame.pack();

// Display the window

// frame.setVisible(true);

// Frame listener checks for window closing event...

// class FrameListener extends WindowAdapter {

// public void windowClosing(WindowEvent e)

// {

// System.out.println("Window closing");

// System.exit(0);

// }

// }


// 3. Get rid of the main method
// public static void main(String[] args) {

// new Calculator();

// }

}
}

TestCalcEngine.java:
<B>
import
java.awt.event.ActionListener;

import
java.awt.event.ActionEvent;

import
javax.swing.JOptionPane;

import
javax.swing.JButton;

public
class TestCalcEngine implements ActionListener {

Calculator
parent; // a reference to the Calculator


// Constructor stores the reference to the Calculator

// window in the member variable parent...

TestCalcEngine(Calculator parent){
this.parent = parent;
}

publicvoid actionPerformed(ActionEvent e){
// Get the source of this action...

JButton clickedButton=(JButton) e.getSource();

// Get the buttons label...

String clickedButtonLabel=clickedButton.getText();


// Calculator code goes here. Do something

// based on the button pressed...

if (clickedButtonLabel=="0"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="1"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}

// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="2"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="3"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="4"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="5"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="6"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="7"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="8"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="9"){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="."){
// Get the existing text from the calculators display field...

String dispFieldText =
parent.getDisplayValue();

if (parent.operator ==""){//operator not set, deal with operand1

parent.operand1=dispFieldText + clickedButtonLabel;
}
else{ //operator already set, deal with operand2

parent.operand2=dispFieldText + clickedButtonLabel;
}
// Update the display...

parent.setDisplayValue(dispFieldText + clickedButtonLabel);

}
elseif (clickedButtonLabel=="/"){
// Clear Display...

parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);

}
elseif (clickedButtonLabel=="+"){
// Clear Display...

parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);

}
elseif (clickedButtonLabel=="-"){
// Clear Display...

parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);

}
elseif (clickedButtonLabel=="x"){
// Clear Display...

parent.setDisplayValue("");
parent.operator = (clickedButtonLabel);

}
elseif (clickedButtonLabel=="="){
// do the arithmetic...

int resulto = 0;
if (parent.operator == "+"){
int operando1 = Integer.parseInt(parent.operand1);
int operando2 = Integer.parseInt(parent.operand2);
resulto = operando1 + operando2;

}
elseif (parent.operator == "-"){
// do the arithmetic...

}
elseif (parent.operator == "/"){
// do the arithmetic...

}
elseif (parent.operator == "x"){
// do the arithmetic...

}

// Update the display...

String resultoo = Integer.toString(resulto);
parent.setDisplayValue(resultoo);

// Clear the operands...

parent.operand1 = "";
parent.operand2 = "";
parent.operator = "";
}

}
}
</B>
Reply With Quote
  #2 (permalink)  
Old December 29th, 2011, 08:16 AM
Authorized User
Points: 30, Level: 1
Points: 30, Level: 1 Points: 30, Level: 1 Points: 30, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Sep 2011
Location: uk
Posts: 10
Thanks: 0
Thanked 0 Times in 0 Posts
Default RE: Lesson 10 Calculator

Hi dthoma128

try adding the following code after
Code:
windowContent.add("Center",p1);
add(widowContent);
Your Calculator Should now be running in your browser.
Reply With Quote
  #3 (permalink)  
Old December 29th, 2011, 02:09 PM
Authorized User
Points: 51, Level: 1
Points: 51, Level: 1 Points: 51, Level: 1 Points: 51, Level: 1
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2006
Location: Webster, NY, .
Posts: 12
Thanks: 0
Thanked 0 Times in 0 Posts
Default Thank you!

That did it...thanks!!
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Lesson 10 - Result Set: must provide a value to the new variable. samirayes BOOK: Knight's 24-Hour Trainer: Microsoft SQL Server 2008 Integration Services 1 January 26th, 2014 10:18 AM
Lesson 4 Mtheys BOOK: Java Programming 24-Hour Trainer by Yakov Fain 3 September 15th, 2011 05:02 AM
Lesson 10 sergeySE BOOK: Java Programming 24-Hour Trainer by Yakov Fain 1 July 3rd, 2011 06:20 PM
Lesson 6 Try It zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 June 6th, 2011 10:07 PM
Chapter 10, listing 10-10-app kiwibrit BOOK: Professional ASP.NET 3.5 : in C# and VB ISBN: 978-0-470-18757-9 2 August 18th, 2009 04:21 AM



All times are GMT -4. The time now is 01:25 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.