FileDocCategorySizeDatePackage
q1.javaAPI DocExample19708Thu Sep 05 15:06:22 BST 2002None

q1

public class q1 extends Object

Fields Summary
static Random
R
static String[]
arr
Constructors Summary
Methods Summary
public static booleancheckInteger(java.lang.String testInteger)

  try
  {
    Integer theInteger = new Integer(testInteger);//fails if not  a vaild number/integer
    return
      (theInteger.toString() != "") && //not empty
      (theInteger.intValue() > 10) && //greater than ten
      ((theInteger.intValue() >= 2) && (theInteger.intValue() <= 100000)) && //2>=X<=100000
      (theInteger.toString().charAt(0) == '3"); //first digit = 3               
  }
  catch (NumberFormatException err)
  {
    return false;
  }     
public static booleancheckInteger2(java.lang.String testInteger)

  for (int i = 0; i < testInteger.length(); i++)
    if (!Character.isDigit(testInteger.charAt(i)))
      return false;
  try
  {
    Integer theInteger = new Integer(testInteger);//fails if not  a vaild number/integer
    return
      (theInteger.toString() != "") && //not empty
      (theInteger.intValue() > 10) && //greater than ten
      ((theInteger.intValue() >= 2) && (theInteger.intValue() <= 100000)) && //2>=X<=100000
      (theInteger.toString().charAt(0) == '3"); //first digit = 3               
  }
  catch (NumberFormatException err)
  {
    return false;
  }     
public static booleancheckInteger3(java.lang.String testInteger)

  try
  {
    if (testInteger.equals("")) return false;
    if (testInteger.charAt(0) != '3") return false;
    Integer theInteger = new Integer(testInteger);//fails if not  a number
    return
      (theInteger.intValue() > 29) &&
      (theInteger.intValue() <= 40000);
  }
  catch (NumberFormatException err)
  {
    return false;
  }     
public static booleancheckInteger4(java.lang.String testInteger)

  for (int i = 0; i < testInteger.length(); i++)
    if (!Character.isDigit(testInteger.charAt(i)))
      return false;
  try
  {
    Integer theInteger = new Integer(testInteger);//fails if not  a number
    return
      (testInteger != "") && //not empty
      (theInteger.intValue() > 10) && //greater than ten
      ((theInteger.intValue() >= 2) && 
           (theInteger.intValue() <= 100000)) && //2>=X<=100000
      (testInteger.charAt(0) == '3"); //first digit is 3               
  }
  catch (NumberFormatException err)
  {
    return false;
  }     
public static booleancheckInteger5(java.lang.String testInteger)

  if (testInteger.equals("")) return false;
  if (testInteger.charAt(0) != '3") return false;
  for (int i = 1; i < testInteger.length(); i++)
    if (!Character.isDigit(testInteger.charAt(i)))
      return false;
  try
  {
    Integer theInteger = new Integer(testInteger);//fails if not  a number
    return
      (theInteger.intValue() > 29) &&
      (theInteger.intValue() <= 40000);
  }
  catch (NumberFormatException err)
  {
    return false;
  }     
public static booleancheckInteger6(java.lang.String testInteger)

  if (testInteger.equals("")) return false;
  if (testInteger.charAt(0) != '3") return false;
  //The character for digit 0 is (char) 48, etc.
  int val = testInteger.charAt(0) - 48;
  for (int i = 1; i < testInteger.length(); i++)
    if (Character.isDigit(testInteger.charAt(i)))
      val = (val*10) + testInteger.charAt(i) - 48;
    else
      return false;
  try
  {
    Integer theInteger = new Integer(val);//fails if not  a number
    return
      (theInteger.intValue() > 29) &&
      (theInteger.intValue() <= 40000);
  }
  catch (NumberFormatException err)
  {
    return false;
  }     
public static booleancheckInteger7(java.lang.String testInteger)

  if (testInteger.length() == 0) return false;
  if (testInteger.charAt(0) != '3") return false;
  //The character for digit 0 is (char) 48, etc.
  int val = testInteger.charAt(0) - 48;
  for (int i = 1; i < testInteger.length(); i++)
    if (Character.isDigit(testInteger.charAt(i)))
      val = (val*10) + testInteger.charAt(i) - 48;
    else
      return false;
  return
      (val > 29) &&
      (val <= 40000);
public static booleancheckInteger8(java.lang.String testInteger)

  int length;
  //integer between 30 and 39999, so string length
  //must be 2 to 5 digits
  if ( ( (length = testInteger.length()) < 2) ||
       (  length > 5) )
    return false;

  //first digit must be '3'
  if (testInteger.charAt(0) != '3") return false;

  //Now we know it is valid if all chars are digits
  switch(length)
  {
    case 5:
     if (!Character.isDigit(testInteger.charAt(4))) return false;
    case 4:
     if (!Character.isDigit(testInteger.charAt(3))) return false;
    case 3:
     if (!Character.isDigit(testInteger.charAt(2))) return false;
    case 2:
     if (!Character.isDigit(testInteger.charAt(1))) return false;
  }
  return true;
public static java.lang.StringgenInvalid()

  return "-" + R.nextInt(10000);
public static java.lang.StringgenNonnumber()

  return "X" + R.nextInt(10000);
public static java.lang.StringgenValid()

  while(true)
  {
    String s = Integer.toString(R.nextInt(100000-10)+11);
    if (s.charAt(0) == '3")
      return s;
  }
public static voidgenerateDataset1()

   

  initRand();
  for (int i = 0; i < arr.length; i++)
    arr[i] = genValid();
public static voidgenerateDataset2()

  initRand();
  for (int i = 0; i < arr.length; i+=2)
  {
    arr[i] = genValid();
    arr[i+1] = genInvalid();
  }
public static voidgenerateDataset3()

  initRand();
  for (int i = 0; i < arr.length; i+=3)
  {
    arr[i] = genValid();
    arr[i+1] = genInvalid();
    arr[i+2] = genNonnumber();
  }
public static voidinitRand()

   

  R = new Random(12345);
public static voidmain(java.lang.String[] args)

  main2(args);
  main2(args);
public static voidmain2(java.lang.String[] args)

  int REPEAT_1 = 1000000;
  int REPEAT_2 = 10;
  boolean[] b = new boolean[arr.length*REPEAT_2];

  if (args[0].equals("1"))
    test1();
  else if (args[0].equals("2"))
    test2(REPEAT_1);
  else if (args[0].equals("3"))
    test3(REPEAT_1);
  else if (args[0].equals("4"))
    test4(REPEAT_2, b);
  else if (args[0].equals("5"))
    test5(REPEAT_2, b);
  else if (args[0].equals("6"))
    test6(REPEAT_2, b);
  else if (args[0].equals("7"))
    test7(REPEAT_2, b);
  else if (args[0].equals("8"))
    test8(REPEAT_2, b);
  else if (args[0].equals("9"))
    test9(REPEAT_2, b);
  else if (args[0].equals("10"))
    test10(REPEAT_2, b);
  else if (args[0].equals("11"))
    test11(REPEAT_2, b);
  else if (args[0].equals("12"))
    test12(REPEAT_2, b);
  else if (args[0].equals("13"))
    test13(REPEAT_2, b);
  else if (args[0].equals("14"))
    test14(REPEAT_2, b);
  else if (args[0].equals("15"))
    test15(REPEAT_2, b);
  else if (args[0].equals("16"))
    test16(REPEAT_2, b);
  else if (args[0].equals("17"))
    test17(REPEAT_2, b);
  else if (args[0].equals("18"))
    test18(REPEAT_2, b);
  else if (args[0].equals("19"))
    test19(REPEAT_2, b);
  else if (args[0].equals("20"))
    test20(REPEAT_2, b);
  else if (args[0].equals("21"))
    test21(REPEAT_2, b);
  else if (args[0].equals("22"))
    test22(REPEAT_2, b);
  else if (args[0].equals("23"))
    test23(REPEAT_2, b);
  else if (args[0].equals("24"))
    test24(REPEAT_2, b);
  else if (args[0].equals("25"))
    test25(REPEAT_2, b);
  else if (args[0].equals("26"))
    test26(REPEAT_2, b);
  else if (args[0].equals("27"))
    test27(REPEAT_2, b);
public static voidtest1()

  long time = System.currentTimeMillis();
  checkInteger("34567");
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger once " + time);
public static voidtest10(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger3(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger3 " + time);
public static voidtest11(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger3(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger3 " + time);
public static voidtest12(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger3(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger3 " + time);
public static voidtest13(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger4(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger4 " + time);
public static voidtest14(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger4(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger4 " + time);
public static voidtest15(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger4(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger4 " + time);
public static voidtest16(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger5(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger5 " + time);
public static voidtest17(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger5(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger0 " + time);
public static voidtest18(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger5(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger5 " + time);
public static voidtest19(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger6(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger6 " + time);
public static voidtest2(int repeat)

  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
    checkInteger("1");
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger(\"1\") " + repeat + " times " + time);
public static voidtest20(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger6(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger6 " + time);
public static voidtest21(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger6(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger6 " + time);
public static voidtest22(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger7(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger7 " + time);
public static voidtest23(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger7(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger7 " + time);
public static voidtest24(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger7(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger7 " + time);
public static voidtest25(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger8(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger8 " + time);
public static voidtest26(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger8(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger8 " + time);
public static voidtest27(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger8(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger8 " + time);
public static voidtest3(int repeat)

  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
    checkInteger("30298");
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger(\"30298\") " + repeat + " times " + time);
public static voidtest4(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger(arr[i]);
//      if (!b[i+offset]) System.out.println(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger0 " + time);
public static voidtest5(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger(arr[i]);
//      if (!b[i+offset]) System.out.println(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger0 " + time);
public static voidtest6(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger(arr[i]);
//      if (!b[i+offset]) System.out.println(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger0 " + time);
public static voidtest7(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset1();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger2(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger2 " + time);
public static voidtest8(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset2();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger2(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger2 " + time);
public static voidtest9(int repeat, boolean[] b)

  System.out.println("Generating");
  generateDataset3();
  System.out.println("Timing");
  long time = System.currentTimeMillis();
  for (int j = 0; j < repeat; j++)
  {
    int offset = j*arr.length;
    for (int i = 0; i < arr.length; i++)
    {
      b[i+offset] = checkInteger2(arr[i]);
    }
  }
  time = System.currentTimeMillis() - time;
  System.out.println("checkInteger2 " + time);