[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [Tutorial] [API]
Back to Reuse Through Inheritance and Polymorphism. On to Lab: Loops.
Held Tuesday, February 8, 2000
Overview
Today, we return to our considerations of programming in Java by investigating conditional operations in Java.
Notes
Contents
Summary
if statement
switch statement
DrawableCircle class.
RainbowCircle.
DrawableCircles we
can do with RainbowCircles.
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, 18 January 2000
Tuesday, 8 February 2000
Back to Reuse Through Inheritance and Polymorphism. On to Lab: Loops.
[Instructions] [Search] [Current] [News] [Syllabus] [Glance] [Links] [Handouts] [Project] [Outlines] [Labs] [Assignments] [Quizzes] [Exams] [Examples] [EIJ] [JPDS] [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/2000S/Outlines/outline.10.html
Source text last modified Tue Feb 8 09:41:18 2000.
This page generated on Sat Feb 19 16:08:08 2000 by Siteweaver. Validate this page's HTML.
Contact our webmaster at rebelsky@grinnell.edu