faqts : Computers : Programming : Languages : Java

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

14 of 21 people (67%) answered Yes
Recently 6 of 10 people (60%) answered Yes

Entry

How can I write a simple flash card type program to help me study for a test?

May 7th, 2002 11:13
Jason Stracner,


I am currently(05/07/2021) studying for the SCJP (Sun Certified Java 2 
Programmer) exam and wrote a little command-line flash card type 
program to help me memorize things for the exam.  The program requires 
a text file that is formatted so that every other line is a question 
and the following line is the answer that you need to type.  The text 
file should be named 'scjp_fill_in_the_blanks.txt' and be in the same 
folder as the program.  You can change the name of the file by editing 
the source code, of course.
Here is some text that I have in my 'scjp_fill_in_the_blanks.txt' for 
an example:
What is the minimum value for the primitive type short?
-32768
What is the maximum value for the primitive type short?
32767
What is the minimum value for the primitive type float?
1.4*10^-45
What is the maximum value for the primitive type float?
3.4*10^38
What is the minimum value for the primitive type double?
4.9*10^-324
What is the maximum value for the primitive type double?
1.8*10^308
At the end of this article is the text for the java source file.  I 
have tested it with the Java 2 SDK version 1.4.  Just copy the code 
into a file named 'SCJP_Tester.java'.  After you have made sure you 
have the Java 2 SDK installed properly and created the java source file 
just open a 'dos command prompt' (or whatever the command line is 
called on you operating system) change the current directory to the 
where the file is and type the following:
javac SCJP_Tester.java
Then to run the program just type:
java SCJP_Tester
That's all.  If you like, you can create another file in that folder 
and call it something like run.bat and just put the text 'java 
SCJP_Tester' in it.  That way all you have to do to run the program is 
double click the run.bat file.
Okay, here is the java code that should go in SCJP_Tester.java.
import java.io.*;
import java.util.Vector;
import java.util.ListIterator;
public class SCJP_Tester{
  static Vector questions = new Vector();
  static Vector answers = new Vector();
  static int iTotalQuestionsAnswered=0;
  static int iTotalQuestionsAnsweredCorrectly=0;
  static float rScore = 0F;
  public static void main(String args[]) throws IOException  {
    BufferedReader oBufferedReader = new BufferedReader(
        new FileReader("scjp_fill_in_the_blanks.txt"));
    String sLineOfText;
    while((sLineOfText = oBufferedReader.readLine())!= null) {
      sLineOfText = sLineOfText.trim();
      if (sLineOfText.startsWith("#")) {
        //This is a comment so
        //we will just continue 
        //with the next line.
        continue;
      }
      questions.add(sLineOfText);
      if ((sLineOfText = oBufferedReader.readLine())!= null)
        answers.add(sLineOfText);
    }
    oBufferedReader.close();
    System.out.println("Here are some questions.");
    System.out.println("Press q to quit.");
    String sUserResponse = new String();
    while(!sUserResponse.equals("q")) {
      System.out.println("____________________________________");
      int iCurrentQuestion=(int)(Math.random()*questions.size());
      System.out.println("Question:");
      System.out.println((String)questions.get(iCurrentQuestion));
      BufferedReader in = new BufferedReader(
          new InputStreamReader( System.in ));
      sUserResponse = in.readLine();
      if (sUserResponse.equals((String)answers.get(iCurrentQuestion))) {
        System.out.println("     Right!");
        iTotalQuestionsAnswered++;
        iTotalQuestionsAnsweredCorrectly++;
      } else if (sUserResponse.equals("q")) {
        //If the user presses q then don't mark it
        //right or wrong.
      } else {
        System.out.println("Wrong.");
        System.out.println("The correct answer is: " + 
          (String)answers.get(iCurrentQuestion));
        iTotalQuestionsAnswered++;
      }
      rScore = (float)iTotalQuestionsAnsweredCorrectly / (float)
                iTotalQuestionsAnswered * 100F;
      rScore = java.lang.Math.round(rScore*10F)/10F;
      System.out.println("Your score is: " + rScore + "%");
      System.out.println("" + iTotalQuestionsAnsweredCorrectly + 
                         " out of " + iTotalQuestionsAnswered);
    }
  }
}