- Joined
- Nov 13, 2006
- Messages
- 15,645 (2.35/day)
- Location
- Mid-Atlantic
System Name | Desktop |
---|---|
Processor | i5 13600KF |
Motherboard | AsRock B760M Steel Legend Wifi |
Cooling | Noctua NH-U9S |
Memory | 4x 16 Gb Gskill S5 DDR5 @6000 |
Video Card(s) | Gigabyte Gaming OC 6750 XT 12GB |
Storage | WD_BLACK 4TB SN850x |
Display(s) | Gigabye M32U |
Case | Corsair Carbide 400C |
Audio Device(s) | On Board |
Power Supply | EVGA Supernova 650 P2 |
Mouse | MX Master 3s |
Keyboard | Logitech G915 Wireless Clicky |
Software | Fedora KDE Spin |
miss me?? ![Smile :) :)](https://tpucdn.com/forums/data/assets/smilies/smile-v1.gif)
ok im taking an old program and converting it to an applet. the original program was the elementary math program where using JOptionPane the user receives a basic addition or subtraction question and then enters their answer and the program tells them if they are correct or incorrect.
so now im converting it to an applet that does the same thing but on a GUI. here is what i have so far. i need to figure out a way to retrieve the textfield input and see if it matches the actual answer.
(note: i dont think i should use a switch statement here but im still in the process of reworking code)
![Smile :) :)](https://tpucdn.com/forums/data/assets/smilies/smile-v1.gif)
ok im taking an old program and converting it to an applet. the original program was the elementary math program where using JOptionPane the user receives a basic addition or subtraction question and then enters their answer and the program tells them if they are correct or incorrect.
so now im converting it to an applet that does the same thing but on a GUI. here is what i have so far. i need to figure out a way to retrieve the textfield input and see if it matches the actual answer.
(note: i dont think i should use a switch statement here but im still in the process of reworking code)
Code:
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JApplet;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class TeachArithmetic extends JApplet implements ActionListener
{
private JPanel mathProblem = new JPanel();
private JPanel enterAnswer = new JPanel();
private JPanel gradeAnswer = new JPanel();
private JPanel clickButton = new JPanel();
private JPanel mainPanel = new JPanel();
private JLabel problem;
private JTextField userInput = new JTextField(5);
private JLabel printResult;
private JButton ok, next;
public void init()
{
ok = new JButton("OK");
ok.addActionListener(this);
ok.setFont(new Font("Comic Sans MS", Font.BOLD,18));
ok.setForeground(Color.blue);
ok.setBackground(Color.white);
next = new JButton("NEXT");
next.addActionListener(this);
next.setFont(new Font("Comic Sans MS", Font.BOLD,18));
next.setForeground(Color.blue);
next.setBackground(Color.white);
mathProblem.setBackground(Color.white);
mathProblem.setForeground(Color.green);
mathProblem.add(problem);
enterAnswer.setBackground(Color.white);
enterAnswer.add(userInput);
gradeAnswer.setBackground(Color.white);
gradeAnswer.setFont(new Font("Comic Sans MS", Font.BOLD,20));
gradeAnswer.setForeground(Color.red);
gradeAnswer.add(printResult);
clickButton.setBackground(Color.white);
clickButton.add(ok);
clickButton.add(next);
mainPanel.setBackground(Color.white);
mainPanel.setLayout(new GridLayout(5, 1));
mainPanel.add(mathProblem);
mainPanel.add(enterAnswer);
mainPanel.add(gradeAnswer);
mainPanel.add(clickButton);
Container pane = getContentPane();
pane.add(mainPanel);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == ok)
{
//check answer to see if it is correct
//if correct then print correct
//if incorrect then print incorrect
}
if(e.getSource() == next)
{
generateProblem(); //generate new math problem
printResult.setText(""); //clear screen
userInput.setText(""); //clear textfield
}
}
public void generateProblem()
{
int num1 = randomNumber();
int num2 = randomNumber();
int user_answer = 0;
int correct_answer = 0;
boolean correct = false;
switch(simple_math)
{
case 1: //addition
user_answer = num1 + num2;
correct_answer = num1 + num2;
if (user_answer == correct_answer)
{
correct = true;
}
else
{
correct = false;
}
break;
case 2: //subtraction
if (num1 < num2)// larger number is first
{
int num3 = num1;
num2 = num1;
num1 = num3;
}
user_answer = num1 - num2;
correct_answer = num1 - num2;
if (user_answer == correct_answer)
{
correct = true;
}
else
{
correct = false;
}
break;
}
if(correct)
{
printResult.setText("You got it right!");
}
else
{
printResult.setText("I'm sorry, but that is incorrect.");
}
}
public int randomNumber()
{
return ((int) (Math.random() * 10));
}
}