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 July 1st, 2011, 09:45 AM
Registered User
 
Join Date: Apr 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default Lesson 10

HI guys .
Stuck at this lesson .Someone help with an explanation of a calculator´s logic
in particular "IF" statement , just a little hint please
package com.practicaljava.lesson9;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import javax.swing.JButton;

public class CalculatorEngine implements ActionListener {

Calculator parent; // a reference to the Calculator
// new variables
double storedNumber , enteredNumber ;
String selectedAction ;




// Constructor stores the reference to the
// Calculator window in the member variable parent
CalculatorEngine(Calculator parent){
this.parent = parent;
}

public void actionPerformed(ActionEvent e){
// Get the source of this action


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


String dispFieldText = parent.getDisplayValue();

String clickedButtonLabel = clickedButton.getText();

if (!dispFieldText.equals("")){

if (clickedButton.getText() == ("+") || clickedButton.getText()
== ("-")|| clickedButton.getText() == ("*") ||
clickedButton.getText() == ("/")){

// Fetching figures from calculator and making it decimal
storedNumber = Double.parseDouble(parent.getDisplayValue());
selectedAction = clickedButton.getText();
// Clean a display
parent.setDisplayValue("");


}
else if ((clickedButton.getText() == ("="))
&& (selectedAction != null)) {


if (selectedAction==("+")) {
storedNumber = storedNumber+Double.parseDouble(parent.getDisplayV alue());
parent.setDisplayValue( "" + storedNumber);
}

}
}

// Get the button's label


else{


parent.setDisplayValue( dispFieldText +clickedButtonLabel);


}

}
}
Reply With Quote
  #2 (permalink)  
Old July 3rd, 2011, 06:20 PM
Registered User
 
Join Date: Apr 2011
Posts: 8
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Definitely was tired this one is working



if (clickedButton.getText() == ("+") || clickedButton.getText()
== ("-")|| clickedButton.getText() == ("*") ||
clickedButton.getText() == ("/")){

// Fetching figures from calculator and making it decimal
storedNumber = Double.parseDouble(parent.getDisplayValue());
selectedAction = clickedButton.getText();
// Clean a display
parent.setDisplayValue("");


}
else if ((clickedButton.getText() == ("="))
&& (selectedAction != null)) {


if (selectedAction==("+")) {
storedNumber = storedNumber+Double.parseDouble(parent.getDisplayV alue());
parent.setDisplayValue( "" + storedNumber);
}

}


// Get the button's label


else {


parent.setDisplayValue( dispFieldText +clickedButtonLabel);


}

}
}
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 5 exercises. thelemur BOOK: Java Programming 24-Hour Trainer by Yakov Fain 9 March 29th, 2013 11:56 PM
Lesson 8 sergeySE BOOK: Java Programming 24-Hour Trainer by Yakov Fain 1 June 29th, 2011 09:49 AM
Lesson 6 Try It zavodney BOOK: Stephens' Visual Basic Programming 24-Hour Trainer 2 June 6th, 2011 10:07 PM
Lesson 3 question soneal777 BOOK: Stephens' C# Programming with Visual Studio 2010 24-Hour Trainer 1 August 30th, 2010 06:17 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 11:26 PM.


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