Ok I kinda need some help with this java program.. the question being asked is:
Creating a class called MyDate
Being by creating a method public boolean isLeapYear (int year) which returns true if the parameter year is a leap year. The rule to use for leap years is:
- Years which end in 00 are not leap years unless the year is divisible by 400 - any year not covered in the above rule is only a leap year if the year is divisible by 4
THEN...
create a method public boolean checkDay(int day, int month, int year). Assume that the month parameter is in the correct range (1-12 where 1 is january, 2 is february... 12 is december). The method should return true if the day paraemter is valid for the month and year combination. It is a valid day parameter if it is in the raneg 1 to the last day of the month.
- February has 28 days in non-leap years and 29 in Leap years - April, June, September and November have 30 days - all other months have 31 days
CODE SO FAR (not much)
class MyDate
public boolean isLeapYear(int year); { if year return true }
public boolean checkDay(int day, int month, int year);
(first public boolean part refers to first question, second boolean refers to start of 2nd question)
TESTER CODE:
public class MyDateTester {
public static void main(String args[]) {
/* * Remove the comments of next two method calls * and compile when Part A is complete */ //testLeapYears(); //testCheckDay();
/* * Remove the comments of next three method calls * and compile when Part B is complete */ // testGetDate(); //once with a bad year // testGetDate(); //once with a bad month // testGetDate(); //once with a bad day
/* * Remove the comments of next three method calls * and compile when Part C is complete */ // testDoomsdays(); // testComputeDayOfWeek(); // findDay();
}
public static void testLeapYears() {
MyDate aDate = new MyDate();
System.out.println("Testing isLeapYear(year):"); // test two non-leap years System.out.println("1999 " + aDate.isLeapYear(1999)); System.out.println("1900 " + aDate.isLeapYear(1900));
// test two leap years System.out.println("1992 " + aDate.isLeapYear(1992)); System.out.println("2000 " + aDate.isLeapYear(2000)); }
System.out.println("\nTesting the Day of the Week Computation:"); //test a few days System.out.println("27/11/1982 is the " + aDate.computeDayOfWeek(27,11,1982) + " Day of the week"); System.out.println("20/7/1969 is the " + aDate.computeDayOfWeek(20,7,1969) + " Day of the week");
}
public static void findDay() {
MyDate aDate = new MyDate(); int day, month, year, dayOfWeek; String weekday="";
year = aDate.getYear(); month = aDate.getMonth(); day = aDate.getDay(month,year); dayOfWeek = aDate.computeDayOfWeek(day,month,year); if (dayOfWeek == 0) weekday="Sunday "; else if (dayOfWeek == 1) weekday="Monday "; else if (dayOfWeek == 2) weekday="Tuesday "; else if (dayOfWeek == 3) weekday="Wednesday "; else if (dayOfWeek == 4) weekday="Thursday "; else if (dayOfWeek == 5) weekday="Friday "; else if (dayOfWeek == 6) weekday="Saturday "; System.out.println("\n" + day + "/" + month + "/" + year + " was a " + weekday);
}
}
KEYBOARD
import java.io.*; //tell the java compiler that we'll be doing i/o public class Keyboard { private static BufferedReader inputStream = new BufferedReader (new InputStreamReader(System.in)); /* Get an integer from the user and return it */ public static int getInteger() { try { return (Integer.valueOf(inputStream.readLine().trim()).intValue()); } catch (Exception e) { e.printStackTrace(); return 0; } } /* Get a double from the user and return it */ public static double getDouble() { try { return (Double.valueOf(inputStream.readLine().trim()).doubleValue()); } catch (Exception e) { e.printStackTrace(); return 0.0; } } /* Get a float from the user and return it */ public static float getFloat() { try { return (Float.valueOf(inputStream.readLine().trim()).floatValue()); } catch (Exception e) { e.printStackTrace(); return 0.0f; } } /* Get a string of text from the user and return it */ public static String getString() { try { return inputStream.readLine(); } catch (Exception e) { e.printStackTrace(); return ""; } } /* Get a char from the user and return it */ public static char getCharacter() { try { String in = inputStream.readLine().trim(); if (in.length() == 0) return (char)0; else return (in.charAt(0)); } catch (Exception e) { e.printStackTrace(); return (char)0; } } /* Get a boolean from the user and return it */ public static boolean getBoolean() { try { return (Boolean.valueOf(inputStream.readLine().trim()).booleanValue()); } catch (Exception e) { e.printStackTrace(); return false; } } }