programming problem repetition and decision making Before Beginning Download Dat
ID: 3575328 • Letter: P
Question
programming problem 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. Also see VII., below
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
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 is supposed to do.
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 will do the following:
1. Create a SpeedDating object
2. Call method auldLangSynch to print the day of the week ("Sunday", "Monday", etc.) on which New Year’s Day will fall for each of 10 consecutive years. The first year is entered by the user
3. Call method countingTheDays to get the number of days between two dates entered by the user. Print the two dates and the number of days between.
4. Call method getThanksgiving to get the date on which Thanksgiving will fall in a given year, entered by the user.
Note: By law, Thanksgiving is the 4th Thursday in November.
Output should be in this form:
In yyyy, Thanksgiving will fall on November dd.
5. Repeat step 4 for a different year.
Data to be Used
Although your SpeedDating methods must work for all valid inputs, use this data in the run you hand in:
First year (for auldLangSynch) : 2017
Start date (for countingTheDays) : November 3, 2016
End date (for countingTheDays) : December 25, 2016
Enter each date as 3 separate ints
Year (for getThanksgiving) : 2016
Another year (for getThanksgiving) : 2017
Date java:
SpeedDating.Java:
Explanation / Answer
Comments added............
/**
*
* @author paramesh
*/
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 New Year's Day will
* fall in 10 consecutive years
*
* @param startYear the first of the 10 consecutive years
*/
public void auldLangSynch(int startYear) {
for(int i=0;i<10;i++){
Date obj = new Date(1,1,startYear);
System.out.println("Day of week :"+obj.getDayOfWeek());
startYear++;
}
}
/**
* Computes and returns the Date on which Thanksgiving will fall in a given
* year.
*
* NOTE: By law, Thanksgiving is the 4th Thursday in November
*
* @param year the year for which to compute the date of Thanksgiving
* @return the Date of Thanksgiving for the specified year
*/
public Date getThanksgiving(int year) {
//creating dummy Date object
Date obj = new Date(01,11,year);
int thursday_count = 0;
while(true){
if(obj.getDayOfWeek().equals("Thursday")){
thursday_count++;
}
obj.next();
if(thursday_count == 4){ // fourth thursday
break;
}
}
return obj;
}
/**
* 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/3/2016 and 11/7/2016 is 4, not 5.
*
* 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 between the two dates
*/
public int countingTheDays(Date start, Date end) {
if(start.getYear() > end.getYear()){ // checking for year constraint
return -1;
}
else if(start.getMonth() > end.getMonth()){// checking for month constraint
return -1;
}
else if(start.getDay() > end.getDay()){// checking for day constraint
return -1;
}
int daysCount = 0;
while(!start.equals(end)){ // counting days
start.next();
daysCount++;
}
return daysCount;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.