How did I miss that?
Nice spot!
Because it was in a separate block of code and your experience wouldn’t have had you write it that way
So what do I do here? I am trying to assign something that will allow this statement to be true.
So what do I need? I need to tell this thing whether or not it is true, and both statements here did not help me. What am I supposed to say to this thing that will provide me with the right output, and if I myself do not have this here, I am kinda loss, unless you want me to put this thing in the very beginning of this section. I am very limited in my knowledge of Java. I appreciate the fact that you and the others are trying to help me, but I don’t think I am going to understand this. Thanks for all the things that you’ve all provided me, but I am going to find out what I need to do here on my own now. I’ll be back sometime later if I think that I need absolute help, and I do not have any other way of getting that help. I told you before that I was extremely limited to very few things in Java, that this was my second assignment, but I don’t think that you are understanding this at all. So let me be clear on this then: I’ve never done this stuff before. When I needed help, I came here. I used to program a little bit in HTML, and I did it last semester of school, and now I am doing Java this semester. I enjoy programming this kind of stuff, and I don’t really like asking for help. But I do if I feel like I am backed in a corner. Y’all have not provided the help I came here for. I want advice, not answers. I want to be able to do this thing on my own if I need to for another assignment, or help my brother if he needs help, but I can’t when all you say is that I don’t need something here, or I need something there. How am I learning a language if I can’t do that language, and do it good? It’s like taking a foreign language that you have never talked before. Same concept. I am learning this language so that I can talk in it. So is my brother. How are we supposed to talk in this language when we do get the advice that we need? Take Spanish for example. If you pronounce something wrong, and the teacher says, “Don’t say like that”, and they don’t provide the advice to say something correctly, how will you ever learn how to say words? YOU CAN’T. All I wanted was advice. Sorry if I did not make that clear enough earlier, but I tried. Thanks for all of your help here, now, but I don’t think I will be back again for a very long time.
Sorry if I am sounding a little rude here, but right now, I feel like you’re trying to teach a new born how to walk right then and there. I feel like your trying to provide a short cut with something that I don’t even know yet, and I don’t want it. See y’all another time.
Y’all have a wonderful day, happy programming.
I apologize if you misconstrue my comments. I come from a background where my first programming teachers were old school mainframers, where code HAD to be written as optimally as possible to save time and money - my one professor was even to the point that if you could do it in five or in ten, you better do it in the five or you lost points. A correct answer wasn’t food enough.
You missed my point. A boolean variable has two possible values, true and false. If it’s not true, then it must be false. So your check of if leapYear == false was not needed. You didn’t need an IF/Else IF structure in that case - a simple IF/ELSE structure is more appropriate.
Again, this is about code refactoring/optimization. You don’t need the boolean because the IF/ELSE structure will serve the same purpose, and is more efficient from a memory and code processing stand point.
Your code WILL work (IF you fix the leapYear check like I did below).
if (leapYear == true)
numDays = 29;
else if (leapYear == false)
numDays = 28;
But a HUGE part of programming is the constant review of your code.
- Does the code work?
- Is the code working as optimally as it can? Are there improvements to be made?
I look at code I wrote even six months ago and sit there and ask myself “What was I thinking?”
So let’s look at the code from post #18 above…
I LOVE that you have validation of the month there, and that it won’t go past until the month is valid. Well DONE!
Could it be improved? Sure. You simplify the if/else if/else structure a little
if (month < 1 || month > 12) {
System.out.println( month + " is not a valid month. Please enter a valid month");
main(args);
}
Notice how the else if has been combined with the first if statement. I used the OR (||) operator, so that if ONE of these is true, it falls in.
Also, notice how I removed the else. It had no code in it, and the compiler would have removed it anyway, so it doesn’t need to be there.
Now we come to the part where the number of days is calculated. Here is your code
//Check to see if leap year
boolean leapYear = false;
String isLeapYear;
if (year % 4==0 && year % 100 !=0 || (year % 400 == 0)) {
leapYear = true;
isLeapYear="Yes, it is a leap year.";
}
else {
isLeapYear="No, it is not a leap year.";
}
int numDays = 0;
switch (month) {
case 1 : case 3: case 5: case 7: case 8: case 10: case 12:
numDays=31;
break;
case 4: case 6: case 9: case 11:
numDays=30;
break;
case 2:
// if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
if (leapYear == true)
numDays = 29;
else if (leapYear == false)
numDays = 28;
break;
}
//Display the results
System.out.println("There are " + numDays + " days in " + month + "/" + year + ". Is this a leap year? " + isLeapYear);
Will this work? Sure (again, I fixed the if statement for you). But is it the optimal way to work?
- No because the leap year check ONLY matters if the month is February.
- The case statement is a little heavier than you need. You only need to check for the months that are different, and use the default to handle the rest
So how can we simplify the code?
int numDays = 0;
// Used in the display, but only show a value if in February...
String isLeapYear;
switch (month) {
case 4: case 6: case 9: case 11:
numDays=30;
break;
case 2:
if (year % 4==0 && year % 100 !=0 || (year % 400 == 0)) {
// Is a leap year, which has 29 days
numDays = 29;
isLeapYear="Yes, it is a leap year.";
} else {
numDays = 28;
isLeapYear="No, it is not a leap year.";
}
default:
// Not a month with only 28, 29 or 30 days, must be 31
numDays = 31;
break;
}
//Display the results
System.out.println("There are " + numDays + " days in " + month + "/" + year + ". " + isLeapYear);
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.