In: Computer Science
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
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
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.