Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes:  Ch 4+5:  Classes, Methods, Conditionals, and Loops (4 of 3)


Logistics

  1. Schedule
    1. Hw5 due today
    2. Quiz 5 tomorrow
    3. Midterm moved to Wednesday!!!!
  2. Reading:
    1. L&L Chapter 4:  Writing Classes  (just static methods; no instance data, instance methods, encapsulation, or constructors)
      L&L Chapter 5:  Conditionals and Loops

Topic Outline:

Mystery Code:

What will the following code fragments print out? Explain each of your answers.
(a)  int i = 0, j = 0;
     while (i <= 10) {
         j++;
         i++;
     }
     System.out.println(i + "," + j);

(b)  int i = 0, j = 0;
     while (i++ <= 10) {
         j++;
         i++;
     }
     System.out.println(i + "," + j);

(c)  int i = 0, j = 0;
     do {
         j++;
         i++;
     } while (i <= 10);
     System.out.println(i + "," + j);

(d)  int i = 0, j = 0;
     do {
         j++;
         i++;
     } while (i++ <= 10);
     System.out.println(i + "," + j);

(e)  int i = 0, j = 0;
     do {
         j++;
         i++;
     } while (++i <= 10);
     System.out.println(i + "," + j);

(f)  int i = 0, j = 0;
     for (i = 1; i <= 20; i += i)
         j++;
     System.out.println(i + "," + j);

(g)  int i = 0, j = 0, k = 0;
     for (i = 1; i <= 20 ; i += 5) {
         for (j = 3; j <= i; j += 3) {
             k++;
         }
     }
     System.out.println(i + "," + j + "," + k);

Practice Methods for Loops + Conditionals + Methods:


          Math Methods

  1. sumFrom1ToN(n)
  2. sumFromNToM(n,m)  [ hint:  watch out for case where m < n ]
  3. factorial(n)
  4. inverseFactorial(n)
  5. fibonacci(n)
  6. inverseFibonacci(n)
  7. isPerfectSquare(n)
  8. isPerfectPower(n,k)
  9. isPalindrome(n)
  10. gcd(n)
  11. lcm(n)
  12. isPrime(n)
  13. nthPrime(n)
  14. isPerfect(n)
  15. nthPerfect(n)
  16. primeCount(n) [pi(n)]
  17. goldbachPrime(n)
  18. nthTwinPrime(n)

    String+Math (NumericString) Methods
    These methods are similar to the BigInteger methods, only here we use Strings of decimal digits as our representation (where the only non-decimal-digit allowed is an optional leading minus sign '-').  Thus, these methods take Strings and return Strings.  We will assume the Strings are valid -- no need to check if they contain non-decimal digits.  And, of course, you may not use BigInteger or any other "Big Math" classes when implementing these methods!  Note that the parameters are named "ns" as a mnemonic for "numeric String".
     
  19. abs(ns)
  20. negate(ns)
  21. equal(ns1, ns2)  [hint: watch out for leading zeros, and for negative zero!]
  22. compare(ns1, ns2)  [ ditto ]
  23. add(ns1, ns2)
  24. subtract(ns1, ns2)   [hint: it is convenient to use "ten's complement" here]
  25. multiply(ns1, ns2)
  26. divide(ns1, ns2)
  27. remainder(ns1, ns2)
  28. pow(ns1, ns2)

    String Methods
    These methods should be implemented using only the following methods from the String class (and no other methods or classes from java.util.*):
    constructors, charAt, concat (and the "+" operator), equals (and "==" for null), and length.
     
  29. caesarEncode(s, shift)
  30. caesarDecode(s, shift)
  31. center(s, width)
  32. contains(s, match)
  33. endsWith(s, match)
  34. equals(s1, s2)
  35. expandTabs(s, tabSize)
  36. getRectangle(w, h)
  37. getRightFacingTriangle(w, h)
  38. indexOf(s, match, fromIndex)
  39. isPalindrome(s)
  40. isPalindrome(s, ignoreCase, alphaOnly)
  41. lastIndexOf(s, match, fromIndex)
  42. mostFrequentChar(s)
  43. nthSplit(s, delimiter, n)
  44. replace(s, c1, c2)
  45. replace(s, s1, s2)
  46. reverse(s)
  47. startsWith(s, match)
  48. substring(s, fromIndex)
  49. substring(s, fromIndex, toIndex)
  50. toLowerCase(s)
  51. toUpperCase(s)
  52. trim(s)

    File Methods
     
  53. inDictionary(s, dictionaryPath)

    Graphics Methods
     
  54. drawCheckerboard
  55. drawHypocycloid

  • carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem   -   carpe diem