Note that there are some explanatory texts on larger screens.

plurals
  1. POunable to call another method within a method in java
    primarykey
    data
    text
    <p>I have this class called Date:</p> <pre><code>public class Date { // fields private int month; private int day; private int year; // constructor public Date(int month, int day, int year) { this.month = month; this.day = day; this.year = year; } //check for leap year or not public static boolean isLeap(int year) { if (year % 4 != 0) { return false; } else if (year % 400 == 0) { return true; } else if (year % 100 == 0) { return false; } else { return true; } } public static boolean isValid(int month, int day, int year) { if (year &lt; 1900) { return false; } else { if (month &gt; 0 &amp;&amp; month &lt; 13) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { return day &gt; 0 &amp;&amp; day &lt;= 31; } else if (month == 4 || month == 6 || month == 9 || month == 11) { return day &gt; 0 &amp;&amp; day &lt;= 30; } else if (month == 2) { if (isLeap(year)) { return day &gt; 0 &amp;&amp; day &lt;= 29; } else { return day &gt; 0 &amp;&amp; day &lt;= 28; } } } else { return false; } } return true; } public int getMonth() { return month } public void setMonth(int month) { this.month = month; } public int getDay() { return day; } public void setDay(int day) { this.day = day; } public int getYear() { return year; } public void setYear(int year) { this.year = year; } public void setDate(int month, int day, int year) { if (isValid(month, day, year)) { setDay(day); setMonth(month); setYear(year); } else { setDay(1); setMonth(1); setYear(1900); } } // public String displayDate() { return month + "/" + day + "/" + year; } public String add(int n) { if (n &lt;= 20) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) { if ((day + n &gt; 0) &amp;&amp; (day + n &lt;= 31)) day = day + n; else month += 1; if (month &gt; 12) year += 1; } if (month == 4 || month == 6 || month == 9 || month == 11) { if ((day + n &gt; 0) &amp;&amp; (day + n &lt;= 30)) day = day + n; else month += 1; if (month &gt; 12) year += 1; } } return month + "/" + day + "/" + year; } public String sub(int n) { if (n &lt;= 20) { if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12) if (day - n &lt; 0) day = 31 + day - n; else month -= 1; if (month &lt; 0) year -= 1; if (month == 4 || month == 6 || month == 9 || month == 11) if ((day - n &lt; 0)) day = 30 + day - n; else month -= 1; if (month &lt; 0) year -= 1; } return month + "/" + day + "/" + year; } } </code></pre> <p>And this class to to test the Date class:</p> <pre><code>public class MyDateTester { public static void main(String[] args) { new MyDateTester(); } Date theClass; public MyDateTester(){ theClass = new Date(6,1,1600); System.out.print("Month is: "); System.out.println(theClass.getMonth()); System.out.print("Day is: "); System.out.println(theClass.getDay()); System.out.print("Year is: "); System.out.println(theClass.getYear()); System.out.println(theClass.displayDate()); System.out.println(theClass.add(20)); System.out.println(theClass.isValid(6,1,2000)); } } </code></pre> <p>When I give 6, 1, 1600 values to Date, it is still printing 6,1,1600, isValid method in setDate method is not setting the dates right. Any ideas?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload