$15.00 Week 3 Help
- From Computer-Science: Programming-Methods
- Closed, but you can still post tutorials
- Due on Mar. 18, 2012
- Asked on Mar 18, 2012 at 6:56:30PM
|
Hello, I am currently working on an assignment that I can not completely finish, I have all but the c step. I will post the assignment then what I have, how can I : Incrementing into the next month in a leap year, for example, STEP 2: Date (20 |
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
This is what I have so far:
package datePackage;
public class Date
{
private int year;
private int month;
private int day;
// constructor
public Date(int year, int month, int day)
{
this.year = year;
this.month = month;
this.day = day;
if (month < 1 || month > 12)
throw new
IllegalArgumentException("Invalid Month");
if (!validDay())
throw new
IllegalArgumentException("Invalid Day");
}
// move to next day
public void nextDay()
{
day++;
if (!validDay())
{
System.out.println("Day "
+ day + " invalid. Set to day 1.");
day = 1;
if (++month == 13)
{
month = 1;
year++;
}
}
}
// check valid
public boolean validDay()
{
int[] monthDay = {31, 28, 31, 30, 31,
30, 31, 31, 30, 31, 30, 31};
boolean leap = (year % 400 == 0) ||
(year % 4 == 0 && year % 100 != 0);
int addDay = (month == 2 &&
leap) ? 1 : 0;
if (month >= 1 && month
<= 12 && day > 0 && day <= monthDay[month - 1] +
addDay)
return true;
return false;
}
// accessors
public int getYear()
{
return year;
}
public int getMonth()
{
return month;
}
public int getDay()
{
return day;
}
}
package datePackage;
public class DateTest
{
// the main method
public static void main(String[] args)
{
System.out.println("Checking
increment");
Date dat = new Date(2010, 11, 21);
System.out.println("Date object
constructor for date " + dat.getMonth() + "/" +
dat.getDay() + "/" +
dat.getYear());
for (int i = 0; i < 100; i++)
{
dat.nextDay();
System.out.println("Incremented Date: " + dat.getMonth() +
"/" +
dat.getDay() +
"/" + dat.getYear());
}
}
}
- This tutorial was purchased 1 time and rated D by students like you.
- Posted on Mar 18, 2012 at 7:23:14PM
- This tutorial was purchased 1 time and rated C- by students like you.
- Posted on Sep. 18, 2012 at 06:20:09AM

Attachments:
[
iLab3/.classpath
iLab3/.project
iLab3/src/Cylinder.class
iLab3/src/Cylinder.java
iLab3/src/CylinderTest.class
iLab3/src/CylinderTest.java
iLab3/src/Date.class
iLab3/src/Date.java
iLab3/src/DateTest.class
iLab3/src/DateTest.java
]
- This tutorial hasn't been purchased yet.
- Posted on Feb. 24, 2013 at 07:45:18AM

- This tutorial hasn't been purchased yet.
- Posted on Apr 13, 2013 at 7:57:56PM