|
 |
|
 |
 |
09-18-2005, 06:19 AM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 10
|
Please help with an assignment [Thanks for the help, please review final code]
Hi guys, it's my first semester of computer science and I'm having a bit of trouble with the second assignment.
Basically, it's supposed to be a program that asks users to input a date (year month day). First, it's supposed to print a proper date (January 1, 2005), then it's supposed to give out the day number (January 1, 2005 = Day 1 of the year) and finally it's supposed to print the NEXT date (January 2, 2005).
I don't want the solution to this, just a nudge in the right direction. Thanks.
EDIT: Is it better if I associate the month days with constants? (final int JANUARY = 31)?
Code:
//********************************************************************************
//AUTHOR: RM
//Purpose: 1. Convert year/month/day into proper date (April 30, 2005)
//. 2. Display the day number. (April 30th, 2005 = Day 120 in the year)
// 3. Display the NEXT date of inputted date (May 1, 2005)
//Last modified: September 17th, 2005
//********************************************************************************
import java.util.*;
public class Date
{
//Initate scanner
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Variable declaration
int year;
int month;
int dayOfMonth;
int numOfDays;
int dayNumber
String monthName = "";
//Prompt user for input
System.out.print("Please enter a date (year month day): ");
year = keyboard.nextInt();
month = keyboard.nextInt();
dayOfMonth = keyboard.nextInt();
//Exclude invalid entries (will give more information to user than just using Switch default cases)
if (dayOfMonth > 31 || dayOfMonth < 1 || month > 12 || month < 1 || year < 1583)
{
System.out.println("\nPlease enter a valid date. Invalid date entries include:\n");
System.out.println("1. Days larger than 31 OR Days lower than 1");
System.out.println("2. Months larger than 12 OR Months lower than 1");
System.out.println("3. Years below 1583\n");
System.exit(0);
}
if (dayOfMonth > 30 && (month == 4 || month == 6 || month == 9 || month == 12))
{
System.out.println("\nThe months of April, June, September and November cannot have more than 30 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
if (dayOfMonth > 29 && month == 2)
{
System.out.println("\nThe month of February cannot have more than 29 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
//FIRST switch statement (Store number of days of months in variable)
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numOfDays = 31; break;
case 4:
case 6:
case 9:
case 11:
numOfDays = 30; break;
case 2:
if ( (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0)) )
numOfDays = 29;
else
numOfDays = 28; break;
default: break;
}
//SECOND switch statement (store month name in String variable)
switch (month)
{
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: break;
}
//Calculate day number
}
}
__________________
Last edited by ZimbuTM : 09-19-2005 at 08:29 PM.
|
|
|
09-18-2005, 05:36 PM
|
#2 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Thanks for the well-thought question. Far too often I've seen people simply ask for the solution without much thought put into the matter - it's nice for a change of pace.
As for the core matter, you seem to have the matter pretty well-in-hand for the error checking and month transformation. Your idea for constants is a good one, but I might suggest that you make the value the month position as opposed to the days in the month. This will be more useful should you ever wish to extend the program in the future, as well as possibly make your program more readable. What you could then do is create an array that contained the number of days for each month. For instance :
Code:
static final int JANUARY = 0;
static final int FEBURARY = 1;
static final int MARCH = 2;
.
.
.
static final int DECEMBER = 11;
static final int[] DAYS_IN_MONTH = { 31, 28, 31, . . . , 31};
int daysOfMarch = DAYS_IN_MONTH[MARCH];
At least I think that would be pretty easy to understand. Of course, dealing Feburary is going to be confusing and a hack, but that would be the case regardless.
Now, as to the problem of the day of the year, I would think simply running through and adding each element in the array up to the month specified, followed by adding the day of the month would yield the total number of days in the year. Let me know if that makes sense to you or not - if not I can provide some code examples.
Now, there is an alternate way to go about this and I think it's important for you to know how to do this. A friend once told me that for most problems in computer science, they have already been solved by people who are specialists in the field and have many more years of experience than us. It doesn't make sense to try to re-invent the wheel when someone's probably already done it and done better than we could, so just take what's already available. In this case, I'm referring to the Core Library of Java. Or more specifically, the Calendar object. It provides all the functionality you need, all you need to do is find out how to use it and very little further effort is needed on your part. As a new programmer (or even as an experienced programmer), any time you work in Java, you should have the API open for quick reference. You'll be surprised just how many things are automatically provided for you. The art of finding out how to use the tools provided is just as important (probably even more so) to a programmer than how to build your own.
|
|
|
09-18-2005, 05:47 PM
|
#3 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 10
|
Thanks for the reply.
I quickly scanned it for now, it's pretty late and I gotta hit the sack soon. I'll read it more thoroughly tomorrow after my classes.
Anyway, the assignment instructs us to solve the problem using IF and switch statements. I figured there might be a package that makes handling dates a piece of cake, but I'm limited to using these methods only. We haven't yet learned about arrays either.
Problem is that math was never my strong suit, so I'm feeling a bit intimidated right now. I'm really liking programming, but I can't help but feel disadvantaged compared to my classmates.
__________________
|
|
|
09-19-2005, 02:07 AM
|
#4 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
There's math, and then there's math. A good chunk of computer science can be accomplished with very little real math knowledge - it's more a matter of problem solving involving numbers. You'll often need to work with a lot of numbers, but you rarely need to do anything fancy with them. It's important not to be intimidated - many programmers are assigned to work on things that at the outset they don't know how to do, just realize that you might need to frequent reference material and surf the internet for a solution.
|
|
|
09-19-2005, 07:39 PM
|
#5 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 10
|
Hey. Thanks again for the help. Here's my finished program, please comment on it. It works perfectly, but I'm sure I didn't take the most efficient approach. For example, I know I didn't have to use a boolean variable, but I was experimenting and learning how to use them:
Code:
//********************************************************************************
//AUTHOR: RM
//Purpose: 1. Convert year/month/day into proper date (April 30, 2005)
//. 2. Display the day number. (April 30th, 2005 = Day 120 in the year)
// 3. Display the NEXT date of inputted date (May 1, 2005)
//Last modified: September 19th, 2005
//********************************************************************************
import java.util.*;
public class Date
{
//Initate scanner
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Variable and constant declaration
int year;
int month;
int dayOfMonth;
int dayNumber;
int nextDay = 0;
int nextYear = 0;
String monthName = "";
String nextMonth = "";
final int DAYS_IN_JANUARY = 31;
int DAYS_IN_FEBRUARY = 28;
final int DAYS_IN_MARCH = 31;
final int DAYS_IN_APRIL = 30;
final int DAYS_IN_MAY = 31;
final int DAYS_IN_JUNE = 30;
final int DAYS_IN_JULY = 31;
final int DAYS_IN_AUGUST = 31;
final int DAYS_IN_SEPTEMBER = 30;
final int DAYS_IN_OCTOBER = 31;
final int DAYS_IN_NOVEMBER = 30;
final int DAYS_IN_DECEMBER = 31;
//Prompt user for input
System.out.print("Please enter a date (year month day): ");
year = keyboard.nextInt();
month = keyboard.nextInt();
dayOfMonth = keyboard.nextInt();
//Boolean for leap year
boolean isLeapYear = ( (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0)) );
//Determine number of days in February
if (isLeapYear)
DAYS_IN_FEBRUARY = 29;
else
DAYS_IN_FEBRUARY = 28;
//Exclude invalid entries (will give more information to user than just using Switch default cases)
if (dayOfMonth > 31 || dayOfMonth < 1 || month > 12 || month < 1 || year < 1583)
{
System.out.println("\nPlease enter a valid date. Invalid date entries include:\n");
System.out.println("1. Days larger than 31 OR Days lower than 1");
System.out.println("2. Months larger than 12 OR Months lower than 1");
System.out.println("3. Years below 1583\n");
System.exit(0);
}
if (dayOfMonth > 30 && (month == 4 || month == 6 || month == 9 || month == 11))
{
System.out.println("\nThe months of April, June, September and November cannot have more than 30 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
if (dayOfMonth > 29 && month == 2)
{
System.out.println("\nThe month of February cannot have more than 29 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
if (!isLeapYear && (month == 2 && dayOfMonth == 29))
{
System.out.println("\nInputted date does not comply with Gregorian standards. \nFebruary can only have 29 days in a leap year.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
//Store month name in variable
switch (month)
{
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: break;
}
//Store day number in variable
switch (month)
{
case 1: dayNumber = dayOfMonth; break;
case 2: dayNumber = (dayOfMonth + DAYS_IN_JANUARY); break;
case 3: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY); break;
case 4: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH); break;
case 5: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL); break;
case 6: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY); break;
case 7: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE); break;
case 8: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY); break;
case 9: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST); break;
case 10: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER); break;
case 11: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER + DAYS_IN_OCTOBER); break;
case 12: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER + DAYS_IN_OCTOBER + DAYS_IN_NOVEMBER); break;
default: dayNumber = (dayOfMonth + DAYS_IN_JANUARY + DAYS_IN_FEBRUARY + DAYS_IN_MARCH + DAYS_IN_APRIL + DAYS_IN_MAY + DAYS_IN_JUNE + DAYS_IN_JULY + DAYS_IN_AUGUST + DAYS_IN_SEPTEMBER + DAYS_IN_OCTOBER + DAYS_IN_NOVEMBER + DAYS_IN_DECEMBER); break;
}
//Store next date values in variables
switch (month)
{
case 1: if (dayOfMonth == DAYS_IN_JANUARY)
{
nextDay = 1;
nextMonth = "February";
}
case 2: if (dayOfMonth == DAYS_IN_FEBRUARY)
{
nextDay = 1;
nextMonth = "March";
}
case 3: if (dayOfMonth == DAYS_IN_MARCH)
{
nextDay = 1;
nextMonth = "April";
}
case 4: if (dayOfMonth == DAYS_IN_APRIL)
{
nextDay = 1;
nextMonth = "May";
}
case 5: if (dayOfMonth == DAYS_IN_MAY)
{
nextDay = 1;
nextMonth = "June";
}
case 6: if (dayOfMonth == DAYS_IN_JUNE)
{
nextDay = 1;
nextMonth = "July";
}
case 7: if (dayOfMonth == DAYS_IN_JULY)
{
nextDay = 1;
nextMonth = "August";
}
case 8: if (dayOfMonth == DAYS_IN_AUGUST)
{
nextDay = 1;
nextMonth = "September";
}
case 9: if (dayOfMonth == DAYS_IN_SEPTEMBER)
{
nextDay = 1;
nextMonth = "October";
}
case 10: if (dayOfMonth == DAYS_IN_OCTOBER)
{
nextDay = 1;
nextMonth = "November";
}
case 11: if (dayOfMonth == DAYS_IN_NOVEMBER)
{
nextDay = 1;
nextMonth = "December";
}
case 12: if (dayOfMonth == DAYS_IN_DECEMBER);
{
nextDay = 1;
nextMonth = "January";
}
default: {
nextDay = (dayOfMonth + 1);
nextMonth = monthName; break;
}
}
//Store next year (if any) value in variable
if (monthName == "December" && dayOfMonth == 31)
{
nextYear = (year + 1);
}
else
{
nextYear = year;
}
System.out.println("\nThe date is: " + monthName + " " + dayOfMonth + ", " + year);
System.out.println("The date's day number is: " + dayNumber);
System.out.println("The next date is: " + nextMonth + " " + nextDay + ", " + nextYear + "\n");
}
}
__________________
|
|
|
09-20-2005, 08:43 AM
|
#6 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
The only major change I would suggest is try to encapsulate more of the functionality in methods. For instance, you could create a method that checks to make sure the date is valid, and then call it from the main method. That will make the whole thing easier to edit and maintain.
Also, not to tout my own idea, but I think you can see the advantage of having the number of days in the month put into an array when you determine dayNumber. For example, with an array, you could do this:
Code:
int daysInMonths[] = { 31, 28, 31, . . ., 31};
.
.
.
int dayNumber = dayOfMonth;
for(int i = 0; i < month, i++){
dayNumber = dayNumber + daysInMonths[i];
}
That would replace your whole switch statement that finds the same thing. Of course you'd need to take into account leap years, but you had to do that anyway.
|
|
|
09-20-2005, 12:32 PM
|
#7 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 10
|
The thing is that the teacher asked us to do this using IF and switch statements to get accustomed to them. We're gonna learn about arrays after doing loops in the next class.
I didn't mean to dismiss your idea, I'm sure it's much better than how I went about it, but at the moment I'm still not fully confident using switches and IF statements, so I didn't want to stray away from the instructions too much.
I did, however, have a better idea on the "day number" part so that the code is better looking and easier to decipher. I'll test it out now and post my results.
__________________
|
|
|
09-20-2005, 04:27 PM
|
#8 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
That's perfectly fine then - constraints are constraints after all. The only thing I would add then is that you might want to adhere to the Java Coding Convention. Often people will transfer coding conventions from other languages or use their own ad-hoc style. For instance, you seem to be using a variety of C-style, where the curly-braces appear below the section of code they belong to. In Java, the opening brace occurs on the same line of code that begins the statement (for example, an "if" or "for"). Also, even single-line "if" statements should really use { }.
Of particular note is the statement section. While it might seem kinda uptight and annoying to harp on style, it really pays off in the long run when you need someone else to sort through a few thousand lines of code - little quirks in style can quickly become annoyances to someone who doesn't share your style.
|
|
|
09-22-2005, 11:53 AM
|
#9 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 10
|
The style I'm using isn't my own, but the one the teacher has taught us. The textbook we have also employs the same style. "Java Programming" by D.S. Malik.
Code:
//********************************************************************************
//AUTHOR: Rahul Malik
//Purpose: 1. Convert year/month/day into proper date (April 30, 2005)
//. 2. Display the day number. (April 30th, 2005 = Day 120 in the year)
// 3. Display the NEXT date of inputted date (May 1, 2005)
//Last modified: September 19th, 2005
//********************************************************************************
import java.util.*;
public class Date
{
//Initate scanner
static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
//Variable and constant declaration
int year = 0;
int month = 0;
int dayOfMonth = 0;
final int DAYS_IN_JANUARY = 31;
int DAYS_IN_FEBRUARY = 28;
final int DAYS_IN_MARCH = 31;
final int DAYS_IN_APRIL = 30;
final int DAYS_IN_MAY = 31;
final int DAYS_IN_JUNE = 30;
final int DAYS_IN_JULY = 31;
final int DAYS_IN_AUGUST = 31;
final int DAYS_IN_SEPTEMBER = 30;
final int DAYS_IN_OCTOBER = 31;
final int DAYS_IN_NOVEMBER = 30;
final int DAYS_IN_DECEMBER = 31;
//Prompt user for input (and detect string inputs)
System.out.print("Please enter a date (year month day): ");
try
{
year = keyboard.nextInt();
month = keyboard.nextInt();
dayOfMonth = keyboard.nextInt();
}
catch (InputMismatchException e)
{
System.out.println("\nError. Numeric value expected\n");
System.exit(0);
}
//create Boolean for leap year
boolean isLeapYear = ( (year % 400 == 0) || ((year % 4 == 0) && !(year % 100 == 0)) );
//Determine number of days in February
if (isLeapYear)
{
DAYS_IN_FEBRUARY = 29;
}
else
{
DAYS_IN_FEBRUARY = 28;
}
//Exclude invalid entries
if (dayOfMonth > 31 || dayOfMonth < 1 || month > 12 || month < 1 || year < 1583)
{
System.out.println("\nPlease enter a valid date. Invalid date entries include:\n");
System.out.println("1. Days larger than 31 OR Days lower than 1");
System.out.println("2. Months larger than 12 OR Months lower than 1");
System.out.println("3. Years below 1583 (for compliance with Gregorian date standards)\n");
System.exit(0);
}
if (dayOfMonth > 30 && (month == 4 || month == 6 || month == 9 || month == 11))
{
System.out.println("\nThe months of April, June, September and November cannot have more than 30 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
if (dayOfMonth > 29 && month == 2)
{
System.out.println("\nThe month of February cannot have more than 29 days.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
if (!isLeapYear && (month == 2 && dayOfMonth == 29))
{
System.out.println("\nInputted date does not comply with Gregorian standards. \nFebruary can only have 29 days in a leap year.");
System.out.println("Please re-run the program\n");
System.exit(0);
}
//Store month name in variable
String monthName = "";
switch (month)
{
case 1: monthName = "January"; break;
case 2: monthName = "February"; break;
case 3: monthName = "March"; break;
case 4: monthName = "April"; break;
case 5: monthName = "May"; break;
case 6: monthName = "June"; break;
case 7: monthName = "July"; break;
case 8: monthName = "August"; break;
case 9: monthName = "September"; break;
case 10: monthName = "October"; break;
case 11: monthName = "November"; break;
case 12: monthName = "December"; break;
default: break;
}
//Store day number in variable
int dayNumber = 0;
switch (month)
{
case 12: dayNumber = dayNumber + DAYS_IN_NOVEMBER;
case 11: dayNumber = dayNumber + DAYS_IN_OCTOBER;
case 10: dayNumber = dayNumber + DAYS_IN_SEPTEMBER;
case 9: dayNumber = dayNumber + DAYS_IN_AUGUST;
case 8: dayNumber = dayNumber + DAYS_IN_JULY;
case 7: dayNumber = dayNumber + DAYS_IN_JUNE;
case 6: dayNumber = dayNumber + DAYS_IN_MAY;
case 5: dayNumber = dayNumber + DAYS_IN_APRIL;
case 4: dayNumber = dayNumber + DAYS_IN_MARCH;
case 3: dayNumber = dayNumber + DAYS_IN_FEBRUARY;
case 2: dayNumber = dayNumber + DAYS_IN_JANUARY;
case 1: dayNumber = dayNumber + dayOfMonth;
default: break;
}
//Store next date values in variables
int nextDay = 0;
int nextYear = 0;
String nextMonth = "";
switch (month)
{
case 1: if (dayOfMonth == DAYS_IN_JANUARY)
{
nextDay = 1;
nextMonth = "February";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 2: if (dayOfMonth == DAYS_IN_FEBRUARY)
{
nextDay = 1;
nextMonth = "March";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 3: if (dayOfMonth == DAYS_IN_MARCH)
{
nextDay = 1;
nextMonth = "April";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 4: if (dayOfMonth == DAYS_IN_APRIL)
{
nextDay = 1;
nextMonth = "May";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 5: if (dayOfMonth == DAYS_IN_MAY)
{
nextDay = 1;
nextMonth = "June";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 6: if (dayOfMonth == DAYS_IN_JUNE)
{
nextDay = 1;
nextMonth = "July";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 7: if (dayOfMonth == DAYS_IN_JULY)
{
nextDay = 1;
nextMonth = "August";
}
else
{ nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 8: if (dayOfMonth == DAYS_IN_AUGUST)
{
nextDay = 1;
nextMonth = "September";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 9: if (dayOfMonth == DAYS_IN_SEPTEMBER)
{
nextDay = 1;
nextMonth = "October";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 10: if (dayOfMonth == DAYS_IN_OCTOBER)
{
nextDay = 1;
nextMonth = "November";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 11: if (dayOfMonth == DAYS_IN_NOVEMBER)
{
nextDay = 1;
nextMonth = "December";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
case 12: if (dayOfMonth == DAYS_IN_DECEMBER)
{
nextDay = 1;
nextMonth = "January";
}
else
{
nextDay = (dayOfMonth + 1);
nextMonth = monthName;
}
break;
default: break;
}
//Store next year value in variable
if (monthName == "December" && dayOfMonth == 31)
{
nextYear = (year + 1);
}
else
{
nextYear = year;
}
System.out.println("\nThe date is: " + monthName + " " + dayOfMonth + ", " + year);
System.out.println("The date's day number is: " + dayNumber);
System.out.println("The next date is: " + nextMonth + " " + nextDay + ", " + nextYear + "\n");
}
}
Here's my final code, I fixed up a few things. The formatting seems to have been messed up by the code tags on the forum.
__________________
|
|
|
09-23-2005, 02:33 AM
|
#10 (permalink)
|
|
Java fanboy
Join Date: Aug 2003
Posts: 1,114
|
Ah, setting up that switch to fall through in order to count the days in the year was clever. It looks good.
As far as Java style, it's more important to learn the basics of programming than learning the style at this stage, so code however you feel comfortable. Just realize that both your book and your teacher using incorrect style. Sun Microsystems is the final authority on the subject, as they are the ones who both own the intellectual property rights to Java and coordinate it's development, and their specs say what the correct style is. At some point in the future, when you feel comfortable working with Java you might want to try to switch styles.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 06:53 PM.
|
Copyright © 2000-2006, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
Open Circle
|
 |
|