By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Aritmetic

P: 10
I am having trouble writing code to multiply the fields times the number put in to equal the total. I was able to make the clear button work

/*
* Order1.java
*
* Created on December 18, 2006, 6:16 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author test
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Order1
{
public static void main(String[] args)
{
MyOrder1 myFrame = new MyOrder1() ;

myFrame.setVisible(true) ;
}
}


class MyOrder1 extends JFrame implements ActionListener
{
private static final int WIDTH = 225 ;
private static final int HEIGHT = 150 ;
private static final int X_ORIGIN = 400 ;
private static final int Y_ORIGIN = 200 ;

// create new buttons and label them
private JButton Total ;
private JButton Reset ;
private JTextField ChickenField ;
private JTextField PorkField ;
private JTextField CattleField ;

//creates new label icons for my items
public MyOrder1()
{


String Response = new String() ;
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
JLabel Pork = new JLabel("Pork: $2.17 LB ") ;
JLabel Cattle = new JLabel("Cattle:$3.27 LB ") ;
ChickenField = new JTextField(5) ;
PorkField = new JTextField(5) ;
CattleField = new JTextField(5) ;

Total = new JButton("Total") ;
Total.addActionListener(this) ;

Reset = new JButton("Reset") ;
Reset.addActionListener(this) ;



// get the content pane
Container contentPane = getContentPane() ;

// create a new layout manager for the pane
FlowLayout aLayout = new FlowLayout() ;

// assign the new layout manager to the content pane
contentPane.setLayout(aLayout) ;

// add the "Exit" button to the content pane
contentPane.add(Chicken) ;
contentPane.add(ChickenField) ;
contentPane.add(Pork) ;
contentPane.add(PorkField) ;
contentPane.add(Cattle) ;
contentPane.add(CattleField) ;
contentPane.add(Total) ;
contentPane.add(Reset) ;






setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
}

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource() ;

if (source == Reset)
{
ChickenField.setText(" ") ;
PorkField.setText(" ") ;
CattleField.setText(" ") ;
}
else if (source == Total)
Dec 20 '06 #1
Share this Question
Share on Google+
9 Replies


10K+
P: 13,264
I am having trouble writing code to multiply the fields times the number put in to equal the total. I was able to make the clear button work

/*
* Order1.java
*
* Created on December 18, 2006, 6:16 PM
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/

/**
*
* @author test
*/
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;

public class Order1
{
public static void main(String[] args)
{
MyOrder1 myFrame = new MyOrder1() ;

myFrame.setVisible(true) ;
}
}


class MyOrder1 extends JFrame implements ActionListener
{
private static final int WIDTH = 225 ;
private static final int HEIGHT = 150 ;
private static final int X_ORIGIN = 400 ;
private static final int Y_ORIGIN = 200 ;

// create new buttons and label them
private JButton Total ;
private JButton Reset ;
private JTextField ChickenField ;
private JTextField PorkField ;
private JTextField CattleField ;

//creates new label icons for my items
public MyOrder1()
{


String Response = new String() ;
JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
JLabel Pork = new JLabel("Pork: $2.17 LB ") ;
JLabel Cattle = new JLabel("Cattle:$3.27 LB ") ;
ChickenField = new JTextField(5) ;
PorkField = new JTextField(5) ;
CattleField = new JTextField(5) ;

Total = new JButton("Total") ;
Total.addActionListener(this) ;

Reset = new JButton("Reset") ;
Reset.addActionListener(this) ;



// get the content pane
Container contentPane = getContentPane() ;

// create a new layout manager for the pane
FlowLayout aLayout = new FlowLayout() ;

// assign the new layout manager to the content pane
contentPane.setLayout(aLayout) ;

// add the "Exit" button to the content pane
contentPane.add(Chicken) ;
contentPane.add(ChickenField) ;
contentPane.add(Pork) ;
contentPane.add(PorkField) ;
contentPane.add(Cattle) ;
contentPane.add(CattleField) ;
contentPane.add(Total) ;
contentPane.add(Reset) ;






setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
}

public void actionPerformed(ActionEvent e)
{
Object source = e.getSource() ;

if (source == Reset)
{
ChickenField.setText(" ") ;
PorkField.setText(" ") ;
CattleField.setText(" ") ;
}
else if (source == Total)

To retrieve a value stored in a JTextField use
field.getText();

eg To get the value in the PorkField JTextField use

Expand|Select|Wrap|Line Numbers
  1. String porkValue = PorkField.getText(); 
So where are you getting the problem?
Dec 21 '06 #2

P: 10
To retrieve a value stored in a JTextField use
field.getText();

eg To get the value in the PorkField JTextField use

Expand|Select|Wrap|Line Numbers
  1. String porkValue = PorkField.getText(); 
So where are you getting the problem?

I am having trouble putting in correct code to multiply the dollar amount times how many selected so when I hit the total button the dollar amount will show depending on how many items I put in the selection whether it is pork chicken or cattle
Dec 21 '06 #3

10K+
P: 13,264
I am having trouble putting in correct code to multiply the dollar amount times how many selected so when I hit the total button the dollar amount will show depending on how many items I put in the selection whether it is pork chicken or cattle


Is the maths correct here?

Expand|Select|Wrap|Line Numbers
  1. /*
  2. * Order1.java
  3. *
  4. * Created on December 18, 2006, 6:16 PM
  5. *
  6. * To change this template, choose Tools | Template Manager
  7. * and open the template in the editor.
  8. */
  9.  
  10. /**
  11. *
  12. * @author test
  13. */
  14. import java.awt.*;
  15. import java.awt.event.ActionEvent;
  16. import java.awt.event.ActionListener;
  17. import javax.swing.*;
  18.  
  19. public class Order1
  20. {
  21. public static void main(String[] args)
  22. {
  23. MyOrder1 myFrame = new MyOrder1() ;
  24.  
  25. myFrame.setVisible(true) ;
  26. }
  27. }
  28.  
  29.  
  30. class MyOrder1 extends JFrame implements ActionListener
  31. {
  32. private static final int WIDTH = 225 ;
  33. private static final int HEIGHT = 150 ;
  34. private static final int X_ORIGIN = 400 ;
  35. private static final int Y_ORIGIN = 200 ;
  36.  
  37. // create new buttons and label them
  38. private JButton Total ;
  39. private JButton Reset ;
  40. private JTextField ChickenField ;
  41. private JTextField PorkField ;
  42. private JTextField CattleField ;
  43.  
  44. //creates new label icons for my items
  45. public MyOrder1()
  46. {
  47.  
  48.  
  49. String Response = new String() ;
  50. JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
  51. JLabel Pork = new JLabel("Pork: $2.17 LB ") ;
  52. JLabel Cattle = new JLabel("Cattle:$3.27 LB ") ;
  53. ChickenField = new JTextField(5) ;
  54. PorkField = new JTextField(5) ;
  55. CattleField = new JTextField(5) ;
  56.  
  57. Total = new JButton("Total") ;
  58. Total.addActionListener(this) ;
  59.  
  60. Reset = new JButton("Reset") ;
  61. Reset.addActionListener(this) ;
  62.  
  63.  
  64.  
  65. // get the content pane
  66. Container contentPane = getContentPane() ;
  67.  
  68. // create a new layout manager for the pane
  69. FlowLayout aLayout = new FlowLayout() ;
  70.  
  71. // assign the new layout manager to the content pane
  72. contentPane.setLayout(aLayout) ;
  73.  
  74. // add the "Exit" button to the content pane
  75. contentPane.add(Chicken) ;
  76. contentPane.add(ChickenField) ;
  77. contentPane.add(Pork) ;
  78. contentPane.add(PorkField) ;
  79. contentPane.add(Cattle) ;
  80. contentPane.add(CattleField) ;
  81. contentPane.add(Total) ;
  82. contentPane.add(Reset) ;
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  90. setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
  91. }
  92.  
  93. public void actionPerformed(ActionEvent e)
  94. {
  95. Object source = e.getSource() ;
  96.  
  97. if (source == Reset)
  98. {
  99. ChickenField.setText(" ") ;
  100. PorkField.setText(" ") ;
  101. CattleField.setText(" ") ;
  102. }
  103. else if (source == Total) {
  104.     int chicken = Integer.parseInt(ChickenField.getText());
  105.     int pork = Integer.parseInt(PorkField.getText());
  106.     int cattle = Integer.parseInt(CattleField.getText());
  107.  
  108.     double chickenTotal = chicken * 1.19;
  109.     double porkTotal = pork * 2.17;
  110.     double cattleTotal = cattle * 3.27;
  111.     double total = chickenTotal + porkTotal + cattleTotal;
  112.  
  113.     JOptionPane.showMessageDialog(this, "Total is $"+total);
  114.  
  115. }
  116. }
  117. }
Dec 21 '06 #4

10K+
P: 13,264
Hi, there are still some things that need to be worked on in your code. So if the maths part is correct confirm so that we can do the next part.
Dec 21 '06 #5

P: 3
Hello..
I am a newbie....
How about you try for this....

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5. import java.lang.NumberFormatException;
  6.  
  7. public class Order1
  8. {
  9. public static void main(String[] args)
  10. {
  11. MyOrder1 myFrame = new MyOrder1() ;
  12.  
  13. myFrame.setVisible(true) ;
  14. }
  15. }
  16.  
  17. class MyOrder1 extends JFrame implements ActionListener
  18. {
  19. private static final int WIDTH = 225 ;
  20. private static final int HEIGHT = 180 ;
  21. private static final int X_ORIGIN = 400 ;
  22. private static final int Y_ORIGIN = 200 ;
  23.  
  24. // create new buttons and label them
  25. private JButton Total ;
  26. private JButton Reset ;
  27. private JTextField ChickenField ;
  28. private JTextField PorkField ;
  29. private JTextField CattleField ;
  30. private JTextField TotalField ;
  31. //creates new label icons for my items
  32. public MyOrder1()
  33. {
  34.  
  35.  
  36. String Response = new String() ;
  37. JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
  38. JLabel Pork = new JLabel   ("     Pork: $2.17 LB ") ;
  39. JLabel Cattle = new JLabel("   Cattle: $3.27 LB ") ;
  40. JLabel total = new JLabel("     Total :          ");
  41. ChickenField = new JTextField(5) ;
  42. PorkField = new JTextField(5) ;
  43. CattleField = new JTextField(5) ;
  44. TotalField = new JTextField(5) ; 
  45. ChickenField.setText("0") ;
  46. PorkField.setText("0") ;
  47. CattleField.setText("0") ;
  48. TotalField.setText(" ") ;
  49.  
  50. Total = new JButton("Total") ;
  51. Total.addActionListener(this) ;
  52.  
  53. Reset = new JButton("Reset") ;
  54. Reset.addActionListener(this) ;
  55.  
  56.  
  57.  
  58. // get the content pane
  59. Container contentPane = getContentPane() ;
  60.  
  61. // create a new layout manager for the pane
  62. FlowLayout aLayout = new FlowLayout() ;
  63.  
  64. // assign the new layout manager to the content pane
  65. contentPane.setLayout(aLayout) ;
  66.  
  67. // add the "Exit" button to the content pane
  68. contentPane.add(Chicken) ;
  69. contentPane.add(ChickenField) ;
  70. contentPane.add(Pork) ;
  71. contentPane.add(PorkField) ;
  72. contentPane.add(Cattle) ;
  73. contentPane.add(CattleField) ;
  74. contentPane.add(total) ;
  75. contentPane.add(TotalField) ;
  76. contentPane.add(Total) ;
  77. contentPane.add(Reset) ;
  78.  
  79. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80. setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
  81. }
  82.  
  83. public void actionPerformed(ActionEvent e)
  84. {
  85. String chicken, pork, cattle;
  86. float ayam=0, babi=0, sapi=0;
  87. int i;
  88.  
  89. Object source = e.getSource() ;
  90.  
  91. if (source == Reset)
  92. {
  93. ChickenField.setText("0") ;
  94. PorkField.setText("0") ;
  95. CattleField.setText("0") ;
  96. }
  97. else if (source == Total)
  98. {
  99. i = 0;
  100. try
  101.     {
  102.         chicken = ChickenField.getText();    
  103.         ayam = Float.valueOf(chicken).floatValue(); 
  104.     }
  105.     catch (Exception d)
  106.     {
  107.         JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",        
  108.             JOptionPane.ERROR_MESSAGE);
  109.         i=1;
  110.     }
  111. try
  112.     {
  113.         pork = PorkField.getText();    
  114.         babi = Float.valueOf(pork).floatValue(); 
  115.     }
  116.     catch (Exception d)
  117.     {
  118.         JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",        
  119.             JOptionPane.ERROR_MESSAGE);
  120.         i=1;
  121.     }
  122. try
  123.     {
  124.         cattle = CattleField.getText();    
  125.         sapi = Float.valueOf(cattle).floatValue(); 
  126.     }
  127.     catch (Exception d)
  128.     {
  129.         JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",        
  130.             JOptionPane.ERROR_MESSAGE);
  131.         i=1;
  132.     }
  133. if (i==0)
  134. {
  135. float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
  136. String tot= String.valueOf(total);
  137. TotalField.setText(tot) ;
  138.  
  139. }
  140.  
  141. }
  142. }
  143. }
Dec 21 '06 #6

P: 3
Integer.parseInt()
What is the function of it ?
I never use it before
Is it use to convert the string to integer?
And how about if the string is alphabet ? Do we need exception to make sure it doesn't error??

Expand|Select|Wrap|Line Numbers
  1. /*
  2.     int chicken = Integer.parseInt(ChickenField.getText());
  3.  
Dec 21 '06 #7

10K+
P: 13,264
Hello..
I am a newbie....
How about you try for this....

Expand|Select|Wrap|Line Numbers
  1. import java.awt.*;
  2. import java.awt.event.ActionEvent;
  3. import java.awt.event.ActionListener;
  4. import javax.swing.*;
  5. import java.lang.NumberFormatException;
  6.  
  7. public class Order1
  8. {
  9. public static void main(String[] args)
  10. {
  11. MyOrder1 myFrame = new MyOrder1() ;
  12.  
  13. myFrame.setVisible(true) ;
  14. }
  15. }
  16.  
  17. class MyOrder1 extends JFrame implements ActionListener
  18. {
  19. private static final int WIDTH = 225 ;
  20. private static final int HEIGHT = 180 ;
  21. private static final int X_ORIGIN = 400 ;
  22. private static final int Y_ORIGIN = 200 ;
  23.  
  24. // create new buttons and label them
  25. private JButton Total ;
  26. private JButton Reset ;
  27. private JTextField ChickenField ;
  28. private JTextField PorkField ;
  29. private JTextField CattleField ;
  30. private JTextField TotalField ;
  31. //creates new label icons for my items
  32. public MyOrder1()
  33. {
  34.  
  35.  
  36. String Response = new String() ;
  37. JLabel Chicken = new JLabel("Chicken: $1.19 LB ") ;
  38. JLabel Pork = new JLabel   ("     Pork: $2.17 LB ") ;
  39. JLabel Cattle = new JLabel("   Cattle: $3.27 LB ") ;
  40. JLabel total = new JLabel("     Total :          ");
  41. ChickenField = new JTextField(5) ;
  42. PorkField = new JTextField(5) ;
  43. CattleField = new JTextField(5) ;
  44. TotalField = new JTextField(5) ; 
  45. ChickenField.setText("0") ;
  46. PorkField.setText("0") ;
  47. CattleField.setText("0") ;
  48. TotalField.setText(" ") ;
  49.  
  50. Total = new JButton("Total") ;
  51. Total.addActionListener(this) ;
  52.  
  53. Reset = new JButton("Reset") ;
  54. Reset.addActionListener(this) ;
  55.  
  56.  
  57.  
  58. // get the content pane
  59. Container contentPane = getContentPane() ;
  60.  
  61. // create a new layout manager for the pane
  62. FlowLayout aLayout = new FlowLayout() ;
  63.  
  64. // assign the new layout manager to the content pane
  65. contentPane.setLayout(aLayout) ;
  66.  
  67. // add the "Exit" button to the content pane
  68. contentPane.add(Chicken) ;
  69. contentPane.add(ChickenField) ;
  70. contentPane.add(Pork) ;
  71. contentPane.add(PorkField) ;
  72. contentPane.add(Cattle) ;
  73. contentPane.add(CattleField) ;
  74. contentPane.add(total) ;
  75. contentPane.add(TotalField) ;
  76. contentPane.add(Total) ;
  77. contentPane.add(Reset) ;
  78.  
  79. setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  80. setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
  81. }
  82.  
  83. public void actionPerformed(ActionEvent e)
  84. {
  85. String chicken, pork, cattle;
  86. float ayam=0, babi=0, sapi=0;
  87. int i;
  88.  
  89. Object source = e.getSource() ;
  90.  
  91. if (source == Reset)
  92. {
  93. ChickenField.setText("0") ;
  94. PorkField.setText("0") ;
  95. CattleField.setText("0") ;
  96. }
  97. else if (source == Total)
  98. {
  99. i = 0;
  100. try
  101.     {
  102.         chicken = ChickenField.getText();    
  103.         ayam = Float.valueOf(chicken).floatValue(); 
  104.     }
  105.     catch (Exception d)
  106.     {
  107.         JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",        
  108.             JOptionPane.ERROR_MESSAGE);
  109.         i=1;
  110.     }
  111. try
  112.     {
  113.         pork = PorkField.getText();    
  114.         babi = Float.valueOf(pork).floatValue(); 
  115.     }
  116.     catch (Exception d)
  117.     {
  118.         JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",        
  119.             JOptionPane.ERROR_MESSAGE);
  120.         i=1;
  121.     }
  122. try
  123.     {
  124.         cattle = CattleField.getText();    
  125.         sapi = Float.valueOf(cattle).floatValue(); 
  126.     }
  127.     catch (Exception d)
  128.     {
  129.         JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",        
  130.             JOptionPane.ERROR_MESSAGE);
  131.         i=1;
  132.     }
  133. if (i==0)
  134. {
  135. float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
  136. String tot= String.valueOf(total);
  137. TotalField.setText(tot) ;
  138.  
  139. }
  140.  
  141. }
  142. }
  143. }
The exception handling introduced here was the next step. It now makes the program more robust. Well done and thanks muliyono. I now have only the following comments:

I would normally use double instead of float though(for starters its bigger).
It does not look right to have the total being shown in a textfield too. A JLabel seems more appropriate since the total should not be editable.
Also if the inputs are quantities then using an int would make more sense that using a float.
Dec 21 '06 #8

P: 10
Thanks that works perfectly, I appreciate your help
Dec 21 '06 #9

10K+
P: 13,264
Thanks that works perfectly, I appreciate your help
Welcome. Remember to come back and help others as well where you can.

Oh and have a merry Christmas.
Dec 21 '06 #10

Post your reply

Sign in to post your reply or Sign up for a free account.