The Artima Developer Community
Sponsored Link

Java Answers Forum
Java help

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Script

Posts: 2
Nickname: script
Registered: Feb, 2003

Java help Posted: Mar 2, 2003 6:15 AM
Reply to this message Reply
Advertisement
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));
}

public static void testCheckDay() {

MyDate aDate = new MyDate();

System.out.println("\nTesting checkDay(day,month,year):");
// test days that don't exist
System.out.print("(32,1,1989) " + aDate.checkDay(32,1,1989));
System.out.println(" (29,2,1989) " + aDate.checkDay(29,2,1989));
System.out.print("(30,2,1992) " + aDate.checkDay(30,2,1992));
System.out.println(" (32,3,1989) " + aDate.checkDay(32,3,1989));
System.out.print("(31,4,1989) " + aDate.checkDay(31,4,1989));
System.out.println(" (32,5,1989) " + aDate.checkDay(32,5,1989));
System.out.print("(31,6,1989) " + aDate.checkDay(31,6,1989));
System.out.println(" (32,7,1989) " + aDate.checkDay(32,7,1989));
System.out.print("(32,8,1989) " + aDate.checkDay(32,8,1989));
System.out.println(" (31,9,1989) " + aDate.checkDay(31,9,1989));
System.out.print("(32,10,1989) " + aDate.checkDay(32,10,1989));
System.out.println(" (31,11,1989) " + aDate.checkDay(31,11,1989));
System.out.println("(32,12,1989) " + aDate.checkDay(32,12,1989));
// check 13 real dates!
System.out.print("(31,1,1989) " + aDate.checkDay(31,1,1989));
System.out.println(" (28,2,1989) " + aDate.checkDay(28,2,1989));
System.out.print("(29,2,1992) " + aDate.checkDay(29,2,1992));
System.out.println(" (31,3,1989) " + aDate.checkDay(31,3,1989));
System.out.print("(30,4,1989) " + aDate.checkDay(30,4,1989));
System.out.println(" (31,5,1989) " + aDate.checkDay(31,5,1989));
System.out.print("(30,6,1989) " + aDate.checkDay(30,6,1989));
System.out.println(" (31,7,1989) " + aDate.checkDay(31,7,1989));
System.out.print("(31,8,1989) " + aDate.checkDay(31,8,1989));
System.out.println(" (30,9,1989) " + aDate.checkDay(30,9,1989));
System.out.print("(31,10,1989) " + aDate.checkDay(31,10,1989));
System.out.println(" (30,11,1989) " + aDate.checkDay(30,11,1989));
System.out.println("(31,12,1989) " + aDate.checkDay(31,12,1989));
}

public static void testGetDate() {

int day, month, year;
MyDate aDate = new MyDate();

year = aDate.getYear();
month = aDate.getMonth();
day = aDate.getDay(month,year);

System.out.println("Date is: " + day + "/" + month + "/" + year);

}

public static void testDoomsdays () {

MyDate aDate = new MyDate();

System.out.println("\nTesting getDoomsdayOfYear(year):");
//testing 3 years
System.out.println("1900 " + aDate.getDoomsdayForYear(1900));
System.out.println("1929 " + aDate.getDoomsdayForYear(1929));
System.out.println("1999 " + aDate.getDoomsdayForYear(1999));
System.out.println("1982 " + aDate.getDoomsdayForYear(1982));
System.out.println("1969 " + aDate.getDoomsdayForYear(1969));
System.out.println("\nTesting getDoomsdayOfMonth(month,year):");
//testing 12 months in non-leap years & jan,feb in a leap year
System.out.print(" 1, 1989 " + aDate.getDoomsdayForMonth(1,1989));
System.out.println(" 2, 1989 " + aDate.getDoomsdayForMonth(2,1989));
System.out.print(" 3, 1989 " + aDate.getDoomsdayForMonth(3,1989));
System.out.println(" 4, 1989 " + aDate.getDoomsdayForMonth(4,1989));
System.out.print(" 5, 1989 " + aDate.getDoomsdayForMonth(5,1989));
System.out.println(" 6, 1989 " + aDate.getDoomsdayForMonth(6,1989));
System.out.print(" 7, 1989 " + aDate.getDoomsdayForMonth(7,1989));
System.out.println(" 8, 1989 " + aDate.getDoomsdayForMonth(8,1989));
System.out.print(" 9, 1989 " + aDate.getDoomsdayForMonth(9,1989));
System.out.println(" 10, 1989 " + aDate.getDoomsdayForMonth(10,1989));
System.out.print("11, 1989 " + aDate.getDoomsdayForMonth(11,1989));
System.out.println(" 12, 1989 " + aDate.getDoomsdayForMonth(12,1989));
System.out.print(" 1, 1992 " + aDate.getDoomsdayForMonth(1,1992));
System.out.println(" 2, 1992 " + aDate.getDoomsdayForMonth(2,1992));

}


public static void testComputeDayOfWeek() {

MyDate aDate = new MyDate();

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;
}
}
}

Topic: Sharing data in threads Previous Topic   Next Topic Topic: Requesting help with a number sort/algorithm

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use