[CLIS logo]

LBSC 790/INFM 718B
Building the Human - Computer Interface
Fall 2004
Self-Graded Exercise 2

This exercise should take about 2 hours to complete. In this exercise, you will write a simple Java program that demonstrates the use of the three basic control structures: (1) sequential, (2) conditional, and (3) iterative. Once you have completed this exercise, you should be ready to combine these three control structures to create more complex programs.

The easiest way to start will be to launch Eclipse. Use the File menu to create a new project; call it Exercise 2. Then create a new class called Exercise and ensure that the checkbox for "public static void main (String args[])" is selected. Then add a single statement inside the main method of the Exercise class that prints out "Hello World!" and run the program. This is exactly what you did in Exercise 1, so if you have trouble getting this far you should rerun the tutorial in exercise 1. You'll do something like this every time you start a new project, so be sure you are comfortable getting this far so that you can focus your attention on what you're really trying to learn.

Now all you need to do is change that one statement to be a sequence of two statements. Just add a second statement under the first that prints out "Goodbye Cruel World!" and run your program again. This demonstrates the sequential control structure; Java system simply executes your statements in the order that they appear in your program.

Okay, next we'll demonstrate conditional execution. Let's change the program so that it will print one or the other of those, but not both. First, define a boolean variable called "optimist" and set its value to "true". You can do this by adding a line at the top of the main class that says "boolean optimist=true;". Next, use an if statement to choose which of the two print statements will be executed. The syntax for an if statement is "if ( ... ) ... else ... ;" where the first "..." is a boolean expression (i.e., something that works out to "true" or "false"), the second "..." is a statement that is executed when the boolean expression is true, and the third "..." is executed when the boolean expression is false. It does not matter whether everything is on the same line or not. Set up your if statement so that "Hello World!" is printed when optimist is true and run your program to be sure that's what gets printed. Then change the program to make optimist false, and run it again to ensure that just by making that single change you have caused the program to print "Goodbye Cruel World!". If so, you have mastered conditional execution!

Okay, now for the third one. Lets modify the program again so that it does whatever it is going to do 5 times. The control structure to do this is the for statement, which has the syntax "for (int i=0; i<5; i++) { ... }". Again, it does not matter whether this all appears on the same line. Put a for statement outside your if statement (in other words, make the "..." in the for statement the entire contents of your main method except the declaration of "optimist" (which should now be setting optimist to false, after finishing the demonstration of the conditional statement). Now run you program; it should print "Goodbye Cruel World!" five times, on five lines. If it does, you've demonstrated the conditional statement and you're done!

But what if you got stuck somewhere? That's actually not bad -- the reason that you do exercises like this is to learn what you know and what you don't know. Obviously, you'll want to try to get unstuck by backing up and trying to figure out what it is you don't understand. If your problem is with Eclipse, run the tutorial again and see if you can figure out how to get it to do what you want. If your problem is with Java, maybe your problem is as simple as a typing mistake -- try typing in and running a few examples from the text (Head First Java) before trying to write your own programs. Next, try to find examples that contain parts that look like what you are trying to do, and see if you can understand how they are doing it; copying snippets of working code is not a sin in programming -- it is often a good idea that can save you a lot of work. The creativity comes in how you put things together.

So what do you do if it still is not working? Send a question to the TA or to me by email. We might be able to help if you can describe what you tried well enough for us to duplicate the problem; if you include your code, this will be much easier for us. What if we can't help? Then you get to the real purpose of this assignment -- bring your question to class. Listening in class is useful, but it has nowhere near the value for learning that asking questions does. So the real purpose of this exercise is to help you to know what questions you should be asking.

One final word of advice. Don't spend more than two hours on this; it is not a hard assignment. Indeed, you will do something very much like this during the final exam, and you will do it then in the first 5 minutes of the exam because it will not be the main point of what you will be demonstrating then -- these are simply the skills that you need any time you write a program in Java using Eclipse. But if you get stuck somewhere and can't get unstuck on your own, you could waste a lot of time running down blind alleys. So give this a couple of hours, and if you're still struggling with it, put it aside and bring your questions to class.

Doug Oard
Last modified: Fri Aug 20 22:06:06 2004