0

I am having trouble getting this program to work. This is my first applet and I can’t seem to get my mind around how to get the commands in correct format. I have completed this program in application and gui with no problems, but applet format is not working in my favor. Any help would be much appreciated. Thanks in advance.
Erich

[TEX] 
/* Java applet that helps students with their multiplication.  This program will
   ask the student a multiplication problem and then display their resluts(correct/wrong).
   The program also needs the have the math.ramdom option to keep different question.
  */ 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 

public class JMath extends JApplet implements ActionListener
{
  
  int Number1 = 0;
  int Number2 = 0;
  int ActualAnswer = 0;

  Number1 = (int)( Math.random () * 9 ); 
  Number2 = (int)( Math.random () * 9 );
  ActualAnswer = Number1 * Number2;
 
  JLabel Mathout = new JLabel (" MATH FUN ");
  Font headlineFont = new Font ("Helvetica", Font.BOLD, 38);
  JLabel MathAnswer = new JTextField (2);
  JButton CheckAnswer = new JButton (" Check Answer ");
  JLabel MathQuestion = new JLabel ("What is " + Number1 + " TIMES " + Number2 + " ? ");
  ActualAnswer = Number1 * Number2;
  JLabel Correct = new JLabel ("Correct");
  JLabel Wrong = new JLabel ("Try again ");
  container con = getContentPane();
   
  public void init ()
{ // publicopen
   Mathout.setFont (headlineFont);
   MathQuestion.setFont (headlineFont);
   con.add (Mathout);
   con.add (MathQuestion);
   con.add (MathAnswer);
   con.add (CheckAnswer);
 
   
} // public end

   public void actionPerformed (ActionEvent e)
{ // openaction

   ActualAnswer = Number1 * Number2;
   int UserAnswer = MathAnswer.getText();
   
  
    if (UserAnswer = ActualAnswer)
 con.add (Correct);
    
    else
        con.add (Wrong);
        
      

} // end
}

[/TEX]

2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by p.bondam
0

sorry, i put my reply in the wrong place...(never too old to learn)

I am having trouble getting this program to work. This is my first applet and I can’t seem to get my mind around how to get the commands in correct format. I have completed this program in application and gui with no problems, but applet format is not working in my favor. Any help would be much appreciated. Thanks in advance.
Erich

hi erich,

i played a little with your program and think it's ok now
i made several changes as you can see and i hope it is not too difficult to understand.
when you have anymore problems : let me know

wiederschauen

peter ;)

[tex] 
/* Java applet that helps students with their multiplication.  This program will
   ask the student a multiplication problem and then display their resluts(correct/wrong).
   The program also needs the have the math.ramdom option to keep different question.
  */ 
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
 

public class JMath extends JApplet implements ActionListener
{
  
  int Number1 = 0;
  int Number2 = 0;
  int ActualAnswer = 0;

  Number1 = (int)( Math.random () * 9 ); 
  Number2 = (int)( Math.random () * 9 );
  ActualAnswer = Number1 * Number2;
 
  JLabel Mathout = new JLabel (" MATH FUN ");
  Font headlineFont = new Font ("Helvetica", Font.BOLD, 38);
  JLabel MathAnswer = new JTextField (2);
  JButton CheckAnswer = new JButton (" Check Answer ");
  JLabel MathQuestion = new JLabel ("What is " + Number1 + " TIMES " + Number2 + " ? ");
  ActualAnswer = Number1 * Number2;
  JLabel Correct = new JLabel ("Correct");
  JLabel Wrong = new JLabel ("Try again ");
  container con = getContentPane();
   
  public void init ()
{ // publicopen
   Mathout.setFont (headlineFont);
   MathQuestion.setFont (headlineFont);
   con.add (Mathout);
   con.add (MathQuestion);
   con.add (MathAnswer);
   con.add (CheckAnswer);
 
   
} // public end

   public void actionPerformed (ActionEvent e)
{ // openaction

   ActualAnswer = Number1 * Number2;
   int UserAnswer = MathAnswer.getText();
   
  
    if (UserAnswer = ActualAnswer)
 con.add (Correct);
    
    else
        con.add (Wrong);
        
      

} // end
} 
 
[/tex]


Attachments
import javax.swing.*;
   import java.awt.*;
   import java.awt.event.*;
   import java.util.*;
   import java.applet.Applet;
 
 
    public class JMath extends Applet implements ActionListener
   {
   
      int Number1;
      int Number2;
      int ActualAnswer;
      int UserAnswer;
      int WrongCount;
      int ccnt;
      int totalTries;
      Font headlineFont    = new Font ("Helvetica", Font.BOLD, 38);
      Label MathQuestion   = new Label("xxxxxxxxxxxxxxxxxxxx"); 
      TextField MathAnswer = new TextField (2);
      // left and right in the assignment should be the same class
      Button CheckAnswer   = new Button (" Check Answer ");
      String us            = "__________________"; // to reserve room for label
      Label MyAnswer       = new Label (us);
      Button AnotherGame         = new Button("Another Game");
              
       public void init () {
         setFont (headlineFont);
         add (new Label("MATH FUN"));
         add (MathQuestion);
         add (MathAnswer);
         MathAnswer.addActionListener(this);
         add (CheckAnswer);
         CheckAnswer.addActionListener(this);
         add (MyAnswer);
         add (AnotherGame);
         AnotherGame.addActionListener(this);
         play(0);
      } 
      
       public void play(int type) {
         if (type == 0) {
            Number1 = (int)( Math.random () * 9 +1);
            Number2 = (int)( Math.random () * 9 +1);
            ActualAnswer = Number1 * Number2;
            WrongCount = 0;
            String s = "What is " + Number1 + " TIMES " + Number2 + " ? ";
            MathQuestion.setText(s);
         }
         MathAnswer.setText("");
         MathAnswer.requestFocus();
      }
      
       public void actionPerformed (ActionEvent e) { // openaction
         Object src = e.getSource();
         int type = 0;
         if (src == MathAnswer || src == CheckAnswer) {
            try {
               String a = MathAnswer.getText();
               if (a.length() == 0) a = "**"; // to force an exception..
               UserAnswer = Integer.parseInt(a); // make an integer from string
            } 
                catch(NumberFormatException nfe) {
                  MyAnswer.setText("Wrong input !!!");
                  type = 2;
               }
            if (type != 2) {   
               totalTries++; 
               if (UserAnswer == ActualAnswer) {
                  ccnt++;
               // always say '==' for a comparison, just '=' means assignment!
                  //MyAnswer.setText("Correct ("+ccnt+" out of "+totalTries+")");
                  int percent = (int)(100f * ccnt / totalTries);
                  MyAnswer.setText("Correct ( "+percent+" % )");
               }
               else {
                  WrongCount++;
                  MyAnswer.setText(""+WrongCount+" Wrong, Try Again");
                  type = 1;
               }
            }
         }
         else if (src == AnotherGame) {
            MyAnswer.setText(us);
            ccnt = totalTries = 0;
         }   
         play(type);
      }
   }
/* 
[/tex]

<br />
Reply With Quote Multi-Quote This Message Quick reply to this message
krauz2
View Public Profile
Send a private message to krauz2
Find all posts by krauz2
Add krauz2 to Your Contact List

Reply 	Post New Thread 	 Previous Thread | Next Thread 


DaniWeb Message
 
Cancel Changes
	
Is DaniWeb Helpful?

Quick Reply

The following errors occurred when this message was submitted
Okay
Message:
Remove Text Formatting
		
Bold
	
Italic
	
Underline
		

	
		
Insert Link
	
Insert Image
		
Wrap [QUOTE] tags around selected text
	  	
Decrease Size
Increase Size
		
Switch Editor Mode
Options
Show your signature
Quote message in reply?
Posting Quick Reply - Please Wait Posting Quick Reply - Please Wait

Similar Threads

Thread 	Thread Starter 	Forum 	Replies 	Last Post
applet not connecting 	sandy183 	Java 	4 	Jun 6th 2006
11:52 pm
Applet Help 	cjd_1986 	Java 	0 	Dec 3rd 2005
1:22 pm
Review my applet 	server_crash 	Geek's Lounge 	1 	Aug 2nd 2005
9:03 pm
Help!! Applet only works in AppletViewer but not in html file with <applet> tags!!! 	rackus 	Java 	9 	May 9th 2005
8:21 am
Applet Help 	Firsttimer 	Java 	3 	Sep 13th 2004
10:17 am

Currently Active Users Viewing This Thread: 1 (1 members and 0 guests)

p.bondam

Display Modes
Linear Mode Linear Mode
Hybrid Mode Switch to Hybrid Mode
Threaded Mode Switch to Threaded Mode
Rate This Thread
ExcellentExcellent
GoodGood
AverageAverage
BadBad
TerribleTerrible
	


SEARCH

Advanced Search
FORUM HIGHLIGHTS

    * Posts Since Last Visit
          o This Category
          o All Forums
    * Today's Posts
          o This Category
          o All Forums
    * Unanswered Threads
    * Related Links

Event Calendar
IRC Chat Network
Link Directory
Newsletter Archive
SYNDICATION
Member Certificate

    * RSS Forum RSS Feed
    * RSS Blog RSS Feed

STATISTICS
Members: 126,769
Online Now: 857
Active IRC Users: 4
Forum Threads: 55,223
Forum Posts: 269,779
Code Snippets: 573
Blogs: 80
Blog Entries: 754
Directory Links: 318
SPONSORS

    * Dental Insurance
    * Restaurant Supplies
    * UK Loans
    * Website Hosting
    * Web Development
    * Domain Hosting
    * PC hardware forum
    * Developer lists archives

ADVERTISEMENT

FAQ - About Us - Contact Us - Advertising PDF - Archive - Privacy Statement
	
All times are GMT +1. The time now is 2:00 am.

Return To Top 2003 - 2006 DaniWeb LLC
vBulletin Copyright 2000 - 2006, Jelsoft Enterprises Ltd.

*/
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.