The Artima Developer Community
Sponsored Link

Java Answers Forum
array

2 replies on 1 page. Most recent reply: May 7, 2002 2:25 PM by Julie

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 2 replies on 1 page
Julie

Posts: 13
Nickname: champ
Registered: Mar, 2002

array Posted: May 6, 2002 8:35 PM
Reply to this message Reply
Advertisement
I am given this method in the class and need to create a program which will allow me to print the days of the week in April 1-30 on my calendar using an array...I just need help with the array set up as I have done everything else:


public static int daysInMonth(int month, int year) {
int numberOfDays = 31;

switch (month) {
case 2: // February
numberOfDays = 28;
if (year % 4 == 0) {
numberOfDays = 29;
if (year % 100 == 0 && year % 400 != 0)
numberOfDays = 28;
}
break;

case 4: // April
case 6: // June
case 9: // September
case 11: // November
numberOfDays = 30;
break;
}

return numberOfDays;
}

/*
* This method returns
* the day of the week for a given date
*/
public static int dayOfTheWeek(int day, int month, int year) {


Thomas SMETS

Posts: 307
Nickname: tsmets
Registered: Apr, 2002

Re: array Posted: May 7, 2002 2:22 PM
Reply to this message Reply
First f all you could modify your code from
public static int daysInMonth ( int month, int year ) 
{
  int numberOfDays = 31;
  switch (month) 
  {
    case 2: // February
       numberOfDays = 28;
       if (year % 4 == 0) 
       {
         numberOfDays = 29;
         if ( year % 100 == 0 && year % 400 != 0 )
           numberOfDays = 28;
       }
       break;
 
     case 4: // April
     case 6: // June
     case 9: // September
     case 11: // November
       numberOfDays = 30;
     break;
}
     return numberOfDays;
}
 
/*
* This method returns
* the day of the week for a given date
*/
public static int dayOfTheWeek (int day, int month, int year) { 

to smthg slightly more efficient :
public static int daysInMonth ( int month, int year ) 
{
  switch (month) 
  {
    case 2: // February
       if ( ( year % 4 == 0 )
            & ( year % 100 != 0) 
            & ( year % 400 != 0 ) )
       return 29;
       retrun 28;   
 
     case 4: // April
     case 6: // June
     case 9: // September
     case 11: // November
       return 30;
     default
    retrun 31;
  } // end of SWITCH
}
 
/*
* This method returns
* the day of the week for a given date
*/
public static int dayOfTheWeek (int day, int month, int year) 


For getting the day of the week of what ever, I believe you should better give a look at java.util.GregorianCalendar...
Everything should be in it !
Unless you need to code it yourself !

Thomas SMETS,
SCJP2 - Brussels

Julie

Posts: 13
Nickname: champ
Registered: Mar, 2002

Re: array Posted: May 7, 2002 2:25 PM
Reply to this message Reply
it is an assignment and I need to code it myself...we were given the class to work with...and need to code the rest ourselves.
Any help would be appreciated...

Flat View: This topic has 2 replies on 1 page
Topic: Swing Applets Previous Topic   Next Topic Topic: telephone lookup using merge sort and binary search

Sponsored Links



Google
  Web Artima.com   

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