Repetition and DecisionMaking Before Beginning Download Date

\"Repetition and Decision-Making\"

Before Beginning

Download Date.java and SpeedDating.java from the class web page and store them in the src folder of your NetBeans project

Read the online documentation for the Date class to learn how to create and manipulate Date objects (or just see the last page of this assignment)

No credit will be given if you modify the Date class in any way. It is not necessary, and in real life you do not have access to the code for most of the classes you use and so cannot modify them.

Complete the SpeedDating Class

Write the method bodies for each of the 3 methods declared in SpeedDating. Study the method declarations and documentation so that you understand what each method does.

No credit will be given if you modify the SpeedDating class in any way other than completing the bodies of the 3 methods.

Do not add any new methods or instance variables, do not modify the constructor, and do not modify the method declarations (“headings”) provided!

To receive credit for a method, it must use one of the Java loops. Nested loops are not necessary.

Write a Test Class for Your SpeedDating Class

Your test class will have a main method that does the following:

Create a SpeedDating object

Have the user enter the month and day of her birthday (as ints) and then call the happyBirthDaze method to print the day of the week (Sunday, Monday, etc) on which her birthday falls for each of the years 2015 through 2025, inclusive.

In the USA, Election Day is fixed by law as the first Tuesday after the first Monday in November. Have the user enter a year, call the pollDancer method, and print the Date returned, properly labeled. Print the Date in your main method, not in SpeedDating.

Microsoft Excel® numbers dates as 1, 2, 3, etc, beginning with January 1st, 1900. E.g., February 6th, 1900 is Excel Date 37. Have the user enter a date (month, day, and year) and call the iExcel method to get the Excel Date for the input date. Print it in your main method, not in SpeedDating.

The creators of Excel mistakenly thought that 1900 was a leap year! (Of course, we know better and so does the Date class.) Therefore, all Excel Dates after 2/28/1900 are “off by one.” Not a problem! Do not attempt to compensate for this error! Just use the value computed using the Date class. In other words, what we are really computing is what the Excel Date should be!

Data to be Used

Although your SpeedDating methods must work for all valid inputs, use this data in the run you hand in:

Birthday month (for happyBirthDaze): 12

Birthday day (for happyBirthDaze) : 10

Election year (for pollDancer)     : 2016

Date for iExcel                    : April 7th, 2015

Due Date:   November 21st, 2016

What to Upload

Upload a Zip file containing your SpeedDating class, your test class, and the output. Do not zip the Date class.

Make sure your SpeedDating methods contain proper “internal” documentation. I.e. explain your logic and local variable declarations (if any)

Using The Date Class - Examples

The Date class is a class for manipulating dates. A Date object represents a date in the Gregorian calendar (i.e., dates after October 15, 1582). Any attempt to create an invalid date will throw an exception.

Creating Objects

Date d1 = new Date(month, day, year) ;

// creates a Date object initialized to int arguments month, day, and // year. E.g.,

Date d1 = new Date(10,31,2013) ; // d1 is October 31st, 2013

   d1 = new Date(1,1,2014) ;      // d1 is now January 1, 2014

Accessor Methods

d1.getMonth()      // returns the month as int 1..12

d1.getDay()        // returns the day of the month, as an int

d1.getYear()     // returns the year as an int

d1.getMonthName() // returns the month as a String (e.g. “March\") d1.getDayOfWeek() // returns the day of the week as a string (e.g.,

                   // “Friday”)

d1.equals(d2)     // returns true if d1 is the same date as d2

// otherwise, returns false

d1.getShortDate() // returns the date as mm/dd/yyyy (e.g. 3/8/2014)

d1.getMediumDate()      // returns the date in the form of “March 8, 2014”

d1.getLongDate() // returns the date in the form of

                   // “Saturday, March 8th, 2014”

Mutator Methods

Date d = new Date(1,1,2012) ; // d is January 1, 2012 (a leap year)

d.next() ;                     // d is January 2, 2012

d.previous() ;                 // d is January 1, 2012

d.add(31) ;                 // d is February 1, 2012

d.subtract(32) ;              // d is December 31, 2011

d.next() ;                         // d is January 1, 2012

d = new Date(3,1,2012) ;    // d is March 1, 2012

d.previous() ;                 // d is now February 29, 2012

Date.java = https://users.cs.fiu.edu/~crahn/COP2210/Content/Unit7/Date.java

SpeedDating.java = https://users.cs.fiu.edu/~crahn/COP2210/Content/Unit7/SpeedDating.java

Solution


/**
* A class to give students experience using loops. This class
* creates and manipulates objects of Greg\'s Date class.
*/
public class SpeedDating
{
   // Note: this class has no instance variables!

   /**
   * Create an empty SpeedDating object
   */
   public SpeedDating()
   {}       // Constructor has empty body
   // (this is known as a \"default\" constructor)

   /**
   * Computes and prints the day of the week on which the user\'s birthday
   * will fall for each year from 2015 to 2025 inclusive
   * @param theMonth the month of the birthday
   * @param theDay the day of the birthday
   */
   public void happyBirthDaze(int theMonth, int theDay)
   {

       int START_YEAR=2015;
       int END_YEAR=2025;


       for (int year = START_YEAR; year <=END_YEAR; year++)
       {
           Date date=new Date(theMonth, theDay, year);
           System.out.printf(\"In %d , the birthday falls on %s\ \",year,
                   date.getDayOfWeek());
       }

   }

   /**
   * Computes and returns the Date on which Election Day will fall
   * in the USA for a given year.
   *
   * NOTE: By law, Election Day is the first Tuesday after the first
   * Monday in November.
   *
   * @param year the year for which to compute the date of Election Day
   * @return the Date of Election Day for the specified year
   */
   public Date pollDancer(int year)
   {
       boolean found=false;
       Date date = null;
       // DAYS_IN_MONTH[1] is 31 (January), DAYS_IN_MONTH[12] is 31 (December)
               final int[] DAYS_IN_MONTH = { 0, 31, 28, 31, 30, 31, 30,
                       31, 31, 30, 31, 30, 31 };
              
       for (int day = 1; day <=DAYS_IN_MONTH[11] && !found; day++)
       {
           date=new Date(11, day, year);
           if(date.getDayOfWeek().equals(\"Monday\"))
           {
               found=true;
               date.next();
           }
           else
               date.next();
              
       }
      
       return date;
   }


   /**
   * Computes and returns the corrected \"Excel Date\" for a given Date.
   * I.e., the number of the given Date where 1/1/1900 is date #1
   *
   * @param aDate the Date for which to return the Excel Date
   * @return the Excel Date of aDate
   */
   public int iExcel(Date aDate)
   {
       int totalDays=aDate.getDay();

       // DAYS_IN_MONTH[1] is 31 (January), DAYS_IN_MONTH[12] is 31 (December)
       final int[] DAYS_IN_MONTH = { 0, 31, 28, 31, 30, 31, 30,
               31, 31, 30, 31, 30, 31 };
       for (int month = 1;
               month <aDate.getMonth();month++)
       {
           totalDays+=DAYS_IN_MONTH[month];
       }
       int year=aDate.getYear();
       // Is the Date in a leap year?     
       if((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0))
           totalDays++;
      
       return totalDays;

   }
}

------------------------------------------------

//SpeedDatingTester.java
public class SpeedDatingTester {
   public static void main(String[] args) {

       SpeedDating date=new SpeedDating();
      
       System.out.println(\"Testing happyBirthDaze\");
       date.happyBirthDaze(5, 28);
      
      
       System.out.println(\"iExcel date : \"+date.iExcel(new Date(5, 28, 1988)));
      
       System.out.println(\"Election Date : \"+date.pollDancer(2016).getLongDate());
   }

}


----------------------------------------------------------

Output:

Testing happyBirthDaze
In 2015 , the birthday falls on Thursday
In 2016 , the birthday falls on Saturday
In 2017 , the birthday falls on Sunday
In 2018 , the birthday falls on Monday
In 2019 , the birthday falls on Tuesday
In 2020 , the birthday falls on Thursday
In 2021 , the birthday falls on Friday
In 2022 , the birthday falls on Saturday
In 2023 , the birthday falls on Sunday
In 2024 , the birthday falls on Tuesday
In 2025 , the birthday falls on Wednesday
iExcel date : 149
Election Date : Tuesday, November 8th, 2016

\
\
\
\
\

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site