----------------------------------------------------------------------------- Sh
ID: 3815592 • Letter: #
Question
-----------------------------------------------------------------------------
Shell:
--------------------------------------------------------------------------------------------------------------
I have almost everything completed, I just need help witht the "Thanks" method or trying to produce what day is thanksgiving every year. This is my code so far:
import java.util.Scanner;
public class PrintCalendar
{
public static void main(String[] args)
{
//create a Scanner class object
Scanner kb = new Scanner(System.in);
String choice;
int year =0;
int month;
boolean repeat = true;
while (repeat)
{
do
{
System.out.println("Enter a valid year after 1800 :");
year=Integer.parseInt(kb.nextLine());
} while(year <1800);
System.out.println("To get the month calendar press m or M");
System.out.println("To get the calendar for the year enter y or Y");
System.out.println("Enter your chocie:");
choice=kb.nextLine();
//check if user enters y or Y
if(choice.equals("y")||choice.equals("Y"))
{
for (int i =1; i<=12; i++)
{
printMonth(year, i);
}
}
//check if user enters m or M
else if(choice.equals("m")||choice.equals("M"))
{
do
{
System.out.println("*Enter the month of the year1..12");
month=Integer.parseInt(kb.nextLine());
}while(month<=0 || month>12);
printMonth(year, month);
}
System.out.println("Do you have another year to print the calender:yes/no");
String answer = kb.nextLine();
if (answer.equalsIgnoreCase("no"))
{
System.out.println(" Bye");
//set false to repeat
repeat = false;
}
}
}
/**
This method calls printMonthTitle and printMonthBody */
public static void printMonth(int year, int month)
{
printMonthTitle(year, month);
printMonthBody(year, month);
}
/**
This method takes year
and month and prints the header of the month
*/
public static void printMonthTitle(int year, int month)
{
System.out.println(" " + getMonthName(month) + " " + year);
System.out.println("----------------------------");
System.out.println(" Sun Mon Tue Wed Thu Fri Sat");
}
/**
calls method getStartDay that returns the starting day of month
and calls print method to print the month .
*/
public static void printMonthBody(int year, int month)
{
int startDay = getStartDay(year, month);
print(startDay, year, month);
}
/**
This method takes start day, year
* and month and print the month of the given year.
* */
public static void print(int startDay, int year, int month)
{
int start = getStartDay(year, month);
int numOfDaysInMonth = getNumberOfDaysInMonth(year, month);
for(int i=0; i {
System.out.print(" ");
}
for(int i=1; i<=numOfDaysInMonth; i++)
{
System.out.printf("%4d", i);
//Prints a new line for every week
if((i+start)%7 ==0)
{
System.out.println();
}
}
System.out.println();
}
/**
This method takes month as input
and returns the name of the month as string.
*/
public static String getMonthName(int month)
{
String monthName = "";
switch(month)
{
case 1: monthName = "January";
break;
case 2: monthName = "February";
break;
case 3: monthName = "March";
break;
case 4: monthName = "April";
break;
case 5: monthName = "May";
break;
case 6: monthName = "June";
break;
case 7: monthName = "July";
break;
case 8: monthName = "Aubuest";
break;
case 9: monthName = "September";
break;
case 10: monthName = "October";
break;
case 11: monthName = "November";
break;
case 12: monthName = "December";
break;
}
return monthName;
}
//this method returns the start day of the month
public static int getStartDay(int year, int month)
{
int start = getTotalNumberOfDays(year, month);
start= start+3;
return start%7;
}
public static int getTotalNumberOfDays(int year, int month)
{
int sum = 0;
for(int i=1800; i<=year; i++)
{
if(isLeapYear(i))
{
sum = sum+366;
}else{
sum = sum+365;
}
}
//add number of days in the year to the variable sum, and the method leapyear
for(int i=1; i {
sum = sum+getNumberOfDaysInMonth(year, i);
}
return sum;
}
public static int getNumberOfDaysInMonth(int year, int month)
{
int days = 0;
switch(month)
{
case 1: days = 30;
break;
case 2: if(isLeapYear(year))
{
days = 29;
}
else
{
days = 28;
}
break;
case 3: days = 31;
break;
case 4: days = 30;
break;
case 5: days = 31;
break;
case 6: days = 30;
break;
case 7: days = 31;
break;
case 8: days = 31;
break;
case 9: days = 30;
break;
case 10: days = 31;
break;
case 11: days = 30;
break;
case 12: days = 31;
break;
}
return days;
}
/**
This method takes year
and returns true if year is leap year
otherwise returs false.
*/
public static boolean isLeapYear(int year)
{
return(year %400==0 || year%4==0 && year%100!=0);
}
}
--------------------------------------------------------------------------------------------------------------
Here is an example of the output:
Any help witht the thanksgiving method would be appreciated. Also if there is a way to make my code more cleaner that would be appreciated as well. Thank you!
----------------------------------------------------------------------------------------------------------------------------------------------------
Problem Write a program that prints the calendar foragiven year. The user will be promoted t choose the calendar for the month or the year. If the year is selected then the calendar for the given year will be displayed. Ifthe month is selected then the user will be prompted to enter the month, then the calendar for the given month will be displayed. Top down design: Break down the problem into sub problems and state what you will achieve for each of the sub problems First we need to break down into two sub problems. l. Get the user's input a. Read the input year) 2. Print the calendar Print the calendar for the year a. Print the calendar for each month ofthe year i. Print the title for each month in the year. l. Print the month and the year 2. Print "Sunday Monday Tuesday Wednesday Thursday Friday Saturday l. Get the number of the days for each month of the year 2. Need to know if the year is a leap year or not. A year is a leap year if it is divisible by 400 or ifthe year is divisible by 4 but not divisible by 100 3. Get the number for the first day in the month. For example November 1" was on a Saturday which is the 6 day ofthe week assuming that Sunday is the zero day of the week. To figure out which day is the first day of the month you need to do the following: a. Calculate the total number of days since l800 up to the month of the year that you are trying to print the calendar i. Calculate the total number of days since 1800 up to one year prior to the given year. Note that you also need to check if the year is a leap year or not. For example if the user enters 2014. we need to calculate how many days from 1800 to 2013- Need a for loop and in the for loop you need to check ifthe year is a leap year. ii. Add the total days from the beginning ofthe given year up to the month. For example if we are trying to print the calendar for the March 2014, then we need to add the total number of days in Jan and Feb to whatever we calculatedExplanation / Answer
public static int thanks(int startDay)
{
//Assuming Sunday is 1, Monday is 2 and so on
//if the difference between Thursday and startDay is greater than zero it means we have startDay as Monday, Tuesday..., Thursday
//so just return the difference + 3 weeks
//else add the 7 to difference that # of days we need to add to get to the first Thursday and add 3 weeks
int difference = 5 - startDay + 1;
if(difference > 0)
return difference + ( 3 * 7 );
else
return ( 7 + difference ) + ( 3 * 7 );
}
//Your code looks ok. But your code indentation and style of braces "{" is mixed up. some part of code uses //different style, other part different.
//For example (different braces style in your code)..always use single style
if(isLeapYear(i))
>>>{
sum = sum+366;
>>>>}else{
sum = sum+365;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.