Results 1 to 10 of 10
  1. #1
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Exclamation Java Arithmetic Program. PLEASE HELP!

    I can't figure out how in the world to increase total, and correct if the user correct or incorrectly answers the randomly generated basic arithmetic problem. I would GREATLY appreciate any help. I know my code is wrong, but can't find anything on how to fix it :// ahh frustration. lol. THANK YOU!

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Random;
    
    public class MathPanel extends JPanel {
    	private JButton addition, subtraction, mult, divison, submit;
    	private JTextField math;
    	private JLabel welcome, question, total, correct;
        private String imageFileName = "sad.gif";
    
    	private ImageIcon icon = new ImageIcon(imageFileName);
    
    	// -----------------------------------------------------------------
    	// Constructor: Sets up the main GUI components.
    	// -----------------------------------------------------------------
    	public MathPanel() {
    
    		welcome = new JLabel("Choose the question type:");
    		question = new JLabel("Question is yet to be generated.");
    		addition = new JButton("Addition +:");
    		subtraction = new JButton("Subtraction -");
    		mult = new JButton("Multiplication *");
    		divison = new JButton("Divison /");
    		submit = new JButton("Submit");
    		correct = new JLabel("Correct:");
    		total = new JLabel("Total:");
    
    		math = new JTextField(35);
    		math.addActionListener(new TempListener());
    		addition.addActionListener(new TempListener());
    		subtraction.addActionListener(new TempListener());
    		mult.addActionListener(new TempListener());
    		divison.addActionListener(new TempListener());
    		
    		add(welcome);
    		add(addition);
    		add(subtraction);
    		add(mult);
    		add(divison);
    		add(question);
    		add(math);
    		add(submit);
    		add(correct);
    		add(total);
    		
    		setPreferredSize(new Dimension(400, 400));
    		setBackground(Color.white);
    
    	}
    
    	// *****************************************************************
    	// Represents an action listener.
    	// *****************************************************************
    	private class TempListener implements ActionListener {
    		// --------------------------------------------------------------
    		// Sets up the Question for the user to answer.
    		// --------------------------------------------------------------
    		public void actionPerformed(ActionEvent event) {
    
    
    				if (event.getSource() == addition) {
    					Random addRand = new Random();
    					int a = addRand.nextInt(10) + 1;
    					int a1 = addRand.nextInt(10) + 1;
    					question.setText(a + " + " + a1);
    
    				}
    			
    
    				if (event.getSource() == subtraction) {
    					Random subRand = new Random();
    					int s = subRand.nextInt(10) + 1;
    					int s1 = subRand.nextInt(10) + 1;
    					question.setText(s + " - " + s1);
    				}
    
    				if (event.getSource() == mult) {
    					Random multRand = new Random();
    					int x = multRand.nextInt(10) + 1;
    					int x1 = multRand.nextInt(10) + 1;
    					question.setText(x + " * " + x1);
    				}
    				if (event.getSource() == divison) {
    					Random multRand = new Random();
    					int d = multRand.nextInt(100) + 1;
    					int d1 = multRand.nextInt(10) + 1;
    					question.setText(d + " / " + d1);
    				}
    				if (event.getSource() == submit){
    					{
    						int mathTemp, qTemp;
    						String text = math.getText();
    						String text2 = question.getText();
    						mathTemp = Integer.parseInt (text);
    						qTemp = Integer.parseInt (text2);
    
    						
    						if (mathTemp == qTemp)
    						{
    							correct = new JLabel("Correct:" + 1);
    							total = new JLabel ("Total:" + 1);
    						}
    						else
    						{
    							correct = new JLabel("Correct " + 0);
    							total = new JLabel ("Total:" + 1);
    						}
    					}
    				}
    			}
    	}
    }
    Last edited by JosAH; 10-31-2011 at 07:22 AM. Reason: added [code] ... [/code] tags

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Arithmetic Program. PLEASE HELP!

    Don't just define JLabels named 'total' and 'correct' but also keep track of two ints 'totalQuestion' and 'correctQuestion'. Adjust those ints after the user has answered a question and adjust the text in those JLabels accordingly.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Arithmetic Program. PLEASE HELP!

    But I can't have an int within the JLabel parameter though can I? I have to have Total: and Correct: there even when the user has yet to answer a question, and the fields have nothing. When I use correctQ and totalQ and put them as the parameters of my JLabel then increase correctQ and totalQ under the Submit part, it says that I can't have an int parameter for a JLabel

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Arithmetic Program. PLEASE HELP!

    Something like this, mayhap?

    Java Code:
    // updating the JLabels: 
    correct.setText("Correct: "+correctQ);
    total.setText("Total: "+totalQ);
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Arithmetic Program. PLEASE HELP!

    Well I tried that, and it compiled fine, but still when I enter my answer, nothing is updated :(, whether my answer is right or wrong, it doesn't add anything to correct or total. I really don't think this portion of my code is working right,

    Java Code:
                         if (event.getSource() == submit){
                        {
                            int mathTemp, qTemp;
                            String text = math.getText();
                            String text2 = question.getText();
                            mathTemp = Integer.parseInt (text);
                            qTemp = Integer.parseInt (text2);
     
                             
                            if (mathTemp == qTemp)
                            {
    							totalQ = totalQ + 1;
    							correctQ = correctQ + 1;
    							correct.setText("Correct: "+correctQ);
    							total.setText("Total: "+totalQ);
    						}
    						else
    						{
    							totalQ = totalQ + 1;
    							total = new JLabel ("Total:" + totalQ);
    Last edited by Ahdood; 10-31-2011 at 08:32 AM. Reason: Forgot some code

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Arithmetic Program. PLEASE HELP!

    Did I create new JLabels in my code snippet?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Arithmetic Program. PLEASE HELP!

    No, I took mine out as well as you can see in the snippet I posted, but it still didn't work after compiling. I don't know where exactly it's wrong, it looks good :/

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Arithmetic Program. PLEASE HELP!

    Use your creativity and imagination: System.out.println( ... ) the new text you're trying to set in your JLabels and see if your code at least attempts to do the correct thing. It doesn't help you much if you only do what was suggested here, come back if it doesn't work as you expected and wait for new suggestions ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    Ahdood is offline Member
    Join Date
    Oct 2011
    Posts
    10
    Rep Power
    0

    Default Re: Java Arithmetic Program. PLEASE HELP!

    It's not a System.out.println, I need my total and correct to show up in the application's frame, not in the eclipse console, I honestly don't know enough about GUI things within Java to creatively guess and have it work, I need some pointers, I understand that I should attempt it on my own, but i'm afraid i've exhausted both my creativity and knowledge. Any help would be greatly appreciated.

  10. #10
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default Re: Java Arithmetic Program. PLEASE HELP!

    Quote Originally Posted by Ahdood View Post
    It's not a System.out.println, I need my total and correct to show up in the application's frame, not in the eclipse console, I honestly don't know enough about GUI things within Java to creatively guess and have it work, I need some pointers, I understand that I should attempt it on my own, but i'm afraid i've exhausted both my creativity and knowledge. Any help would be greatly appreciated.
    I didn't write that: use System.out.println( ... ) to see if your code at least attempts to set those new JLabel values; you can always remove those System.out.println( ... ) later but in the mean time you can actually see what your program is doing so you don't have to guess and you don't have to come here after every little step that didn't work as you expected. Programming also is about taking initiative. It is not about throwing your hands up in the air and let others solve your issue(s) and complaining.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Java Thread arithmetic value
    By mike_ledis in forum Threads and Synchronization
    Replies: 0
    Last Post: 03-15-2011, 06:28 AM
  2. Arithmetic using doubles
    By Black_Eye in forum New To Java
    Replies: 4
    Last Post: 10-19-2010, 08:35 AM
  3. Abbreviated Arithmetic
    By SweetLD215 in forum New To Java
    Replies: 19
    Last Post: 10-13-2010, 12:46 AM
  4. Polynomial Arithmetic Java
    By thisisIT in forum New To Java
    Replies: 3
    Last Post: 03-09-2010, 05:33 PM
  5. Arithmetic Stacks
    By unc123w in forum New To Java
    Replies: 22
    Last Post: 10-21-2008, 08:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •