[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
Back to Object Basics Lab. On to Conditionals (2).
Held: Monday, February 14, 2005
Summary: Today we investigate conditional operations in Java.
Overview:
Notes:
Overview:
if statement.int, double, and the ilk)
String)
Point,
ThreeWordPhrase)
boolean value.
true or false. They
can take on no other values.
boolean values with
Boolean expressions.
x < y (less than)
x <= y (less than or equal to)
x == y (equal to; note there are two
equals signs)
x != y (not equal to)
x >= y (greater than or equal to)
x > y (greater than)
comes between) rather than prefix (
comes before) operations.
equals
method. (It does need to be defined for most classes.)
compareTo method.
boolean methods. For example,
consider Is this point within 1 unit of the origin?
/**
* Determine if the current point is within one unit of the origin.
*/
public boolean isNearOrigin() {
return (this.getRadius() < 1.0);
} // isNearOrigin()
b1 && b2 (and)
b1 || b2 (or)
!b (not)
bothersomeif it is within half a unit from either axis. The Boolean operations help us define such a function.
/**
* Determine if the current point is bothersome. That is, it is
* within half a unit from either axis.
*/
public boolean bothersome() {
return (Math.abs(this.xcoordinate) < 0.5) ||
(Math.abs(this.ycoordinate) < 0.5);
} // bothersome()
if or conditional statement is one of the
simplest control structures.
if statement.
if (test) {
statements;
} // if
boolean (truth) value.
For fluffier pancakes, decrease the milk by 10%.
If above 5000 ft, decrease the oven temperature by 25 degrees F.
if (test) {
statements;
} // if text
else {
statements;
} // not test
if is slightly different from Scheme's.
if selects between expressions and returns a value.
ifselects between statements and does not return
a value.
if
statements, such as Scheme's cond.
Instead, use the following:
if (test1) {
stuff1
}
else if (test2) {
stuff2
}
else if (test3) {
stuff3
}
else if (testn) {
stuffn
}
else {
default-stuff
}
Back to Object Basics Lab. On to Conditionals (2).
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[Honesty]
[On Teaching and Learning]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Outlines]
[Readings]
[Reference]
Misc:
[SamR]
[Java 1.5 API]
[Espresso]
[TAO of Java]
[CS152 2004F]
Disclaimer:
I usually create these pages on the fly
, which means that I rarely
proofread them and they may contain bad grammar and incorrect details.
It also means that I tend to update them regularly (see the history for
more details). Feel free to contact me with any suggestions for changes.
This document was generated by
Siteweaver on Wed May 11 10:55:50 2005.
The source to the document was last modified on Mon Jan 24 10:17:06 2005.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2005S/Outlines/outline.13.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby