Advertisement
Farahim

q2

Sep 9th, 2015
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. /**
  2. * Farahim Ibrahimli
  3. *
  4. * Question 2
  5. */
  6.  
  7. import java.util.Scanner;
  8.  
  9. public class Q2 {
  10.  
  11. private static int printCalendar(String title, int startingDay, int numDays) {
  12. System.out.println(title);
  13. System.out.println("Su Mo Tu We Th Fr Sa");
  14.  
  15. int column = 0;
  16.  
  17. for (int i = 0; i < startingDay; i++) {
  18. System.out.printf("%2s ", " ");
  19. column++;
  20. }
  21.  
  22. for (int day = 1; day <= numDays; day++) {
  23. if (column % 7 == 0) {
  24.  
  25. System.out.println();
  26. }
  27.  
  28. System.out.printf("%2d ", day);
  29. column++;
  30. }
  31.  
  32. System.out.println();
  33.  
  34. return column % 7;
  35. }
  36.  
  37. public static void main(String[] args) {
  38. int[] daysInMonths = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  39. String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  40.  
  41. Scanner in = new Scanner(System.in);
  42.  
  43. System.out.print("Enter the year: ");
  44. int year = in.nextInt();
  45.  
  46. if (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0)) {
  47.  
  48. daysInMonths[1] = 29;
  49. }
  50.  
  51. System.out.print("What day January start (0-Sun,1-Mon,2-Tue,3-Wed,4-Thu,5-Fri,6-Sat): ");
  52. int day = in.nextInt();
  53.  
  54. for (int i = 0; i < months.length; i++) {
  55. day = printCalendar(months[i] + " " + year, day, daysInMonths[i]);
  56. System.out.println();
  57. }
  58. }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement