Computer Science Fundamentals (CS153 2003S)
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
Back to Objects, Methods, and Classes. On to Loops.
Held: Monday, 31 March 2003
Related Pages:
Assignments:
Notes:
Overview:
if statement.switch statement.int and its
ilk)
String)
Point,
Rational)
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)
equals
method. (It does need to be defined for most classes.)
boolean methods.
For example,
consider Is this point within 1 unit of the origin?We might code this as follows:
/**
* Determine if the current point is within one unit of the origin.
*/
public boolean nearOrigin() {
return (this.distanceFromOrigin() < 1.0);
} // nearOrigin()
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
if (test1) {
stuff1
}
else if (test2) {
stuff2
}
else if (test3) {
stuff3
}
else if (testn) {
stuffn
}
else {
default-stuff
}
if statements, such a solution is neither readable nor
efficient.
switch statement
(like the case statement in Pascal).
switch (integer-valued-expression) {
case value1:
stuff-to-do;
break;
case value2:
stuff-to-do;
break;
...
default:
stuff-to-do;
break;
}
break, execution will continue into
the next case.
byte, short, int,
long, or char.
break statement has other uses, which you'll see later.
DateTester.java,
SimpleDate.java,
SimpleInput.java, and
SimpleOutput.java.
Rational class that we discussed before break?
Thursday, 15 January 2003 [Samuel A. Rebelsky]
Sunday, 30 March 2003 [Samuel A. Rebelsky]
Monday, 31 March 2003 [Samuel A. Rebelsky]
Back to Objects, Methods, and Classes. On to Loops.
[Skip to Body]
Primary:
[Front Door]
[Current]
[Glance]
-
[EC]
[Honesty]
[Instructions]
[Links]
[Search]
[Syllabus]
Groupings:
[EBoards]
[Examples]
[Exams]
[Handouts]
[Homework]
[Labs]
[Lab Writeups]
[Outlines]
[Readings]
[Reference]
ECA:
[About]
[Grades]
[Quizzes]
[Submit Work]
[Change Password]
[Reset Password]
Misc:
[Experiments in Java]
[Scheme Reference]
[Scheme Report]
[CS153 2002S (Walker)]
[CS151 2003S (Rebelsky)]
[CS152 2000F (Rebelsky)]
[SamR]
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 Tue May 6 09:20:50 2003.
The source to the document was last modified on Mon Mar 31 08:28:15 2003.
This document may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS153/2003S/Outlines/outline.33.html.
You may wish to
validate this document's HTML
;
;
Check with Bobby