Aritmetic

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bputley
    New Member
    • Oct 2006
    • 10

    Aritmetic

    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.setVisi ble(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.addAction Listener(this) ;

    Reset = new JButton("Reset" ) ;
    Reset.addAction Listener(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.set Layout(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) ;






    setDefaultClose Operation(JFram e.EXIT_ON_CLOSE );
    setBounds(X_ORI GIN, Y_ORIGIN, WIDTH, HEIGHT) ;
    }

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

    if (source == Reset)
    {
    ChickenField.se tText(" ") ;
    PorkField.setTe xt(" ") ;
    CattleField.set Text(" ") ;
    }
    else if (source == Total)
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by bputley
    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.setVisi ble(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.addAction Listener(this) ;

    Reset = new JButton("Reset" ) ;
    Reset.addAction Listener(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.set Layout(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) ;






    setDefaultClose Operation(JFram e.EXIT_ON_CLOSE );
    setBounds(X_ORI GIN, Y_ORIGIN, WIDTH, HEIGHT) ;
    }

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

    if (source == Reset)
    {
    ChickenField.se tText(" ") ;
    PorkField.setTe xt(" ") ;
    CattleField.set Text(" ") ;
    }
    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

    Code:
    String porkValue = PorkField.getText();
    So where are you getting the problem?

    Comment

    • bputley
      New Member
      • Oct 2006
      • 10

      #3
      Originally posted by r035198x
      To retrieve a value stored in a JTextField use
      field.getText() ;

      eg To get the value in the PorkField JTextField use

      Code:
      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

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by bputley
        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?

        Code:
        /*
        * 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) {
        	int chicken = Integer.parseInt(ChickenField.getText());
        	int pork = Integer.parseInt(PorkField.getText());
        	int cattle = Integer.parseInt(CattleField.getText());
        
        	double chickenTotal = chicken * 1.19;
        	double porkTotal = pork * 2.17;
        	double cattleTotal = cattle * 3.27;
        	double total = chickenTotal + porkTotal + cattleTotal;
        
        	JOptionPane.showMessageDialog(this, "Total is $"+total);
        
        }
        }
        }

        Comment

        • r035198x
          MVP
          • Sep 2006
          • 13225

          #5
          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.

          Comment

          • muliyono
            New Member
            • Dec 2006
            • 3

            #6
            Hello..
            I am a newbie....
            How about you try for this....

            Code:
            import java.awt.*;
            import java.awt.event.ActionEvent;
            import java.awt.event.ActionListener;
            import javax.swing.*;
            import java.lang.NumberFormatException;
            
            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 = 180 ;
            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 ;
            private JTextField TotalField ;
            //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 ") ;
            JLabel total = new JLabel("     Total :          ");
            ChickenField = new JTextField(5) ;
            PorkField = new JTextField(5) ;
            CattleField = new JTextField(5) ;
            TotalField = new JTextField(5) ; 
            ChickenField.setText("0") ;
            PorkField.setText("0") ;
            CattleField.setText("0") ;
            TotalField.setText(" ") ;
            
            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(TotalField) ;
            contentPane.add(Total) ;
            contentPane.add(Reset) ;
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
            }
            
            public void actionPerformed(ActionEvent e)
            {
            String chicken, pork, cattle;
            float ayam=0, babi=0, sapi=0;
            int i;
            
            Object source = e.getSource() ;
            
            if (source == Reset)
            {
            ChickenField.setText("0") ;
            PorkField.setText("0") ;
            CattleField.setText("0") ;
            }
            else if (source == Total)
            {
            i = 0;
            try
            	{
            		chicken = ChickenField.getText();	
            		ayam = Float.valueOf(chicken).floatValue(); 
            	}
            	catch (Exception d)
            	{
            		JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",		
            			JOptionPane.ERROR_MESSAGE);
            		i=1;
            	}
            try
            	{
            		pork = PorkField.getText();	
            		babi = Float.valueOf(pork).floatValue(); 
            	}
            	catch (Exception d)
            	{
            		JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",		
            			JOptionPane.ERROR_MESSAGE);
            		i=1;
            	}
            try
            	{
            		cattle = CattleField.getText();	
            		sapi = Float.valueOf(cattle).floatValue(); 
            	}
            	catch (Exception d)
            	{
            		JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",		
            			JOptionPane.ERROR_MESSAGE);
            		i=1;
            	}
            if (i==0)
            {
            float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
            String tot= String.valueOf(total);
            TotalField.setText(tot) ;
            
            }
            
            }
            }
            }
            Last edited by r035198x; Dec 21 '06, 07:13 AM. Reason: code tags

            Comment

            • muliyono
              New Member
              • Dec 2006
              • 3

              #7
              Integer.parseIn t()
              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??

              Originally posted by r035198x
              Code:
              /*
              	int chicken = Integer.parseInt(ChickenField.getText());

              Comment

              • r035198x
                MVP
                • Sep 2006
                • 13225

                #8
                Originally posted by muliyono
                Hello..
                I am a newbie....
                How about you try for this....

                Code:
                import java.awt.*;
                import java.awt.event.ActionEvent;
                import java.awt.event.ActionListener;
                import javax.swing.*;
                import java.lang.NumberFormatException;
                
                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 = 180 ;
                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 ;
                private JTextField TotalField ;
                //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 ") ;
                JLabel total = new JLabel("     Total :          ");
                ChickenField = new JTextField(5) ;
                PorkField = new JTextField(5) ;
                CattleField = new JTextField(5) ;
                TotalField = new JTextField(5) ; 
                ChickenField.setText("0") ;
                PorkField.setText("0") ;
                CattleField.setText("0") ;
                TotalField.setText(" ") ;
                
                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(TotalField) ;
                contentPane.add(Total) ;
                contentPane.add(Reset) ;
                
                setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                setBounds(X_ORIGIN, Y_ORIGIN, WIDTH, HEIGHT) ;
                }
                
                public void actionPerformed(ActionEvent e)
                {
                String chicken, pork, cattle;
                float ayam=0, babi=0, sapi=0;
                int i;
                
                Object source = e.getSource() ;
                
                if (source == Reset)
                {
                ChickenField.setText("0") ;
                PorkField.setText("0") ;
                CattleField.setText("0") ;
                }
                else if (source == Total)
                {
                i = 0;
                try
                	{
                		chicken = ChickenField.getText();	
                		ayam = Float.valueOf(chicken).floatValue(); 
                	}
                	catch (Exception d)
                	{
                		JOptionPane.showMessageDialog(null, "Quantity of Chicken is not valid...", "Error",		
                			JOptionPane.ERROR_MESSAGE);
                		i=1;
                	}
                try
                	{
                		pork = PorkField.getText();	
                		babi = Float.valueOf(pork).floatValue(); 
                	}
                	catch (Exception d)
                	{
                		JOptionPane.showMessageDialog(null, "Quantity of Pork is not valid...", "Error",		
                			JOptionPane.ERROR_MESSAGE);
                		i=1;
                	}
                try
                	{
                		cattle = CattleField.getText();	
                		sapi = Float.valueOf(cattle).floatValue(); 
                	}
                	catch (Exception d)
                	{
                		JOptionPane.showMessageDialog(null, "Quantity of Cattle is not valid...", "Error",		
                			JOptionPane.ERROR_MESSAGE);
                		i=1;
                	}
                if (i==0)
                {
                float total = ayam * 1.19f + babi * 2.17f + sapi * 3.27f ;
                String tot= String.valueOf(total);
                TotalField.setText(tot) ;
                
                }
                
                }
                }
                }
                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.

                Comment

                • bputley
                  New Member
                  • Oct 2006
                  • 10

                  #9
                  Thanks that works perfectly, I appreciate your help

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by bputley
                    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.

                    Comment

                    Working...