Java arithmetic test Applet

Source: Internet
Author: User

 

TestFrame. java

Import javax. swing .*;

Import java. awt. FlowLayout;

Import java. awt. event .*;

 

Public class TestFrame extends JFrame implements ActionListener

{

JTextField textOne, textTwo, textResult;

JButton getProblem, givean.pdf;

JLabel operatorJLabel, message;

TestOperator testOperator;

 

Public TestFrame ()

{

TestOperator = new TestOperator ();

SetLayout (new FlowLayout ());

TextOne = new JTextField (10); // create textOne with a visible length of 10 Characters

TextTwo = new JTextField (10); // create textTwo. The visible character length is 10.

TextResult = new JTextField (10); // create a textResult. The visible character length is 10.

OperatorJLabel = new JLabel ("+ ");

Message = new JLabel ("You have not answered yet ");

GetProblem = new JButton ("obtain question ");

Giveanbutton = new JButton ("Confirm answer ");

 

Add (getProblem );

Add (textOne );

Add (operatorJLabel );

Add (textTwo );

Add (new JLabel ("= "));

Add (textResult );

Add (giveAnwser );

Add (message );

 

TextOne. setEditable (false );

TextTwo. setEditable (false );

GetProblem. addActionListener (this); // ActionEvent monitor registered as getProblem in the current window

Giveanener. addActionListener (this); // The ActionEvent event monitor registered as giveAnwser in the current window

TextResult. addActionListener (this); // ActionEvent monitor registered as textResult in the current window

}

Public void actionreceivmed (ActionEvent e)

{

If (e. getSource () = getProblem) // determine whether the event source is getProblem

{

Int number1 = testOperator. giveNumberOne (100 );

Int number2 = testOperator. giveNumberTwo (100 );

String operator = testOperator. giveOperator ();

TextOne. setText ("" + number1 );

TextTwo. setText ("" + number2 );

OperatorJLabel. setText (operator );

Message. setText ("Please answer ");

TextResult. setText (null );

}

If (e. getSource () = givean) // determine whether the event source is giveAnwser

{

String answer = textResult. getText ();

Try {int result = Integer. parseInt (answer );

If (testOperator. getRight (result) = true)

{

Message. setText ("your answer is correct ");

}

Else

{

Message. setText ("your answer is incorrect ");

}

}

Catch (NumberFormatException ex)

{

Message. setText ("enter a number character ");

}

}

}

}

TestOperator. java

Public class TestOperator

{

Int numberOne, numberTwo;

String operator = "";

Boolean right;

 

// Generate the first number randomly

Public int giveNumberOne (int n)

{

NumberOne = (int) (Math. random () * n) + 1; // forcibly converted to int type

// Because Math. random () returns the double type

Return numberOne;

}

 

// Generate the second number randomly

Public int giveNumberTwo (int n)

{

NumberTwo = (int) (Math. random () * n) + 1;

Return numberTwo;

}

 

// Randomly generate the operator number

Public String giveOperator ()

{

Double d = Math. random ();

If (d >=0 & d <0.25)

Operator = "+ ";

 

Else if (d> 0.25 & d <= 0.5)

Operator = "-";

Else if (d> 0.5 & d <= 0.75)

Operator = "*";

Else

Operator = "/";

Return operator;

}

 

// Determine whether the test result is correct

Public boolean getRight (int answer)

{

If (operator. equals ("+ "))

{

If (answer = numberOne + numberTwo)

Right = true;

 

Else

Right = false;

 

}

 

Else if (operator. equals ("-"))

{

If (answer = numberOne-numberTwo)

Right = true;

 

Else

Right = false;

}

Else if (operator. equals ("*"))

{

If (answer = numberOne * numberTwo)

Right = true;

 

Else

Right = false;

}

Else if (operator. equals ("/"))

{

If (answer = numberOne/numberTwo)

Right = true;

 

Else

Right = false;

}

Return right;

}

}

MainClass. java

Import javax. swing. JFrame;

Import javax. swing. UIManager;

 

Public class MainClass

{

Public static void main (String args [])

{

Try {

UIManager. setLookAndFeel ("com. sun. java. swing. plaf. windows. WindowsLookAndFeel"); // windows interface style

} Catch (Exception e ){

E. printStackTrace ();

}

TestFrame frame = new TestFrame ();

Frame. setTitle ("arithmetic test applet ");

Frame. setBounds (100,100,500,100 );

Frame. setVisible (true );

Frame. setLocationRelativeTo (null); // The center of the form is displayed.

Frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE );

Frame. setResizable (false );

}

}

:

 

This article is from the "IT" blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.