Computer Science APEA 15-100, Summer 2009
Lab 2
Read these instructions first!
- The homework collaboration policy does not apply to labs. In
labs, you may work alone or in small groups. And you may show each
other your code, and help each other in any manner. This is
encouraged, in fact!
- That said, the participation policy is still in effect -- you must
not only attend labs, but actually work during the labs. If you have
completed the assigned work, then you should either help others, if they
request it, or you should delve deeper into the assigned material.
But you must use the lab time exclusively to explore the material covered in
that lab.
- In particular, you may not use lab time to do your homework.
- There is nothing to submit for labs.
- If you have your laptop with you, get Java and DrJava or JCreator
installed on it and use your laptop for the lab. (It is not required
that you have a laptop for lab, but today in particular it is a good idea to
get your laptop's environment properly set up.)
- Run the examples from today's lecture. Fiddle around with them a
bit, changing this and that and seeing how it changes their runtime
behaviors.
- Write a program that reads in a temperature in Celsius and displays the
same temperature in Fahrenheit. Use integer values with integer
division. Using your program, convert 1234°C
into °F. Be sure that your program
has a good user interface, with prompts for input and reasonably descriptive
output.
- Write a program that reads in a number and prints out twice its value.
Use this program to demonstrate overflow. In particular, using your
program, what is the exact value Java finds when doubling the value
1,987,654,321?
- Write a program that reads in two positive numbers x and y and prints
out the remainder when x is divided by y. Use this program to
demonstrate a runtime exception due to division by zero. Also use this
program to test how the % operator works when either or both of x and y are
negative.
- Rewrite the previous problem so that it works the same way, only it does
not use the % operator. Instead, use integer division along with a
couple other arithmetic operators to achieve the same effect.
- Flag of Belgium
In the file Lab2FlagOfBelgium.java, write a program that displays the
flag of Belgium:
(larger
image with details)
Note: This flag image is from the very informational
CIA
World Factbook, which includes a
flags-of-the-world page.
Your program should paint three filled rectangles, and should use built-in
colors (so the colors might not match exactly, but they will be close).
Also, your flag may not be fixed-sized, but rather must entirely fill the
window, even when the window is resized. While the window's size may
change, you may assume the window will be roughly "flag-shaped" -- in the
assigned homework, you will not be graded on how your flag appears in, say,
a tall thin window (which is not at all "flag-shaped").
- Flag of the Central African Republic
In the file Lab2FlagOfCentralAfricanRepublic.java, write a program that
displays the flag of the Central African Republic:
(larger
image with details)
Note: here you must color match using custom colors.
Also, draw the star using a circle that would just enclose the star.
And, as with all flags, be sure your flag fills the window and draws
correctly even when the window is resized.
Carpe diem!