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: 3862692 • 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.

If this helps some chapters we went through have been primitive data and definite loops, Introduction to Parameters and Objects and Conditional Execution

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

//Please insert your name inside the instructions which are printed in the "printDirections" method.

import java.util.Scanner;

public class HolidayFinder {

   public static void main(String[] args){
      
       // initializing the variables
       int year=0;
       int month_number=0;
       String month_string;
       int day=0;
       int runTime = 0;
       String holiday="";
      
       //method for printing the instructions
       printDirections();

       //scanner object for reading input from the user
       Scanner scan = new Scanner(System.in);
      
       // taking no of times program needs to run
       runTime = scan.nextInt();
      
       //forloop for running "runTime" no of times
       for(int i=0;i<runTime;i++){
          
          
           // Reading the month
           System.out.print("Enter the month: ");
           if(scan.hasNextInt())
           {
           // if month is integer, get in string format also
           month_number = scan.nextInt();  
           month_string = getMonthString(month_number);  
           }
           else{
              
               // if month is given in string format, retrieve the numerical month
               month_string = scan.next();
               month_number = getMonthNumber(month_string);
           }
          
          
           // Reading the day
           System.out.print("Enter the day: ");
           day = scan.nextInt();

           // Reading the year
           System.out.print("Enter the year: ");
           year = scan.nextInt();
          
           //finding the holiday
           holiday = getHoliday(month_number, day, year);
          
           //printing the result
           System.out.println("The Date is:"+month_number+"/"+day+"/"+ year + " " + month_string + " " + day + ", " + year + " "+ holiday + " ");
  
       };

   }
  
   public static void printDirections(){
      
       // Simple println statement to print the instructions
       System.out.print("CSC 15 – Chapter 4 – 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: ");
      
   }
  
   public static String getMonthString(int month){
  
       //finding the string representation of month
       String monthString = "";
      
       switch (month) {
       case 1:
           monthString = "January";
           break;
       case 2:
           monthString = "February";
           break;
       case 3:
           monthString = "March";
           break;
       case 4:
           monthString = "April";
           break;
       case 5:
           monthString = "May";
           break;
       case 6:
           monthString = "June";
           break;
       case 7:
           monthString = "July";
           break;
       case 8:
           monthString = "August";
           break;
       case 9:
           monthString = "September";
           break;
       case 10:
           monthString = "October";
           break;
       case 11:
           monthString = "November";
           break;
       case 12:
           monthString = "December";
           break;
       default:
           monthString = "Invalid month";
           break;      
       }
      
       return monthString;
   }
  
  
   public static int getMonthNumber(String month_string){
      
      
       // finding the numerical month from string month
       int month_number = 0;
      
       switch(month_string){
       case "January" :
           month_number = 1;
           break;
       case "February" :
           month_number = 2;
           break;
       case "March" :
           month_number = 3;
           break;
       case "April" :
           month_number = 4;
           break;
       case "May" :
           month_number = 5;
           break;
       case "June" :
           month_number = 6;
           break;
       case "July" :
           month_number = 7;
           break;
       case "August" :
           month_number = 8;
           break;
       case "September" :
           month_number = 9;
           break;
       case "October" :
           month_number = 10;
           break;
       case "November" :
           month_number = 11;
           break;
       case "December" :
           month_number = 12;
           break;
       default :
           month_number = 0;
           break;

       }
      
      
       return month_number;
      
      
   }
  
   public static String getHoliday(int month,int day,int year){
      
       String holiday = "";

       switch(month){
      
       case 1:{
           switch(day){
          
           case 1: holiday = "New Year's Day";
           break;
           case 18: holiday = "Martin Luther King Jr. Day";
           break;   
           }
          
           // breaking from the outer switch statement
           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;
       case 22: holiday = "Earth Day";
       break;
       case 30: holiday = "Arbor Day";
       break;
       }
      
       break;
           }
      
       case 7:{
       switch(day){
      
       case 4: holiday = "Independence Day";
       break;
       }
      
       break;
           }
      
       case 8:{
       switch(day){
      
       case 1: holiday = "International Friendship Day";
       break;
       }
      
       break;
           }
      
       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;
           }
       }
      
      
       // check if the given day is easter or not
       if(isEaster(month, day, year)){
      
           //checkif holiday has length > 0 (i.e. presence of any other holiday)
       if(holiday.length() > 0){
           holiday += " and Easter";
       }
       else{
           holiday = "Easter";
       }
       }
      
       return holiday;
   }
  
   public static Boolean isEaster(int month,int day,int year){
      
       // calculating as per the problem statement to find easter month and day using year
       int golden_number = (year % 19) + 1;
      
       int a = (24 + 19*(golden_number - 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);
      
      
       //checking if calculated easter month and day matches the user supplied day and month
       if(easterMonth == month && easterDay==day){
           return true;
       }
      
       return false;
   }
  
}