473,503 Members | 1,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calendar program. Help with steps

55 New Member
I am supposed to be makig a calendar program that will eventually allows the user to insert a number and the date will come up according to that number. But before we do that I need help from someone to give me small steps until we get it working correctly.
This calendar program is supposed to have a number of if then statements. Here is my calendar program so far. Right now my calendar has a constructor identifying a month, day, and year. I need help with the next method(ToString). I want this method to have something like if calendarMonth =1 then it equals January, or if calendarMonth = 2 then it is Febuary and so on.. I also want it to say if it doesn't equal a number 1-12 then it should print out invalid number in a string.


Expand|Select|Wrap|Line Numbers
  1.  
  2. import java.util.Calendar;
  3.  
  4. public class Date
  5. {
  6.    int calendarDay;
  7.    int calendarMonth;
  8.    int calendarYear;
  9.    int currentDate;
  10.  
  11.     public Date(int day, int month, int year)
  12.     {
  13.        if((calendarMonth >=1) && (calendarMonth <=12))
  14.  
  15.           calendarMonth = month;
  16.           else
  17.           {
  18.           calendarMonth = 0;    
  19.           calendarDay = 0;
  20.           }    
  21.  
  22.            if ((calendarDay >=1) && (calendarDay <=31))
  23.  
  24.            calendarDay = day;
  25.            else
  26.            calendarDay = 0;
  27.  
  28.  
  29.      }
  30.  
  31.     public String toString()
  32.     {
  33.         String calMonth;
  34.         {   
  35.             {
  36.             if (calendarMonth != (>=1 && <=12))
  37.             calMonth = "invalid number";
  38.             }
  39.  
  40.             {
  41.             if(calendarMonth == 1)
  42.             calMonth = "January";
  43.             return calMonth;
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.             elseif(calendarMonth == 2)
  51.             calMonth = "Febuary";
  52.             return calMonth; 
  53.             }
  54.  
  55.  
  56.     }
  57.  
  58.  
  59.  }
  60.  

Do you see what I am trying to say in line 36, which is an error. I am trying to say if a number does not equal a number between 1 and 12 then calendarMonth equals a string saying invalid number
Feb 9 '09 #1
38 3046
r035198x
13,262 MVP
1.)
Expand|Select|Wrap|Line Numbers
  1. if(month < 1 || month > 12) {
  2.     calMonth = "Invalid Month";
  3. }
  4.  
2.) Multiple statements inside the if must be enclosed in braces
Expand|Select|Wrap|Line Numbers
  1. if(month == 1) {
  2.        calMonth = "January";
  3.        return calMonth;
  4. }
  5.  
3.) If you are supposed to write your own calendar class then why are you importing the one from the java.util package?
4.) Enums do a better job for storing constants with properties. Have a read at what they are about.
5.) At the top of the Java forum threads listing is a thread titled "Read this First". It contains links that you need ... badly.
Feb 9 '09 #2
falconsx23
55 New Member
Ok thanks alot for the first method, I will try it out when I get the chance. As for the import calendar class, I thought I was supposed to type that in even though I am making my own calendar program. And I willd read the thread that states "Read this First". I am sorry for that
Feb 9 '09 #3
JosAH
11,448 Recognized Expert MVP
This is what boolean expressions are all about: if a condition X is true you want to do something; it X is not true you want to do something else:

Expand|Select|Wrap|Line Numbers
  1. if (X) {
  2.    // do something
  3. }
  4. if (X) {
  5.    // don't do anything
  6. }
  7. else {
  8.    // do something else
  9. }
  10.  
That empty if statement in the second case is clumsy; the Java language offers a few operators that help us:

Expand|Select|Wrap|Line Numbers
  1. if (!X) {
  2.    // do something else
  3. }
  4.  
That looks a bit better already. Your condition X is:

Expand|Select|Wrap|Line Numbers
  1. month >= 1 && month <= 12
  2.  
So your condition for an illegal month number looks like this:

Expand|Select|Wrap|Line Numbers
  1. if (!(month >= 1 && month <= 12)) {
  2.    // do something else
  3. }
  4.  
I literally substituted your condition for X but I had to add a couple of parentheses to make it work. Here's the boolean logic part: every expression !(A&&B) can be rewriten as (!A) || (!B). De Morgan figured that out. Lets apply that rule:

Expand|Select|Wrap|Line Numbers
  1. if (!(month >= 1) || !{month <= 12)) {
  2.    // do something else
  3.  
That doesn't look much better; we can do better: if it is not so that a month is greater or equal to one, it must be less than one. The same applies for a month not being less or equal than twelve. Our last version looks like this:

Expand|Select|Wrap|Line Numbers
  1. if (month < 1 || month > 12) {
  2.    // do something else
  3. }
  4.  
Which is exactly what was suggested in an above reply. btw, that Calendar interface stinks.

kind regards,

Jos
Feb 9 '09 #4
falconsx23
55 New Member
Ok thanks alot Jos. I tried out the statement and here is what it looks like

Expand|Select|Wrap|Line Numbers
  1. public String toString()
  2.     {
  3.         String calMonth;
  4.         {
  5.             if (calendarMonth < 1 || calendarMonth > 12) 
  6.             { 
  7.               calMonth = "Invalid Date"; // do something else 
  8.               return calMonth;
  9.             } 
  10.  
  11.  
  12.             if(calendarMonth == 1)
  13.             {
  14.             calMonth = "January";
  15.             return calMonth;
  16.             }
  17.  
  18.  
  19.             if(calendarMonth == 2)
  20.             {
  21.             calMonth = "Febuary";
  22.             return calMonth; 
  23.             }
  24.         }  
  25.  
  26.     }
I changed Month into calendarMonth in line 5 so that the compiler would recongnize the variable. I also have an error stating that I am missing a return statement. Or is the error displayed because I have yet to insert all of the months.
Feb 9 '09 #5
Nepomuk
3,112 Recognized Expert Specialist
You have to insert the other months and the last one should be something like
Expand|Select|Wrap|Line Numbers
  1. else
  2. {
  3.    calMonth = "December";
  4.    return calMonth;
  5. }
otherwise it will not realise that all of the possible cases are dealt with.

Apart from that, the code block after the initial String calMonth; doesn't need braces (although they aren't wrong either). Oh, and I'd recommend using else if(calenderMonth == x) for all 2 <= x <= 11.

Greetings,
Nepomuk
Feb 9 '09 #6
falconsx23
55 New Member
Thanks for replying. And I tried programming the if statements without the braces, but the compiler states that I have an else statement without an if statement. I inserted the method, but now I have an error stating that I have a missing return statement.

Expand|Select|Wrap|Line Numbers
  1.     public String toString()
  2.     {
  3.         String calMonth;
  4.         {
  5.             if (calendarMonth < 1 || calendarMonth > 12) 
  6.               {
  7.               calMonth = "Invalid Date"; // do something else 
  8.               return calMonth;
  9.               }
  10.  
  11.  
  12.             else if(calendarMonth == 1)
  13.             {
  14.             calMonth = "January";
  15.             return calMonth;
  16.             }
  17.  
  18.  
  19.             else if(calendarMonth == 2)
  20.             {
  21.             calMonth = "Febuary";
  22.             return calMonth; 
  23.             }
  24.  
  25.  
  26.  
  27.             else if(calendarMonth == 3)
  28.             {
  29.             calMonth = "March";
  30.             return calMonth; 
  31.             }
  32.  
  33.  
  34.            else if(calendarMonth == 4)
  35.             {
  36.             calMonth = "April";
  37.             return calMonth; 
  38.             }
  39.  
  40.  
  41.             else if(calendarMonth == 5)
  42.             { 
  43.             calMonth = "May";
  44.             return calMonth; 
  45.             }
  46.  
  47.  
  48.             else if(calendarMonth == 6)
  49.             {
  50.             calMonth = "June";
  51.             return calMonth; 
  52.             }
  53.  
  54.  
  55.             else if(calendarMonth == 7)
  56.             {
  57.             calMonth = "July";
  58.             return calMonth; 
  59.             }
  60.  
  61.             else if(calendarMonth == 8)
  62.             {
  63.             calMonth = "August";
  64.             return calMonth; 
  65.             }
  66.  
  67.             else if(calendarMonth == 9)
  68.             {
  69.             calMonth = "September";
  70.             return calMonth; 
  71.             }
  72.  
  73.             else if(calendarMonth == 10)
  74.             {
  75.             calMonth = "October";
  76.             return calMonth; 
  77.             } 
  78.  
  79.             else if(calendarMonth == 11)
  80.             {
  81.             calMonth = "November";
  82.             return calMonth; 
  83.             }
  84.  
  85.             else if(calendarMonth == 12)
  86.             {
  87.             calMonth = "December";
  88.             return calMonth; 
  89.             }
  90.  
  91.  
  92.  
  93.         }  
  94.  
  95.     }
ok nevermind that error. I see the problem. I just simply deleted all of the return calMonth; methods and just put one at the end. I also set String calMonth = null;
Feb 10 '09 #7
falconsx23
55 New Member
Ok sorry for the double post, but I could not find the edit button for some reason. The next method I will have to do is a leap year method. I am first gonna see if I can do it on my own before I ask for your help. I will let you know in time.
Feb 10 '09 #8
falconsx23
55 New Member
Once again I am sorry for the double post, but the edit post button was not up for some reason. Anyway I am trying to get the leap year to work for my calendar, but before I do that I felt that I had to identify what a year is first.

So here is my whole program. I thought If I give you my whole program again, it will make it easier for you guys to understand. I want to first make sure that I am on the right track. The part that I inserted was line 26 - line 30. The next step that I want to do is make the leap year method.



Expand|Select|Wrap|Line Numbers
  1. import java.util.Calendar;
  2. public class Date
  3. {
  4.    int calendarDay;
  5.    int calendarMonth;
  6.    int calendarYear;
  7.    int currentDate;
  8.  
  9.     public Date(int day, int month, int year)
  10.     {
  11.        if((calendarMonth >=1) && (calendarMonth <=12))
  12.  
  13.           calendarMonth = month;
  14.           else
  15.           {
  16.           calendarMonth = 0;    
  17.           calendarDay = 0;
  18.           }    
  19.  
  20.            if ((calendarDay >=1) && (calendarDay <=31))
  21.  
  22.            calendarDay = day;
  23.            else
  24.            calendarDay = 0;
  25.  
  26.            if(calendarYear <= 1999)
  27.  
  28.             calendarYear = year + 1900;
  29.             else
  30.             calendarYear  = year + 2000;       
  31.  
  32.      }
  33.  
  34.     public String toString()
  35.     {
  36.         String calMonth = null;
  37.  
  38.             if (calendarMonth < 1 || calendarMonth > 12) 
  39.               {
  40.               calMonth = "Invalid Date"; // do something else 
  41.  
  42.               }            
  43.             else if(calendarMonth == 1)
  44.             {
  45.             calMonth = "January";
  46.  
  47.             }
  48.  
  49.  
  50.             else if(calendarMonth == 2)
  51.             {
  52.             calMonth = "Febuary";
  53.  
  54.             }
  55.  
  56.  
  57.  
  58.             else if(calendarMonth == 3)
  59.             {
  60.             calMonth = "March";
  61.  
  62.             }
  63.  
  64.  
  65.            else if(calendarMonth == 4)
  66.             {
  67.             calMonth = "April";
  68.  
  69.             }
  70.  
  71.  
  72.             else if(calendarMonth == 5)
  73.             { 
  74.             calMonth = "May";
  75.  
  76.             }
  77.  
  78.  
  79.             else if(calendarMonth == 6)
  80.             {
  81.             calMonth = "June";
  82.  
  83.             }
  84.  
  85.  
  86.             else if(calendarMonth == 7)
  87.             {
  88.             calMonth = "July";
  89.  
  90.             }
  91.  
  92.             else if(calendarMonth == 8)
  93.             {
  94.             calMonth = "August";
  95.  
  96.             }
  97.  
  98.             else if(calendarMonth == 9)
  99.             {
  100.             calMonth = "September";
  101.  
  102.             }
  103.  
  104.             else if(calendarMonth == 10)
  105.             {
  106.             calMonth = "October";
  107.  
  108.             }             else if(calendarMonth == 11)
  109.             {
  110.             calMonth = "November";
  111.  
  112.             }
  113.  
  114.             else if(calendarMonth == 12)
  115.             {
  116.             calMonth = "December";
  117.  
  118.  
  119.  
  120.             }
  121.  
  122.  
  123.             return calMonth; 
  124.  
  125.  
  126.     }
  127.  
  128. }
Feb 10 '09 #9
Nepomuk
3,112 Recognized Expert Specialist
Well, those year correcting lines can't be quite correct - just try out a few years:
Expand|Select|Wrap|Line Numbers
  1. if(calendarYear <= 1999)
  2.    calendarYear = year + 1900;
  3. else
  4.    calendarYear  = year + 2000;
Say, calendarYear = 2000, then you'll end up in the else branch and that will produce the year 4000. If you enter 00 instead, you'll get 1900. So overthink those lines again, will you?

Apart from that, the algorithm for calculating if a year is a leap year can be found on Wikipedia, so try to translate it to Java code. It's really not difficult.

Greetings,
Nepomuk
Feb 11 '09 #10
falconsx23
55 New Member
Ok I think I see what you are saying. When I tested out the method I did recieve a number like 3890 when I entered 1990. So I changed the lines and is this what you mean. It looked correct to me.

Expand|Select|Wrap|Line Numbers
  1. if(calendarYear <= 1999) 
  2.  
  3.             calendarYear = year + 00; 
  4.             else 
  5.             calendarYear  = year + 00;
ok nevermind. That has to be right because I got the leap year method to work perfectly(Well at least when I tested it). Here is the leap year method:


Expand|Select|Wrap|Line Numbers
  1. public String leapYear()
  2.           {
  3.              String calYear = null; 
  4.               if (calendarYear % 4 == 0)
  5.               {
  6.  
  7.                   if (calendarYear % 100 != 0)
  8.                   {
  9.                    calYear = calendarYear + " is a leap Year";
  10.                    return calYear;
  11.                   }
  12.                   else if (calendarYear % 400 == 0)
  13.                   {
  14.                    calYear = calendarYear + " is a leap Year";
  15.                    return calYear;
  16.                   }
  17.                 }  
  18.                 else
  19.                   {
  20.                    calYear = calendarYear + " is not a leap year";
  21.                    return calYear;
  22.                   }
  23.  
  24.  
  25.  
  26.               return calYear;
  27.             }
Feb 11 '09 #11
falconsx23
55 New Member
Ok the next thing I must do is create a DaysInMonth method, that returns the correct number of days in a month. This may be a little challenging. Ill try it out myself, but feel free to give me somewhere to start.
Feb 11 '09 #12
JosAH
11,448 Recognized Expert MVP
@OP: I've seen these huge posts of you passing by again and again; have a look at the following solution for the month names; no comment because it's a spoiler:

Expand|Select|Wrap|Line Numbers
  1. private static final String[] months= {
  2.    "month < 1",
  3.    "January", "February", "March", "April", "May", "June",
  4.    "July", "August", "September", "October", "November", "December",
  5.    "month > 12"
  6. };
  7. ...
  8. public static String monthName(int month) {
  9.    return months[Math.min(13, Math.max(0, month))];
  10. }
  11.  
Do with it what you want but please note that there's no repeating code in there.

kind regards,

Jos
Feb 11 '09 #13
falconsx23
55 New Member
Thanks alot Jos. I will try out the code. I wasn't even close to what you had.
Feb 11 '09 #14
Nepomuk
3,112 Recognized Expert Specialist
OK, now this code here:
Expand|Select|Wrap|Line Numbers
  1. if(calendarYear <= 1999) 
  2.  
  3.             calendarYear = year + 00; 
  4.             else 
  5.             calendarYear  = year + 00;
works, but it's completely useless. What ever the result, it always adds 0 to the year.

What I expect you were trying to do is make sure, that if e.g. 99 is entered, that is counted as 1999. What I'd suggest you do is something like this:
  • Choose some year, which should separate the dates. I'll explain more about this later, but basically it's what you did with 1999. (A common choice is 1970 by the way, for various reasons.)
  • Check, if the year given is under 100
  • If so, check if it would count as a year before or after the date you chose. So for example, would the number 60 count as before or after 1970 (so would it be 1960 or 2060). Then change the year it according to that result.
  • If not, it will probably be bigger than, say, 1900 and you don't have to do anything.
@falconsx23
OK, let's have a quick look at that method. Say, we enter the year 2000 (which wasn't a leap year), it will check:
Expand|Select|Wrap|Line Numbers
  1. if (1900 % 4 == 0) // true
  2.               {
  3.  
  4.                   if (1900 % 100 != 0) // false
  5.                   {
  6.                    //...
  7.                   }
  8.                   else if (1900 % 400 == 0) // false
  9.                   {
  10.                    //...
  11.                   }
  12.                 }  
  13.                 //...
So, no output? I tested it and it didn't work for me.

What you want can be done with the algorithms I posted in my last answer, so have another look at those.

Greetings,
Nepomuk
Feb 11 '09 #15
JosAH
11,448 Recognized Expert MVP
@Nepomuk
Ahem, it was a leap year because 2000 can be evenly divided by 400.

kind regards,

Jos
Feb 12 '09 #16
falconsx23
55 New Member
umm Nepomuk, are you taling about something like this when you talked about my calendar year mathod.

Expand|Select|Wrap|Line Numbers
  1. if(calendarYear < 100) 
  2.            {
  3.  
  4.                 if (calendarYear > 40)
  5.                 {
  6.                 calendarYear = calendarYear + 1900;
  7.                 }
  8.                 else 
  9.                 {
  10.                 calendarYear  = year + 2000;        
  11.                 }
  12.  
  13.  
  14.           } 
And for some reason I cannot seem to enter 08 as the year. It will say that the integer is too large. And it always adds the year to 2000. Im getting confused here. Little help or explanation please. You are right Nepomuk that is what i was trying to do with the year. For example when I entered 99 for the year it would just come out as 2099.
Feb 12 '09 #17
JosAH
11,448 Recognized Expert MVP
@falconsx23
For the compiler an integer that starts with a zero introduces a number in octal representation: 00, 01, 02 ... 07. There is no digit 8 nor 9 in an octal number hence the compiler wining.

kind regards,

Jos

ps. Do you want to introduce new millenium bugs or what?
Feb 12 '09 #18
falconsx23
55 New Member
Ok JosAH thanks. Than made sense. And umm no I do not want you to introduce me into new millenium bugs yet. But now now I am trying to revise the constructor to use the daysInMonth method to determine whether the
date is valid. I have an idea on how to do this, but Im not to sure that it will work.
Feb 12 '09 #19
JosAH
11,448 Recognized Expert MVP
@falconsx23
Take the array approach again:

Expand|Select|Wrap|Line Numbers
  1. int daysInMnt[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
  2.  
Only when there are 28 days in the month you should check the year: if it's a leap year add one.

kind regards,

Jos
Feb 12 '09 #20
falconsx23
55 New Member
Thanks again. I am terrible when it comes to arrays. Thats why I try my best to stay away from them. I have a hard time understanding them. But thanks for the help
Feb 12 '09 #21
JosAH
11,448 Recognized Expert MVP
@falconsx23
You can't stay away from them, you have to deal with them. An array is like a block of identical houses. The first house has number 0, the next has number 1, the next 2, the next 3 and so forth. Every 'house' can contain one single thing, just as one single variable can contain one single thing. So if I have this:

Expand|Select|Wrap|Line Numbers
  1. int thing;
  2.  
or I can have this:

Expand|Select|Wrap|Line Numbers
  1. int[] thing= new int[42];
  2.  
The first example is just one int and the second one is a block of 42 houses where each one can contain one int. Their house numbers are 0,1,2 ... 41 (check that) and an integer in house, say, 32, can be reached as thing[32].

There's not much more to arrays.

kind regards,

Jos
Feb 12 '09 #22
falconsx23
55 New Member
Ok thanks for the quick explanation. I actually understand what you mean. But am I supposed to use your method in a If statement. That seems the only way that I can determine if a date is valid or not. Also I know I said revise my constructor to determine whether the date is valid or not , but would it make sense to put the statment you gave me into the constructor.

I tried using the int daysInMnt array as an If statement in my constructor and it would not work
Feb 12 '09 #23
falconsx23
55 New Member
Ok here is my program. It took me a while and I am still not finished. This calendar prgrom now returns the number of days in a month when a month is entered including when Febuary is a leap year. I also revised my constructor using the daysInMonth method to determine if the number of days in a month are valid or invalid.

Now I have to write a function called changeDate that takes a day, month, and year as parameters. And if the date is valid it will change the date and return true. If invalid it will make no changes and return false.

Expand|Select|Wrap|Line Numbers
  1. import java.util.Calendar; 
  2. public class Date 
  3.    int calendarDay; 
  4.    int calendarMonth; 
  5.    int calendarYear; 
  6.    int currentDate; 
  7.  
  8.     public Date(int day, int month, int year) 
  9.     { 
  10.  
  11.        if((calendarMonth >=1) && (calendarMonth <=12)) 
  12.  
  13.           calendarMonth = month; 
  14.  
  15.           else 
  16.           { 
  17.           calendarMonth = 0;     
  18.           calendarDay = 0; 
  19.           }     
  20.  
  21.  
  22.          if ((day > 0) && (day <= month))
  23.  
  24.             calendarDay = day;
  25.             else
  26.             calendarDay = 0;
  27.  
  28.  
  29.            if(calendarYear <= 1999)  
  30.  
  31.  
  32.  
  33.                 calendarYear = year + 00; 
  34.  
  35.                 else  
  36.  
  37.                 calendarYear  = year + 00;         
  38.  
  39.  
  40.  
  41.  
  42.               }
  43.  
  44.     public String toString() 
  45.     { 
  46.         String calMonth = null; 
  47.  
  48.             if (calendarMonth < 1 || calendarMonth > 12)  
  49.               { 
  50.               calMonth = "Invalid Date"; // do something else  
  51.  
  52.               }             
  53.             else if(calendarMonth == 1) 
  54.             { 
  55.             calMonth = "January"; 
  56.  
  57.             } 
  58.  
  59.  
  60.             else if(calendarMonth == 2) 
  61.             { 
  62.             calMonth = "Febuary"; 
  63.  
  64.             } 
  65.  
  66.  
  67.  
  68.             else if(calendarMonth == 3) 
  69.             { 
  70.             calMonth = "March"; 
  71.  
  72.             } 
  73.  
  74.  
  75.            else if(calendarMonth == 4) 
  76.             { 
  77.             calMonth = "April"; 
  78.  
  79.             } 
  80.  
  81.  
  82.             else if(calendarMonth == 5) 
  83.             {  
  84.             calMonth = "May"; 
  85.  
  86.             } 
  87.  
  88.  
  89.             else if(calendarMonth == 6) 
  90.             { 
  91.             calMonth = "June"; 
  92.  
  93.             } 
  94.  
  95.  
  96.             else if(calendarMonth == 7) 
  97.             { 
  98.             calMonth = "July"; 
  99.  
  100.             } 
  101.  
  102.             else if(calendarMonth == 8) 
  103.             { 
  104.             calMonth = "August"; 
  105.  
  106.             } 
  107.  
  108.             else if(calendarMonth == 9) 
  109.             { 
  110.             calMonth = "September"; 
  111.  
  112.             } 
  113.  
  114.             else if(calendarMonth == 10) 
  115.             { 
  116.             calMonth = "October"; 
  117.  
  118.             }             else if(calendarMonth == 11) 
  119.             { 
  120.             calMonth = "November"; 
  121.  
  122.             } 
  123.  
  124.             else if(calendarMonth == 12) 
  125.             { 
  126.             calMonth = "December"; 
  127.  
  128.  
  129.  
  130.             } 
  131.  
  132.  
  133.             return calMonth;  
  134.  
  135.  
  136.     } 
  137.  
  138.           public String leapYear()
  139.           {
  140.              String calYear = null; 
  141.               if (calendarYear % 4 == 0)
  142.               {
  143.  
  144.                   if (calendarYear % 100 != 0)
  145.                   {
  146.                    calYear = calendarYear + " is a leap Year";
  147.                    return calYear;
  148.                   }
  149.                   else if (calendarYear % 400 == 0)
  150.                   {
  151.                    calYear = calendarYear + " is a leap Year";
  152.                    return calYear;
  153.                   }
  154.                 }  
  155.                 else
  156.                   {
  157.                    calYear = calendarYear + " is not a leap year";
  158.                    return calYear;
  159.                   }
  160.  
  161.  
  162.  
  163.               return calYear;
  164.             }
  165.  
  166.  
  167.             public static final String[] months= 
  168.             { 
  169.  
  170.                 "month < 1", 
  171.                 "January", "February", "March", "April", "May", "June", 
  172.                 "July", "August", "September", "October", "November", "December", 
  173.                 "month > 12" 
  174.             };            
  175.  
  176.             public static String monthName(int month) 
  177.             { 
  178.                 return months[Math.min(13, Math.max(0, month))]; 
  179.  
  180.             }             public int daysInMonth(int month)
  181.             {
  182.  
  183.                 if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month ==12 )
  184.                 {
  185.                    calendarDay = 31;
  186.                 } 
  187.  
  188.                 else if(month == 4 || month == 6 || month == 9 || month == 11)
  189.                 calendarDay = 30;
  190.  
  191.                 else if(calendarYear % 4 == 0 && month == 2 )
  192.  
  193.                      calendarDay = 29;
  194.                 else if(month == 2)
  195.                 calendarDay = 28;
  196.  
  197.  
  198.  
  199.  
  200.  
  201.                 else
  202.                    {
  203.                    calendarDay = 0;
  204.  
  205.                    }
  206.  
  207.                 return calendarDay;
  208.            }
  209.  
  210.            public String changeDate(int day, int month, int year)
  211.            {   
  212.                String g = null
  213.                if(calendarMonth = month && calendarDay = day &&  calendarYear = year + 00) 
  214.                g = "True";
  215.                else
  216.                g = "False";
  217.  
  218.                return g
  219.  
  220.            }
  221.  
  222.  
  223.  
  224.  
  225. }  
Lines 211- 219 is what I tried to change
Feb 13 '09 #24
JosAH
11,448 Recognized Expert MVP
This entire thing is getting messier by the day; first design the API of your class, i.e. define which methods you have and what they're supposed to do; don't think about how they're supposed to do it.

Also define a small contract, e.g. you don't want to carry around an invalid Date object, e.g. new Date(42, 42, 42) is not supposed to succeed, the constructor can simply throw an IllegalArgumentException (one of the standard exception classes).

Get rid of your double implementations (e.g. your toString() method is the same as the monthName() method). Define separate methods for separate tasks, e.g. define a small boolean isLeapYear(int year) method and use it where needed.

Only keep your current code to laugh at and pick out the few useful parts but don't keep on hacking on this non-designed monster.

Also study arrays, you need them and study the boolean type because things such as 'return "True"' are preposterous.

A last tip: look for the file src.zip in your JDK installation directory. If contains the source code for all the standard classes that come with your distribution. Read (parts of) the code for the GregorianCalendar class and see for yourself.

kind regards,

Jos
Feb 13 '09 #25
Nepomuk
3,112 Recognized Expert Specialist
@JosAH
You're right, I changed the year a few times and forgot to change that "wasn't" into a "was".

About the code, I'll have another look at it later, but I'm sure Jos' advice is good, so keep to that.

Greetings,
Nepomuk
Feb 13 '09 #26
falconsx23
55 New Member
Ok this is the last exercise and I have been struggling with this the past two days. It has to do the following.
**
* Method that returns the difference in days
* between this date and another date.
* If dates are equal, then the result is 0
* If current date (this) is before the
* parameter date (that) then answer is positive.
* @return
*/

I put my program in javadoc, but I have no idea on how to upload it onto this site or thread so that you guys can view it.



Expand|Select|Wrap|Line Numbers
  1. /**
  2.  * A calendar program that returns
  3.  * a date, by month, day and year
  4.  * @return     
  5.  */
  6.  
  7. import java.util.Calendar;  
  8. public class Date  
  9. {  
  10.    int calendarDay;  
  11.    int calendarMonth;  
  12.    int calendarYear;  
  13.    int currentDate;  
  14.  
  15.    /**
  16.     * A constructor with year, month, and day 
  17.     * specified as parameters. 
  18.     * @return     
  19.     */
  20.     public Date(int day, int month, int year)  
  21.     {  
  22.         /**
  23.          * Month is on range from 1 -12
  24.          */
  25.        if((calendarMonth >=1) && (calendarMonth <=12))  
  26.           calendarMonth = month;  
  27.        else  
  28.        {  
  29.           calendarMonth = 0;      
  30.           calendarDay = 0;  
  31.           }      
  32.  
  33.   /**
  34.    * The days is the number that is
  35.    * greater than 0 and less than 
  36.    * or equal to the month.
  37.    * month is the number of days in a month.
  38.    * @return     
  39.    */
  40.          if ((day > 0) && (day <= month)) 
  41.             calendarDay = day; 
  42.          else 
  43.             calendarDay = 0; 
  44.  
  45.   /**
  46.    * To determine the year.
  47.    * calendar year = year.
  48.    * when user puts in a year.
  49.    * Ex 1998
  50.    * Program returns 1998.
  51.    * @return     
  52.    */
  53.            if(calendarYear <= 1999)   
  54.                 calendarYear = year;  
  55.           else   
  56.                 calendarYear  = year;          
  57.     } 
  58.  
  59.     /**
  60.      * This will return a string like Febuary
  61.      * for the current date.
  62.      * If the month or dat is wrong than
  63.      * it is set to invalid date.
  64.      * @return     
  65.      */
  66.     public String toString()  
  67.     {  
  68.         String calMonth = null;  
  69.         if (calendarMonth < 1 || calendarMonth > 12)   
  70.         {  
  71.               calMonth = "Invalid Date"; // do something else   
  72.         }              
  73.        else if(calendarMonth == 1)  
  74.        {  
  75.             calMonth = "January";  
  76.         }  
  77.         else if(calendarMonth == 2)  
  78.         {  
  79.             calMonth = "Febuary";  
  80.          }  
  81.          else if(calendarMonth == 3)  
  82.          {  
  83.             calMonth = "March";  
  84.          }  
  85.          else if(calendarMonth == 4)  
  86.          {  
  87.             calMonth = "April";  
  88.          }  
  89.          else if(calendarMonth == 5)  
  90.          {   
  91.             calMonth = "May";  
  92.          }  
  93.          else if(calendarMonth == 6)  
  94.          {  
  95.             calMonth = "June";  
  96.          }  
  97.          else if(calendarMonth == 7)  
  98.          {  
  99.             calMonth = "July";  
  100.          }  
  101.          else if(calendarMonth == 8)  
  102.          {  
  103.             calMonth = "August";  
  104.          }  
  105.          else if(calendarMonth == 9)  
  106.          {  
  107.             calMonth = "September";  
  108.          }  
  109.          else if(calendarMonth == 10)  
  110.          {  
  111.             calMonth = "October";  
  112.          }
  113.          else if(calendarMonth == 11)  
  114.          {  
  115.             calMonth = "November";  
  116.          }  
  117.          else if(calendarMonth == 12)  
  118.          {  
  119.             calMonth = "December";  
  120.          }  
  121.          return calMonth;   
  122.     }  
  123.     /** 
  124.      * A leap year method. 
  125.      * Every year that is divisble by 4
  126.      * is a leap year. 
  127.      * A year is not a leap year if it is divisble by 100.
  128.      * If year is divisible by 400 than it is a leap year.
  129.      * Method returns "true" is year is a leap year.
  130.      * @return     
  131.      */
  132.  
  133.           public String leapYear() 
  134.           { 
  135.              String calYear = null;  
  136.               if (calendarYear % 4 == 0) 
  137.               { 
  138.                   if (calendarYear % 100 != 0) 
  139.                   { 
  140.                    calYear = calendarYear + " is a leap Year"; 
  141.                    return calYear; 
  142.                   } 
  143.                   else if (calendarYear % 400 == 0) 
  144.                   { 
  145.                    calYear = calendarYear + " is a leap Year"; 
  146.                    return calYear; 
  147.                   } 
  148.                 }   
  149.                 else 
  150.                   { 
  151.                    calYear = calendarYear + " is not a leap year"; 
  152.                    return calYear; 
  153.                   } 
  154.               return calYear; 
  155.             } 
  156.  
  157.             public static final String[] months=  
  158.             {  
  159.                 "month < 1",  
  160.                 "January", "February", "March", "April", "May", "June",  
  161.                 "July", "August", "September", "October", "November", "December",  
  162.                 "month > 12"  
  163.             };             
  164.  
  165.             public static String monthName(int month)  
  166.             {  
  167.                 return months[Math.min(13, Math.max(0, month))];  
  168.             }            
  169.    /** 
  170.     * method that returns number of days in a month.
  171.     * @return     
  172.     */
  173.             public int daysInMonth(int month) 
  174.             { 
  175.                 if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12 ) 
  176.                    calendarDay = 31; 
  177.                 else if(month == 4 || month == 6 || month == 9 || month == 11) 
  178.                    calendarDay = 30; 
  179.                 else if(calendarYear % 4 == 0 && month == 2 ) 
  180.                    calendarDay = 29;                  
  181.                 else if(month == 2) 
  182.                    calendarDay = 28; 
  183.                else 
  184.                { 
  185.                    calendarDay = 0; 
  186.                } 
  187.                 return calendarDay; 
  188.            } 
  189.  
  190.   /** 
  191.    * boolean method that takes day, month and year as parameters.
  192.    * When user enters a valid date than the date is changed.
  193.    * If user inserts invalid date then date stays the same.
  194.    * @return     
  195.    */
  196.            public boolean changeDate(int day, int month, int year) 
  197.            {    
  198.                if((month >=1) && (month <=12) &&(day > 0) && (day <= daysInMonth(month)))
  199.                   return true;
  200.                else
  201.                   return false;
  202.            } 
  203.  
  204.            public int differenceInDates(int day, int month)
  205.            {
  206.                if(this
  207.                (
  208.                return 
  209.             }
  210. }   
Feb 15 '09 #27
JosAH
11,448 Recognized Expert MVP
You still have that loooong method toString() while you also have my method monthName(); how come?

w.r.t. your question: just suppose you have a method isValid(int year, int month, int day); then your changeDate() method can be as simple as:

Expand|Select|Wrap|Line Numbers
  1. public boolean changeDate(int year, int month, int day) {
  2.    if (isValid(year, month, day)) {
  3.       this.year= year;
  4.       this.month= month;
  5.       this.day= day;
  6.       return true;
  7.    }
  8.    return false;
  9. }
  10.  
Your do have the logic for the isValid() method scattered all over the place and repeated here and there. Tidy up your class (design).

kind regards,

Jos
Feb 16 '09 #28
falconsx23
55 New Member
To be honest I have no idea why monthName is still there. I will change it asap. And my question previously was I needed a method that Method that can do the following. I have no idea on how to approach this. The following is the same way it was given to me from my assignment.

returns the difference in days between this date and another date.
If dates are equal, then the result is 0
If current date (this) is before the
parameter date (that) then answer is positive.
Feb 16 '09 #29
JosAH
11,448 Recognized Expert MVP
@falconsx23
Google for "Zeller's congruence". It's just one formula that converts a Gregorian date to a Julian day number; the difference between two Julian day numbers is the difference in days between two dates.

kind regards,

Jos
Feb 16 '09 #30
falconsx23
55 New Member
Ok thanks for the help. I will try it out and let you know how it goes
Feb 16 '09 #31
falconsx23
55 New Member
Ok ummm. I do not want to use that "Zeller's congruence" only because I do not think my professor would want us to use it. So I need help finding another way on how to get the difference between two dates(mainly if statements). This is what I came up with. I know it is not right, in fact far from being right.

How can I use the keywords "this" and "that" to get the difference between two dates? Is is possible?





Expand|Select|Wrap|Line Numbers
  1. public int differnceInDate(int day, int month, int year)
  2.            {
  3.  
  4.                if((month >=1) && (month <=12) &&(day > 0) && (day <= daysInMonth(month)))
  5.                {
  6.  
  7.                    currentDate = daysInMonth(month);
  8.                     differenceBetweenDates = currentDate - dateEntered 
  9.  
  10.                }    
  11.  
  12.                else 
  13.                {
  14.                currentDate = 0;
  15.                }
  16.  
  17.                return daysInMonth(month);
  18.  
  19.  
  20.           }
Feb 17 '09 #32
JosAH
11,448 Recognized Expert MVP
@falconsx23
When I was young my professor really valued a bit of initiative. Zeller's congruence is easy: simply plug in the year, month and date and you've got a Julian date number. It is the best way to do these type of calculations.

kind regards,

Jos
Feb 17 '09 #33
falconsx23
55 New Member
sorry JosAH, but I chose to do it this way even though your way may seem better and easier. This way worked for me even though it took me a while. I also had to make some minor changes in my constructor.
If you need me to explain what is going on I will.


Expand|Select|Wrap|Line Numbers
  1.   if((month >=1) && (month <=12) &&(day > 0) && (day <= daysInMonth(month)))
  2.                {
  3.  
  4.                    currentDay = calendarDay - day;
  5.  
  6.  
  7.  
  8.                    currentMonth = calendarMonth - month;  
  9.                    currentDay = currentMonth * 30 + currentDay;
  10.  
  11.  
  12.  
  13.                    currentYear = calendarYear - year; 
  14.                    currentDay = currentYear * 365 + currentDay;
  15.  
  16.  
  17.                }    
  18.  
  19.                else 
  20.                {
  21.                currentDay = 0;
  22.                }
  23.  
  24.                return currentDay;
  25.  
  26.  
  27.           }
Feb 17 '09 #34
JosAH
11,448 Recognized Expert MVP
That doesn't work for leap years and/or months that don't have thirty days. But be my guest to turn that in. Good luck with it.

kind regards,

Jos
Feb 17 '09 #35
falconsx23
55 New Member
Yea you a right. I may turn it in since it is already pass the deadline. But I will continue to work on it more to day. But could I just simply insert an if statement saying that

Expand|Select|Wrap|Line Numbers
  1. if  calDay = 31 then currentMonth * 31 + currentDay.
Thats not how I would really write it, I am just trying to give you an idea.
Feb 17 '09 #36
JosAH
11,448 Recognized Expert MVP
@falconsx23
Yes and you have to do that for every month; don't forget the leap year check. When you're finished you've reinvented what Zeller had done ages ago.

kind regards,

Jos
Feb 18 '09 #37
falconsx23
55 New Member
Just wanted to thank you for helping me
Feb 21 '09 #38
JosAH
11,448 Recognized Expert MVP
@falconsx23
You're welcome of course; did you finish your project?

kind regards,

Jos
Feb 22 '09 #39

Sign in to post your reply or Sign up for a free account.

Similar topics

1
1348
by: clemke | last post by:
Hello My department has a SQL server database that is used to all facets of production within our department. This includes key production job steps, project names, numbers and descriptions. ...
7
15134
by: Shuffs | last post by:
Could someone, anyone please tell me what I need to amend, to get this function to take Sunday as the first day of the week? I amended the Weekday parts to vbSunday (in my code, not the code...
8
2006
by: Larry R Harrison Jr | last post by:
Sometime, if an error has occured or even if a user has answered NO to a MsgBox Y/N question, the Lebans calendar control will prevent the form from closing. I know the Lebans calendar control...
8
2441
by: Shyguy | last post by:
Is it possible to create a calendar that shows previous input data and also allows for input of new data?
15
3831
by: tigrfire | last post by:
I'm trying to write a program that will display the following output: MONTHLY CALENDAR This program displays a calendar. You need to provide the day of the week on which January 1 falls, and...
2
11755
by: dunk_king | last post by:
Hi Anybody, i´m trying to show a calendar (dayview) in my own vb.net programm. This calendar sheet should get all items (of this day) of my local outlook calendar (Outlook 2000). Is something...
0
1990
by: GV | last post by:
Hi all, New to developing in VS 2005 ASP 2.0 Trying to have a easy pop calender for a button on a web page. I keep getting a error message in IE6 that says: Line 69 Char 3 Error:...
6
3365
by: Tony Girgenti | last post by:
Hello. Developing a VS2005, SP4, VB, .NET 2.0, ASP.NET 2.0, web site program using a calendar. I tried using the validators to see if i can validate dates using a calendar control. It does...
4
5306
by: slapsh0t11 | last post by:
Hello! I need help with a program that I believe I am nearly done with. However, there seems to be a few details that preclude me from success. Here is my assignment: Here is my class file...
0
7203
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7089
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6995
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7463
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5017
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3157
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1515
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
738
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
389
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.