Create a Java application that would allow a person to practice math (either addition, subtraction, multiplication or division). The questions must be randomly generated ( the values for each number should be in the range from 1 to 10). In total, 10 questions should be asked. After 10 questions are asked and answered, display a message to the user that would state either ?Good Job? if the ratio of correct to incorrect answers exceeds 70% or you would do better next time if the ratio is smaller than 70%.
this is what i got so far. so basically what i did is i used the java palletes to make a application and put two buttons one button makes two random numbers and the other button tells if u got it right or wrong and also i gave them some coding but i can't make it do only 10 questions for button1 and not able to add them so i can get answers.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
Random rand = new Random();
for(int i = 0;i <= 10;++i){
int rad = rand.nextInt(10)+1;
jLabel4.setText("" + rad);
jLabel6.setText("" + rad);
}
}
private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
int num = Integer.parseInt(jLabel4.getText());
int num1 = Integer.parseInt(jLabel6.getText());
int num2 = Integer.parseInt(jTextField1.getText());
if(evt.getSource() == jButton2){
num2 = num + num1;
jLabel2.setText("Good job!");
}else
num2 = num - num1;
jLabel2.setText("Try Again");
}catch(Exception e){
JOptionPane.showMessageDialog(null, "Numbers only");
}
}
import java.awt.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
class Calculate{
static int res=0;
static int count=0;
static int resultarray[]=new int[10];
static int answerarray[]=new int[10];
static int i=0;
public static String get (String[] array) {
Random generator = new Random();
int rnd = generator.nextInt(array.length);
return array[rnd];
}
public static void main(String[] args){
final JButton b1=new JButton("Next");
final JButton b2=new JButton("Check");
final JButton b3=new JButton("Show Result");
final JLabel l1=new JLabel();
final JLabel l2=new JLabel();
final JLabel l3=new JLabel();
final JTextField text=new JTextField(10);
l1.setBounds(20,20,40,20);
l2.setBounds(60,20,20,20);
l3.setBounds(100,20,40,20);
text.setBounds(120,20,80,20);
b1.setBounds(20,50,80,20);
b2.setBounds(100,50,80,20);
b3.setBounds(50,80,120,20);
final Random rand = new Random();
final String[] options = { "+", "-","*", "/"};
String ret = Calculate.get(options);
int rad1 = rand.nextInt(10)+1;
int rad2 = rand.nextInt(10)+1;
if(rad1>rad2){
l1.setText("" + rad1);
l2.setText(ret);
l3.setText("" + rad2);
}
else{
l1.setText(""+rad2);
l2.setText(ret);
l3.setText("" + rad1);
}
b1.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if (e.getSource() == b1) {
text.setText(" ");
count++;
if(count>8){
b1.setEnabled(false);
}
l1.setText(" ");
l2.setText(" ");
l3.setText(" ");
int count=0;
int rad1 = rand.nextInt(10)+1;
int rad2 = rand.nextInt(10)+1;
if(rad1>rad2){
l1.setText("" + rad1);
String s = Calculate.get(options);
l2.setText(s);
l3.setText("" + rad2);
}
else{
l1.setText(""+rad2);
String s = Calculate.get(options);
l2.setText(s);
l3.setText("" + rad1);
}
}
}
});
continue..
b2.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
int num1=Integer.parseInt(l1.getText().trim());
int num2=Integer.parseInt(l3.getText().trim());
int result=Integer.parseInt(text.getText().trim());
int value=0;
switch(l2.getText()){
case "+":
value=num1+num2;
break;
case "-":
value=num1-num2;
break;
case "*":
value=num1*num2;
break;
case "/":
value=num1/num2;
break;
}
if(value==result){
JOptionPane.showMessageDialog(null,"It is right!");
}
else{
JOptionPane.showMessageDialog(null,"It is wrong!");
}
answerarray[i]=value;
resultarray[i]=result;
i++;
}
});
b3.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
for(int i=0;i<10;i++){
if(answerarray[i]==resultarray[i]){
res++;
}
}
double v=res/10;
double p=v*100;
if(p>=70){
JOptionPane.showMessageDialog(null,"Good Job!");
}
else{
JOptionPane.showMessageDialog(null,"you would do better next time!");
}
}
});
JFrame f=new JFrame();
f.setLayout(null);
f.add(l1);
f.add(l2);
f.add(l3);
f.add(text);
f.add(b1);
f.add(b2);
f.add(b3);
f.setSize(300,200);
f.setVisible(true);
}
}