JAVA Please put detailed comments as well explaining your program. http://imgur.
ID: 3833926 • Letter: J
Question
JAVA
Please put detailed comments as well explaining your program.
http://imgur.com/a/xx9Yc
THE PROBLEM:
You must use switch statements for some part of the homework.
Write a program that inputs a month, day, and year from the user and outputs the corresponding date in the following two standard date formats:
6/12/2005 June 12, 2005
Also your program must print the name of any holiday associated with the date.
For example:
3/17/2010 March 17, 2010 St. Patrick’s Day
Your program should ask the user how many times the user wants to run the code and then you need to use a for loop to repeat the run that many times.
REQUIREMENTS:
You must use good programming style.
The user can enter the month either as a numeric value or a String. i.e. the user could enter a 5 or May.
You may assume the data that the user enters is valid data.
Your program must print a report similar to that shown in the sample output on the last page of this handout.
You must solve this problem by implementing and using the following methods:
printDirections
void method that prints a message to the user that explains what the program will do and how the month data can be entered. (See the sample output above.)
getMonthString
method that has 1 parameter, the month as an integer (1..12).
This method returns the corresponding name of the month as a String.
getMonthNumber
method that has 1 parameter, the name of the month in a String.
This method returns the corresponding integer value for that month name.
HINT: Before you read the data for the month, use the Scanner class hasNextInt method to determine what kind of data the user entered for the month. Using hasNextInt you can determine if the user is entering an integer for the month or not.
getHoliday
This method has 2 int type parameters for month and day.
The method returns a String that is the name of a holiday that is associated with the date represented by the parameters. If there is no holiday associated with that date, then the method returns an empty String (“”).
Use nested switch statements to implement this method. Use one switch statement for the months. Inside the case for each month, use a switch statement that has a case for each day in that month that is a holiday. Inside each of those day cases, set the holiday string to the name of the holiday.
NOTE: You can also include holidays for your birthday, anniversary, or whatever …
isEaster
This method has 3 int type parameters for month, day and year.
This method returns true if the date represented by the 3 parameters is Easter, otherwise it returns false.
To implement this method: use the following formula to figure out the month and the day of easter for the given year.
goldenNumber = (year % 19) + 1;
a = (24 + 19*(goldenNumber - 1)) % 30; b = a - a/28;
c = (year + year/4 + b - 13) % 7; d = b - c;
easterMonth = 3 + (d + 40)/44;
easterDay = d + 28 - 31*(easterMonth/4);
To use this method, you will have to add a third parameter to the getHoliday method for the year, and then inside the getHoliday method, after the switch statements, call this method.
NOTE: If the holiday string is already longer than 0, this day is already associated with another holiday,
(For example, maybe “Grandma’s Birthday”, then getHoliday should return “Easter and Grandma’s Birthday”.
To test the isEaster method, make April 4 “Grandma’s Birthday” and test this method using 4/4/2010. Also test it for Easter in a year where Easter is not on April 4.
Here is a list of holidays that your need needs to generate the appropriate output:
1/1 "New Year's Day";
1/18 "Martin Luther King Jr. Day";
2/2 "Ground Hog Day";
2/12 "Abraham Lincoln's Birthday";
2/14 "St. Valeninte's Day";
2/22 "George Washington's Birthday";
3/17 "St. Patrick's Day"; 4/1 "April Fool's Day";
4/4 "Grandma's Birthday";
4/22 "Earth Day";
4/30 "Arbor Day";
5/1 "May Day";
5/5 "Cinco de Mayo";
7/4 "Independence Day";
8/1 "International Friendship Day";
10/1 "Columbus Day";
10/31 "Halloween";
11/11 "Vereran's Day";
12/25 "Christmas";
12/31 "New Year's Eve";
?? "Easter";
Sample outputs
CSC 15 – Chapter 4 – [Your Name]
This program will ask you for a month, day, and year and will print the corresponding date in two standard date formats.
You may enter the month as: * a numeric value (1..12) or as
* an unabbreviated month name (January or February etc....)
How many times do you want to run the program: 3
Enter the month: 10
Enter the day: 31
Enter the year: 2010
The Date is: 10/31/2010 October 31, 2010 Halloween
Enter the month: 1
Enter the day: 1
Enter the year: 2014
The Date is: 1/1/2014 January 1, 2014 New Year's Day
Enter the month: 4
Enter the day: 4
Enter the year: 2010
The Date is: 4/4/2010 April 4, 2010 Easter and Grandma's Birthday
Pseudocode for the main method:
Print the headings
Print the directions
Prompt for the month
If the month is an integer (Hint: Use the Scanner class hasNextInt method.)
Input the integer for the month
Get the string for the month (Use your method)
Otherwise
Input the string for the month
Get the integer for the month (Use your method)
Prompt for the day
Input the day
Prompt for the year
Input the year
Get the holiday
Print the output
CSC 15 – Chapter 4 – [Your Name]
This program will ask you for a month, day, and year and will print the corresponding date in two standard date formats.
You may enter the month as: * a numeric value (1..12) or as
* an unabbreviated month name (January or February etc....)
How many times do you want to run the program: 3
Enter the month: 10
Enter the day: 31
Enter the year: 2010
The Date is: 10/31/2010 October 31, 2010 Halloween
Enter the month: 1
Enter the day: 1
Enter the year: 2014
The Date is: 1/1/2014 January 1, 2014 New Year's Day
Enter the month: 4
Enter the day: 4
Enter the year: 2010
The Date is: 4/4/2010 April 4, 2010 Easter and Grandma's Birthday
THE PROBLEM: You must use switch statements for some part of the homework. Write a program th puts a month, day, and year fro the user and outputs the corresponding dale in following two sta ndand date formats: 612/200 June 12, 2005 Also print the name of any holiday associated th the date program For example 7/2010 March 17, 2010 St. Patrick's Da should ask the de and th Progra to rep REQUIREMENTS: You must use good programming style. The user can enter the month either as a numeric value or a String i e the user could enter a 5 or May You may assume data that the user ters is valid data. Your program must punt a report sumilar to that shown in the sample output on the last page of this handout ust sol this problem by ting and ng the full o pruntDirecuons ethod that prints a essage to the user that explains nt th do and how the month data can be enterad. (See the sample output above) o getMonthString A method that has l paramet the month as an integer This method returns the corresponding name of the month as a String. o ge Month Number has parameter, the name of the a S This method ponding integer HINT: Before you read the dat for the month, use th Scanner class hasNextl nd to determine what kind of data the user entered for the manrh. Using hasNextInt you can determine if the user is entering an integer for the month or not.Explanation / Answer
Program
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class Month
{
//reading the input from the user
static InputStreamReader in=new InputStreamReader(System.in);
static BufferedReader br=new BufferedReader(in);
//method for getting the year and day
public String getDateOrYear()
{
String yearOrDay="";
try
{
yearOrDay=br.readLine();
} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
return yearOrDay;
}
//method for getting the month
public String getMonth()
{
int mont=0;
String demo="";
try
//for getting the user input value
{
demo=br.readLine();
mont=Integer.parseInt(demo);
}
catch (NumberFormatException e)
{
demo=demo.toLowerCase();
switch(demo)
{
case "january":
return "1";
case "february":
return "2";
case "march":
return "3";
case "april":
return "4";
case "may":
return "5";
case "june":
return "6";
case "july":
return "7";
case "august":
return "8";
case "september":
return "9";
case "october":
return "10";
case "november":
return "11";
case "december":
return "12";
default:
System.out.println("Enter a valid input");
}
}
catch (IOException e)
{
e.printStackTrace();
}
return demo;
}
//method for getting the holiday list
public String getHolidayLis(String month,String day)
{
int mont=Integer.parseInt(month);
int day1=Integer.parseInt(day);
switch(mont)
{
case 1:
switch(day1)
{
case 18:
return "Martin Luther King Jr. Day";
default:
return "";
}
case 2:
switch(day1)
{
case 2:
return "Ground Hog Day";
case 12:
return "Abraham Lincoln's Birthday";
case 14:
return "st. Valeninte's Day";
case 22:
return "George Washington's Birthday";
default:
return "";
}
case 3:
switch(day1)
{
case 17:
return"St. Patrick's Day";
default:
return "";
}
case 4:
switch(day1)
{
case 1:
return "April Fool's Day";
case 4:
return"Grandma's Birthday";
case 22:
return"Earth Day";
case 30:
return"Arbor Day";
default:
return"";
}
case 5:
switch(day1)
{
case 1:
return"May Day";
case 5:
return"Cinco de Mayo";
default:
return"";
}
case 6:
return "";
case 7:
switch(day1)
{
case 4:
return"Independence Day";
default:
return "";
}
case 8:
switch(day1)
{
case 1:
return "International Friendship Day";
default:
return "";
}
case 9:
return "";
case 10:
switch(day1)
{
case 1:
return"Columbus Day";
case 31:
return"Halloween";
default:
return"";
}
case 11:
switch(day1)
{
case 1:
return "Vereran's Day";
default:
return"";
}
case 12:
switch(day1)
{
case 25:
return "Christmas";
case 31:
return"";
default:
return"New Year's Eve";
}
}
return null;
}
//method for finding the date is easter
public boolean isEaseter(String year,String moth,String day)
{
int year1=Integer.parseInt(year);
int month1=Integer.parseInt(moth);
int day1=Integer.parseInt(day);
int goldenNumber = (year1 % 19) + 1;
int a = (24 + 19*(goldenNumber - 1)) % 30;
int b = a - a/28;
int c = (year1 + year1/4 + b - 13) % 7;
int d = b - c;
int easterMonth = 3 + (d + 40)/44;
int easterDay = d + 28 - 31*(easterMonth/4);
if(easterMonth==month1&&easterDay==day1)
{
return true;
}
return false;
}
//main method
public static void main(String s[])
{
Month mth=new Month();
int count=0;
System.out.println("This program will ask you for a month, day, and year and will print the corresponding date in two standard date formats."
+ "You may enter the month as: "
+ " * a numeric value (1..12) "
+ " or as * an unabbreviated month name (January or February etc....) ");
System.out.println("How many times do you want to run the program:");
try
{
count=Integer.parseInt(br.readLine());
}
catch (NumberFormatException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
//for entering the user input
for(int i=0;i<count;i++)
{
System.out.println("Enter The Mnth : ");
String month=mth.getMonth();
System.out.println("Enter The Day: ");
String day=mth.getDateOrYear();
System.out.println("Enter The Year: ");
String year=mth.getDateOrYear();
String date=month+"/"+day+"/"+year;
SimpleDateFormat smp=new SimpleDateFormat("MM/dd/yyy");
Date dateP1=null;
String datep2=null;
String datep3=null;
try
{
dateP1 =smp.parse(date);
datep2=smp.format(dateP1);
smp=new SimpleDateFormat("MMMMM d,yyyy");
datep3=smp.format(dateP1);
}
catch (ParseException e)
{
System.out.println("The input pattern is not good");
}
//calling the isEaseter method
boolean flag=mth.isEaseter(year, month, day);
if(flag)
{
System.out.println("The Date is:"+datep3+" "+datep2+"Easter and"+mth.getHolidayLis(month, day));
}
else
{
System.out.println("The Date is:"+datep3+" "+datep2+" "+mth.getHolidayLis(month, day));
}
}
}
}
OUTPUT
This program will ask you for a month, day, and year and will print the corresponding date in two standard date formats.You may enter the month as: * a numeric value (1..12) or as * an unabbreviated month name (January or February etc....)
How many times do you want to run the program:
1
Enter The Mnth :
1
Enter The Day:
18
Enter The Year:
2016
The Date is:January 18,2016 01/18/2016 Martin Luther King Jr. Day
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.