/* Method3.java CIS 160 Dave Klick 2004-10-18 user-defined methods returning values */ import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Method3 { private static BufferedReader kbd = new BufferedReader( new InputStreamReader(System.in)); public static void main(String[] args) throws IOException { int year; boolean more = true; do { System.out.println("This program determines whether a given year is a leap year"); year = getYear(); if (isLeap(year)) { System.out.println(year + " is a leap year."); } else { System.out.println(year + " is not a leap year."); } System.out.println(); more = moreData(); } while (more); } private static int getYear() throws IOException { int y = 0; boolean valid = false; do { System.out.print("Enter a year: "); String s = kbd.readLine(); try { y = Integer.parseInt(s); valid = true; } catch (NumberFormatException e) { System.out.println("Invalid year entry. Try again!"); } } while (!valid); return y; } private static boolean isLeap(int y) { boolean leap = false; if (y % 4 == 0) leap = true; if (y % 100 == 0) leap = false; if (y % 400 == 0) leap = true; return leap; } private static boolean moreData() throws IOException { boolean go_on = false; char c; do { System.out.print("Do you want to enter more data? y/n: "); c = kbd.readLine().toUpperCase().charAt(0); if (c == 'Y') go_on = true; else if (c != 'N') System.out.println("Invalid response. Try again!"); } while (c != 'Y' && c != 'N'); return go_on; } }