Computer Science APEA 15-100, Summer 2009
Lab 2


Read these instructions first!


  1. 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.)
     
  2. 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.
     
  3. 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.
     
  4. 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?
     
  5. 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.
     
  6. 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.
     
  7. 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"). 
     
  8. 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!