|
Lesson FiveInput, selection, and repetition | |
Now we'll take a look at input, selection, and repetition. All the programs we will be writing be interactive, which means you and the programs you write will exchange communication. This will occur while the programs are in run time, or while the program is running. To provide the method to receive input, you must write System.in.read(). This method will tell the System.out.print() what the user typed from the keyboard. For example: the program prompts the user to type his or her initials and type ENTER after each one. The words throws Exception passes the error situation to the operating system. The two extra System.in.read(); statements absorb the ENTER key after the first one retrieves the information. Finally, the last System.out.println("Your initials are " + firstInit + middleInit + lastInit); echoes (or repeats) the users input back for the user for confirmation. The proper way of designing programs that interact with the user is by way of creating flowcharts as diagrams that show the steps needed to complete the program. Flowcharts are especially useful when designing programs using if() statements. This next program is a fill-out form that uses if() and if()...else statements: Notice the type of wood and the size of table gives you two different results. Using the if statement is called decision making. The program above allows the user to make the decision of what kind of wood the table is to be made out of and what size the table is to be. Once that decision is made, the user can find out how much the table is going to cost. You can also nest if...else statements within other if...else statements. Here's another program does calculations to the users input. There are special characters that you can use in decision making. Say you asked a child to give you a number between 1 and 10, and the child said, "13". 13 is not between 1 and 10. To write an argument to take care of this, you'd need to compare the number given with the numbers you provided. This is where the && (AND operator) comes in to play. You'd write your argument if(num >= 1 && num <= 10). The || (OR operator) can be used in cases such as the case of a character: if(letter == 'A' || letter == 'a'). Another way of making decisions is to use the switch() statement. With the switch() statement, everything is put in its' own catagory and read as needed. Below is ChooseManager program that calls the Event program for its' constructor and then uses the switch() statement along with the case:, break;, and default; statements. The switch() statement takes the "eventType" and asks if the case: is "C", "P", or default: for "N", then prints the proper message accordingly. Notice that this program also features the while loop as well as the AND (&&), and NOT (!=) operators, but not the OR (||) operator. These are allowed for the boolean (true or false) statement to work. In short, while eventType is not equal to "C", "P", and "N", the statement "Entry must be C or P or N!" must execute, otherwise produce the output from the switch() statement. Another looping command is the for() loop. The for() loop uses shortcut arithmetic operators. One of them are ++ and the other is --. If you wanted "num" to be incremented by one, you'd write ++num. The same goes for decrementing. However, if you want to write "num = num + 1;" in shorthand, you'd write num += 1;. Here is an example of how a for() loop works: As you can see, there are two for() loops in this program. The first one sets "testNum" to 1, says "process this loop until you've done it 100 times, then quit", and then adds 1 to "testNum". Nested inside the loop is an equation that uses modulo division (%) to determine when this loop ends. The second for() loop sets "num" to 1, says "as long as 'num' is less than 'testNum', process this loop", then adds 1 to "num". This loop also has a similar equation to find when the loop ends. However, it is designed to let the viewer see 20 lines at one time before going on to the next 20. |
|
Question: It seems as though these programs are the background of some similar forms I've filled out on the web. But, the thing is, I was able to type numbers. Why, after trying programs of my own like these, can't I use numbers? Answer: That's what we are going to cover next. The System.in.read() only reads character input, not integer. In order to read integers, you need to create an array. | |
If you have a questions about any of the lessons, feel free to ask. ![]() |