Computer Science 15-100 (Sections T & U), Spring 2008
Class Notes: Ch 4+5: Classes, Methods, Conditionals, and Loops (1 of
3)
Logistics
- Schedule
- Hw4 due tomorrow
Tonight's office hours to be announced in today's class
- Quiz 4 on Tuesday (does not cover today's material)
- Reading:
- L&L Chapter 4: Writing Classes
Section 4.4 today: Anatomy of a Method
- L&L Chapter 5: Conditionals and Loops
Sections 5.1, 5.2, parts of 5.8 today
Topic Outline:
On the Merits of Attending
Recitation
Methods, Conditionals, and Loops
(Oh my!)
- Writing Static Methods
a) void methods with no parameters
class MyCode {
public static void
doSomething() {
System.out.println("Carpe diem");
}
public static void main(String[] args) {
doSomething();
doSomething();
}
}
b) void methods with one parameter
class MyCode {
public static void printSquare(int
n) {
System.out.println(n + "^2 = " + (n*n));
}
public static void main(String[] args) {
printSquare(2);
printSquare(3);
}
}
c) void methods with multiple parameters
class MyCode {
public static void printPower(double base, double exponent) {
System.out.println(base + "^" + exponent + " = " +
Math.pow(base,exponent));
}
public static void main(String[] args) {
printPower(2,3);
printPower(2,0.5); // square root!
}
}
d) int methods (Methods that Return Values)
class MyCode {
public static int getSquare(int n) {
return n*n;
}
public static void main(String[] args) {
int n, nSquared;
n = 3;
nSquared = getSquare(n);
System.out.println(n + "^2 = " + nSquared);
n = 4;
nSquared = getSquare(n);
System.out.println(n + "^2 = " + nSquared);
}
}
e) Methods can call other methods:
class MyCode {
public static int getSquare(int n) {
return n*n;
}
public static void printSquare(int n) {
System.out.println(n + "^2 = " + getSquare(n));
}
public static void main(String[] args) {
printSquare(3);
printSquare(4);
}
}
f) Can also have methods return double, char, String, boolean, or any other
type!
class MyCode {
public static double getCubeRoot(double d) {
return Math.pow(d,1.0/3.0);
}
public static void printCubeRoot(double d) {
System.out.println(d + "^(1/3) = " + getCubeRoot(d));
}
public static void main(String[] args) {
printCubeRoot(8);
printCubeRoot(9);
}
}
g) Local variables
Aside: Methods can have local variables (just like main):
h) Parameters are local variables!
i) Local variables are local -- they only exist within the method
where they are defined!
Aside: this means different methods can have local variables with the
same name, but these are different variables.
- Simple "for" Loops
a) Print the numbers from 1 to 5:
int x;
for (x=0; x<=5; x++) {
System.out.println("x = " + x);
}
b) Print the SUM of the numbers from 1 to 5:
int x;
int sum = 0;
for (x=0; x<=5; x++) {
System.out.println("x = " + x);
sum += x;
}
System.out.println("sum = " + sum);
- Simple "if" statements
a) Print the sum of the ODD numbers from 1 to 5:
int x;
int sum = 0;
for (x=0; x<=5; x++) {
if (x % 2 == 1) {
System.out.println("x = " + x);
sum += x;
}
}
System.out.println("sum = " + sum);
- Putting it together: Using Methods, Conditionals, and Loops:
isPrime and nthPrime!
carpe diem -
carpe diem - carpe diem - carpe diem
- carpe diem - carpe diem -
carpe diem - carpe diem - carpe
diem