Lab #2: Simple Java Programs
Fall 1999
(To be completed in your lab period, week of September 20)
In this lab you will be writing some simple programs in order to
get a feel for the language and to become
more familiar with the JDK. These programs, using variables, arithmetic
operators, and predefined methods, will perform input, calculate
results using the input, and output these results.
In the lectures, as well as in the textbook, several examples of simple Java
programs were presented and discussed. Using these examples as a
basis, you are to write four simple Java programs from scratch.
These programs will be compiled and executed in the same manner as
Hello.java was in lab #1.
The first program you are to write will print your name and home address on several lines. Your name will occur on the first line, with your street address on the second line, the city on the third line, the province (state or county) on the fourth line and the country on the fifth line. The following example illustrates the format you should use.
skule.ecf% java Address John Doe 123 College Street West Toronto Ontario Canada
For the second program, create a copy of the source code
(the .java file) for the program you
just wrote, and modify it so that it surrounds your name and address in a
box made of asterisks (* symbols). Do not modify the original
program since you need to be able to demonstrate it to the marker.
When executed, your output should look something like the example on the
next page. Note that the asterisks must be correctly aligned vertically on
the two sides of the box.
skule.ecf% java Square *************************** * * * John Doe * * 123 College Street West * * Toronto * * Ontario * * Canada * * * ***************************
For the next two programs, you will need to use the Stdin class,
which can be found in:
/share/copy/aps105/Stdin.java
In the third program, you are to have the user input a floating point
number, of type double in Java, and then to emit some properties
of this number.
Specifically, for an input value of x, you are to output, one
item per line:
Assume that numbers to be input are all within the range that can be
stored by a variable of type double.
The fourth program calculates the coinage required to make up a specific monetary value. It will accept as input two integer values, one for dollars and the other for cents, and will print as output the number of Toonies ($2 coins), Loonies ($1 coins), Quarters (25 cent coins), Dimes (10 cent coins), Nickels (5 cent coins), and Pennies (1 cent coins) where the sum of the coins is the value input. You must use as many of each type of coin as you can, starting with the largest denomination coin possible.
You do not need to worry about changing the coin name so that English
grammar of the output is correct in the case of a single coin.
That is, it is acceptable to have
1 Loonies instead of the more grammatically correct 1 Loonie.
You can also assume that users will enter numbers which are within
the correct range (i.e., cent values between 0 and 99).
The following example illustrates the format required for the output.
skule.ecf% java Coins How many dollars? 5 How many cents? 99 You will need 2 Toonies You will need 1 Loonies You will need 3 Quarters You will need 2 Dimes You will need 0 Nickels You will need 4 Pennies
Hint: Use modulo arithmetic on integers to determine the
number of each coin that you will need.
After you have all four programs working, you are ready to have your
lab marked. Ensure that each program works correctly by testing it on a
full range of input prior to marking.