[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [API]
Back to Subclassing, Inheritance, and Polymorphism. On to Loops.
Held Friday, September 10, 1999
Overview
Today, we will move from high-level consideration of classes and objects to more detailed consideration of the imperative internals of objects. In particular, we will consider the basic conditional statements of Java, which let your methods choose between alternatives.
Notes
Contents
Handouts
Summary
if
switch
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. (Some other languages do.)
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?
Tuesday, 10 August 1999
Thursday, 9 September 1999
Back to Subclassing, Inheritance, and Polymorphism. On to Loops.
[Instructions] [Search] [Current] [Syllabus] [Links] [Handouts] [Outlines] [Labs] [More Labs] [Assignments] [Quizzes] [Exams] [Examples] [Book] [Tutorial] [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.math.grin.edu/~rebelsky/Courses/CS152/99F/Outlines/outline.10.html
Source text last modified Thu Sep 9 22:08:12 1999.
This page generated on Tue Sep 21 11:09:55 1999 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu