CIS-2271 Homework #2: Decisions

Due: Thursday, September 11, 2014

In Chapter 2 read the section 2.5 about string handling. Read Chapter 3 on decisions.

For this assignment I ask you to write two short programs. In your Eclipse you can create a project, say Hw02, for this assignment and include two different source files in the project. It is okay if both files have a main method. They can be run separately.

  1. Write a program that accepts an integer representing a year (such as 2014) and then prints a message saying if that year is a leap year or not. The leap year rules are as follows. A year is a leap year if...

    1. ... it is divisible by 4
    2. ... unless it is also divisible by 100, in which case it is not a leap year
    3. ... unless it is also divisible by 400, in which case it is a leap year anyway despite rule #2.

    You can check if an integer is divisible by another using the remainder operator. For example, year % 4 == 0 is true if year is divisible by 4 (are parentheses needed in that expression?).

    The leap year rules are as complicated as they are because the time it takes for the Earth to go around the Sun is not exactly 365 days. It's not even exactly 365.25 days (if it were rule #1 above would be enough). The Julian calendar put in place by the Romans used only rule #1 and as a result the calendar was slowly drifting relative to the seasons. By the 16th century the drift was a full 10 days. Pope Gregory XIII introduced calender reform in 1582 which, among other things, entailed new leap year rules intended to keep the calendar from drifting (as quickly). Adoption of the "Gregorian Calendar" was not uniform and many countries delayed adoption for years, not all being entirely willing to follow the pope's lead on such matters. As a result for many parts of the world, the year 2000 was the first time in history when rule #3 above was applied. That won't happen again until 2400 which, by the way, is a leap year. See Wikipedia for more information.

  2. It is possible to write an "infinite loop" (a loop that executes forever) using code such as

            while (true) {
                // This code executes repeatedly forever.

    Technically the body of the loop runs as long as "true" is not "false" which is, of course, forever (perhaps if the universe ends in a singularity true will become false but I don't think we have to worry about that here).

    Write a program that prints a prompt such as "> " and the accepts an entire line of input using the nextLine() method in the Scanner class. The program should print that line unless it is either the string "quit" or "exit." In that case it the program should end by calling System.exit(0). Here is a sample run of the program:

            > Hello?
            > Is anyone there?
            Is anyone there?
            > exit

Be sure to include your name in comments in your programs. Be sure to add comments that explain what the different parts of your program are doing (they can be short comments, the programs aren't long).

Last Revised: 2014-09-03
© Copyright 2014 by Peter C. Chapin <>