Question

In: Computer Science

Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...

Date
- month: int
- day: int
- year: int
+Date()
+Date(month: int, day: int, year: int)

+setDate(month: int, day: int, year: int): void
-setDay(day: int): void
-setMonth(month: int): void
-setYear(year: int): void

+getMonth():int
+getDay():int
+getYear():int

+isLeapYear(): boolean
+determineSeason(): string

+printDate():void

Create the class

  • Constructor with no arguments
    • sets the date to be January 1, 1900
  • Constructor with arguments
    • CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER
  • Get methods (accessors) return the field that the get refers to
  • setDate - CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER
  • setMonth - checks to see if it is a valid month, if it isn't valid, it prints a message and sets the month to 1
  • setYear - checks to see if it is a valid year, if it isn't valid (negative is invalid), it prints a message and sets the year to 1900
  • setDay - checks to see if it is a valid day for the month that was entered. It also check for leap year (using the method isLeapYear), If the day is not valid, it prints a message and sets the day to 1
  • isLeapYear- returns true if it is a leap year and false if it is not
  • determineSeason - returns the season. The seasons are Winter, Spring, Autumn, or Summer The dates for each season are:
    Spring: March 20 - June 20
    Summer: June 21 - September 21
    Autumn: September 22 - December 20
    Winter: December 21 - March 19
  • printDate- prints the date in the following format: XX/XX/XXXX it does not have a new line at the end.

Once the Date class is complete, Create a Main class and copy the main given below into it.

Follow the directions in the main method. There is one method you have to write

Put your statements below the comments, so you know what the directions are for that particular section

import java.util.Scanner;
public class Main
{
    public static Scanner kb = new Scanner(System.in);
    public static void main(String [] args)
    {
        Date birth;
        String again;

        // Refer to format for all formatting
        // Ask the user to type in if they want to enter their birthday



        // make a loop that will allow them to continue entering dates
        // until the don't answer with a y - you are just adding the boolean expression
        while(...)
        {
            // call the enterDate method to allow user to enter the date


            System.out.println();
            // Call method to print date


            //Using method in class print out if it is a leapyear or not


            //Using method in class print out the season


           // Ask the user to type in if they want to enter their birthday
           // This is basically asking if they want to do it again



            System.out.println();
        }

    }

    // Method: enterDate
    // This method asks the user to enter the month, day, and year
    // It then creates an object and returns the object


}

Sample Output

Do you want information about your birthday Y/N? Y
Enter the month: 3
Enter the day: 20
Enter the year: 2000

Date entered: 03/20/2000
You were born in a leap year
You were born in the Spring
Do you want information about your birthday Y/N? y

Enter the month: 12
Enter the day: 21
Enter the year: 1998

Date entered: 12/21/1998
You were not born in a leap year
You were born in the Winter
Do you want information about your birthday Y/N? y

Enter the month: 2
Enter the day: 29
Enter the year: 1995
29 is an invalid day. Day will be set to 1.

Date entered: 02/01/1995
You were not born in a leap year
You were born in the Winter
Do you want information about your birthday Y/N? y

Enter the month: 2
Enter the day: 29
Enter the year: 2008

Date entered: 02/29/2008
You were born in a leap year
You were born in the Winter
Do you want information about your birthday Y/N? Y

Enter the month: 6
Enter the day: 20
Enter the year: 1999

Date entered: 06/20/1999
You were not born in a leap year
You were born in the Spring
Do you want information about your birthday Y/N? y

Enter the month: 9
Enter the day: 22
Enter the year: -5
-5 is an invalid year. Year will be set to 1900.

Date entered: 09/22/1900
You were not born in a leap year
You were born in the Autumn
Do you want information about your birthday Y/N? Y

Enter the month: 13
Enter the day: 15
Enter the year: 2000
13 is an invalid month. Month will be set to 1.

Date entered: 01/15/2000
You were born in a leap year
You were born in the Winter
Do you want information about your birthday Y/N? y

Enter the month: 9
Enter the day: 31
Enter the year: 1984
31 is an invalid day. Day will be set to 1.

Date entered: 09/01/1984
You were born in a leap year
You were born in the Summer
Do you want information about your birthday Y/N? n

Solutions

Expert Solution

Code

Date Class

public class Date {
private int month;
private int day;
private int year;

public Date() {
month=1;
day=1;
year=1900;
}

public Date(int month, int day, int year) {
setMonth(month);
setYear(year);
setDay(day);
}
  
public void setDate(int month,int day,int year)
{
setMonth(month);
setYear(year);
setDay(day);
}

public void setMonth(int month) {
if(month>=1 && month<=12)
this.month = month;
else
{
System.out.println(month +" is an invalid month. Month will be set to 1");
this.month=1;
}
}
public void setDay(int day) {
if(day<1)
{
System.out.println(day +" is an invalid Day. Day will be set to 1");
this.day=1;
return;
}
if(isLeapYear() && month==2)
{
if(day>29)
{
System.out.println(day +" is an invalid Day. Day will be set to 1");
this.day=1;
return;
}
}
else if(month==2)
{
if(day>28)
{
System.out.println(day +" is an invalid Day. Day will be set to 1");
this.day=1;
return;
}
}
else if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
{
if(day>31)
{
System.out.println(day +" is an invalid Day. Day will be set to 1");
this.day=1;
return;
}
}
else
{
if(day>30)
{
System.out.println(day +" is an invalid Day. Day will be set to 1");
this.day=1;
return;
}
}
this.day = day;
}
public void setYear(int year) {
if(year>0)
this.year = year;
else
{
System.out.println("-5 is an invalid year. Year will be set to 1900.");
this.year=1900;   
}
}

public int getMonth() {
return month;
}

public int getDay() {
return day;
}

public int getYear() {
return year;
}
  
public boolean isLeapYear()
{
if(year % 4 == 0)
{
if( year % 100 == 0)
{
// year is divisible by 400, hence the year is a leap year
if ( year % 400 == 0)
return true;
else
return false;
}
else
return true;
}
else
return false;
}
  
public String determineSeason()
{
if(month==4 || month==5 || (month==3 && day>=20) || (month==6 && day<=20))
return "Spring";
if(month==7 || month==8 || (month==6 && day>=21) || (month==9 && day<=21))
return "Summer";
if(month==10 || month==11 || (month==9 && day>=22) || (month==12 && day<=20))
return "Autumn";
return "Winter";
}
public void printDate()
{
System.out.printf("%s/%s/%s",String.format("%02d", month),String.format("%02d",day),String.format("%d", year));
}
}

Main class

import java.util.Scanner;
public class Main
{
public static Scanner kb = new Scanner(System.in);
public static void main(String [] args)
{
Date birth;
String again;

System.out.print("Do you want information about your birthday Y/N? ");
again=kb.next();
// make a loop that will allow them to continue entering dates
// until the don't answer with a y - you are just adding the boolean expression
while(again.equalsIgnoreCase("Y"))
{
// call the enterDate method to allow user to enter the date
birth=enterDate();
System.out.println();
// Call method to print date
System.out.print("Date entered: ");
birth.printDate();
if(birth.isLeapYear())
System.out.println("\nYou were born in a leap year");
else
System.out.println("\nYou were not born in a leap year");
//Using method in class print out the season
System.out.println("You were born in the "+birth.determineSeason());

// Ask the user to type in if they want to enter their birthday
// This is basically asking if they want to do it again
System.out.print("Do you want information about your birthday Y/N? ");
again=kb.next();
System.out.println();
}
}

// Method: enterDate
// This method asks the user to enter the month, day, and year
// It then creates an object and returns the object

private static Date enterDate() {
int m,d,y;
System.out.print("Enter the month: ");
m=kb.nextInt();
System.out.print("Enter the day: ");
d=kb.nextInt();
System.out.print("Enter the year: ");
y=kb.nextInt();
return new Date(m, d, y);
}
}

outputs

If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.


Related Solutions

Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int...
Given the following int variables which represent a date, int day; // 0-6 for Sunday-Saturday int month; // 1-12 for January-December int date; // 1-31 int year; // like 2020 Define the function void tomorrow(int& day, int& month, int& date, int& year); which updates the variables to represent the next day. Test in main().
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month...
Coding in Java private int getLastDayOfMonth(int m, int y)– return the last day of the month and year entered as input parameter. Remember to invoke the isLeapYear method when appropriate. If the month entered as input parameter is 2 (February) and the year passed as input parameter is a leap year this method should return 29. private boolean isDateValid (int m, int d, int y) – return true if day, month, and year entered as input parameters form a valid...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable...
public class Date { private int dMonth; //variable to store the month private int dDay; //variable to store the day private int dYear; //variable to store the year //Default constructor //Data members dMonth, dDay, and dYear are set to //the default values //Postcondition: dMonth = 1; dDay = 1; dYear = 1900; public Date() { dMonth = 1; dDay = 1; dYear = 1900; } //Constructor to set the date //Data members dMonth, dDay, and dYear are set //according to...
Create a "Date" class that contains: three private data members: month day year (I leave it...
Create a "Date" class that contains: three private data members: month day year (I leave it to you to decide the type) "setters" and "getters" for each of the data (6 functions in total) One advantage of a "setter" is that it can provide error checking. Add assert statements to the setter to enforce reasonable conditions. For example, day might be restricted to between 1 and 31 inclusive. one default constructor (no arguments) one constructor with three arguments: month, day,...
Write a c++ program that asks the user for a date (Month and Day only) and...
Write a c++ program that asks the user for a date (Month and Day only) and displays the season in this day.The program asks then the user if he needs to enter another date, if he answers with ’Y’ or ’y’, the programwill take another date from the user and displays the season in this day.The program will keep repeating this until the user enters a character different than ’Y’ and ’y’.•Fall starts in September 21st•Winter starts in December 21st•Spring...
How to tokenize a string date of format        dd/mm/yyyy into day,month and year without using built...
How to tokenize a string date of format        dd/mm/yyyy into day,month and year without using built in function of strok() or any other built in function in C++ (without classes). Kindly help Please .
C++ program: Create a Date class that contains three members: the month, the day of the...
C++ program: Create a Date class that contains three members: the month, the day of the month, and the year, all of type int. The user should enter a date in the format 12/31/2001, store it in an object of type Date, then retrieve the object and print it out in the same format. Next create an employee class. The member data should comprise an employee number (type int) and the employee’s compensation (in dollars; type float). Extend the employee...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year,...
c++ Exercise 1: Make a struct “Fate” that contains the following variables: int month, int year, int day. Make another struct Product that would contain the following variables: 1- Product name. 2- An array for the product ingredients. Set its max size to 3. 3- Product date of expiration. Use the date struct you made. Use those structs in entering the data for two products of your choice. Make a function printProduct(Product product) that prints out the contents of the...
int getNumOfDaysInMonth(int mon, int year) {    if (mon == 2) if ( (year % 400...
int getNumOfDaysInMonth(int mon, int year) {    if (mon == 2) if ( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) return 29; else return 28; else if (mon == 4 || mon == 6 || mon == 9 || mon == 11) return 30; else if (mon == 1 || mon==3 || mon == 5 || mon == 7 || mon == 8 || mon == 10 || mon ==...
Write a query to return the date and day of the week of the first day...
Write a query to return the date and day of the week of the first day of the month two years from today. If today is 10/16/20, then the expect output is 10/01/2022 and the day of the week is thursday. I am using postgresql.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT