Search code examples
javacommand-linestatic-methodsnon-static

How to print a non-static method from a static method?


I need to print a calendar of a given input from the command line. The main problem I am having is trying to print the actual body of the calendar. I have a method:

printMonth(); 

that is used to print the calendar. The method is void and non-static. However, when I call the method from main (static), there is no output.

Here is my code so far:

class MyDate {

private int day;
private int month;
private int year;

public MyDate(int theDay, int theMonth, int theYear){

    theDay = day;

    theMonth = month;

    theYear = year;

}

public MyDate(){

}

public int getDay() {

    return day;
}

public int getMonth() {

    return month;
}

public String nameOfMonth() {
    switch (month) {
    case 1:
        return "January";
    case 2:
        return "February";
    case 3:
        return "March";
    case 4:
        return "April";
    case 5:
        return "May";
    case 6:
        return "June";
    case 7:
        return "July";
    case 8:
        return "August";
    case 9:
        return "September";
    case 10:
        return "October";
    case 11:
        return "November";
    case 12:
        return "December";
    // for any number less than 1 and greater than 12 throw an exception
    default:
        throw new RuntimeException("Invalid month number");
    }
}

public int getYear() {

    return year;
}



public String getDate(String[] datePartsObj) {

        day = Integer.parseInt(datePartsObj[0]);
        month = Integer.parseInt(datePartsObj[1]);
        year = Integer.parseInt(datePartsObj[2]);

        return day + "/" + month + "/" + year ;

}


  public int getStartDay(int theYear, int theMonth) {

      year = theYear;
      month = theMonth;

        // Get total number of days since 1/1/1800
        int startDay1800 = 3;

        int totalNumberOfDays = getTotalNumberOfDays(theYear, theMonth);



        // Return the start day
        return (totalNumberOfDays + startDay1800) % 7;

      }


public int getTotalNumberOfDays(int theYear, int theMonth) {

    year = theYear;

    month = theMonth;

    int total = 0;



    // Get the total days from 1800 to year - 1
    for (int i = 1800; i < theYear; i++)

    if (isLeapYear(i))

      total = total + 366;

    else

      total = total + 365;



    // Adding days from January to the month prior to the calendar month
    for (int i = 1; i < theMonth; i++)

      total = total + getNumberOfDaysInMonth(theYear, i);



    return total;

  }


 public int getNumberOfDaysInMonth(int theMonth, int theYear) {

     month = theMonth; 

     year = theYear;

        if (theMonth == 1 || theMonth == 3 || theMonth == 5 || theMonth == 7 ||

                theMonth == 8 || theMonth == 10 || theMonth == 12)

          return 31;



        if (theMonth == 4 || theMonth == 6 || theMonth == 9 || theMonth == 11)

          return 30;



        if (theMonth == 2) return isLeapYear(theYear) ? 29 : 28;



        return 0; // If month is incorrect
      }


 public boolean isLeapYear(int theYear) {

     year = theYear;

        if  ((theYear % 4 == 0) && (theYear % 100 != 0)) return true;
        if  (theYear % 400 == 0) return true;
        return false;
    }

}


public class MyCalendar {

private static MyDate myDateObj;


public enum WeekDay{
    Monday,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday;
}

public MyCalendar(MyDate newdate){



}

public MyCalendar(){

}

public int weekOfMonth() {

    return;

}

public void printMonth() {

    int theYear = myDateObj.getYear();

    int theMonth = myDateObj.getMonth();

    // Get start day of the week for the first date in the month
    int startDay = myDateObj.getStartDay(theYear, theMonth);

    // Get number of days in the month
    int numberOfDaysInMonth = myDateObj.getNumberOfDaysInMonth(theYear, theMonth);

    // Pad space before the first day of the month
    int i = 0;

    for (i = 0; i < startDay; i++)

      System.out.print("    ");



    for (i = 1; i <= numberOfDaysInMonth; i++) {

      if (i < 10)

        System.out.print("   " + i);

      else

        System.out.print("  " + i);



      if ((i + startDay) % 7 == 0)

        System.out.println();

    }



    System.out.println();


}

public static void main(String [] args) {

    String[] datePartsObj;
    datePartsObj = args[0].split("/");
    int day = Integer.parseInt(datePartsObj[0]);
    int month = Integer.parseInt(datePartsObj[1]);
    int year = Integer.parseInt(datePartsObj[2]);



    myDateObj = new MyDate(day, month, year);
    MyCalendar myCalendar = new MyCalendar();


    System.out.println(myDateObj.getDate(datePartsObj) + " is a " + " and locates in the " + myDateObj.nameOfMonth());

    myDateObj.nameOfMonth();

    System.out.println("The calendar of " + myDateObj.nameOfMonth() + " " + myDateObj.getYear() + " is: ");

    System.out.println();

    System.out.println(" Sun Mon Tue Wed Thu Fri Sat");

    myCalendar.printMonth();


    }


}

I am quite new to java, so any assistance with this problem would be greatly appreciated!


Solution

  • In your method printMonth() you have called method getNumberOfDaysInMonth(theMonth, theYear) and you have passed arguments in wrong order

    change this line:

    int numberOfDaysInMonth = myDateObj.getNumberOfDaysInMonth(theYear, theMonth);
    

    to this one:

    int numberOfDaysInMonth = myDateObj.getNumberOfDaysInMonth(theMonth, theYear);