Wrox Programmer Forums
| 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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #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);


}

}
}
  #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);


}

}
}


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





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