Docsity

Speed Dating Java - Assignment 7 | COP 2210, Study notes of Computer Science

Toggle sidebar
bg1
Date Assignment (7) (Copy and paste when necessary)
SpeedDating.java
/* File: SpeedDating.java
*
* @ Name
*
* I affirm that this program is entirely my own work and
* none of it is the work of any other person.
*/
/**
* 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
* (this is known as a "default" constructor)
*/
pf3
pf4
pf5
pf8
pf9

Partial preview of the text

Download Speed Dating Java - Assignment 7 | COP 2210 and more Study notes Computer Science in PDF only on Docsity!

Date Assignment (7) (Copy and paste when necessary) SpeedDating.java /* File: SpeedDating.java

  • @ Name
  • I affirm that this program is entirely my own work and
  • none of it is the work of any other person. / /*
  • 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
  • (this is known as a "default" constructor) */

public SpeedDating() { // Constructor has empty body } /**

  • Prints the day of the week (e.g. "Thursday") on which the 4th of July
  • will fall in 10 consecutive years
  • @param startYear the first of the 10 consecutive years */ public void print4thsOfJulys( int startYear ) { // The loop that limits the occurrences of the 4th of July for 10 years for ( int year = 1 ; year <= 10 ; year++ ) { // The object that defines the fourth of July Date the4thOfJuly = new Date( 7, 4, startYear) ; // Answering: Which day of the week does the holiday occur? String print = the4thOfJuly.getDayOfWeek() ; System.out.println("The fourth of July for the year " + startYear

isDayOfThanksgiving.next() ; // Add days until it's that time! } // Eventually, the fourth Thursday will appear while ( !isDayOfThanksgiving.getDayOfWeek().equals( "Thursday" ) ) ; int day = isDayOfThanksgiving.getDay() ; // Get day of holiday // Return the holiday to be on that day in the year given return isDayOfThanksgiving ; } /**

  • Computes and returns the number of days between two dates,
  • counting the end date but not the start date. E.g., the
  • number of days between 11/2/2010 and 11/5/2010 is 3, not 4.
  • Precondition: The start date must occur on or before the end date.
  • @param start the earlier of the two dates
  • @param end the later of the two dates
  • @return the number of days elapsed between the start date and the
  • end date */ public int getDaysBetween(Date start, Date end) { // NOTE: Do NOT modify the parameters "start" or "end" in this method! // Doing so will modify the arguments in the calling method! // If you need to do this (most likely you will need to modify one), // use one of the copies provided here instead: // Date d1 = new Date(start.getMonth(), start.getDay(), start.getYear()); // Date d2 = new Date (end.getMonth (), end.getDay (), end.getYear () ) ; int daysInBetween = 0 ; // Number of days between dates while ( !start.equals(end) ) // If dates are different, carry on { start.next() ; // Adding dates will lessen the difference daysInBetween++ ; // Incrementing this will find the number, } // assuming that the while line holds.

text = JOptionPane.showInputDialog("Type: 11_2_2011_12_25_2011 where "

  • "_ means space.") ; while ( text != null ) // While dates have been entered { Scanner input = new Scanner(text) ; int month = input.nextInt() ; int day = input.nextInt() ; // These 3 lines are tokens int year1 = input.nextInt() ; // for date object one int month2 = input.nextInt() ; int day2 = input.nextInt() ; // These 3 lines are tokens int year2 = input.nextInt() ; // for date object two Date date = new Date(month, day, year1) ; // Date object one Date date2 = new Date(month2, day2, year2) ; // Date object two // Finding the number of days in between the two dates // A string to organize dates String dates = date.getShortDate() + ", & " + date2.getShortDate()
  • "," ; System.out.println("\n\nThe number of days between " + dates + " is "
  • countdown.getDaysBetween(date, date2)
  • ".\n\n\n") ; // The safeguard against an infinite loop is below text = JOptionPane.showInputDialog("Press cancel to complete the "
  • "scan; otherwise, re-run this "
  • "program.") ; } // Define the year Thanksgiving occurs text = JOptionPane.showInputDialog("Type a year for Thanksgiving"
  • ".") ; year = Integer.parseInt(text) ; // Determine the date Thanksgiving occurs in the given year

Date thanksgiving = countdown.getThanksgiving(year) ; // You can't print the date directly, so this is required for printing String print = countdown.getThanksgiving(year).getLongDate() ; System.out.println("Thanksgiving for the year " + year + " is on "

  • print + ".\n") ; // Repeat the Thanksgiving procedure text = JOptionPane.showInputDialog("Type a new year for Thanksgiving"
  • ".") ; int year2 = Integer.parseInt(text) ; // Find the date for Thanksgiving for the new year again thanksgiving = countdown.getThanksgiving(year2) ; // You still can't print the date directly, so this is required for printing print = countdown.getThanksgiving(year2).getLongDate() ; System.out.println("Thanksgiving for the year " + year2 + " is on "
  • print + ".\n") ; } } /* OUTPUT @ Name (§U0#) The fourth of July for the year 2011 is on a Monday. The fourth of July for the year 2012 is on a Wednesday. The fourth of July for the year 2013 is on a Thursday. The fourth of July for the year 2014 is on a Friday. The fourth of July for the year 2015 is on a Saturday. The fourth of July for the year 2016 is on a Monday. The fourth of July for the year 2017 is on a Tuesday. The fourth of July for the year 2018 is on a Wednesday. The fourth of July for the year 2019 is on a Thursday.