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 4 of 4

Thread: My first week learning Java!

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default My first week learning Java!

    I have been teaching myself Java for the past week or so. I'm brand new to programming. My code compiles just fine, but the applet does not display any text fields and the genRandomNumbers() method doesn't seem to be working. Could you point me in the right direction? Any help is greatly appreciated!

    Here is my code:

    //Generate 2 random numbers
    //Post a question to multiply the two numbers
    //Verify the answer entered
    //Post a new question if the solution is correct

    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
     
    public class LearnMultiplication extends JApplet implements ActionListener
    {
     
       JLabel answerLabel;
       JTextField answerTextField, commentTextField, questionTextField;
       int random1, random2;
     
       public void init()
       {
       Container c = getContentPane();
       c.setLayout(new FlowLayout() );
     
       JTextField questionTextField = new JTextField(30);
       c.add(questionTextField);
     
       JLabel answerLabel = new JLabel("Enter you answer here");
       c.add(answerLabel);
     
       JTextField answerTextField = new JTextField(5);
       answerTextField.addActionListener(this);
       c.add(answerTextField);   
     
       JTextField commentTextField = new JTextField(30);
       c.add(commentTextField);
     
       genRandomNumbers(); // invoke method to generate 2 random numbers
       }
     
     
     
       public void actionPerformed (ActionEvent e)
       {
          int a = Integer.parseInt(e.getActionCommand() ); 
    	  verifyAnswer(a); // invoke method to verify the product
       }
     
     
       //method to generate 2 random numbers
       public void genRandomNumbers()
       {
            random1= 1 + (int)(Math.random() * 9	);	  
    	    random2 = 1 + (int)(Math.random() * 9	);
    	    questionTextField.setText("Multiply " + Integer.toString(random1) + "and " + Integer.toString(random2) + ".");
     
      }  
     
       // method to verify the product of the 2 random numbers
       public void verifyAnswer(int answer)
       {
    	  int correctAnswer = random1 * random2;
     
    	  if ( correctAnswer == answer)
    	  {
    	     commentTextField.setText("Very Good!");
    		 genRandomNumbers(); //call the method again to generate 2 new random numbers
    	  }	 
    	  else
    	  {
    	     commentTextField.setText("No, try again!!");
    	  }	 
     
    	}
    }
    Last edited by jps; September 12th, 2013 at 12:47 AM. Reason: code tags


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: My first week learning Java!

    Welcome to the forum
    Please use code tags when posting code, instructions can be found on the Announcements page
    "doesn't seem to be working" is not very descriptive. What does it do? What should it do instead? What have you tried so far in fixing it? What were the results of those tests?
    Look for error messages when you try to run it

  3. #3
    Junior Member
    Join Date
    Sep 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: My first week learning Java!

    The JTextFields are displaying properly on my applet. The genRandomNumbers() method is not invoked though. So the question prompting the user to multiply two numbers is not displayed on the applet.

    In my code, genRandomNumber() is being invoked from init(). Could that cause an issue? I tried to do this from paint() instead. That caused bigger issues - none of the text fields were displaying on the applet. So I moved the call to genRandomNumber() back to init().

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: My first week learning Java!

    Quote Originally Posted by SelfTaught View Post
    The JTextFields are displaying properly on my applet.
    Then post the same code you are running. The code posted in post #1 does not display any JTextFields.
    Check the error messages when you run...

Similar Threads

  1. learning java
    By javaman93 in forum Java Theory & Questions
    Replies: 2
    Last Post: June 13th, 2013, 12:52 PM
  2. I'm just learning Java
    By michael.duffy31 in forum Object Oriented Programming
    Replies: 2
    Last Post: June 5th, 2013, 11:27 AM
  3. Learning java!
    By gagaenter in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 29th, 2013, 07:21 AM
  4. 2nd week with Java. Help with a few basic syntax errors?
    By D P in forum What's Wrong With My Code?
    Replies: 7
    Last Post: January 28th, 2012, 11:13 PM
  5. Learning Java
    By destruxion in forum Member Introductions
    Replies: 1
    Last Post: September 12th, 2011, 04:13 AM