Simple Date class

Gregorian Calendar Project

Page 1 of 1

4 Replies - 2730 Views - Last Post: 02 December 2009 - 11:06 AM Rate Topic: -----

#1 montiedsadsae999999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 09

Simple Date class

Posted 01 December 2009 - 11:07 PM

Hi I need help with this project. It is for dr.java

The instructions are :

New Methods for the SimpleDate Class

/* Change this date to tomorrow's date. Do this by incrementing myDayOfMonth. How do you know if myDayOfMonth is too large? What should you do in this case? If you increment myMonth so it changes to 13, what do you need to do? */
public void changeToTomorrow( )

/* Change this date to yesterday's date. Do this by decrementing myDayOfMonth. If this changes myDayOfMonth to zero, what do you need to do? What should be assigned to myDayOfMonth in this case? If you decrement myMonth so it changes to zero, what do you need to do? */
public void changeToYesterday( )

/* Return a copy of this SimpleDate, i.e., construct and return a SimpleDate object using myYear, myMonth, and myDayOfMonth. */
public SimpleDate copyDate( )

/* getDayOfWeekString returns the String that corresponds to the result of the getDayOfWeek method. */
public String getDayOfWeekString( )

Modified Methods for the SimpleDate Class

/* Return the number of days from this SimpleDate to the other SimpleDate. Make a copy of this SimpleDate. While the copy is not equal to the other SimpleDate, change the copy to tomorrow (or yesterday), and increment (or decrement) a counter. Return the value of the counter. How will you determine whether the copy is equal to the other SimpleDate? How will you determine whether to use the changeToTomorrow or changeToYesterday method? */
public int numberOfDaysTo(SimpleDate other)

/* Return a string describing this SimpleDate. The toString method should be modified to call getDayOfWeekString so it prints the day of the week. For example, if myYear, myMonth, and myDayOfMonth are 2009, 9, and 7, respectively, toString should return "Monday, September 7, 2009". */
public String toString( )

Testing your SimpleDate class:

Use a do-while loop to allow the user to enter a series of dates. Before the loop, initialize and construct a SimpleDate object named previousDate with 2009, 10, and 4. At the end of each iteration, ask the user if they would like to continue. In this loop do the following.

1. Ask the user to enter a string and three integers from the keyboard using a Scanner object. Store the responses in y, m, and d.

2. Instantiate a SimpleDate object named currentDate using the input from above.

3. Print the number of days from previousDate to currentDate using a descriptive label. Also print the number of days from currentDate to previousDate using a descriptive label.

4. Print currentDate using the toString method. Be sure to use a descriptive label.

5. Use a while loop to ensure that the user enters a valid date. This will be done using the validDate method, i.e., calling currentDate.validDate( ). While the values are not valid, obtain new values for y, m, and d; use the setYear, setMonth, and setDayOfMonth methods to change currentDate; and print currentDate using a descriptive label.

6. Use the copyDate method to make a copy of currentDate. Use a for loop to call the changeToTomorrow method seven times on the copy. Print the copy after each call to changeToTomorrow.

7. Use the copyDate method to make a copy of currentDate. Use a for loop to call the changeToYesterday method seven times on the copy. Print the copy after each call to changeToYesterday.

8. Print the number of days from previousDate to currentDate using a descriptive label.

9. Assign currentDate to previousDate.

10. Ask if the user would like to continue. Store the user's input in a String named answer.


My code is :
 public class SimpleDate {
  
  // myYear - an int representing the year
  // myMonth - an int representing the month
  // myDayOfMonth - an int representing the day of the month 
  private int myYear;
  private int myMonth;
  private int myDayOfMonth;
  
  /* The constructor has three parameters: the year, the month, */
  /* and the day of the month. */ 
  public SimpleDate(int year, int month, int dayOfMonth) {
	myYear = year;
	myMonth = month;
	myDayOfMonth = dayOfMonth;
  }
	
  /* getYear returns the year */ 
  public int getYear() {
	return myYear;
  }
  
  /* getMonth returns the month */ 
  public int getMonth() {
	return myMonth;
  }
  
  /* getDayOfMonth returns the day of the month */ 
  public int getDayOfMonth() {
	return myDayOfMonth;
  }
  
  /* setYear sets the year */ 
  public void setYear(int year) {
	myYear = myYear;
  }
  
  /* setMonth sets the month */ 
  public void setMonth(int month) {
	myMonth = myMonth;
  }
	
  /* setDayOfMonth sets the day of the month */ 
  public void setDayOfMonth(int dayOfMonth) {
	myDayOfMonth = myDayOfMonth;
  }
  
  /* numberOfDaysTo returns the number of days */
  /* from this SimpleDate to the other SimpleDate. */
  /* Note that the keyword 'this' refers to the first date, */
  /* and the variable 'other' refers to the second date. */ 
  public int numberOfDaysTo(SimpleDate other) {
	return 360*(other.myYear - this.myYear) + 30*(other.myMonth - this.myMonth) + other.myDayOfMonth - this.myDayOfMonth;
  }
	
  /* getDayOfWeek returns the day of the week for this SimpleDate */
  /* First, construct a SimpleDate named epoch for October 4, 2009. */
  // This returns a number from 0 to 6
  /* Second, call the numberOfDaysTo method on epoch with input 'this', i.e., */
  /* epoch.numberOfDaysTo(this) */
  /* Set days to this value and consult the assignment for */
  /* how to calculate the day of the week from days. */ 
  public int getDayOfWeek() {
	SimpleDate epoch = new SimpleDate(2009, 10, 4);
	int days = epoch.numberOfDaysTo(this);
	days = days % 7;
	if ( days < 0 ) days += 7;
	return days;
  }
  
  /* toString returns a string describing this SimpleDate. */
  /* All the instance fields should be in the description. */ 
  public String toString() {
	return getMonthString() + " " + myDayOfMonth + ", " + myYear;
  }
  
  // leapYear returns true if myYear is a leap year
  public boolean leapYear() {
	if ( myYear % 400 == 0 )  // divisible by 400 is a leap year
	  return true;
	else if (myYear % 100 == 0 )  // divisible by 100 is not a leap year
	  return false;
  else if (myYear % 4 == 0 )  // divisible by 4 is a leap year
	  return true;
  else
	return false;
  }
  
  // getDaysInMonth returns the number of days in myMonth, taking leap years into account. If myMonth is not between 1 and 12, return 0
  public int getDaysInMonth() {
	if ( myMonth == 1 ) 
	  return 31;
	else if ( myMonth == 2 && leapYear() ) 
	  return 29;
	else if ( myMonth == 2 && ! leapYear() ) 
	  return 28;
	else if ( myMonth == 3 )
	  return 31;
	else if ( myMonth == 4 )
	  return 30;
	else if ( myMonth == 5 )
	  return 31;
	else if ( myMonth == 6 )
	  return 30;
	else if ( myMonth == 7 )
	  return 31;
	else if ( myMonth == 8 )
	  return 31;
	else if ( myMonth == 9 )
	  return 30;
	else if ( myMonth == 10 )
	  return 31;
	else if ( myMonth == 11 )
	  return 30;
	else if ( myMonth == 12 )
	  return 31;
	else
	  return 0;
  }
  
  // validDate returns true if the instance fields describe a valid date
  // myDayInMonth should be between 1 and getDaysInMonth(). myMonth should be between 1 and 12
  public boolean validDate() {
	if ( myDayOfMonth < 1 || myDayOfMonth > getDaysInMonth() || myMonth < 1 || myMonth > 12 )
	  return false;
	else
	  return true;
  }
  
  // equalDate returns true if this SimpleDate is equal to the other SimpleDate. This returns true if all three instance fields are equal to each other
  public boolean equalDate( SimpleDate other ) {
	return ( this.myYear == other.myYear && this.myMonth == other.myMonth && this.myDayOfMonth == other.myDayOfMonth );
  }
  
  // compareTo returns -1, 0, or 1 depending on whether this SimpleDate is before, equal, or after the other SimpleDate
  public int compareTo( SimpleDate other ) {
	if ( myYear < other.myYear )
	  return -1;
	else if ( myYear > other.myYear )
	  return 1;
	else if ( myMonth < other.myMonth )
	  return -1;
  else if ( myMonth > other.myMonth )
	  return 1;
  else if ( myDayOfMonth < other.myDayOfMonth )
	  return -1;
  else if ( myDayOfMonth > other.myDayOfMonth )
	  return 1;
  else
	return 0;
  }
  
  // getMonthString returns String that corresponds to myMonth
  public String getMonthString() {
	if ( myMonth == 1 )
	  return "January";
	else if ( myMonth == 2 )
	  return "February";
	else if ( myMonth == 3 )
	  return "March";
	else if ( myMonth == 4 )
	  return "April";
	else if ( myMonth == 5 )
	  return "May";
	else if ( myMonth == 6 )
	  return "June";
	else if ( myMonth == 7 )
	  return "July";
	else if ( myMonth == 8 )
	  return "August";
	else if ( myMonth == 9 )
	  return "Septmeber";
	else if ( myMonth == 10 )
	  return "October";
	else if ( myMonth == 11 )
	  return "November";
	else if ( myMonth == 12 )
	  return "December";
	else
	  return "Imaginary";
  }  
  
  // Change this date to tomorrow's date. Do this by incrementing myDayOfMonth. 
  // How do you know if myDayOfMonth is too large? What should you do in this case?  
  // If you increment myMonth so it changes to 13, what do you need to do?
  public void changeToTomorrow( ) {
	myDayOfMonth ++;
	if ( this.myDayOfMonth > this.getDaysInMonth() ) {
	  myDayOfMonth = 1;	  
	  myMonth ++;
	  if ( this.myMonth > 12 ) {
		myYear ++;
		myMonth = 1;
	  }
	}
  }

	// Change this date to yesterday's date. Do this by decrementing myDayOfMonth. 
	// If this changes myDayOfMonth to zero, what do you need to do? What should be assigned to myDayOfMonth in this case? 
	// If you decrement myMonth so it changes to zero, what do you need to do? 
  public void changeToYesterday( ) {
	this.myDayOfMonth --;
	if ( this.myDayOfMonth < 1 ) {
	  this.myMonth --;
	  this.myDayOfMonth = this.getDaysInMonth();
	  if ( this.myMonth < 1 ) {
		this.myMonth = 12;
		this.myDayOfMonth = this.getDaysInMonth();
		this.myYear --;
	  }
	}
  }

	// Return a copy of this SimpleDate, i.e., construct and return a SimpleDate object using myYear, myMonth, and myDayOfMonth.
  public SimpleDate copyDate() {
	SimpleDate copy = new SimpleDate(myYear, myMonth, myDayOfMonth);
	return copy;
  }

	// getDayOfWeekString returns the String that corresponds to the result of the getDayOfWeek method.
  public String getDayOfWeekString() {
	return "i";
  }

}




Thank you very much...

Is This A Good Question/Topic? 0
  • +

Replies To: Simple Date class

#2 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8328
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Simple Date class

Posted 02 December 2009 - 05:20 AM

You must be in the same classroom :D

http://www.dreaminco...topic142701.htm
Was This Post Helpful? 0
  • +
  • -

#3 montiedsadsae999999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 09

Re: Simple Date class

Posted 02 December 2009 - 09:46 AM

View Postpbl, on 2 Dec, 2009 - 04:20 AM, said:

You must be in the same classroom :D

http://www.dreaminco...topic142701.htm


yes but there are a few codes, and the testing part that that person didnt have...
Was This Post Helpful? 0
  • +
  • -

#4 montiedsadsae999999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 09

Re: Simple Date class

Posted 02 December 2009 - 10:28 AM

can you help me with that?
please...
i will really appreciate it
Was This Post Helpful? 0
  • +
  • -

#5 montiedsadsae999999  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 01-December 09

Re: Simple Date class

Posted 02 December 2009 - 11:06 AM

more specific i am having problems with step 5-10 of the test and the public String getDayOfWeekString( ) and public string to string method. the rest i've done it correct..i think

here is my test code:
import java.util.Scanner;

public class SimpleDateTest {
	public static void main(String[] args) {
	  
	  // Initialize and construct a SimpleDate object named date1 with 2009, 9, 7 
	  SimpleDate date1 = new SimpleDate(2009, 9,7);
	  // Create a variable named year1 that contains a int value
	  int year1 = date1.getYear();
	  // Print the value of year1
	  System.out.println("year1 is " + year1);
	  // Print the month and day
	  System.out.println("The month of date1 is " + date1.getMonth() );
	  System.out.println("The day of month of date1 is " + date1.getDayOfMonth() );
	  

	  // Initialize and contruct a SimpleDate object named date2 with 2009, 10, 30
	  SimpleDate date2 = new SimpleDate(2009, 10,30);
	  // Print date2
	  System.out.println("date2 is " + date2);
	  // Print the number of days from date1 to date2 and viceversa
	  int days = date1.numberOfDaysTo( date2 );
	  System.out.println("The number of days from date1 to date2 is " + days);
	  days = date2.numberOfDaysTo( date1 );
	  System.out.println("The number of days from date2 to date1 is " + days);
	  // Print the day of the week of date1 and 2
	  int dayOfWeek = date1.getDayOfWeek();
	  System.out.println("The day of the week for date1 is " + dayOfWeek);
	  dayOfWeek = date2.getDayOfWeek();
	  System.out.println("The day of the week for date2 is " + dayOfWeek);
	  // Use setYear; setMonth; and setDayOfMonth methods to change date1 to December 21, 2012
	  date1.setYear(2012);
	  date1.setMonth(12);
	  date1.setDayOfMonth(21);
	  // Print date1, number of days from date1 to date2 and viceversa, and the day of the week of date1
	  System.out.println("date1 is " + date1);
	  days = date1.numberOfDaysTo(date2);
	  System.out.println("The number of days from date1 to date2 is " + days);
	  days = date2.numberOfDaysTo(date1);
	  System.out.println("The number of days from date2 to date1 is " + days);
	  dayOfWeek = date1.getDayOfWeek();
	  System.out.println("The day of the week for date1 is " + dayOfWeek);
	  
	  // Ask for a year, month, and day; store them in y, m, and d respectively
	  Scanner in = new Scanner (System.in);
	  System.out.print("Enter a year: ");
	  int y = in.nextInt();
	  System.out.print("Enter a month: ");
	  int m = in.nextInt();
	  System.out.print("Enter a day of month: ");
	  int d = in.nextInt();
	  
	  // Instantiate a SimpleDate object named simpleDate1 using the input provided and print simpleDate1
	  SimpleDate simpleDate1 = new SimpleDate( y, m, d );
	  System.out.println("simpleDate1 is " + simpleDate1);	  
	  // Print a warning message if validDate returns false
	  if ( ! simpleDate1.validDate() )
		System.out.println("Warning: simpleDate1 is not a valid date.");
	  // Print results of leapYear, getDaysInMonth , and getMonthString
	  boolean leap = simpleDate1.leapYear();
	  System.out.println("leapYear on simpleDate1 returns " + leap);
	  int dim = simpleDate1.getDaysInMonth();
	  System.out.println("getDaysInMonth on simpleDate1 returns " + dim);
	  String mstring = simpleDate1.getMonthString();
	  System.out.println("getMonthString on simpleDate1 returns " + mstring);
	  // Ask user to enter another year, month and day; stored the values in y, m, and d respectively
	  System.out.print("Enter another year: ");
	  y = in.nextInt();
	  System.out.print("Enter another month: ");
	  m = in.nextInt();
	  System.out.print("Enter another day of month: ");
	  d = in.nextInt();
	  // Instantiate a SimpleDate object named simpleDate2 using the input provided and print simpleDate2
	  SimpleDate simpleDate2 = new SimpleDate( y, m, d );
	  System.out.println("simpleDate2 is " + simpleDate2);	  
	  // Print a warning message if validDate returns false
	  if ( ! simpleDate2.validDate() )
		System.out.println("Warning: simpleDate2 is not a valid date.");
	  // Print results of leapYear, getDaysInMonth , and getMonthString
	  leap = simpleDate2.leapYear();
	  System.out.println("leapYear on simpleDate2 returns " + leap);
	  dim = simpleDate2.getDaysInMonth();
	  System.out.println("getDaysInMonth on simpleDate2 returns " + dim);
	  mstring = simpleDate2.getMonthString();
	  System.out.println("getMonthString on simpleDate2 returns " + mstring);	  
	  // Use the equalDate method to determine whether simpleDate1 is equal to simpleDate2, and assign the result to a boolean variable named b. 
	  // Use if statements to print the results
	  boolean b = simpleDate1.equalDate( simpleDate2 );
	  if ( b == true )
		System.out.println("simpleDate1 and simpleDate2 are equal.");
	  else
		System.out.println("simpleDate1 and simpleDate2 are different.");
	  // Use the compareTo method to determine whether simpleDate1 is before, equal to, or after simpleDate2
	  int compare = simpleDate1.compareTo( simpleDate2);
	  System.out.println(simpleDate1 + " is");
	  if ( compare < 0 ) System.out.print("Before ");
	  else if ( compare > 0 ) System.out.print("After ");
	  else System.out.println("Equal to ");
	  System.out.println(simpleDate2 + ".");
	  
	}
}


help will be really appreciate it...thanks in advance
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1