Asgore
Asgore

Reputation: 21

Printing a 3x4 calendar in Java

I would appreciate any tips or advice. I have to print a years Calendar in Java given the year and the day that Jan.1 falls on. I am able to print the calendar, but the challenge is the calendar has to be in a 3x4 format(Jan. Feb. March side by side then on the next "row" Apr. May June, etc. This is my code at the moment which prints the calendar top-down. I just have no idea where to get started. I can only use selection structues, loops, and methods.

import javax.swing.JOptionPane;
public class Assignment4DONOTCHANGE 
{
  //get headers set up to be printed in main
  public static void printHeader(int month)
  {
    switch (month) 
    {
      case 1: 
        System.out.println("                  January"); break;
      case 2: 
        System.out.println("                  February"); break;
      case 3: 
        System.out.println("                  March"); break;
      case 4: 
        System.out.println("                  April"); break;
      case 5: 
        System.out.println("                  May"); break;
      case 6: 
        System.out.println("                  June"); break;
      case 7: 
        System.out.println("                  July"); break;
      case 8: 
        System.out.println("                  August"); break;
      case 9: 
        System.out.println("                  September"); break;
      case 10: 
        System.out.println("                  October"); break;
      case 11: 
        System.out.println("                  November"); break;
      case 12: 
        System.out.println("                  December"); break;
    }
    // Display header and days of the week 
    System.out.println("---------------------------------------------");
    System.out.println(" Su     M      Tu     W      Th     F      S");
    System.out.println();
   }
   //compute last day of each month
   public static int lastDayM(int month, int year)
  {
    //reset each iteration
    int lastDay = 0;
    if ( month == 1 || month == 3  || month == 5 || month == 7 || month == 8 || month == 10 ||month == 12)
  lastDay = lastDay + 31;
else 
{
  if (month == 4 || month == 6 || month == 9 || month == 11)
    lastDay = lastDay + 30;
  else 
  { // Test for leap year
    if (year % 4 == 0)
      lastDay = lastDay + 29;
    else
      lastDay = lastDay + 28;
  }
}
  return lastDay;
  }
  public static void main(String args[]) 
  {
//declaration
String yearstr, daystr;
int year, day, lastDay;

// Prompt the user to enter the year and first day of the year
yearstr = JOptionPane.showInputDialog("Enter a year: ");
year = Integer.parseInt(yearstr);
daystr = JOptionPane.showInputDialog("Enter a day for Jan.1: 0-S, 1-M, 2-Tu, etc.");
day = Integer.parseInt(daystr);

System.out.println("                   "+year);
int month;
for (month = 1; month <= 12; month++)
{
  printHeader(month);

  // Compute beginning day of the week
  day = day % 7;
  for (int b = 1; b <= day * 7; b++) 
  {
    System.out.print(" ");
  }

  // Compute last day of present month
  lastDay = lastDayM(month, year);

  // Display calender for current month
  int d;
  for (d = 1; d <= lastDay; d++) 
  {
    // Add a black space before numbers less than 10
    if (d < 10) 
      System.out.print(" ");
    // Start new line after satuarday
    if (day % 7 == 6)
    {
      System.out.print(d+" ");
      System.out.println();
      System.out.println();
    }

    else 
    {
      System.out.print(d + "     ");

      // After last day of the month go to new line
      if (d == lastDay) 
        System.out.println();
    }
    day = day + 1; 
  }
  System.out.println();
}
System.exit(0);
  }
} 

Upvotes: 0

Views: 444

Answers (2)

MyStackRunnethOver
MyStackRunnethOver

Reputation: 5275

Think about what your limitation (printing in a 3x4 grid, with no fancy string format help) means: you have to print one row at a time. This means you're going to print the first three months, then their header rows and days of the week (for all three in one row), then 4-5 rows of numbered days, where each row contains one week of each of the three months.

Given the above, this is really a problem of program design and separation of logic. What would be really nice, for example, would be a function that could take a year, a month and a week number, and return the string you need to print for that week. E.g. for a year starting Monday, you would want foo("January", 1, 20xx) to return " 1 2 3 4 5 6" and foo("January", 2, 20xx) to return " 7 8 9 10 11 12 13" and foo("January", 5, 20xx) to return "28 29 30 31 "

This one function would let you loop through your printing one row at a time, just by calling it three times for each row in which you have to print months' days. You should be able to implement the logic of this function, and the rest of the formatting, without too much difficulty, judging from your code above.

What's important here is to think carefully about what needs to be separated into its own function. In this case, you need the flexibility of getting a single "row" (week) of days from one month, so you need to encapsulate that in its own function.

Upvotes: 1

Fady Saad
Fady Saad

Reputation: 1189

I think the key point is your switch case: It should looks like that:

switch (month) 
    {
      case 1: 
        System.out.print("                  January"); break;
      case 2: 
        System.out.print("                  February"); break;
      case 3: 
        System.out.println("                  March"); break;
      case 4: 
        System.out.print("                  April"); break;
      case 5: 
        System.out.print("                  May"); break;
      case 6: 
        System.out.println("                  June"); break;
      case 7: 
        System.out.print("                  July"); break;
      case 8: 
        System.out.print("                  August"); break;
      case 9: 
        System.out.println("                  September"); break;
      case 10: 
        System.out.print("                  October"); break;
      case 11: 
        System.out.print("                  November"); break;
      case 12: 
        System.out.println("                  December"); break;
    }

Upvotes: 0

Related Questions