Program #2 = DONE
Sep. 25th, 2011 04:30 pm![[personal profile]](https://www.dreamwidth.org/img/silk/identity/user.png)
I was frustrated as hell yesterday looking at it, just trying to figure out how to start it. I'd gotten as far as the cases before I quit. Finally decided to leave it until today. Today, BOOM, done.
I am going to ask if there's an easier way to write those if statements, though. The problem was this:
Problem:
Write a program that reads a string from the keyboard as a date in the format MM/DD/YYYY. Display the date of the following day in the same format. For example:
if the user enters 03/23/2010, your program will display 03/24/2010
if the user enters 03/31/2010, your program will display 04/01/2010
if the user enters 12/31/2010, your program will display 01/01/2011
if the user enters 02/28/2010, your program will display 03/01/2010
Remember to consider the leap year for February! A leap year is any year that is divisible by 4 but not divisible by 100 unless it is also divisible by 400. So for example, the year 1600 and 2000 were leap years, but 1800 was not.
Other than February with 28 or 29 days, months have 31 days except for September, April, June, and November have 30 days.
You may assume that the date the user enters will be a valid date. It is also okay if your program does not display leading zeros, 3/1/2010 instead of 03/01/2010 for example.
I had to write cases for each month (1, 3, 5, etc. have 31 days, 4, 6, 9, 11 have 30 days, 2 has 28 or 29) and a separate if else statement for each month, and I tripped myself up for a bit with December: I couldn't get it to jump to the next year; instead of adding one to the year like I intended, it literally added a one to the end of the year, so it would come out "12/31/20111". Very simple fix that I should've noticed. And then I realized I had to switch the if statement order for February in order to get the leap years to even be considered by Java. Putting something like "if the date's 2/28, go to 3/1, UNLESS the year is a leap year" doesn't work. The exception MUST go first, otherwise Java just sees that the date is indeed 2/28 and assumes automatic truth to the first part of the statement and ignores the rest. That happened with Lab 4.
Sadly, my dates don't display leading zeroes, but eh, they don't have to. Anyway, that was a CRAPload of statements I needed to write. I copypasted everything. I was looking for ways to combine cases and if statements, but from what I read, you can't do it, so it looks like spelling out each if statement is just the way it has to be done. I was extremely annoyed when looking for help online, as everyone kept bringing up the Calendar class. We haven't used that yet.
I woke up far too early today. I didn't go to sleep until six (seriously, I have become a SERIOUS night owl...), and I woke up at eleven and couldn't go back to sleep, despite being completely exhausted. I'm sitting in bed watching Hetalia because I don't trust myself to read for class and actually retain anything. I might just take a nap. Freaking exhausted. Oh, speaking of class, 86% on my math exam and 87% on the religion one. Fairly happy with those. That's like, what, a B or B+ in both? I could've done better, but I'm all right with that. They were the first tests. Next time I'll know what to expect.