Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 07-05-2008, 06:33 PM
Member
 
Join Date: Jul 2008
Posts: 2
clark_sandy is on a distinguished road
Calculator Problem. Thanks for helping! ^^
greetings programmers. . can u help me with this project of mine?

it needs a pemdas and mdas rule . but i dont know how to put it.

can u please help me with this?

i really appreciate ur help. thanks guys. .

God bless


import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.util.*;

public class MDAS {

private String inputString = null;

JLabel expression = new JLabel("Expression: ");
JLabel result = new JLabel("Result: ");
JTextField expression_textf = new JTextField(20);
JTextField result_textf = new JTextField(10);


JButton calculate = new JButton(" Calculate");
JButton clear = new JButton(" Clear ");
JButton cancel = new JButton (" Cancel ");

//text functions
public JPanel text_2() {


JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();

Panel1.setLayout(new GridLayout(4,4));
Panel1.add(expression);
Panel1.add(expression_textf);
Panel1.add(result);
Panel1.add(result_textf);
result_textf.setEnabled(false);
result_textf.setFont(new Font("Comic Sans Ms", Font.PLAIN, 18));
Panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
Panel2.add(Panel1);

return Panel2;

}
//Button functions
public JPanel buttons_3(){

clear.addActionListener ( new ActionListener () {
public void actionPerformed (ActionEvent e) {

clear();
}
});

calculate.addActionListener ( new ActionListener () {
public void actionPerformed (ActionEvent e) {

process();
}
});
cancel.addActionListener ( new ActionListener () {
public void actionPerformed (ActionEvent e) {

process();
}
});


JPanel Panel1 = new JPanel();
JPanel Panel2 = new JPanel();
Panel1.setLayout(new GridLayout(5, 6));


Panel2.add(calculate);
Panel2.add(clear);
Panel2.add(cancel);

Panel2.setLayout(new FlowLayout(FlowLayout.RIGHT));
Panel2.add(Panel1);

return Panel2;


}

//Processing and display output
protected void process()
{
// get the input string
inputString = expression_textf.getText();

// Out the string
inputString = inputString.trim();

// Ensure there are characters to process
if (inputString.length() > 0)
// check syntax, evaluate and display results if correct
if ( checkSyntax() )
result_textf.setText( ""+ evaluate() );

}

protected boolean checkSyntax()
{
int opCount = 1; // Start with a value
boolean hasDec = false; // Does the current # have a decimal component?
int i;

// Check each character
for (i = 0; i < inputString.length(); i++)
switch(inputString.charAt(i))
{
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':

opCount = 0;
break;
case '.':
if (((opCount += 2) > 2) || hasDec)

hasDec = true;
break;
case '%':
case '/':
case '*':
if (opCount++ > 0)

hasDec = false;
break;
case '%':
case '+':
case '-':
if (opCount++ > 1)

hasDec = false;
break;
default:
return reportError(i);
}

// Make sure expression ends with a value

return true;
}

protected double evaluate()
{
// Temp token handle
String tok1, tok2;

// Create a string tokenizer object, keeping the delimiters as tokens
StringTokenizer tokens = new StringTokenizer( inputString, "+-/*", true );

// get the first token and put its value into result
if ((tok1 = tokens.nextToken()).equals("*"))
tok1 = tokens.nextToken();
else if (tok1.equals("/"))
tok1 = "-".concat(tokens.nextToken());
else if (tok1.equals("+"))
tok1 = "-".concat(tokens.nextToken());
else if (tok1.equals("-"))
tok1 = "-".concat(tokens.nextToken());
else if (tok1.equals("%"))
tok1 = "%".concat(tokens.nextToken());
double result = new Double(tok1).doubleValue();

// Evaluate the expression
while (tokens.hasMoreTokens())
{
// Get the operator token
tok1 = tokens.nextToken();

// Get the second operand token
if ((tok2 = tokens.nextToken()).equals("*"))
tok2 = tokens.nextToken();
else if (tok2.equals("/"))
tok2 = "-".concat(tokens.nextToken());
else if (tok2.equals("+"))
tok2 = "-".concat(tokens.nextToken());
else if (tok2.equals("-"))
tok2 = "-".concat(tokens.nextToken());
else if (tok2.equals("%"))
tok2 = "%".concat(tokens.nextToken());

// Evaluate the subexpression
switch (tok1.charAt(0))
{
case '+':
result += new Double(tok2).doubleValue();
break;
case '-':
result -= new Double(tok2).doubleValue();
break;
case '/':
result /= new Double(tok2).doubleValue();
break;
case '*':
result *= new Double(tok2).doubleValue();
break;
case '%':
result %= new Double(tok2).doubleValue();
break;
}
}
return result;
}
public void clear() {

expression_textf.setText("");
result_textf.setText("");

}
protected boolean reportError( int index )
{
// create a new string with the first line of output
String outputString = new String("Invalid Number\n");


// copy the output string text to the output text area
result_textf.setText( outputString );

return false;
}

public JFrame buildNewFrame() {

try{
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());


}catch(Exception e){}

MDAS re = new MDAS();

JPanel nPanel = new JPanel();
JPanel sPanel = new JPanel();
JPanel fPanel = new JPanel();

nPanel = re.text_2 ();
sPanel = re.buttons_3 ();


fPanel.setLayout( new BorderLayout());
fPanel.add(nPanel, BorderLayout.CENTER);
fPanel.add(sPanel, BorderLayout.SOUTH);


JFrame frame = new JFrame("MDAS");

frame.setSize(50,50);
frame.getContentPane().add(fPanel);

frame.addWindowListener( new WindowAdapter() {

public void windowClosing( WindowEvent e) {

System.exit(0);
}
});

return frame;

}

}


















/// runner ///

import javax.swing.JFrame;

public class MDAS_Runner {

public static void main(String args[]){

JFrame run = new JFrame();
MDAS rece = new MDAS ();
run = rece.buildNewFrame();

run.pack();
run.setVisible(true);

}

}
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-06-2008, 12:36 AM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
Could define: pemdas and mdas rule
and how that relates to java programming?
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 07-06-2008, 05:00 AM
Member
 
Join Date: Jul 2008
Posts: 2
clark_sandy is on a distinguished road
^^
well. . i got the calculator working but its not following the mdas rule. u know wen i type 5-1*8-9 , the answer would be -12 by follwing the mdas rule. but in my calculator program, the result will be 23.

Does anyone have any suggestions? thanks for the reply. .

God bless
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 07-06-2008, 05:01 PM
Norm's Avatar
Senior Member
 
Join Date: Jun 2008
Location: SW MO, USA
Posts: 945
Norm is on a distinguished road
What is the mdas rule?
Does it relate to operator precedence?
Your code will have to parse the expression and do the operations in the order of precedence. There are various methods for doing that. A simple one would be to scan the expression for the highest level operation, pick up its operands, do the operation and replace the operands and operation with the results and do it again until there are no operations left to do.
It looks like your code is doing the operations from left to right.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
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

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
Java Calculator aapanju New To Java 3 04-17-2008 06:33 AM
Java calculator decimal cart1443 New To Java 2 04-16-2008 02:19 PM
calculator not working Renegade85 New To Java 5 03-10-2008 04:27 PM
Create a Calculator in Java Albert New To Java 2 07-04-2007 09:01 AM
Swing Calculator nemo AWT / Swing 1 05-28-2007 12:07 PM


All times are GMT +3. The time now is 10:08 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org