Introduction to Programming (CPSC 124)
—Hobart & William Smith Colleges, Fall 2014
Class Notes—September 15, 2014
Home | Syllabus | Calendar | Class Notes | Labs and Projects | General Notes |

Topics

Conditional Execution

We developed a program that took a single command-line argument representing a year. The program then printed a sentence stating whether that year is a leap year:

public class Leapyear { public static void main(String[] args) { if (args.length >= 1) { // Food for future thought: Can we also test whether args[0] is // going to parse as an int? int y = Integer.parseInt(args[0]); boolean isLeap = (y%4) == 0 && (y % 100 != 0 || y%400 == 0); System.out.print("The year " + y); if (y < 1582) { System.out.print(" is too early to be "); } else if (isLeap) { System.out.print(" is "); } else { System.out.print(" is not "); } System.out.println("a leap year."); } else { System.out.println("USAGE: java LeapYear <year>"); } } }

John Lasseter