-
May 20th, 2012, 08:41 PM
#1
Help writing java code
I have this code thus far:
Code:
public class Date
{
int month;
int day;
int year;
public Date(int month,int day,int year)
{
this.month=month;
this.day=day;
this.year=year;
System.out.println("Date object constructor for date "+month+"/"+day+"/"+year);
}
public static void main(String args[])
{
Date d=new Date(11,27,1988);
for(int i=1;i<=365;i++)
{
String date=d.nextDay();
System.out.println("Incremented Date: "+date);
}
}
public String nextDay()
{
this.day = day + 1;
if(month <=7)
{
if(month % 2 ==0)
{
if(month==2)
{
if(year%4 == 0)
{
if(day>29)
{
System.out.println("Day "+day+" invalid. Set to day 1.");
this.day=1;
this.month=month+1;
if(month>12)
{
this.month=1;
this.year=year+1;
}
}
}
else if(day>28)
{
System.out.println("Day "+day+" invalid. Set to day 1.");
this.day=1;
this.month=month+1;
if(month>12)
{
this.month=1;
this.year=year+1;
}
}
}
if( day>30)
{
System.out.println("Day "+day+" invalid. Set to day 1.");
this.day=1;
this.month=month+1;
if(this.month>12)
{
this.month=1;
this.year=year+1;
}
}
}
else
{
if( day>31)
{
System.out.println("Day "+day+" invalid. Set to day 1.");
this.day=1;
month=month+1;
if(month>12)
{
month=1;
year=year+1;
}
}
}
}
else
{
if(month % 2 ==0)
{
if( day>31)
{
System.out.println("Day "+day+" invalid. Set to day 1.");
this.day=1;
month=month+1;
if(month>12)
{
month=1;
year=year+1;
}
}
}
else
{
if( day>30)
{
System.out.println("Day "+day+" invalid. Set to day 1.");
this.day=1;
month=month+1;
if(month>12)
{
month=1;
year=year+1;
}
}
}
}
return this.month+"/"+this.day+"/"+year;
}
}
What I need to do from here is write a seperate .java file that will ask a user to input a month day and year, and then ask how many days from the date entered to increment. I am STUCK on how to code that piece, can a more elite programmer assist in the coding of this?
Last edited by jo15765; May 20th, 2012 at 08:46 PM.
Reason: Had code commented out that shouldn't have been
-
May 21st, 2012, 05:39 AM
#2
Re: Help writing java code
Hi!
First of all, you should avoid to name your class Date. It is not a good coding style naming classes after a java object class.
Next, avoid to use too many nested if and else statements. Instead use switch/case statements wherever you can.
Now to your problem. If I get you right, all you need is to write is a new Java class containing a main method (similiar to the one you already have in your Date class). Read in the user input, call the Date's class constructor with current date as parameters, code a FOR loop and call nextDay method in Date class as often as the user specified.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|