RE: [SLUG] Java question

From: Craig Zeigler (craig@caffeine-addict.com)
Date: Sat Nov 01 2003 - 11:53:29 EST


its been a while since I've used this stuff, but I would check your msg
string. Have you been able to display that someplace else?

-----Original Message-----
From: Russ Wright [mailto:rwrigh10@tampabay.rr.com]
Sent: Saturday, November 01, 2003 11:39 AM
To: slug@nks.net
Subject: [SLUG] Java question

Hello Sluggers!

I'm just learning Java and my classroom assignment is to create a simple
math
tutor that asks a multiplication question in the statusbar of an applet
and
allows the user to type in an aswer and see if their answer is correct.

It all seems to work except the status bar is not displaying the
question.
See function generateQuestion. Any ideas?

Regards
Russ

My Code:

/*
 * MathTrainer.java
 *
 * Created on October 31, 2003, 6:56 PM
 */

/**
 *
 * @author rwright
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JOptionPane;

public class MathTrainer extends JApplet implements ActionListener {
    
    //gui label for question
        JLabel questionLabel;
        JTextField answerField;
        JButton evaluateButton,startButton,stopButton;
     
    //my two mupltipliers
    int m_intValue1 = 0,m_intValue2 = 0;
    
    /** Initialization method that will be called after the applet is
loaded
     * into the browser.
     */
    
    public void init()
    {
        Container cont = getContentPane();
        cont.setLayout(new FlowLayout());
        
        //set up the gui
        //label for input text
        questionLabel = new JLabel ( "Your Answer:" );
        //add the label
        cont.add(questionLabel);
        
        //textbox for input
        answerField = new JTextField(10);
        cont.add(answerField);
        
        //Evaluate button
        evaluateButton = new JButton( "Check Answer" );
        //add a listener
        evaluateButton.addActionListener(this);
        //add the button to the gui
        cont.add(evaluateButton);
        
        //ask the first question
        //generate a question and display it
        generateQuestion();
        
    } //init
    
    
    public void actionPerformed(ActionEvent e) {
        
            //call event to check the answer
            checkAnswer();
        
        
    }
    
    public void checkAnswer()
    {
       //evaluate the users response
        boolean isCorrect = evaluateAnswer();
        
        
        //if it is correct affirm user and display a new question
        
        if (isCorrect == true)
        {
                //tell the user they got it right
                JOptionPane.showMessageDialog(null,"Correct!");
                
                //ask a new question
                    generateQuestion();
        }
        else
        {
            //if it is incorrect tell user and wait for new response
            //tell the user they got it wrong
                JOptionPane.showMessageDialog(null, "Wrong Answer Try
Again!");
            
        }
        
    }//checkanswer
  
    //function to evaluate user answer
     public boolean evaluateAnswer()
    {
        //get users response
        long userAnswer = Long.parseLong(answerField.getText() );
        
        //compare user's response to answer
        if ((m_intValue1 * m_intValue2) == userAnswer)
        {
            //if the answer is correct return true
            return true;
            
        }
        else
        {
            //if the answer is incorrect return false
            return false;
        }
        
    }//evaluateAnswer
    

    public void generateQuestion()
    {
        //create the 2 random numbers
        //assign their values to the module level variables
        m_intValue1 = 1 + ( int) ( Math.random() * 9);
        m_intValue2 = 1 + ( int) ( Math.random() * 9);
                
        //assemble message
        String msg = "What is " + m_intValue1 + " times " + m_intValue2
+ "?";
        
        //display message in statusbar
        showStatus( msg );
        
    }//generateQuestion
 
}//mathTrainer

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.

-----------------------------------------------------------------------
This list is provided as an unmoderated internet service by Networked
Knowledge Systems (NKS). Views and opinions expressed in messages
posted are those of the author and do not necessarily reflect the
official policy or position of NKS or any of its employees.



This archive was generated by hypermail 2.1.3 : Fri Aug 01 2014 - 20:35:55 EDT