Question

In: Computer Science

General Requirements: • You should create your programs with good programming style and form using proper...

General Requirements:

• You should create your programs with good programming style and form using proper blank spaces, indentation and braces to make your code easy to read and understand;

• You should create identifiers with sensible names;

• You should make comments to describe your code segments where they are necessary for readers to understand what your code intends to achieve.

• Logical structures and statements are properly used for specific purposes.


Objectives
This assignment requires you to write a program in Java that calculates the day of week and the week of month for a given date, as well as to print the calendar of the month where the given date exists. Please note, you are not allowed to use any date based classes predefined in Java JDK.
Background




For a given date, we can use the Gregorian calendar to find the corresponding the day of the week, and the week of the month. For example, May 25 2019 is a Saturday and locates in the fourth week of May 2019. In general, we can use the following formula (Zeller’s congruence) to calculate the day of a week for any given date after October 15 1582.



where
• h is the day of the week (0 = Saturday, 1 = Sunday, 2 = Monday, 3=Tuesday, 4=Wednesday, 5=Thursday, 6 = Friday)

• q is the day of the month

• m is the month (3 = March, 4 = April, 5 = May, 6=June, 7=July, 8=August, 9=September, 10=October, 11=November, 12=December, 13=January, 14 = February)

• K is the year of the century (year mod 100).

• J is the zero-based century (year/100). For example, the zero-based centuries for 1995 and 2000 are 19 and 20 respectively.
For example, For 1 January 2000, the date would be treated as the 13th month of 1999, so the values would be: q=1, m=13, K=99, J=19, so the formula is
h = (1+[13*(13+1)/5]+99+[99/4]+[19/4]+5*19) mod 7 = (1+[182/5]+99+[99/4]+[19/4]+95) mod 7 = (1+[36.4]+99+[24.75]+[4.75]+95) mod 7 = (1+36+99+24+4+95) mod 7 = 0 = Saturday

However, for 1 March 2000, the date is treated as the 3rd month of 2000, so the values become q=1, m=3, K=00, J=20, so the formula is

h = (1+[13*(3+1)/5]+00+[00/4]+[20/4]+5*20] mod 7 = (1+[10.4]+0+0+5+100) mod 7 = (1+10+5+100) mod 7 = 4 = Wednesday






Tasks You will create one program called MyCalendar.java. You should first implement the MyCalendar and MyDate classes from the following UML class diagrams. You can add extra attributes and methods, but the attributes and methods shown in the UML diagrams can’t be modified.


  

MyCalendar - myDate: MyDate - day: Day + Main(String[] args) + MyCalendar(MyDate myDate) + dayOfWeek(): Day + weekOfMonth(): int + printCalendar(): void
• Day is an enumeration data type which contains the days of the week, i.e., Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, and Sunday. • MyCalendar(MyDate myDate)is the construction method of the class MyCalendar. • dayOfWeek() method will return the day of the week for myDate. • weekOfMonth() method will return the week of the month for myDate. • printCalendar() method will print the calendar of the month for myDate. • Main(String[] args) method will read a given date from the command line, the format of the given date is dd/mm/yyyy. You can assume the date input format is always correct, but the input date may be invalid. For example, 31/02/2017 is not a valid date. If the input date is not a valid date, the program will ask user to input another valid date through keyboard. This process will be repeated until a valid date is input by the user. The valid input date will be used to create the object (myDate) of class MyDate, then the object (myCalendar) of class MyCalendar. The dayOfWeek() method, weekOfMonth() method, and printCalendar() method of myCalendar object will be called to display the day of the week, the week of the month, and the calendar of the month for the input date by the user.


MyDate
- day: int - month: int - year: int + MyDate(int day, int month, int year) + getDay(): int + getMonth(): int + getYear(): int + isDateValid(): boolean
• MyDate(int day, int month, int year) is the constructor of the class MyDate. • getDay() method returns the day of myDate object. • getMonth() method returns the month of myDate object. • getYear() method returns the year of myDate object. • isDateValid() method returns a Boolean value, i.e., a true when the myDate object is valid, and a false otherwise (notes: January, March, May, July, August, October, December has 31 days. April, June, September, November has 30 days. February has 29 days in a leap year, and 28 days in a common year.).



  
Your program must have the exact same output as the following example. Example of the program output:
java MyCalendar 29/02/2019 29/02/2019 in not a valid date, please re-input a valid date: 25/05/2019 25/05/2019 is a Saturday and located in the fourth week of May 2019 The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
(Note: Each column indicates a day and the column width are fixed to 3 characters. The distance between two adjacent columns is fixed to three characters as well.)

Solutions

Expert Solution

//MyDate.java

public class MyDate
{
int day,month,year;

boolean isvaliDate=true;

public MyDate(int day,int month,int year)
{
   this.day=day;
   this.month=month;
   this.year=year;

if(month>12)   //day and month validation

   {

     isvaliDate=false;

   }

else if(month==1 || month==3 || month==5 || month==7 || month==9 || month==12)

{

    if(day<=31)

   {

     isvaliDate=true;

   }

else if(day>=31)

   {

     isvaliDate=false;

   }

}

else if(month==2 || month==4 || month==6 || month==8 || month=10 || month==12)

{

if(day<=30)

   {

     isvaliDate=true;

   }

else if(day>=30)

   {

     isvaliDate=false;

   }

}

else if(month==2) //Feb month and leap year validation

{

   if(year%4==0)

{

    if(day<=29)

   {

     isvaliDate=true;

   }

else if(day>=29)

   {

     isvaliDate=false;

   }

}

else if(year%4!==0)

{

    if(day<=28)

   {

     isvaliDate=true;

   }

else if(day>=28)

   {

     isvaliDate=false;

   }

}

}

}

boolean isvalidDate()

{

if(isvalidDate)

{

System.out.println("is a Valid Date");

return true;

}

if( ! isvalidDate)

{

System.out.println("is not a Valid Date,please re-input Date");

return false;

}

return isvalidDate;

}

public int getDay()
{
   return day;
}
public int getMonth()
{
   return month;
}
public int getYear()
{
   return year;
}
public static void main(String[] args)
{
   MyDate d=new MyDate(29, 02, 2019);
   System.out.println("Date"+d.getDay()+"/"+d.getMonth()+"/"+d.getYear());
   d.isvalidDate();
  
   MyDate d1=new MyDate(25, 02, 2019);
   System.out.println("Date"+d1.getDay()+"/"+d1.getMonth()+"/"+d1.getYear());
   d1.isvalidDate();
}
              
}


Related Solutions

Create a form using the following HTML elements at a minimum. Style the form in your...
Create a form using the following HTML elements at a minimum. Style the form in your CSS. You do not need to include a form action (i.e. the form doesn't have to do anything - we're designing the front end of the form). textarea textbox input type of "email" select radio button checkbox submit button style at least three elements of your form in your stylesheet, including your submit button Use a comment to delineate the beginning and end of...
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be...
Focus on: Basic list operations, methods, use of functions, and good programming style. Program should be in basic python. Part 1. Write a program that does the following: 1. Create a list of length N where N is a randomly selected integer between 10 and 20 and whose elements are randomly selected integers between 0 and 19. 2. Print out the list. 3. Create a copy of the list. Sort the copy into decreasing order and print it. 4. Report...
This is an exercise to design and write a Python program in good programming style for...
This is an exercise to design and write a Python program in good programming style for a simulation of stock price over a period of 100 days. In this exercise, you are asked to simulate the stock price starting at $100.00 for 100 days with a daily fluctuation based on the Normal Distribution with mean = 0.0 & sigma = 0.0125. The program will show the daily stock price, the 7-day minimum, the 7-day maximum, the 7-day average, and the...
) Use functions and arrays to complete the following programs. Requirements: • You should use the...
) Use functions and arrays to complete the following programs. Requirements: • You should use the divide-and-conquer strategy and write multiple functions. • You should always use arrays for the data sequences. • You should always use const int to define the sizes of your arrays. a. Write a C++ program. The program first asks the user to enter 4 numbers to form Sequence 1. Then the program asks the user to enter 8 numbers to form Sequence 2. Finally,...
Using PHP, create a form that uses the method GET. The form should capture a date...
Using PHP, create a form that uses the method GET. The form should capture a date using two numbers        Month (1 - 12) and Day (1 - 31) Make sure the form is filled with the user's input after the form is submitted and processed, and the page is redisplayed. If data is being passed into the page, then the page should display the date entered by the user and the season of the year. For example, if the...
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program,...
In this programming challenge you are to create two Python programs: randomwrite.py and randomread.py. One program, randomwrite.py, is to write a set of random numbers to a file. The second program, randomread.py, is to read a set of random numbers from a file, counts how many were read, displays the random numbers, and displays the total count of random numbers. Random Number File Writer (randomwrite.py) Create a program called randomwrite.py that writes a series of random integers to a file....
C Programming Language: For this lab, you are going to create two programs. The first program...
C Programming Language: For this lab, you are going to create two programs. The first program (named AsciiToBinary) will read data from an ASCII file and save the data to a new file in a binary format. The second program (named BinaryToAscii) will read data from a binary file and save the data to a new file in ASCII format. Specifications: Both programs will obtain the filenames to be read and written from command line parameters. For example: - bash$...
An example of a health form with the following requirements: 1. Create a meaningful form for...
An example of a health form with the following requirements: 1. Create a meaningful form for your application, for example, a health monitoring app 2. Edit CSS for your application. The page must be styled with at least 10 CSS styles This is using Visual Studio to use reactive form application
Your solution should be in good form with amounts clearly labeled and should use appropriate account...
Your solution should be in good form with amounts clearly labeled and should use appropriate account titles. Consider each of the transactions below. All of the expenditures were made in cash. The Edison Company spent $12,000 during the year for experimental purposes in connection with the development of a new product. On September 1, 2011, Tristar signed a $40,000 noninterest-bearing note to purchase equipment. The $40,000 payment is due on September 1, 2012. Assume that 8% is a reasonable interest...
log in form Requirements: Form: There should be at least 2 input fields for username and...
log in form Requirements: Form: There should be at least 2 input fields for username and password. Use an array as a database in which there are several usernames and passwords: Usernames can be either emails or nick names (WITHOUT WHITE SPACE) After the user submitted the form, students must: sanitize the inputs check if there is a match with one username and one password in the array. If there is no match, print out an error message to user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT