Exercise_06_34 Answer!
See original GitHub issueHey man,
I could not find your answer to Programming Exercise Chapter. 6 Number 34. so I thought I could share my answer 😃. PS: Don’t know it’s efficient, or even a good implementation, but it works pretty good, for any given year. PS2: I could not find the answer to question No. 33 too, and unfortunately I couldn’t solve it yet, though I am working hard on it. When I have written the answer, I will share the code. 😃
import java.util.Scanner;
/**
* Created by SquirrelCoder on 29-Jan-17.
*/
public class ZellerAlgorithm {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
// getting user input
System.out.print("Enter full year (e.g., 2012): ");
int year = input.nextInt();
System.out.print("Enter month as number between 1 and 12: ");
int month = input.nextInt();
// check user input
if (!checkMonth(month)) {
System.exit(0);
}
// print the calendar header
printCalendarHeader(month, year);
// print the calendar first day
printFirstDay(month, year);
// print the calendar itself
printCalendarItself(month, year);
}
public static int weekDay(int day, int month, int year) {
if (month == 1 || month == 2) {
month = month + 12;
year--;
}
int q, m, k, j, h;
q = day;
m = month;
k = year % 100;
j = (int) (year / 100.0);
h = (q + (int) ((13 * (m + 1)) / 5.0) + k + (int) (k / 4.0) + (int) (j / 4.0) + (5 * j)) % 7;
return h;
}
public static boolean isLeapYear(int year) {
return year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);
}
public static void printCalendarHeader(int month, int year) {
switch (month) {
case 1:
System.out.print("\t\tJanuary\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 2:
System.out.print("\t\tFebruary\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 3:
System.out.print("\t\tMarch\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 4:
System.out.print("\t\tApril\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 5:
System.out.print("\t\tMay\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 6:
System.out.print("\t\tJune\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 7:
System.out.print("\t\tJuly\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 8:
System.out.print("\t\tAugust\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 9:
System.out.print("\t\tSeptember\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 10:
System.out.print("\t\tOctober\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 11:
System.out.print("\t\tNovember\t");
System.out.println(year);
System.out.println("---------------------------");
break;
case 12:
System.out.print("\t\tDecember\t");
System.out.println(year);
System.out.println("---------------------------");
break;
}
System.out.println("Sun\tMon\tTue\tWed\tThu\tFri\tSat");
}
public static int lastDayOfMonth(int month, int year) {
int lastDayOfMonth;
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) {
lastDayOfMonth = 31;
} else if (month == 2) {
if (isLeapYear(year)) {
lastDayOfMonth = 29;
} else {
lastDayOfMonth = 28;
}
} else {
lastDayOfMonth = 30;
}
return lastDayOfMonth;
}
public static boolean checkMonth(int month) {
if (month > 12 || month < 0) {
System.out.println("Invalid Month!");
return false;
}
return true;
}
public static void printFirstDay(int month, int year) {
int firstDay = weekDay(1, month, year);
switch (firstDay) {
case 0:
System.out.print("\t\t\t\t\t\t1");
break;
case 1:
System.out.print("1");
break;
case 2:
System.out.print("\t1");
break;
case 3:
System.out.print("\t\t1");
break;
case 4:
System.out.print("\t\t\t1");
break;
case 5:
System.out.print("\t\t\t\t1");
break;
case 6:
System.out.print("\t\t\t\t\t1");
break;
}
System.out.print("\t");
}
public static void printCalendarItself(int month, int year) {
// find out the last day of that month
// whether it's 28/29/30/31 days
int lastDayOfMonth = lastDayOfMonth(month, year);
// print the calendar itself
for (int i = 2; i <= lastDayOfMonth; i++) {
int printedDay = weekDay(i, month, year);
if (printedDay == 1) {
System.out.println();
}
System.out.print(i + "\t");
}
}
}
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
Answers are given at the end of these exercises. If ... - YouTube
If you get a wrong answer, read the pages listed in red. The slope of the line containing the points (-2,3) and (3,8)...
Read more >Answers
Sample answer: If you need to write the equation of a line given the slope and a point on the line, you can...
Read more >Simplify. Express answers in standard form. 34. frac{2^{4}cdot4^{2 ...
Solution for Exercise 34 from Chapter 1: Basic Concepts of Algebra of Algebra 1 Practice Book Book for Class 9th Grade, 10th Grade,...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@SquirrelCoder I was inspired and ended up making all the edits I mentioned (plus a few more). I was wrong in some of my earlier comments, but you can see the final version in a pull request or check out my actual commit I made some structural edits (mostly which method comes first is kind of subjective) and I renamed some variables/methods to what I personally think is more helpful
Kool im glad i asked you cause, forking, cloning, pulling and pushing, that is what github is all about. SleekPanther defined the concept well. You should try and learn these concepts and practice them while learning java and working in github. Like SleekPanter said it can be confusing in the beginning but learning them will pay off big time.