WarHammer91 0 Newbie Poster

I need to write a program that will print a calendar for any given month and year since January 1900. I don't think I can use any calendar utility. To be honest, I'm slaving my way through this, I don't really have any idea what I'm doing. Here's what I have so far:

import java.util.Scanner;

class Calendar
{
    int yyyy, mm;
    Calendar(int m, int y)
    {
        mm = m;
        yyyy = y;
    }
    /*int getDayofWeek()
    {
        
    }*/
    int getDaysOfMonth(boolean leap)
    {
        int days;

        switch (mm)
        {
            case 1: days = 31;
            case 2: 
                if(leap = true)
                    days = 29;
                else
                    days = 28;
            case 3: days = 31;
            case 4: days = 30;
            case 5: days = 31;
            case 6: days = 30;
            case 7: days = 31;
            case 8: days = 31;
            case 9: days = 30;
            case 10: days = 31;
            case 11: days = 30;
            default: days = 31;
        }

        return days;
    }
    long getTotalDays(boolean leap)
    {
        int TotalDays, leapDays;
        TotalDays = (365*(yyyy-1900));
        leapDays = TotalDays / 4;
        TotalDays = TotalDays + leapDays;
        return TotalDays;
    }

    boolean isLeap()
    {
        if (yyyy % 4 == 0)
        {
            if(yyyy % 100 != 0)
                return true;
            else if (yyyy % 400 == 0)
                return true;
            else
                return false;            
        }
        else
            return false;
    }
    String printMonth(int days)
    {
        

        String S = "";
        for (int i = 1; i <= days; i++)
        {
            if (i%7 == 1)
                S = S + '\n';
            if (i < 10)
                S = S + "  " + i;
            else
                S = S + " " + i;
        }
        return S;
    }
}
class CalDemo
{
    public static void main(String [] args)
    {
        Scanner in = new Scanner(System.in);

        System.out.println("Enter the year (yyyy)");
        int year = in.nextInt();
        System.out.println("Enter the month (mm)");
        int month = in.nextInt();

        Calendar C = new Calendar(month, year);
        
        boolean leap = C.isLeap();
        C.getTotalDays(leap);
        int days = C.getDaysOfMonth(leap);

        System.out.print(C.printMonth(days));
        System.out.println();
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.