تم الحل ✓
categoryبرمجة وتطوير البرمجيات
schoolبكالوريوس
event_available2026-07-13
السؤال
Transcribed Image Text:
Implementing a Gregorian Date Class
Implement a basic Date class whose objects represent dates on the Gregorian calendar with no
exception-handling capability. Also, write a tester class, Date Demo, that creates Date objects
using the constructors, modifies them and prints them out in the day-of-the-week, month-name
day, year format using the printf statement.
The Date Class
The Date class should have month, day and year, all integers, as the fields of a Date object.
The class should include the following constructors:
Date(): creates January 1, 1970.
.Date(int m, int d, int y): creates a Date object using the specified parameters, where m
represents the month, d, the day, and y, the year.
Date(Date date): creates an object of the Date class equivalent to the specified date object.
The Date class should have the following instance (accessor and mutator) methods:
int getMonth(): returns the month in an object of the Date class.
Int getDay(): returns the day in an object of the Date class.
int getYear(): returns the year in an in object of the Date class.
void setDate(int m, int d, int y): modifies a Date object using the specified parameters,
where m represents the month, d, the day, and y, the year.
The DateDemo Class
Write a program, DateDemo, that does the following:
1. Prompts the user for the first day of class.
2. Reads a string in the format mm/dd/yyyy. parses the string into three integers representing
the month, day and year, and uses the parameterized constructor to create a Date object
using the three values.
3. Using the copy constructor, creates a second Date object that represents the last day of
class. Use the first day of class as an explicit parameter to the constructor.
4. Using appropriate accessor methods, prints the first day of class class in day-of-the-week
month-name day, year format.
5. Using appropriate accessor methods, prints the last day of class in day-of-the-week, month-
name day, year format.
6. Using the mutator, modifies last day of class using appropriate parameters.
7. Again, prints both the first and last day of class in day-of-the-week, month-name day, year
format using appropriate accessor methods.
In order to determine the weekday on which a day falls, use the following algorithm:
1. u=2(3-(century mod 4)), where century is the first two digits of the year. For example,
century is 20 for the year 2011.
2. the last two digits of the year. For example, is 11 for 2011.
3., using integer division. For example, v = 11/4 = 2 for 2011.
4. is determined using the month Table 1. For example, is 3 for February 2011.
Month
1
0 (6 in a leap year)
2
3 (2 in a leap year)
3
3
4
6
5
1
6
4
7
6
8
2
9
5
10
0
11
3
12
5
Table 1: Month
There are two ways in which a year may be a leap year.
DEFINITION 1. Any year that is either a multiple of 400 or is not a multiple of 100, but is a
multiple of 4 is a leap year.
5. yu++++ day. y=6+11+2+3+3 25 for February 3, 2011.
6. day of the week mod 7. For example, February 3, 2011 occurs on Thursday, since 25
mod 7 is 4. Note that 0 Sunday, 1 Monday..... 6 = Saturday.
Implement a method, static String findWeekDay (int month, int day, int year), in the main class,
Date Demo, that returns the weekday for a given date using the algorithm described above. Your
main class will consist of two methods: this method and the main.
Read the Java online API documentation for the split method in the String class and the In-
teger.parseInt method in the Integer class. They may be handy when dealing with inputs.
Additional Requirements
All methods must be public and all Instance variables private. Do not use if-statements to deter-
mine the name of the weekday or month: declare two arrays of strings, one with the names of the
months and the other with the days of the week, and use the appropriate indexes to obtain them
when needed. Avoid any other unnecessary us of if-statements. Exhaustively, test your program
to ensure that it works. (You may find the calendar generator at http://www.dayoftheweek.org
helpful in verifying the correctness of your program.) Do not test your program using an invalid
date or a date before 1583.
Here are two sample program interactions:
Listing 1: Sample Run 1.
1 Enter the first day of class -> 8/22/2016
2
3 First day of class: Monday, August 22, 2016
4 Last day of class: Monday, August 22, 2016
5
6 Enter the last day of class -> 12/8/2016
7
8 First day of class: Monday, August 22, 2016
9 Last day of class: Thursday, December 8, 2016
Listing 2: Sample Run 2.
1 Enter the first day of class -> 2/7/1900
2
3 First day of class: Wednesday, February 7, 1900
4 Last day of class: Wednesday, February 7, 1900
5
6 Enter the last day of class -> 1/19/1901
7
8 First day of class: Wednesday, February 7, 1900
9 Last day of class: Saturday, January 19, 1901
Start Page
X
Source
History
DateDemo.java X Date.java x
package datedemo;
द
4 import java.util.Scanner;
12345670
12
8
10
2012
9
11
{
13
34
14
Π
O
* @author
public class DateDemo
public static String findWeekDay (int month, int day, int year)
{
{
public static void main(String[] args)
Scanner scan = new Scanner(System.in);
上
16
17
18
☐
19
20
21
22
23
24
25
26
30
System.out.println("Enter the first day of class -> ");
String line = scan.nextLine();
System.out.println();
String[] nums = line.split("/");
m1 =
Integer.parseInt(nums [0]);
di = Integer.parseInt(nums[1]);
www.
=
X1 Integer.parseInt(nums [2]);
31
32
33
34
35
36
}
Start Page
x DateDemo.java xDate.java x
History
package datedemo;
Source
1
2
3
4
5
6
7
8
9
10
11
12
13
* @author
public class Date (
private int day;
private int month;
private int year;
public Date()
{
day = 0;
month 0;
14
15
}
year = 0;
16
17
18
19
20
public Date (int m, int d, int y)
this.day day;
this.month = month;
this.year = year;
21
26
27
29
30
31
public Date (Date date)
{
day date.day;
month
date.month;
year date. year;
}
33
34
public int getDay ()
f
return day;
I
36
37
}
25228252232252
public int getMonth().
{
return month;
39
I
40
41
42
43
public int getYear()
44
45
{
46
return year;
47
}
48
public void setDate (int m, int d, int y)
{
this.month = month;
this.day day;
this. year
year;
I
49
50
54
55
56
}
57
58
3 3 3 2 3 3 4 95
check_circle الجواب — حل مفصل خطوة بخطوة
hourglass_top
🔒
الحل الكامل متاح للمشتركين
اشترك في أرشيف الأسئلة لعرض هذا الحل وآلاف الحلول المفصلة خطوة بخطوة من معلمين معتمدين.