Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 13 of 13

Thread: Problem understanding Java code modification

  1. #1
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Problem understanding Java code modification

    The following is a problem that just does addition. I need to modify it to include buttons and calculation for multiplication,division and subtraction. Regarding the section that shows the addition portion of program, do i just take those same lines and,inserting the different operations, add those lines to the code after the addition or am i way off? just want to know in order to modify program to include all operations if i just simply need to add other operations after the addition or is there something i am not seeing. Thank you



    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class BasicCalculator {
    	private static final int FRAME_WIDTH = 150;
    	private static final int FRAME_HEIGHT = 120;
     
    	// Keeps track of the current operation (subtract, add, etc)
    	private static final int NO_OPERATION = 0;
    	private static final int ADDITION = 1;
    	public static int operation = NO_OPERATION;
     
    	public static JTextField textFieldDisplay;
    	public static double Value1 = 0; // holds the value before the operation
     
    	public static void main(String[] args) {
    		// Set up the user interface
    		JFrame frame = new JFrame();
    		JPanel buttonPanel = new JPanel();
    		frame.add(buttonPanel);
     
    		// create two buttons, plus and equal and a text box for answers
    		textFieldDisplay = new JTextField(10);
    		buttonPanel.add(textFieldDisplay);
    		JButton buttonPlus = new JButton(" + ");
    		buttonPanel.add(buttonPlus);
    		JButton buttonEqual = new JButton(" = ");
    		buttonPanel.add(buttonEqual);
     
    		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		frame.setTitle("Basic Calculator");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
     
    		// called when the equal sign '=' is pressed
    		class EqualSignListener implements ActionListener {
     
    			public void actionPerformed(ActionEvent event)
    			{
    				double Value2 = Double.parseDouble(textFieldDisplay.getText());
    				if (operation == ADDITION) {
    					// plus sign pressed before the equal sign
    					Value2 += Value1;
    				}
    				// Convert from a answer to a string
    				Double answer = new Double(Value2);
    				textFieldDisplay.setText( answer.toString() );
    				// Reset the operation to show no current operation
    				operation = NO_OPERATION;
    			}
    		}
     
    		// called when a plus sign '+' is pressed
    		class PlusSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = ADDITION;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener plusSignListener = new PlusSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
     
    	}
    Last edited by helloworld922; June 25th, 2011 at 01:23 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem understanding Java code modification

    Since those operations all take two operands, you should be able to use the logic for the add with the other three operations. That would be the simplest way.
    Another more OOP technique could involve a base class and extended classes for each of the operators.
    An evaluate() method would take two operands, do the operation and return a value.
    The main program would have a HashMap to look up the class object to use with the operator as the key.


    Please enclose your posted code in code tags to preserve formatting. Use the # icon

  3. #3
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem understanding Java code modification

    So just take the information within the aditon section of the code and add it after the addition section just chnaging the adition portions to the appropriate operations?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem understanding Java code modification

    Yes that should work. The code for the four operations should be almost the same except for the operation itself.

  5. #5
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem understanding Java code modification

    How would you add next section to ensure it is in proper place? after what line?

  6. #6
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem understanding Java code modification

    Here is what i did..is this correct for my problem where I need to modify it to include buttons and calculation for multiplication,division and subtraction.?

    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
     
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
     
    public class BasicCalculator {
    	private static final int FRAME_WIDTH = 150;
    	private static final int FRAME_HEIGHT = 120;
     
    	// Keeps track of the current operation (subtract, add, etc)
    	private static final int NO_OPERATION = 0;
    	private static final int ADDITION = 1;
    	public static int operation = NO_OPERATION;
     
    	public static JTextField textFieldDisplay;
    	public static double Value1 = 0; // holds the value before the operation
     
    	public static void main(String[] args) {
    		// Set up the user interface
    		JFrame frame = new JFrame();
    		JPanel buttonPanel = new JPanel();
    		frame.add(buttonPanel);
     
    		// create two buttons, plus and equal and a text box for answers
    		textFieldDisplay = new JTextField(10);
    		buttonPanel.add(textFieldDisplay);
    		JButton buttonPlus = new JButton(" + ");
    		buttonPanel.add(buttonPlus);
    		JButton buttonEqual = new JButton(" = ");
    		buttonPanel.add(buttonEqual);
     
    		frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    		frame.setTitle("Basic Calculator");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
     
     
    		// called when the equal sign '=' is pressed
    		class EqualSignListener implements ActionListener {
     
    			public void actionPerformed(ActionEvent event)
    			{
    				double Value2 = Double.parseDouble(textFieldDisplay.getText());
    				if (operation == ADDITION) {
    					// plus sign pressed before the equal sign
    					Value2 += Value1;
    				}
    				// Convert from a answer to a string
    				Double answer = new Double(Value2);
    				textFieldDisplay.setText( answer.toString() );
    				// Reset the operation to show no current operation
    				operation = NO_OPERATION;
    			}
    		}
     
    		// called when a plus sign '+' is pressed
    		class PlusSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = ADDITION;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener plusSignListener = new PlusSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
     
    	}
    }
    // called when a subtraction sign '-' is pressed
    		class SubtractionSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = Subtraction;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener subtractionSignListener = new SubtractionSignListener();
    		buttonPlus.addActionListener(subtractionSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
             }
    )
    // called when a division sign '/' is pressed
    		class DivisionSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = DIVISION;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener divisionSignListener = new DivisionSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
              }
    ) 
    // called when a plus sign '*' is pressed
    		class MulitplicationSignListener implements ActionListener {
    			public void actionPerformed(ActionEvent event)
    			{
    				Value1 = Double.parseDouble(textFieldDisplay.getText());
    				operation = Multiplication;
    			}
    		}
     
    		// Add the methods that will be called when these buttons are pressed
    		ActionListener multiplicationSignListener = new MultiplicationSignListener();
    		buttonPlus.addActionListener(plusSignListener);
     
    		ActionListener equalSignListener = new EqualSignListener();
    		buttonEqual.addActionListener(equalSignListener);
     
    	}
    }

  7. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem understanding Java code modification

    Please enclose your posted code in code tags to preserve formatting. Use the # icon above the input area to generate the code tags. Not the # character. See: BB Code List - Java Forums
      int 1 = 1; // sample code in tags

    Does the code execute the way you want it to?
    Last edited by Norm; June 26th, 2011 at 07:04 AM.

  8. #8
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem understanding Java code modification

    class, interface, or enum expected
    ActionListener subtractionSignListener = new SubtractionSignListener();

    Getting the above mentioned errors on numerous lines,program won't compile

  9. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem understanding Java code modification

    Hope you backed up the working version.
    I would suggest that you start with the working version and make small changes to it, compile it to test for errors and then make some more small changes, compile it, etc.

    Your copying and pasting over and over picked up too much code and is FULL of errors. Go back to the working version and try again.

  10. #10
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem understanding Java code modification

    Instead of copying and pasting the similar sections. Should i just re type them? Also section that began with // called when a '-' sign is pressed.. is that correct or close? Confused on how to approach this

  11. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem understanding Java code modification

    Copy and pasting can be faster but can be a problem if you forget to change it for the new code.

    Sorry, when I searched for "// called when a '-' sign is pressed" it was not found in this post (except in your last post).

  12. #12
    Junior Member
    Join Date
    Jun 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Problem understanding Java code modification

    I was told problem could also be one too many closing braces above error lines( the first error message comes in line 94) where is the extra or mismatched brace..i cannot find it. if I can i think that may solve issues

  13. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Problem understanding Java code modification

    where is the extra or mismatched brace..i cannot find it.
    Many IDEs have tools to find matching {}s. Put the cursor on {, press the keys/icon and the cursor jumps to the matching }.

    Use this tool starting in the middle and work out, making sure that all {s are paired with a matching }

Similar Threads

  1. reformatting java code problem
    By Fordy252 in forum File I/O & Other I/O Streams
    Replies: 0
    Last Post: November 23rd, 2010, 02:44 PM
  2. unable to get values for modification
    By javaking in forum JavaServer Pages: JSP & JSTL
    Replies: 2
    Last Post: September 17th, 2010, 05:27 AM
  3. Re: Java Newbie Code Problem
    By erinbasim in forum What's Wrong With My Code?
    Replies: 1
    Last Post: January 17th, 2010, 02:05 AM
  4. [SOLVED] Java Newbie Code Problem
    By lee in forum What's Wrong With My Code?
    Replies: 6
    Last Post: January 16th, 2010, 03:05 PM
  5. Help understanding this code
    By Zepx in forum Java Theory & Questions
    Replies: 2
    Last Post: October 20th, 2009, 10:18 AM