Computer Science APEA 15-100, Summer 2009
Lab 6


Read these instructions first!


Note:  You may use loops and conditionals now.  You may not use Java concepts we have not yet covered, including arrays or methods from any classes in java.util.* to solve these problems.  Also, you may only use the charAt and length methods from the String class.  While they may be helpful, every problem here is solvable without them.


Also note:  For each of these problems, you should write your own tests!


1.  Class Notes on Loops
In class today, we explored using while and for loops to solve various problems (sumFrom1toN, isPrime, nthPrime).  As it turns out, we never looked at the online course notes for that lecture.  As step 1 in this lab, review the actual online notes and be sure to fully understand them!  There is nothing to submit for this portion of the lab.
 

2.  Programming Problems
Start with this file:  Lab6.java.  It contains these practice problems:

  1. factorial
  2. gcd
  3. lcm
  4. equal
  5. compare
  6. indexOf
  7. lastIndexOf

// Lab6.java

// Note:  You may not use Java concepts we have not yet covered,
// including arrays, or methods from any classes in java.util.* to
// solve these problems.  Also, you may only use the charAt and length
// methods from the String class. While these other methods or techniques
// may be helpful, every problem here is solvable without them.

public class Hw2Practice {

  ////////////////////////////////////////////////////
  /// factorial
  ////////////////////////////////////////////////////

  // This method takes an int x and returns the value
  // of x!, that is, x factorial, which is the product
  // of x * (x-1) * ... * 2 * 1.  Note that 0! equal 1.
  // The method returns -1 for any negative parameter.
  public static int factorial(int x) {
    return 42;
  }

  public static void testFactorial() {
    System.out.print("Testing factorial... ");
    assert(factorial(0) == 1);
    assert(factorial(1) == 1);
    assert(factorial(2) == 2);
    assert(factorial(3) == 6);
    assert(factorial(4) == 24);
    assert(factorial(5) == 120);
    assert(factorial(6) == 720);
    assert(factorial(-1) == -1);
    assert(factorial(-3) == -1);
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// gcd
  ////////////////////////////////////////////////////

  // This method takes two ints, x and y, and returns their
  // greatest common divisor, or gcd, using Euclid's method.
  // Returns -1 if x or y is non-positive.
  // According to Euclid, the gcd(x,y) == gcd(y,r), where
  // r is the remainder when x is divided by y.  This process
  // is continued until y equal 0, at which point x is the gcd.
  // Let's see it in action, finding the gcd of 100 and 80:
  //  gcd(100, 80) = gcd(80,20)  20 is the remainder of 100 / 80
  //               = gcd(20, 0)  0 is the remainder of 80 / 20
  // So the gcd of 100 and 80 is 20, which is correct.
  public static int gcd(int x, int y) {
    return 42;
  }

  public static void testGcd() {
    System.out.print("Testing gcd... ");
    assert(gcd(5,4) == 1);
    assert(gcd(100,80) == 20);
    assert(gcd(100,150) == 50);
    assert(gcd(1,33) == 1);
    assert(gcd(0, 204) == -1);
    assert(gcd(204, 0) == -1);
    assert(gcd(2*2*2*3*3*5*11*11,2*2*3*3*3*11*17) == (2*2*3*3*11));
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// lcm
  ////////////////////////////////////////////////////

  // This method takes two ints, x and y, and returns their
  // least common multiple, or lcm, or -1 if x or y is non-positive.
  // This does not require a loop, actually, because the product
  // of the lcm(x,y) and the gcd(x,y) equal the product x*y,
  // so we can use the gcd method to find the lcm.
  public static int lcm(int x, int y) {
    return 42;
  }

  public static void testLcm() {
    System.out.print("Testing lcm... ");
    assert(lcm(5,4) == 20);
    assert(lcm(100,80) == 400);
    assert(lcm(100,150) == 300);
    assert(lcm(1,33) == 33);
    assert(lcm(0, 204) == -1);
    assert(lcm(204, 0) == -1);
    assert(lcm(2*2*2*3*3*5*11*11,2*2*3*3*3*11*17) == (2*2*2*3*3*3*5*11*11*17));
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// equal
  ////////////////////////////////////////////////////

  // This method takes two strings and returns true if
  // both are non-null, both are the same length, and both
  // contain the same characters in the same order,
  // returning false otherwise.
  public static boolean equal(String s1, String s2) {
    return false;
  }

  public static void testEqual() {
    System.out.print("Testing equal... ");
    assert(equal("Abc","Abc") == true);
    assert(equal("A B cd!","A B cd!") == true);
    assert(equal("A B cd!","A B cd") == false);
    assert(equal("ABC","abc") == false);
    assert(equal("","") == true);
    assert(equal(null,"") == false);
    assert(equal(null,null) == false);
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// compare
  ////////////////////////////////////////////////////

  // This method takes two strings s1 and s2 and returns
  // the result of comparing them lexicographically.
  // If s1 < s2, this method must return a negative int
  // (any negative int will do!).  If s1 equals s2, this
  // method must return 0.  And if s1 > s2, this method must
  // return a positive value (any positive value will do!).
  // This test is case sensitive, so 'A' < 'a'.  In fact,
  // 'Z' < 'a'.
  // If either of s1 or s2 is null, the method should return 0.
  public static int compare(String s1, String s2) {
    return 42;
  }

  public static void testCompare() {
    System.out.print("Testing compare... ");
    assert(compare("Z","a") < 0);
    assert(compare("a","Z") > 0);
    assert(compare("ab","abc") < 0);
    assert(compare("abc","ab") > 0);
    assert(compare("Abc","Abc") == 0);
    assert(compare("A B cd!","A B cd!") == 0);
    assert(compare("A B cd!","A B cd") > 0);
    assert(compare("","") == 0);
    assert(compare("","a") < 0);
    assert(compare(null,"") == 0);
    assert(compare(null,null) == 0);
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// indexOf
  ////////////////////////////////////////////////////

  // This method takes a String s and a char c, and returns
  // the first index in s where c occurs.  If c does not occur
  // in s (or if s is null), the method returns -1.
  public static int indexOf(String s, char c) {
    return 42;
  }

  public static void testIndexOf() {
    System.out.print("Testing indexOf... ");
    assert(indexOf("",'a') == -1);
    assert(indexOf("a",'a') == 0);
    assert(indexOf("abcd",'c') == 2);
    assert(indexOf("abcd",'d') == 3);
    assert(indexOf("a 1#1 a",'#') == 3);
    assert(indexOf("a 1#1 a",'1') == 2);
    assert(indexOf(null,'a') == -1);
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// lastIndexOf
  ////////////////////////////////////////////////////

  // This method takes a String s and a char c, and returns
  // the LAST index in s where c occurs.  If c does not occur
  // in s (or if s is null), the method returns -1.
  public static int lastIndexOf(String s, char c) {
    return 42;
  }

  public static void testLastIndexOf() {
    System.out.print("Testing lastIndexOf... ");
    assert(lastIndexOf("",'a') == -1);
    assert(lastIndexOf("a",'a') == 0);
    assert(lastIndexOf("abcd",'c') == 2);
    assert(lastIndexOf("abcd",'d') == 3);
    assert(lastIndexOf("a 1#1 a",'#') == 3);
    assert(lastIndexOf("a 1#1 a",'1') == 4);
    assert(lastIndexOf("a 1#1 a",'a') == 6);
    assert(lastIndexOf(null,'a') == -1);
    System.out.println("Passed all tests!");
  }

  ////////////////////////////////////////////////////
  /// additional test methods
  ////////////////////////////////////////////////////

  public static void checkAssertsAreEnabled() {
    boolean assertsEnabled = false;
    try { assert(false); }
    catch (Throwable t) { assertsEnabled = true; }
    if (!assertsEnabled)
      throw new RuntimeException("assert statements not enabled!");
  }

  public static void testAll() {
    testFactorial();
    testGcd();
    testLcm();
    testEqual();
    testCompare();
    testIndexOf();
    testLastIndexOf();
  }

  ////////////////////////////////////////////////////
  /// main method
  ////////////////////////////////////////////////////

  public static void main(String[] args) {
    checkAssertsAreEnabled();
    testAll();
  }
}

Carpe diem!