Random Math Java Program Showing Incorrect Answers

wuno

My program generates radnom math problems. Onclick it shows the answer to the math problems.

Every answer is wrong and I cannot figure out what the relevance of the numbers shown as answers is.

Here is the full program,

Driver Class

import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.*;

import javax.swing.*;

public class Driver extends MathProblems {

    MathProblems problems = new MathProblems();

    String s = "Welcome Students!";
    String b = "Start!";
    private JFrame f;
    private JPanel p;

    JFrame frame = new JFrame();

    JButton b1 = new JButton(b);

    JLabel jl = new JLabel(s);

    int i;

    public Driver () {      
        gui();  
    }

    public void gui() { 
        f = new JFrame("Flash Card Program");       
        p = new JPanel();   
        f.setLayout( new GridLayout( 2, 1 ) );
        f.add(jl);
        f.add(p);
        p.setLayout( new GridLayout( 2, 1 ) );
        p.add(b1);

        jl.setHorizontalAlignment(JLabel.CENTER);

        // pack the frame for better cross platform support
        f.pack();
        // Make it visible
        f.setVisible(true);
        f.setSize(560,400); // default size is 0,0
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);


        b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if(b1.getText().equals("Click For Answer"))
                {
                    problems.run();
                    jl.setText(problems.toString());
                    b = "Next Question";
                    b1.setText(b);
                }
                else
                {
                    problems.run();
                    jl.setText(problems.getQuestion());
                    b = "Click For Answer";
                    b1.setText(b);

                }
            }
        });
    }


    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
           public void run() {
                new Driver();
           }
        });
    } // End main Method

} // End class Driver

MathProblems Class

import java.util.Random;

public class MathProblems {
     private static final int MAX_NUMBER = 10;
     private static final Random random = new Random();

     private int expected = 0;
     private String question = "";

     public void run() {
         final int a = random.nextInt(MAX_NUMBER);
         final int b = random.nextInt(MAX_NUMBER);

         final int type = random.nextInt(4);

         switch (type) {
             case 0: 
                 add(a, b);
                 break;
             case 1: 
                subtract(a, b);
                break;
             case 2:
                multiply(a, b);
                break;
             case 3:
                 divide(a, b);
                 break;
         }
     }

     private void add(final int a, final int b) {
         expected = a + b;

         askQuestion(a + " + " + b + " = ");
     }

     private void subtract(final int a, final int b) {
         expected = a - b;

         askQuestion(a + " - " + b + " = ");
     }

     private void multiply(final int a, final int b) {
         expected = a * b;

         askQuestion(a + " * " + b + " = ");
     }

     private void divide(final int a, final int b) {
         expected = a / b;

         askQuestion(a + " / " + b + " = ");
     }

     private  void askQuestion(final String question) {
         this.question = question;
     }  

     public String getQuestion() {
         return question;
     }

     public int getAnswer() {
         return expected;
   }

     @Override
     public String toString(){
     return Integer.toString(expected);
     }
}
user3298955

Change this,

  b1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e){
                if(b1.getText().equals("Click For Answer"))
                {
                    problems.run();
                    jl.setText(problems.toString());
                    b = "Next Question";
                    b1.setText(b);
                }
                else
                {
                    problems.run();
                    jl.setText(problems.getQuestion());
                    b = "Click For Answer";
                    b1.setText(b);

                }
            }
        });

To this,

       b1.addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent e){
                    if(b1.getText().equals("Click For Answer"))
                    {

                        jl.setText(problems.toString());
                        b = "Next Question";
                        b1.setText(b);
                    }
                    else
                    {
                        problems.run();
                        jl.setText(problems.getQuestion());
                        b = "Click For Answer";
                        b1.setText(b);

                    }
                }
            });

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Incorrect Loop in Java Program

From Dev

Java Math.random period

From Dev

Java Math.random() concern

From Dev

Java Math.random() How random is it?

From Dev

Java Math.random not random in while loop

From Dev

Java program showing primes

From Dev

Java program not showing output

From Dev

Im making a program that makes random math, but it does not work

From Dev

Java Math.random returning 0?

From Dev

Math.random() in Java, how is this possible?

From Dev

Java VS Matlab : Math.random() and rand

From Dev

Why is this Java program showing no error?

From Dev

Why is this Java program showing no error?

From Dev

incorrect answers for quadratic equations

From Dev

Coding a random answer generator in Java - answers appear in a JFrame

From Dev

In Java 8 why we cannot convert Math.random() to Math::random using method references

From Dev

incorrect math output in C

From Dev

Generate fake answers for a math calculation

From Dev

Swing - Java program showing blank screen

From Dev

Java Program Not Showing Contents of Text File in Jar

From Dev

HttpUrlConnection not showing exception thrown by called java program

From Java

(Java) Generating random number for division program

From Dev

my random walk program outputs nothing (java)

From Dev

Java BigDecimal operations "arbitrarily" showing incorrect number of decimal places

From Dev

Java Apache Math3 MersenneTwister VS Python random

From Dev

Java Math.abs random vs. nextInt()

From Dev

Java functional programming random int stream math abs not working

From Dev

how to create a java function that makes random math problems.

From Dev

Java Math.abs random vs. nextInt()

Related Related

HotTag

Archive