Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Login         

View Questions and Answers by Category

Advertisements


 
Have Programming Question? Ask it here!
 
 
 


Tanveer Singh
Gui plz help
2 Answer(s)      2 years and 8 months ago
Posted in : Java Beginners


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");
        }
}

Advertisement
View Answers

March 2, 2012 at 4:23 PM


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);
        }
                }
         }
        });


March 2, 2012 at 4:24 PM


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);
  }
}


Related Tutorials/Questions & Answers:
Gui plz help
Gui plz help   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
plz help me to create gui using Java netbeans
plz help me to create gui using Java netbeans  Hi, I am unable to fetch a particular data from DB.I am using netbeans for creating GUI. If I want.... I am unable to fetch the particular data. Plz help me   Hi Friend
Advertisements
plz help
plz help  what is the procedure and code to design an interactive GUI in java using swings
Plz Help
Plz Help  Hello Guys!! Plz. Help I need to write a program to display the astrological sign based on your birthday for example If we enter the date of birth in the following format from console--13/08/1990 it should give you
Help me quickly plz??
Help me quickly plz??  Can you help me to write code quickly this code is a java code take input as double and should use command line arguments and enhanced for statement then find the combine of the numbers plz help quickly
plz help me for this question
plz help me for this question  Apply simplex procedure to solve the L.P.P. maximize z = 3x1 + 4x2 subject to 5x1 + 4x2 ? 200; 3x1 + 5x2 ? 150; 5x1 + 4x2 ? 100; 8x1 + 4x2 ?? 80, x1 ? 0, x2 ? 0
java coding help plz ?
java coding help plz ?  Given two integers N and M (N â?¤ M), output all the prime numbers between N and M inclusive, one per line. N and M will be positive integers less than or equal to 1,000,000,000. The difference
Plz Help Me
Plz Help Me  Write a program for traffic light tool to manage time giving between Main-Street and sub-Street. firstly, give green light for 40... traffic light on the frame. We have used java swing. It may help you. import
plz help - Java Beginners
plz help  i have to programs and it takes too long to run them so i got a solution that makes programs run much faster which is threading programming.. but i dont know how to aplly that on my 2 programs: the first code
plz help me find a program
plz help me find a program  plz help..i want a source code in jsp for order processing
plz help me - Java Beginners
plz help me  Deepak I can write a sessioon code plz help me admin_home.jsp page is display but data is not disply plz help me what is wrong
plz help me - Java Beginners
plz help me  Hi, I want to search all field from database using name, and display in text box, then data is update and delete. i want to search name using alphabets a-z, plz reply fast. plz help me this is very urgent
plz help meto solve this question
plz help meto solve this question  for a file that exist what will be the length() method in the file class return
plz Help me - Java Beginners
plz Help me  Hi, I want learn struts,I dont have any idea about this plz help how can i make a framework.If u have any information then plz send my personal id plz tell me that whose software installed.and give me brief
plz help me today plz plz - Java Interview Questions
plz help me today plz plz  2.) Suppose list is an array of six elements of type int. What is stored in list after the following java code executes? . for (i = 0; i < 5; i++) { list[i] = 2 * i + 5
plz help me - Java Beginners
plz help me  deepak hw can i create a data grid in jsp and also how can i connect to the data base plz help me  to create data grid in jsp use html tables with borders. to connect to data base use scriptlets. ex
plz help me - Java Beginners
plz help me  Thanks deepak continue response..i face some problem i hv a these file index.jsp,sessionvalid.jsp, both r store in project folder... is true...but i very confuse that how it is not displayed admin page plz any one
plz help me!!!!!!!! - JSP-Servlet
plz help me!!!!!!!!  i`ve set the environment varaibles for tomcat as well as jdk.. but i`m not understanding where i`ve to save my servlet..i.e.... there are compilation errors.. plz do help me.   make sure that you did
help plz - Java Interview Questions
, if the input string is abcd, the output is : edcba plz plz plz plz plz help...help plz  1 )write a program that does the following : a. prompts the user to input five decimal numbers representing the scores? b. prints
i need help plz .... Quickly
i need help plz .... Quickly   how can i count how many numbers enterd by the user so the output would be like this Total number of Scores = .... this is my code :- import java.util.Scanner; public class SCORES { public
help plz - Java Interview Questions
help plz   1 )write a program that does the following : a. prompts the user to input five decimal numbers representing the scores? b. prints the five decimal numbers? c. converts each decimal number to the nearest integer? d
plz help - Java Interview Questions
plz help  1)write a java program that prompts the user to input a decimal number and print the number rounded to the nearest integer? 2)write aprogram that prompts the user to input the five decimal numbers representing scors
PLZ Need some help JAVA...HELP !!
PLZ Need some help JAVA...HELP !!  Create a class names Purchase Each purchase contains an invoice number, amount of sale and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set
Plz help me in writing the code - Java Beginners
Plz help me in writing the code   Write a two user Chess Game. (users must be on different systems
Plz help me in writing the code - Java Beginners
Plz help me in writing the code   Write a two user Chess Game. (users must be on different systems
Help me plz in Probablity distribution of Java
Help me plz in Probablity distribution of Java  Hello, I have...% is insert or update queries. Which distribution to use and how to achieve it.. Plz help thanks for your time
Intranet Website creation - plz help me
Intranet Website creation - plz help me   hi.. I have assign with the intranet website creation work..right from scratch to end..And honestly I know nothing about it.My domain is totally different .. can anybody pls help me
PLZ HELP ME. i need php code.
PLZ HELP ME. i need php code.   I want php code for bellow OUTPUT. output is just example but it must be letters only. abc bcd efg jku rgt azs hje qqc wws adt
facing problem plz help me out - Framework
Facing problem plz help me out  hi i am new to servlet i deployed... the web.xml file too parallel to the classes folder now i am facing this problem.plz tell me what to do... error:The requested resource (Servlet servlet
help me plz befor 27 februry
help me plz befor 27 februry  what is rung with this ?? help me plzzzz import java.util.Scanner; public class Initials { public static void main (String [] args) { String firstname,lastname
help today plz:( - Java Interview Questions
help today plz:(  write a program that promptes the user to enter the weight of a package in pounds, and then outputs the weight of the backage in to kilograms (one kilogram is approximately, 2.2 pounds)? but i don't use
 

 

 

DMCA.com