CSC152 2005FS, Class 13: Conditionals
Overview:
* Happy Valentine's Day!
* Reading may be available for tomorrow
Admin:
* Questions on classes?
* Basics of Booleans
* If statements
* Lab
/Types in Java/
* Primitive types: int, float, long, double, ...
* Predefined classes (including some that correspond to primitive types)
* Your own classes
* One more primitive type: boolean
* Truth values: true, false
* Named after George Boole: Developed "calculus" of truth values
* Interesting operations:
Numbers n and m:
n < m
n > m
n <= m
n >= m
n == m ; Equal to, two signs to distinguish from assignment
n != m ; Not equal to
Boolean expressions b and c
(b && c) ; AND
(b || c) ; OR
!b ; NOT
In Scheme, you have "false" and "everything else"
In Java, a variable of type boolean can only be true or false
In Java, when you need a boolean expression, that expression can only evaluate to true or false
* Why use booleans?
if (boolean-expression) {
stuff-to-do-if-the-expression-evaluates-to-true
}
else {
stuff-to-do-if-the-expression-evaluates-to-false
}
* Meaning:
* Evaluate the boolean-expression
* If it evaluates to true, do "stuff...true"
* If it evaluates to false, do "stuff...false"
* Java won't have compiled the program if it can evaluate to soemthing else
* The else part is optional, in which case you "do nothing" if the boolean expression evaluates to false
* When you want to test a sequence of expressions
if (boolean-expression) {
stuff-to-do-if-the-expression-evaluates-to-true
}
else if (another-boolean-expression) {
stuff-to-do-if-the-first-evaluates-to-false-and-the-second-to-true
}
else if (yet-another-boolean-expression) {
stuff-yadda
}
else {
stuff-to-do-if-nothing-evaluates-to-true
}
public String method grade()
{
if ((!this.name.equals("ROLF") && (this.numeric > 93)) || (this.numeric >= 94)) {
return "A";
}
else if (this.numeric >= 90) {
return "A-";
}
else {
return "B-";
}
}
DO the lab!
Be prepared to reflect
* How do you call a method within a constructor?
this.method(params);
For example
this.cleanup();
How far did you get?
* Currently working on problem 1 (no negative denominator)
* Currently working on problem 2 (simplify)
* Currently working on problem 3 (comparison)
* Currently working on problem 4 (comparison, revisited)