In: Computer Science
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
import java.util.*;
class DisplayDate{
static void printDirections(){
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.\n You may enter the month as: \n * a numeric value (1..12)
\n or as\n * an unabbreviated month name (January or February
etc....) ");
}
static String getMonthString(int month){
if(month==1){
return "January";
}else if (month==2){
return "February";
}else if (month==3){
return "March";
}else if (month==4){
return "April";
}else if (month==5){
return "May";
}else if (month==6){
return "June";
}else if (month==7){
return "July";
}else if (month==8){
return "August";
}else if (month==9){
return "September";
}else if (month==10){
return "October";
}else if (month==11){
return "November";
}else if (month==12){
return "December";
}else{return "Invalid Entry";}
}
static int getMonthNumber(String monthstr){
if(monthstr .equalsIgnoreCase( "January")){
return 1;
}else if (monthstr.equalsIgnoreCase("February")){
return 2;
}else if (monthstr.equalsIgnoreCase("March")){
return 3;
}else if (monthstr.equalsIgnoreCase("April")){
return 4;
}else if (monthstr.equalsIgnoreCase("May")){
return 5;
}else if (monthstr.equalsIgnoreCase("June")){
return 6;
}else if (monthstr.equalsIgnoreCase("July")){
return 7;
}else if (monthstr.equalsIgnoreCase("August")){
return 8;
}else if (monthstr.equalsIgnoreCase("September")){
return 9;
}else if (monthstr.equalsIgnoreCase("October")){
return 10;
}else if (monthstr.equalsIgnoreCase("November")){
return 11;
}else if (monthstr.equalsIgnoreCase("December")){
return 12;
}else{return 0;}
}
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;
}
return false;
}
static String getHoliday(int month,int day,int year){
String str;
if(isEaster(month,day,year)== true){
str="Easter Day and ";
}else{
str="";}
switch(month){
case 1:
switch(day){
case 1:
return str+"New Year's Day";
case 18:
return str+"Martin Luther King Jr. Day";
}
case 2:
switch(day){
case 2:
return str+"Ground Hog Day";
case 12:
return str+"Abraham Lincoln's Birthday";
case 14 :
return str+"St. Valeninte's Day";
case 22 :
return str+"George Washington's Birthday";
}
case 3:
switch(day){
case 17:
return str+"St. Patrick's Day";
}
case 4:
switch(day){
case 1:
return str+"April Fool's Day";
case 4:
return str+"Grandma's Birthday";
case 22:
return str+"Earth Day";
case 30:
return str+"Arbor Day";
}
case 5:
switch(day){
case 1:
return str+"May Day";
case 5:
return"Cinco de Mayo";
}
case 7:
switch(day){
case 4:
return str+ "Independence Day";
}
case 8:
switch(day){
case 1:
return str+ "International Friendship Day";
}
case 10:
switch(day){
case 1:
return str+ "Columbus Day";
case 31:
return str+ "Halloween";
}
case 11:
switch(day){
case 11:
return str+ "Vereran's Day";
}
case 12:
switch(day){
case 25: return str+ "Christmas";
case 31 : return str+ "New Year's Eve";
}
default:
return str+" ";
}
}
public static void main(String[] args) {
System.out.println("CSC 15 \n"); /* HEADING */
printDirections();
System.out.println("How many times do you want to run the program :
\n");
Scanner sc = new Scanner(System.in); //Read How many times program
should run into count
int count;
int month=0,day,year;
String monthstr;
count=sc.nextInt();
for(int i=0;i<count;i++){ //loop the program
System.out.println("Enter the month \n ");
monthstr=sc.next();
Scanner scanner = new Scanner(monthstr);
if (scanner.hasNextInt()) { //check if the scanner"s next token is
an int
month=Integer.parseInt(monthstr); //if true month variable store
the integer
monthstr= getMonthString(month);
}else{
month=getMonthNumber(monthstr);
}
//System.out.println("Month value is "+ monthstr+" and month number
is \n"+month);
System.out.println("Enter the day: \n");
day=sc.nextInt();
System.out.println("Enter the year: \n");
year=sc.nextInt();
System.out.println("The Date is "+ month+"/"+day+"/"+year+"
"+monthstr+" "+day+","+year +" "+getHoliday(month,day,year));
}
}
}