Guys I need help in Java about what is the code for exponent.

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2008
Posts: 5
Reputation: ocreds has a little shameless behaviour in the past 
Solved Threads: 0
ocreds ocreds is offline Offline
Newbie Poster

Guys I need help in Java about what is the code for exponent.

 
0
  #1
Aug 31st, 2008
I try add this code(below) after "else if(token.equals("*"))" in PostfixEvaluator.java but it doesn't work like exponent. Can you help me to figure it out what is the right code for exponent.


  1. else if(token.equals("^"))
  2. {
  3. val1 = Integer.parseInt(stack.pop().toString());
  4. val2 = Integer.parseInt(stack.pop().toString());
  5.  
  6. result = val2 ^ val1;
  7.  
  8. stack.push(new Integer(result));
  9. }


And lastly, Can you point to me which part of the code where I can see the code in "spacing",
like: 1_2_3_*_+ where "_" is spacing or simply 1 2 3 * +.
Why the program understands the spacing? Which part of the code?



Thanks for reading...


Here's the entire code:


  1. import javax.swing.*;
  2.  
  3. public class StackArray
  4. {
  5. private Object stack[];
  6. private int sizeIndex = 5;
  7. private int topPtr;
  8.  
  9. public StackArray()
  10. {
  11. stack = new Object[sizeIndex];
  12. }
  13.  
  14. public void push(Object stackItem)
  15. {
  16. if(!isFull())
  17. {
  18. stack[topPtr] = stackItem;
  19. topPtr++;
  20. }
  21. else
  22. {
  23. JOptionPane.showMessageDialog(null,"Stack is Full!","Error",JOptionPane.ERROR_MESSAGE);
  24.  
  25. }
  26. }
  27.  
  28. public Object pop()
  29. {
  30. Object x = null;
  31. if(!isEmpty())
  32. {
  33. x = stack[--topPtr];
  34. stack[topPtr] = null;
  35. }
  36. return x;
  37. }
  38.  
  39. public Object top()
  40. {
  41. Object ts = null;
  42. if(!isEmpty())
  43. {
  44. ts = stack[topPtr - 1];
  45. }
  46. return ts;
  47. }
  48.  
  49. public boolean isEmpty()
  50. {
  51. return topPtr == 0;
  52. }
  53.  
  54. public boolean isFull()
  55. {
  56. return topPtr == sizeIndex;
  57. }
  58.  
  59.  
  60. public boolean contains(Object stackItem)
  61. {
  62. boolean contains = false;
  63. if(!isEmpty())
  64. {
  65. for(int i = 0; i < sizeIndex; i++)
  66. {
  67. if(stack[i].equals(stackItem))
  68. {
  69. contains = true;
  70. break;
  71. }
  72. }
  73. }
  74. return contains;
  75. }
  76.  
  77. public String toString()
  78. {
  79. StringBuffer display = new StringBuffer();
  80.  
  81. display.append("{");
  82. for(int i = 0; i< sizeIndex; i++)
  83. {
  84. if(stack[i] != null)
  85. {
  86. display.append(stack[i]);
  87. if( i < topPtr - 1)
  88. {
  89. display.append(",");
  90. }
  91. }
  92. }
  93.  
  94. display.append("}");
  95. return display.toString();
  96. }
  97. }


=========================================

  1.  
  2. public class Numeral
  3. {
  4. public static boolean isOperand(String o)
  5. {
  6. boolean operand = true; //why boolean and why true?
  7. for(int i = 0; i < o.length(); i++) //purpose of this: Why o.length and i++?
  8. {
  9. char c = o.charAt(i);
  10. if(!Character.isDigit(c)) //purpose of this and how to read this?
  11. {
  12. operand = false; //Why operand is false?
  13. break;
  14. }
  15. }
  16. return operand; //Why return to operand?
  17. }
  18.  
  19. public static boolean isOperator(String o)
  20. {
  21. return(o.equals("+") || o.equals("*") || //Why return to this?
  22. o.equals("-") || o.equals("/"));
  23. }
  24. }



==========================================

  1.  
  2.  
  3. import javax.swing.*;
  4. import java.util.*;
  5.  
  6. public class PostfixEvaluator
  7. {
  8.  
  9. StackArray stack = new StackArray();
  10. StringTokenizer tok;
  11.  
  12. private int val1,val2;
  13. private int result;
  14.  
  15. public PostfixEvaluator(String postfix)
  16. {
  17. tok = new StringTokenizer(postfix);
  18.  
  19. while(tok.hasMoreElements())
  20. {
  21. String token = tok.nextToken();
  22. if(Numeral.isOperand(token))
  23. {
  24. stack.push(token);
  25. }
  26.  
  27. else if (token.equals("a") || token.equals("b") || token.equals("c"))
  28. {
  29. JOptionPane.showMessageDialog(null,"Invalid Input","Error",JOptionPane.ERROR_MESSAGE);
  30. }
  31. else if(Numeral.isOperator(token))
  32. {
  33. if(token.equals("+"))
  34. {
  35. val1 = Integer.parseInt(stack.pop().toString());
  36. val2 = Integer.parseInt(stack.pop().toString());
  37.  
  38. result = val1 + val2;
  39.  
  40. stack.push(new Integer(result));
  41. }
  42. else if(token.equals("-"))
  43. {
  44. val1 = Integer.parseInt(stack.pop().toString());
  45. val2 = Integer.parseInt(stack.pop().toString());
  46.  
  47. result = val2 - val1;
  48.  
  49. stack.push(new Integer(result));
  50. }
  51. else if(token.equals("*"))
  52. {
  53. val1 = Integer.parseInt(stack.pop().toString());
  54. val2 = Integer.parseInt(stack.pop().toString());
  55.  
  56. result = val1 * val2;
  57.  
  58. stack.push(new Integer(result));
  59. }
  60. else if(token.equals("/"))
  61. {
  62. val1 = Integer.parseInt(stack.pop().toString());
  63. val2 = Integer.parseInt(stack.pop().toString());
  64.  
  65. result = val2 / val1;
  66.  
  67. stack.push(new Integer(result));
  68. }
  69.  
  70. }
  71. }
  72. }
  73.  
  74. public String getResult()
  75. {
  76. return String.valueOf(stack.top());
  77. }
  78. }




===========================================


This is the runner.

  1.  
  2. This is the runner.
  3.  
  4.  
  5.  
  6.  
  7.  
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.*;
  11.  
  12. public class Postfix
  13. {
  14.  
  15. JTextField TxtPostfix = new JTextField(15);
  16. JTextField TxtResult = new JTextField(15);
  17.  
  18. private String inputString = null;
  19. private JFrame frame;
  20. private JLabel label;
  21. private JPanel boxLayout,panel1,panel2,panel3;
  22.  
  23. JButton eval = new JButton("Evaluate");
  24. JButton clear = new JButton("Clear");
  25. JButton sample = new JButton("Ex. 2 3 4 * + ");
  26.  
  27. private Font font;
  28.  
  29. public Postfix()
  30. {
  31. boxLayout = new JPanel();
  32.  
  33. font = new Font("Serif",Font.BOLD,12);
  34.  
  35. //For postfix notation
  36. panel1 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  37. panel1.add(label = new JLabel("Postfix : "));
  38. panel1.add(TxtPostfix);
  39.  
  40.  
  41. label.setFont(font);
  42. label.setPreferredSize(new Dimension(49,16));
  43. label.setHorizontalAlignment(JLabel.RIGHT);
  44.  
  45.  
  46. panel2 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  47. panel2.add(label = new JLabel("Result : "));
  48. panel2.add(TxtResult);
  49.  
  50. panel3 = new JPanel(new FlowLayout(FlowLayout.CENTER,5,0));
  51. panel3.add(eval);
  52. panel3.add(clear);
  53. panel3.add(sample);
  54.  
  55.  
  56. label.setFont(font);
  57. label.setForeground(Color.red);
  58. label.setPreferredSize(new Dimension(49,16));
  59. label.setHorizontalAlignment(JLabel.RIGHT);
  60.  
  61. TxtResult.setEditable(false);
  62. }
  63.  
  64. public Container createContent()
  65. {
  66.  
  67. eval.addActionListener ( new ActionListener () {
  68. public void actionPerformed ( ActionEvent e) {
  69.  
  70. PostfixEvaluator p;
  71. p = new PostfixEvaluator(TxtPostfix.getText());
  72.  
  73. TxtResult.setText(p.getResult());
  74.  
  75. }
  76. });
  77.  
  78. clear.addActionListener ( new ActionListener () {
  79. public void actionPerformed ( ActionEvent e) {
  80. TxtPostfix.setText("");
  81. TxtResult.setText("");
  82.  
  83. }
  84. });
  85.  
  86. sample.addActionListener ( new ActionListener () {
  87. public void actionPerformed ( ActionEvent e) {
  88. TxtPostfix.setText("2 3 4 * +");
  89.  
  90.  
  91. }
  92. });
  93.  
  94.  
  95. boxLayout.setOpaque(true);
  96.  
  97. boxLayout.add(panel1);
  98. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  99. boxLayout.add(panel2);
  100. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  101. boxLayout.add(panel3);
  102. boxLayout.add(Box.createRigidArea(new Dimension(0,5)));
  103.  
  104.  
  105. JPanel layoutPanel = new JPanel();
  106.  
  107. layoutPanel.add(boxLayout);
  108.  
  109. return layoutPanel;
  110. }
  111.  
  112. public void showGUI()
  113. {
  114.  
  115. frame = new JFrame("Midterm Project");
  116. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  117. frame.setContentPane(createContent());
  118.  
  119. frame.pack();
  120. frame.setVisible(true);
  121. frame.setResizable(false);
  122.  
  123. }
  124.  
  125. public static void main(String[] args)
  126. {
  127. Runnable doRun = new Runnable(){
  128. public void run()
  129. {
  130. new Postfix().showGUI();
  131. }
  132. };
  133. javax.swing.SwingUtilities.invokeLater(doRun);
  134. }
  135.  
  136.  
  137. }
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 795
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Guys I need help in Java about what is the code for exponent.

 
0
  #2
Aug 31st, 2008
Hi ocreds,

In order to raise a number to a certain power, you need to use the Math.pow(double, double) method. The documentation on it is found here in the Math class. Basically it accepts two numbers as parameters and returns the first number raised to the power of the second number. Notice that they are doubles though - I'll leave it up to you to figure out how to use that method with integers.

I'm not sure what you mean by your spacing question, maybe someone else can answer that one.
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Guys I need help in Java about what is the code for exponent.

 
-1
  #3
Aug 31st, 2008
Just to provide direct link to Math.pow() method as mentioned by darkang
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 795
Reputation: darkagn has a spectacular aura about darkagn has a spectacular aura about darkagn has a spectacular aura about 
Solved Threads: 110
darkagn's Avatar
darkagn darkagn is offline Offline
Master Poster

Re: Guys I need help in Java about what is the code for exponent.

 
0
  #4
Sep 1st, 2008
Ah, thanks peter - my mistake!
There are no stupid questions, only those too stupid to ask for help.
echo is a web developer's best friend.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 4,208
Reputation: peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of peter_budo has much to be proud of 
Solved Threads: 486
Moderator
Featured Poster
peter_budo's Avatar
peter_budo peter_budo is offline Offline
Code tags enforcer

Re: Guys I need help in Java about what is the code for exponent.

 
-1
  #5
Sep 1st, 2008
No mistake, you pointed in right direction and that matters
Learn to see in another's calamity the ills which you should avoid.
Publilius Syrus
(~100 BC)

LJC - London Java Community, Graduate & Undergraduate Software Development Community, JAVAWUG (Java Web User Group), The London Android Group
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC