So i have to make a java program that will increment and print each date from the date the user input. I can not get it to actually print increment. What do I have wrong. I have already turned in what I have since it was due but still would like to resolve problem. i would love to use JOptionPane also but I am very bad at this. Its my first time.
instructions: Create a program called Date.java to perform error-checking on the initial values, for instance: fields month, day, and year. Also, provide a method nextDay() to increment the day by one. The Date object should always remain in a consistent state.
Write a program called DateTest.java that prompts the user to enter the month, day, and year as numeric values. This program then creates a Date object using the Date class you just created and tests the nextDay() method. This can be done in a loop of 40 iterations: the Date object calls the nextDay() method and prints the date during each iteration of the loop. This loop is to illustrate that the nextDay() method works correctly. Test the following cases:
Incrementing into the next month, for example, use date: 02/28/2011 Incrementing into the next year, for example, use date: 11/27/2011 Incrementing into the next month in a leap year, for example, use date: 02/28/2012 Sample Program Output:
Checking increment Date object constructor for date 11/27/2011 Incremented Date:11/28/2011 Incremented Date:11/29/2011 Incremented Date:11/30/2011 Day 31 invalid. Set to day 1. Incremented Date:12/1/2011 Incremented Date:12/2/2011 ... Incremented Date:12/30/2011 Incremented Date:12/31/2011 Day 32 invalid. Set to day 1. Incremented Date:1/1/2012 Incremented Date:1/2/2012 Incremented Date:1/3/2012 Incremented Date:1/4/2012 Incremented Date:1/5/2012 Incremented Date:1/6/2012
My Code:
//date.java class
import javax.swing.JOptionPane;
import java.util.Scanner;
public class Date
{
private int MIN_YEAR = 1990;
private int MAX_YEAR = 2050;
private int DEFAULT_YEAR = 2012;
private int MIN_MONTH = 1;
private int MAX_MONTH = 12;
private int DEFAULT_MONTH = 1;
private int MIN_DAY = 1;
private int MAX_DAY = 31;
private int DEFAULT_DAY = 1;
private int year = 2012;
private int month = 1;
private int day = 1;
private int daysInMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
public Date()
{
month = DEFAULT_MONTH;
day = DEFAULT_DAY;
year = DEFAULT_YEAR;
}
public Date(int year, int month, int day)
{
this.month = month;
this.year = year;
this.day = day;
}
public void setYear(int year)
{
if(year < MIN_YEAR || year > MAX_YEAR)
{
System.out.println("The year entered has to be between" + MIN_YEAR + "-" + MAX_YEAR + ". Try Again!");
}
else
{
this.year = year;
}
}
public int getYear()
{
return year;
}
public void setMonth(int month)
{
if(month < MIN_MONTH)
{
this.month = MIN_MONTH;
}
else if(month > MAX_MONTH)
{
this.month = MAX_MONTH;
}
}
public int getMonth()
{
return month;
}
public void setDay(int day)
{
if(month < MIN_DAY)
{
this.month = MIN_DAY;
}
else if(month > MAX_DAY)
{
this.month = MAX_DAY;
}
}
public int getDAY()
{
return day;
}
public void nexyDay()
{
int currentMonthMaxDays = daysInMonth[month];
boolean leapYear = isLeapYear();
if( day < currentMonthMaxDays)
{
day += 1;
}
else if(month == 2 && leapYear == true && day == 28)
{
day = 29;
}
else
{
day = 1;
if(month!= 12)
{
month += 1;
}
else
{
year +=1;
month = 1;
}
}
}
public String toString()
{
return month + "/" + day + "/" + year;
}
private boolean isLeapYear()
{
boolean leapYear = false;
if(year % 400 == 0)
{
leapYear = true;
}
else if (year % 4 == 0 && year % 100 != 0)
{
leapYear = true;
}
return leapYear;
}
}
//my datetest class
import javax.swing.JOptionPane;
import java.util.Scanner;
public class DateTest {
public static void main(String[] args)
{
Date[] date = new Date[1];
for (int i = 0; i < date.length; i++)
{
System.out.print("Enter deired Year: ");
int year = new Scanner(System.in).nextInt();
System.out.print("Enter deired Month: ");
int month = new Scanner(System.in).nextInt();
System.out.print("Enter deired Day: ");
int day = new Scanner (System.in).nextInt();
date[i].nextDay(year, month, day);
}
for (int i = 0; i < 40; i++)
{
date[i].nextDay();
System.out.print("Incremented Date:" + date[i].toString());
}
}
}