Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable

Thread: Implementing a Timer - Confused (royally).

  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Implementing a Timer - Confused (royally).

    Okay, so the code that I have is a 'game' I have started making for my nephew. The premise is to ask the user how many questions they want on the quiz. Then ask for a name. The test begins and an int is pulled from an array, and multiplied to a second int from a second array. This is done for the amount of questions and then the score is displayed, along with a 'grade'. All this is working fine, but this is my learning project so I am trying to add to it. I thought about giving the user for a timed test to see how many questions they can get, versus the above mentioned type.

    I then went on to this so far:
    Java Code:
    import java.util.Scanner;
    import javax.swing.Timer;
    import java.awt.event.*;
    
    public class TimesTables implements ActionListener{
              
              
              public static String stars  = "*********************************************************";
              // - Weighted for extra help
              public static int[] fNum = {0,1,2,3,4,5,6,7,8,9,10,11}; // 12
              public static int[] sNum = {3,4,5,6,7,7,7,8,8,8,9,9,}; // 12
              public static Scanner scan = new Scanner(System.in);
              public static int counter = 1;
              public static int correct = 0;
              public static int incorrect = 0;
              public static int delay = 1000;
              
              public static ActionListener taskPerformed = new ActionListener(){
                        public void actionPerformed(ActionEvent e){
                        System.out.println("TEST ACTION EVENT");
              }
                        
              };
              public static Timer timer = new Timer(delay, taskPerformed);
              
              
              
              public static int RandomNum(){
                        int num = (int) ((Math.random()*12));
                        return(num);
              }
              
              public static void main(String[] args){
                        System.out.println("\n"+stars);
                        System.out.println("This program is set to help with multiplication tables.");
                        System.out.println("How many questions would you like on the quiz ?");
                        int quizQuestions = scan.nextInt();
                        System.out.println("INSTRUCTIONS:");
                        System.out.println("This quiz has "+quizQuestions+" questions.");
                        System.out.println("To begin the quiz, type your name and press the <ENTER> key.");
                        System.out.println("At the end of the quiz you will find out your score.");
                        
                        String name = scan.next();
                        
                        System.out.println("Let's Go, "+name+"\n"+stars);
                        timer.start();
                        
                        while(counter<=quizQuestions){
                                  int x = fNum[RandomNum()];
                                  int y = sNum[RandomNum()];
                                  int sum = x*y;
                                  int guess;
                                  
                                  System.out.println("QUESTION " + counter + ":\n" + x +" X "+ y + " = ?");
                                  guess = scan.nextInt();
                                  if(guess!=sum){
                                            System.out.println("INCORRECT\n");
                                            incorrect++;}
                                  else{
                                            System.out.println("CORRECT\n");
                                            correct++;}
                                  counter++;
                        }      
    
                        timer.stop();
                        double ppQ = 100/quizQuestions;
                        double score = (ppQ * correct);
                                  
                        if(score>=90){
                                  System.out.println("Congrats! You scored an A");}
                        else if(score>=80){
                                  System.out.println("You scored a B! Great Job");}
                        else if(score>=70){
                                  System.out.println("Keep studying. You got a C");}
                        else if(score>=60){
                                  System.out.println("D - Please report to me after class");}
                        else{
                                  System.out.println("F - Must have returned with parent signature");
                        }
                        
                        System.out.println(name + " scored " + correct + " correct, out of " + quizQuestions + " questions for a score of " + ((int)score));
                        }           
    }
    I have been getting numerous errors, and most I have been able to understand and figure out. Interfaces still confuse me so I decided to get to the lowest amount of errors I could manage, and then post in hopes that someone can help me wrap my brain around this one.

    **ERROR**
    Java Code:
    1 error found:
    File: C:\Users\Ryan\Desktop\TimesTables.java  [line: 5]
    Error: The type TimesTables must implement the inherited abstract method java.awt.event.ActionListener.actionPerformed(java.awt.event.ActionEvent)
    QUESTION: Can you break down a definition for my error? I did look at Cannot instantiate the type Foo - JavaErrors for some clarification, but didn't receive much. I know the question is more of an education request, and I appologize, but I just can't get it to click.
    Thanks guys.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Implementing a Timer - Confused (royally).

    Any time your class implements an interface, it must implement the methods of that interface. Your class is not implementing the actionPerformed method of the ActionListener interface, and so the compiler is justifiably complaining. But more importantly, why is this class even implementing that interface if it is not being used in that fashion. It looks like you may be borrowing code from here and there and trying to paste t his together, and that will never work.

    I suggest that you
    • scrap this code and start over from scratch,
    • get rid of all static variables and methods except for the main method
    • that your main method be short, that all it do is create an instance of your GUI and set it visible and that's it
    • that you read up on how to create classes, and GUI's in the Java tutorials.
    • that you read up on how to use Swing Timers in the tutorials as well.
    • that you read up on use of interfaces.


    Luck!

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Implementing a Timer - Confused (royally).

    As for borrowing the code from here and there, I would rather say, try to learn and use 1 example but rather see if I can get multiple examples and incorporate what I might have picked up into existing code that I already had that worked.

    Java Code:
    import java.util.Scanner;
     
    public class TimesTables{
               
              public static String stars  = "*********************************************************";
              // - Weighted for extra help
              public static int[] fNum = {0,1,2,3,4,5,6,7,8,9,10,11}; // 12
              public static int[] sNum = {3,4,5,6,7,7,7,8,8,8,9,9,}; // 12
              public static Scanner scan = new Scanner(System.in);
              public static int counter = 1;
              public static int correct = 0;
              public static int incorrect = 0;
               
                        
              public static int RandomNum(){
                        int num = (int) ((Math.random()*12));
                        return(num);
              }
               
              public static void main(String[] args){
                        System.out.println("\n"+stars);
                        System.out.println("This program is set to help with multiplication tables.");
                        System.out.println("How many questions would you like on the quiz ?");
                        int quizQuestions = scan.nextInt();
                        System.out.println("INSTRUCTIONS:");
                        System.out.println("This quiz has "+quizQuestions+" questions.");
                        System.out.println("To begin the quiz, type your name and press the <ENTER> key.");
                        System.out.println("At the end of the quiz you will find out your score.");
                         
                        String name = scan.next();
                         
                        System.out.println("Let's Go, "+name+"\n"+stars);
                         
                        while(counter<=quizQuestions){
                                  int x = fNum[RandomNum()];
                                  int y = sNum[RandomNum()];
                                  int sum = x*y;
                                  int guess;
                                   
                                  System.out.println("QUESTION " + counter + ":\n" + x +" X "+ y + " = ?");
                                  guess = scan.nextInt();
                                  if(guess!=sum){
                                            System.out.println("INCORRECT\n");
                                            incorrect++;}
                                  else{
                                            System.out.println("CORRECT\n");
                                            correct++;}
                                  counter++;
                        }     
     
                        double ppQ = 100/quizQuestions;
                        double score = (ppQ * correct);
                                   
                        if(score>=90){
                                  System.out.println("Congrats! You scored an A");}
                        else if(score>=80){
                                  System.out.println("You scored a B! Great Job");}
                        else if(score>=70){
                                  System.out.println("Keep studying. You got a C");}
                        else if(score>=60){
                                  System.out.println("D - Please report to me after class");}
                        else{
                                  System.out.println("F - Must have returned with parent signature");
                        }
                         
                        System.out.println(name + " scored " + correct + " correct, out of " + quizQuestions + " questions for a score of " + ((int)score));
                        }          
    }
    Do you have any VIDEO tutes you would recommend? Reading stuff you don't understand well, that is written in a way you don't understand well, isn't good. Obviously what I have been looking at isn't good. Purchasing a book isn't an option so I have been trying to follow along with Bucky(?) on YouTube.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Implementing a Timer - Confused (royally).

    All I can advise you on is where I learned my Java and Swing coding, and that's the Java tutorials.

    You state
    Reading stuff you don't understand well, that is written in a way you don't understand well, isn't good. Obviously what I have been looking at isn't good.
    Reading a tutorial is an acquired skill that gets better with practice. So the more you do it, the better you'll get at it.
    DarrylBurke likes this.

  5. #5
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Implementing a Timer - Confused (royally).

    Well... I have done some reading (including Java for Dummies). I guess I am at a point where maybe going over everything might make it all stick to the grey matter a little better.

    As for the GUI aspect of it though, that was not something I have even thought of implementing at this time. Should I have used java.util.Timer if I wasn't planning on doing a swing application?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Implementing a Timer - Confused (royally).

    Yes a java.util.Timer is great for non-Swing timer routines, although the API for this class suggests that you may be better off using a ScheduledThreadPoolExecutor, stating,
    Java 5.0 introduced the java.util.concurrent package and one of the concurrency utilities therein is the ScheduledThreadPoolExecutor which is a thread pool for repeatedly executing tasks at a given rate or delay. It is effectively a more versatile replacement for the Timer/TimerTask combination, as it allows multiple service threads, accepts various time units, and doesn't require subclassing TimerTask (just implement Runnable). Configuring ScheduledThreadPoolExecutor with one thread makes it equivalent to Timer.
    If it's a Swing application, then a javax.swing.Timer is the way to go. I would suggest that you don't have your overall class implement the ActionListener interface, and in fact I recommend that no GUI component class implement any listener interfaces. Rather I usually create anonymous inner classes for my listener interfaces as this is much more flexible and less risky.
    rru96 likes this.

  7. #7
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    98
    Rep Power
    0

    Default Re: Implementing a Timer - Confused (royally).

    Thank you. I think I am going to go back over some prior reading, and retry my code with a java.util.Timer or look into the concurrent package.
    Last edited by rru96; 11-05-2012 at 06:59 AM.

Similar Threads

  1. Timer And Timer Task
    By Ashish_jain in forum New To Java
    Replies: 3
    Last Post: 06-22-2011, 09:41 AM
  2. Confused :?
    By jESTPHROSTY in forum New To Java
    Replies: 2
    Last Post: 10-17-2010, 07:38 PM
  3. Stopping a Timer from Inside the timer
    By krishnan in forum Java Applets
    Replies: 2
    Last Post: 10-04-2010, 11:15 PM
  4. confused
    By updev in forum AWT / Swing
    Replies: 6
    Last Post: 11-14-2008, 03:33 PM
  5. Replies: 0
    Last Post: 04-04-2008, 02:46 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •