[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
Back to Reuse Through Inheritance and Polymorphism. On to Lab: Loops.
Held Monday, September 11, 2000
Summary
Today we return to our considerations of programming Java by
investigating conditional operations in Java, particularly the
if and cond operations.
Notes
Overview
int and its
ilk)
String)
Point)
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)
/**
* 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.
if (test) {
statements;
} // if text
else {
statements;
} // not test
if
statements, such as Scheme's cond
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 (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.
Do sections J4.2 and J4.4 of lab J4: conditionals.
What are some other ways we might use conditionals with the
Point class?
What are some other ways in which you might expect to use conditionals?
Wednesday, 23 August 2000
Thursday, 24 August 2000
Monday, 11 September 2000
Back to Reuse Through Inheritance and Polymorphism. On to Lab: Loops.
[Current] [News] [Glance] [Search] [Instructions] [Links] [Handouts] [Project] [Outlines] [Labs] [Homeworks] [Quizzes] [Exams] [Examples] [EIJ] [API]
Disclaimer Often, these pages were created "on the fly" with little, if any, proofreading. Any or all of the information on the pages may be incorrect. Please contact me if you notice errors.
This page may be found at http://www.cs.grinnell.edu/~rebelsky/Courses/CS152/2000F/Outlines/outline.10.html
Source text last modified Wed Oct 25 10:05:37 2000.
This page generated on Fri Oct 27 08:19:49 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu