Espresso: A Concentrated Introduction to Java

Laboratory: Conditionals

Summary: In this laboratory, you will begin working with conditional operations in Java.



Exercise 0: Preparation

a. Create a project and package for this lab. I'd recommend Conditional for the project and username.conditionals for the package.

b. Start Eclipse and load the project.

Exercise 1: Signing Numbers

a. Write a program, Sign, that prompts the user for a number, determines the sign of the number, and then prints out a message about the sign of the number. For example

Please enter a number: 23
23 appears to be positive.
Please enter a number -123.2
-123.2 appears to be negative.

b. Experimentally determine whether there are any inputs that you would consider non-zero that your program considers zero.

Exercise 2: Translating Nicknames

Pick five friends with nicknames and write a program, NickName, that prompts for a nickname and then outputs the person's real name. For example,

Please enter a nickname: SamR
SamR appears to be a nickname for Samuel A. Rebelsky.
Please enter a nickname: Ireland
Ireland appears to be a nickname for Erin O'Neil.
Please enter a nickname: Rusty K
Rusty K appears to be a nickname for Russell King Osgood.

Exercise 3: Near Equality

As you discovered in your explorations with numbers, double approximates real numbers. One problem with approximation is that a sequence of operations can lead to less and less good approximations. For example, you discovered that if you square the square root of two, you do not get something exactly equal to two.

In response to this problem, many programmers write code that determines whether two values are approximately equal.

Write a program that reads in two real numbers and determines whether they are approximately equal. You may choose the metric for approximate equality, but it should be a sensible metric.

Exercise 4: Simple Alphabetization

Write a program that reads in three strings and prints them out in alphabetical order. For example,

Enter a word: hello
Enter a word: goodbye
Enter a word: zebra
In alphabetical order: goodbye hello zebra

Alternatively, you can read all three words on the same line.

Enter three words separated by spaces: this is silly
In alphabetical order: is silly this


Monday, 14 February 2005 [Samuel A. Rebelsky]

Tuesday, 20 September 2005 [Samuel A. Rebelsky]

Wednesday, 8 February 2006 [Samuel A. Rebelsky]

This page was generated by Siteweaver on Thu Mar 30 15:24:35 2006.
The source to the page was last modified on Thu Feb 9 11:57:28 2006.
This page may be found at

You may wish to validate this page's HTML ; Valid CSS! ; Check with Bobby

Samuel A. Rebelsky