Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes: Ch 4+5: Classes, Methods, Conditionals, and Loops (4 of
3)
Logistics
- Schedule
- Hw5 due today
- Quiz 5 tomorrow
- Midterm moved to
Wednesday!!!!
- Reading:
- 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
- sumFrom1ToN(n)
- sumFromNToM(n,m) [ hint: watch out for case where m < n ]
- factorial(n)
- inverseFactorial(n)
- fibonacci(n)
- inverseFibonacci(n)
- isPerfectSquare(n)
- isPerfectPower(n,k)
- isPalindrome(n)
- gcd(n)
- lcm(n)
- isPrime(n)
- nthPrime(n)
- isPerfect(n)
- nthPerfect(n)
- primeCount(n) [pi(n)]
- goldbachPrime(n)
- 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".
- abs(ns)
- negate(ns)
- equal(ns1, ns2) [hint: watch out for leading zeros, and for
negative zero!]
- compare(ns1, ns2) [ ditto ]
- add(ns1, ns2)
- subtract(ns1, ns2) [hint: it is convenient to use "ten's
complement" here]
- multiply(ns1, ns2)
- divide(ns1, ns2)
- remainder(ns1, ns2)
- 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.
- caesarEncode(s, shift)
- caesarDecode(s, shift)
- center(s, width)
- contains(s, match)
- endsWith(s, match)
- equals(s1, s2)
- expandTabs(s, tabSize)
- getRectangle(w, h)
- getRightFacingTriangle(w, h)
- indexOf(s, match, fromIndex)
- isPalindrome(s)
- isPalindrome(s, ignoreCase, alphaOnly)
- lastIndexOf(s, match, fromIndex)
- mostFrequentChar(s)
- nthSplit(s, delimiter, n)
- replace(s, c1, c2)
- replace(s, s1, s2)
- reverse(s)
- startsWith(s, match)
- substring(s, fromIndex)
- substring(s, fromIndex, toIndex)
- toLowerCase(s)
- toUpperCase(s)
- trim(s)
File Methods
- inDictionary(s, dictionaryPath)
Graphics Methods
- drawCheckerboard
- drawHypocycloid
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem