#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2013
    Posts
    1
    Rep Power
    0

    Calendar Assignment


    Hi, I'm stuck on this project. This is the instructions:

    CMPS 135 Programming Assignment 4

    Problem:
    Write a Java program to print out the calendar of any year provided that the day of January 1 of that year is known. (Note: Leap year is a year whose number is divisible by 4. The Feb. of a leap year has 29 days. You will receive extra points if you can print the calendar with 3 months in a row.)

    Speci cations:
    Use modular programming to write the program.





    Currently, this is the code I have, but the program isn't working correctly:



    import javax.swing.JOptionPane;

    public class Assignment4Test
    {
    public static void display()
    { System.out.println(" S M T W T F S");
    System.out.println("---------------------");
    }

    public static void main(String args[])

    { int year;
    String yearstr;
    int startday;
    String startdaystr;
    int month;
    int daysinmonth;
    String monthname;
    int spacesforstartday;
    int date;

    yearstr = JOptionPane.showInputDialog("What is the number of the year?");
    year = Integer.parseInt(yearstr);

    startdaystr = JOptionPane.showInputDialog("What is the day of January 1? (S-1 M-2 T-3 W-4 T-5 F-6 S-7)");
    startday = Integer.parseInt(startdaystr);

    System.out.println(" "+year);
    System.out.println(" ====");

    for (month=1; month<=12; month++)
    {
    monthname="";
    daysinmonth=0;
    switch (month)
    {

    case 1:

    monthname="Jan ";
    daysinmonth=31;
    break;

    case 2:

    monthname="Feb ";
    if (year%4 == 0 && year%100 != 0 || year%400 == 0)
    { daysinmonth = 29; }
    else
    { daysinmonth = 28; }
    break;

    case 3:

    monthname = "March ";
    daysinmonth = 31;
    break;

    case 4:

    monthname="April ";
    daysinmonth = 30;
    break;

    case 5:

    monthname="May ";
    daysinmonth = 31;
    break;

    case 6:

    monthname="June ";
    daysinmonth = 30;
    break;

    case 7:

    monthname="July ";
    daysinmonth = 31;
    break;

    case 8:

    monthname="Aug ";
    daysinmonth = 31;
    break;

    case 9:

    monthname="Sept ";
    daysinmonth = 30;
    break;

    case 10:

    monthname="Oct ";
    daysinmonth = 31;
    break;

    case 11:

    monthname="Nov ";
    daysinmonth = 30;
    break;

    case 12:

    monthname="Dec ";
    daysinmonth = 31;
    break;

    default:

    System.out.println("Invalid data");
    break;

    }

    System.out.println("");
    System.out.println(" "+monthname);
    System.out.println("");

    display();

    for(spacesforstartday=1;spacesforstartday<=startday;spacesforstartday++)
    {System.out.print(" ");}

    for(date=1; date<=daysinmonth; date++)
    {
    if(startday%7 == 0 && startday != 0)
    {System.out.println();}
    if(date<10)
    {System.out.print(" "+date);}
    else
    {System.out.print(" "+date);}
    startday=startday+1;
    }



    startday%=7;
    System.out.println("");
    System.out.println("");
    }



    System.exit(0);

    }}
  2. #2
  3. Contributing User
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Aug 2010
    Location
    Eastern Florida
    Posts
    3,724
    Rep Power
    348
    Also posted at http://www.javaprogrammingforums.com/whats-wrong-my-code/28726-homework-calendar-assignment.html

IMN logo majestic logo threadwatch logo seochat tools logo