Example. Write a program that asks the user to enter an integer between 1 and 7. The program then displays the corresponding day of the week.
//File: Days.java
import CSLib.*;
public class Days
{
static final int
SUNDAY=1,
MONDAY=2,
TUESDAY=3,
WEDNESDAY=4,
THURSDAY=5,
FRIDAY=6,
SATURDAY=7;
public static void main (String [] args)
{
InputBox in = new InputBox();
in.setPrompt("Pick a number from 1 to 7
and "
+ "I'll give you the corresponding"
+ " day of the week........");
int dayType = in.readInt();
OutputBox out = new
OutputBox();
switch (dayType)
{
case
SUNDAY: out.print("Day " + dayType +
" is Sunday");
break;
case
MONDAY: out.print("Day " + dayType +
" is Monday");
break;
case
TUESDAY: out.print("Day " + dayType +
" is Tuesday");
break;
case
WEDNESDAY: out.print("Day " + dayType
+ " is Wednesday");
break;
case
THURSDAY: out.print("Day " + dayType +
" is Thursday");
break;
case
FRIDAY: out.print("Day " + dayType +
" is Friday");
break;
case
SATURDAY: out.print("Day " + dayType +
" is Saturday");
break;
default:
out.println ("You idiot. I said a number
between 1 and 7.");
}
}
}


Lab Exercise. (p. 141/# 6) Write an application to read an integer n between 0 and 100, and write n followed by the appropriate ordinal suffix (i.e., th, st, rd, or nd). So if the user types "3" your program should output "3rd". Update the Days program above to take advantage of your code.
Lab Exercise. (p. 144/# 12) As part of a medical diagnosis system for a poison control center you have to prepare the switch statement to identify proper treatment for ingestion of various substances. The types of poison being considered are aspirin, alcohol, tobacco, chloroform, rat poison, strong acid, strong alkali, strychnine, and kerosene. Except for strong acids, strong alkalis, strychnine, and kerosene, the proper treatment is to administer fluid in large quantities, induce vomiting, and administer the universal antidote. For poisoning with strong acids, administer water and then milk of magnesia or baking soda solution; do not induce vomiting. For poisoning with strong alkalis, administer water and then vinegar or lemon juice; do not induce vomiting. For strychnine or kerosene ingestion, do not induce vomiting; get medical help immediately! Define nine symbolic integer constants for these nine poisons. Write the switch statement using an integer variable PoisonType; the statement should print the proper treatment.
This is a good time to summarize where we are going. I would place our studies into three categories. First, there is the Java syntax, necessary to run the programs, but of limited long term usefulness. After all, Java is changing all the time. Come back in a few years and the syntax may be quite different. Second, there are the technical issues, the strategies and tactics which are essentially independent of the language. I would place initializing variables, the use of counters, and scope of variables in that category. Finally, there are the BIG ideas. These are the ideas that transcend the language and techniques. I would hope that, no matter what your major or your career goals, you come away from the course with a working knowledge of those big ideas.
All programs, whether in Pascal, FORTRAN, COBOL, BASIC, or Java are executed in top to bottom sequence.
This is implemented by such programming language structures as if-else and switch.
We are about to embark on a study of this last, and most important, programming structure.
Loops
Let's begin by recalling an example from a few days ago. The user enters an integers from 0 to 100, and the computer returns the integer with the appropriate suffix. For example, 4 returns 4th, 11, returns 11th, 3 returns 3rd, and so forth.
//File: Suffix.java
/*
ANALYSIS OF THE PROBLEM
INPUT: integer between 0 and 100
COMPUTATION: add appropriate suffix to number
Ex. 4 4th
Ex. 2 2nd
Ex. 3 3rd
Ex. 1 1st
OUTPUT: output the input data and the results
Where do I begin?
if (number is 11, 12, or 13)
do it separately
else
take advantage of the pattern
switch (number)
{
case 0: case 10: case 20: case 30: etc
add "th"
case 1:case 21:case 31:case 41: etc
add "st"
case 2: case 22: case 32: case 42: etc
add "nd"
etc.
NOT TOO CREATIVE
}
MORE CREATIVE
switch (number % 10) ==> divide by 10 and take the remainder
{
case 0: suffix = "th"; ==> this gets 0,
10, 20, 30, 40, 50, 60, 70, 80, 90, and 100!!!!
case 1: suffix = "st"; ==> this gets 1, 21, 31, 41, 51,
61, 71, 81, and 91
etc.
}
I think I've got it!
*/
import CSLib.*;
public class Suffix
{
public void doIt()
{
//INPUT SECTION
InputBox in = new InputBox();
in.setPrompt("Please type in a
number"
+ " between 0 and 100.");
int number = in.readInt();
String suffix=" ";
//If you don't INITIALIZE this, you may get
//error message
//CALCULATION SECTION
if
(number==11||number==12||number==13) //The ODDBALLS
suffix = "th";
else
//take advantage of the pattern
{
switch
(number % 10)
{
case 1: suffix =
"st";
break;
case 2: suffix =
"nd";
break;
case 3: suffix =
"rd";
break;
default: suffix =
"th";
}//end switch
}//end else
//OUTPUT SECTION
OutputBox out = new
OutputBox("Add Suffixes");
//All those \n's give you carriage returns or
extra spacing
out.println ("You gave me the number
"
+ number + ".\n\n\n");
out.println ("That corresponds to
the"
+ " adjective " + number
+ suffix + ".");
}//end doIt()
}


Upon trying this several times, it became obvious to everyone that it would be nice if we the computer could just ask us if we wanted to some more before quitting on us. This lead us to the third and most important of the big three ideas of structured programming, repetition or loops. The following modification of the Suffix program illustrates how easy it is to accommodate our wishes.
//File: SuffixLoop.java
/*
After trying to test all of the possibilities, we decided
that it would be nice if the computer could just ask us if
we wanted to continue. Then, we wouldn't have to keep
running our program again to try different numbers.
This brings us to the third of the big three ideas in
structured programming, repetition or loops.
All we need do is wrap up what we had done before into
a loop that keeps repeating for as long as we want it to.
*/
import CSLib.*;
public class SuffixLoop
{
public void doIt()
{
//KEEP REPEATING THE FOLLOWING
//UNTIL THE USER SAYS STOP
while (true)
{
//INPUT SECTION
InputBox in = new
InputBox();
in.setPrompt("Please type in a number"
+ " between 0 and 100.");
int
number = in.readInt();
String suffix="
";
//CALCULATION SECTION
if
(number==11||number==12||number==13)
suffix = "th";
else
//take advantage of the pattern
{
switch (number % 10)
{
case 1: suffix =
"st";
break;
case 2: suffix =
"nd";
break;
case 3: suffix =
"rd";
break;
default: suffix = "th";
}//end switch
}//end else
//OUTPUT SECTION
OutputBox out = new
OutputBox("Add Suffixes");
out.println ("You
gave me the number "
+ number + ".\n\n\n");
out.println ("That
corresponds to the"
+ " adjective " + number
+ suffix + ".");
//ASK THE USER IF WANTS
TO CONTINUE
//NOTE:
The InputBox method, eoi(), returns true
//
if we're at the "end of input"
//
That condition is generated if
//
the user types nothing in the textfield.
in.setPrompt("Again?
(hit OK or type N to quit)"); //Typing N causes
//eoi() to be false
char
answer = in.readChar();
if
(!in.eoi()) //NOTE: The symbol ! means NOT
break;
}//end while
}//end doIt()
}
Note that very little code was added to allow us to achieve our goals. We just surrounded our previous code with a while() loop. Otherwise, we were required to become familiar with the InputBox method, eoi(). That method returns a boolean value (either true or false), depending upon whether anything was entered into the box (false) or not (true).
//File: SuffixLoop2.java
/*
After trying to test all of the possibilities, we decided
that it would be nice if the computer could just ask us if
we wanted to continue. Then, we wouldn't have to keep
running our program again to try different numbers.
This brings us to the third of the big three ideas in
structured programming, repetition or loops.
All we need do is wrap up what we had done before into
a loop that keeps repeating for as long as we want it to.
*/
import java.awt.*;
import CSLib.*;
public class SuffixLoop2
{
public void doIt()
{
//KEEP REPEATING THE FOLLOWING
//UNTIL THE USER SAYS STOP
while (true)
{
//INPUT SECTION
InputBox in = new
InputBox();
in.setPrompt("Please type in a number"
+ " between 0 and 100.");
int
number = in.readInt();
String suffix="
";
//CALCULATION SECTION
if
(number==11||number==12||number==13)
suffix = "th";
else
//take advantage of the pattern
{
switch (number % 10)
{
case 1: suffix =
"st";
break;
case 2: suffix =
"nd";
break;
case 3: suffix =
"rd";
break;
default: suffix = "th";
}//end switch
}//end else
//OUTPUT SECTION
OutputBox out = new
OutputBox("Add Suffixes");
out.println ("You gave me the number "
+ number + ".\n\n\n");
out.println ("That
corresponds to the"
+ " adjective " + number
+ suffix + ".");
//ASK THE USER IF WANTS
TO CONTINUE
//NOTE:
The InputBox method, eoi(), returns true
//
if we're at the "end of input"
//
That condition is generated if
//
the user types nothing in the textfield.
in.setLocation (100,
100);
in.setPrompt("Again? (hit OK or type N to quit)"); //Typing N
causes eoi() to be false
char
answer = in.readChar();
//This is what makes
the loop end.....
if
(!in.eoi()) //NOTE: The symbol ! means NOT
System.exit(0); //closes the OutputBox and exits the program
}//end while
}//end doIt()
}
You'll need a client program to run this.
Note that I've bolded and enlarged several lines in the program to illustrate how inheritance (from parent classes) allows our InputBox objects and OutputBox objects to do things that we didn't expect at first glance at the documentation.
Lab Exercise. Rewrite homework #1 (HeartRate) as a loop, so that the user doesn't have to restart the program each time new data is entered.