CS 11 Fall 2004 -- Lab 2


This week, we are going to write a couple of programs from scratch. These programs will require arithmetic and boolean operations, as well as conditional statements and loops.


Determining a leap-year

How do you know if a particular year is a leap-year? The rules are a little more complex than you might think: A leap year is divisible by 4, but not by 100, unless it is divisible by 400. So, 1984 was a leap-year. 1900 was not a leap-year. 2000 was, as you may recall, a leap year, and so will 2400.

Your assignment: Write a program named LeapYear that takes, as an integer, a year number. Your program should determine whether the given year is or is not a leap-year, and should emit output indicating the conclusion to the user.

Your program should have a particular limitation. Specifically, the concept of a leap-year was only instituted about 250 years ago; it only applies to years beyond 1753. Therefore, if the user enters a year prior to 1753, your program should (politely and clearly) inform the user that the requested year pre-dates leap-years. It should then give the user another opportunity to enter a valid year number.

How to get started: Unlike last week, you will be starting your program from scratch. The steps below will get you started.

  1. From your home directory, create a new directory for this lab:
    mkdir lab-2
  2. Change into your new directory to do some work:
    cd lab-2
  3. Start XEmacs (note that XEmacs is very similar to Emacs, but is a bit nicer -- we will use XEmacs from now on):
    xemacs &
  4. Open a new Java source code file. You will write your program in this file. Recall that for XEmacs, C-x C-f allows you to open a file. If the file doesn't exist, then XEmacs will create it. Open the file: LeapYear.java
  5. Begin your program with the following "magic" text:
    class LeapYear {
    
        public static void main (String[] args) {
    
        } // End of main
    
    } // End of class LeapYear
    	
  6. You can now add code into the middle of this file! Be sure to save your work periodically with C-x C-s. When you would like to test your code, be sure to compile it first with javac and then run it with java.

Submitting your work

When your work is complete, you should submit it using the cs11-submit command, like so:

cs11-submit lab-2 LeapYear.java

Remember that you can test to be sure that your work was submitted with the cs11-check-submission command. Also remember that if you re-submit your work for a given lab, the previous submission will be clobbered. Be sure, therefore, always to submit a completely working program.


This lab is due on Thursday, September 23rd at 11:59 pm!

Scott F. Kaplan
Last modified: Thu Sep 16 23:57:34 EDT 2004