Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

JAVA Please put detailed comments as well explaining your program. I\'m in a beg

ID: 3862686 • Letter: J

Question

JAVA

Please put detailed comments as well explaining your program.

I'm in a beginning programming class, and so please use more basic techniques such as if else statements, switch operators, and for loops if needed.

http://imgur.com/a/xx9Yc

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

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

Explanation / Answer

JAVA CODE:

import java.util.Scanner;
import java.util.*;
public class HolidayFinder{
  
static String[] monthInString={"January","February","March","April","May","June","July","August","September","October","November","December"};

public static void printDirection(){
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.");
System.out.println("You may enter the month as: * a numeric value (1..12) or as "+"* an unabbreviated month name (January or February etc....) ");
}
  
public static String getMonthString(int month){
return monthInString[month-1];
}

public static int getMonthNumber(String month){
int monthInt=-1;
for(int i=0;i<monthInString.length;i++){
if(month.equalsIgnoreCase(monthInString[i]))
monthInt=i+1;
}
return monthInt;
}
  
public static String getHoliday(int month,int day,int year){
String holiday="",easter="Easter and ";
switch(month){
case 1:
switch(day){
case 18:
holiday= "Martin Luther King Jr. Day";
break;
}
break;
case 2:
switch(day){
case 2:
holiday= "Ground Hog Day";
break;
case 12:
holiday= "Abraham Lincoln's Birthday";
break;
case 14:
holiday= "St. Valeninte's Day";
break;
case 22:
holiday="George Washington's Birthday";   
break;
}
break;
case 3:
switch(day){
case 17:
holiday= "St. Patrick's Day";
break;
  
}
break;
case 4:
switch(day){
case 1:
holiday= "April Fool's Day";
break;
case 4:
holiday= "Grandma's Birthday";
break;
case 22:
holiday= "Earth Day";
break;
case 30:
holiday= "Arbor Day";
break;
}
break;
case 5:
switch(day){
case 1:
holiday= "May Day";
break;
case 5:
holiday= "Cinco de Mayo";
break;
}
break;
case 6:
  
case 7:
switch(day){
case 4:
holiday= "Independence Day";
break;
}
break;
case 8:
switch(day){
case 1:
holiday= "International Friendship Day";
break;
}
break;
case 9:
case 10:
switch(day){
case 1:
holiday="Columbus Day";
break;
case 31:
holiday="Halloween";
break;
}
break;
case 11:
switch(day){
case 11:
holiday= "Vereran's Day";
break;
  
}
break;
case 12:
switch(day){
case 25:
holiday= "Christmas";
break;
case 31:
holiday= "New Year's Eve";
break;
}
break;
default:
holiday= "";
}
  
if(isEaster(month,day,year)){
if(holiday.equals(""))
return "Easter";
else
return (easter.concat(holiday));
}
else   
return holiday;
}
  
public static boolean isEaster(int month,int day,int year){
int goldenNumber = (year % 19) + 1;
int a = (24 + 19*(goldenNumber - 1)) % 30;
int b = a - a/28;
int c = (year + year/4 + b - 13) % 7;
int d = b - c;
int easterMonth = 3 + (d + 40)/44;
int easterDay = d + 28 - 31*(easterMonth/4);
  
if((easterMonth==month)&&(easterDay==day))
return true;
else
return false;
}
  
public static void main(String []args){
int times,month,year,day;
String monthString;
Scanner in=new Scanner(System.in);
printDirection();
System.out.print("How many times do you want to run the program:");
times=in.nextInt();
for(int i=0;i<times;i++){
System.out.print("Enter the month:");
if(in.hasNextInt())
{
month=in.nextInt();
monthString=getMonthString(month);
  
}
else{
monthString=in.next();
month=getMonthNumber(monthString);
}
  
System.out.print("Enter the day:");
day=in.nextInt();
System.out.print("Enter the year:");
year=in.nextInt();
System.out.println(" ");
  
System.out.println("The Date is: "+month+"/"+day+"/"+year+" "+getMonthString(month)+" "+day+","+" "+year+" "+getHoliday(month,day,year));   
}
}
}

Note: copy the code to file named HolidayFinder.java . compile and run