1

When I try to run this program, the result is always null, 0 0. Why do the values of monthName, day, and year not show up when the getDay() method is invoked and printed on the screen.

public class Assignment1 {

    public static void main(String[] args) {

        //creates an array of type Date filled with two LongDate objects
        Date [] collectionOfDates = { new LongDate("February",2,1996), new LongDate("February",13,1999) };

        // loops through the array and displays output of getDate() for each object
        for( int i = 0; i < collectionOfDates.length; i++ ) {
            System.out.println( collectionOfDates[i].getDate() );
        }

    }

}

For your information, the LongDate class is a subclass of the Date class which contains methods editDay() and editYear() along with several others. The LongDate method is listed below.

Any help greatly appreciated, Thanks. Also, feel free to comment if you want more information.

public class LongDate extends Date {

    private String monthName;
    private int day;
    private int year;

    public LongDate() {

    }

    public LongDate(String m, int d, int y) {

        super.editday(d);
        super.edityear(y);
        editMonth(m);

    }

    public void setDate(String m, int d, int y) {

        monthName = m;
        day = d;
        year = y;

    }


    public String getDate() {

        StringBuilder fullDate = new StringBuilder();
        fullDate.append(monthName);
        fullDate.append(" ");
        fullDate.append(day);
        fullDate.append(", ");
        fullDate.append(year);

        return fullDate.toString();
    }

    public String getShortDate() {

        int month = 0;

        if (monthName == "January") {
            month = 1;
        } else if (monthName == "February") {
            month = 2;
        } else if (monthName == "March") {
            month = 3;
        } else if (monthName == "April") {
            month = 4;
        } else if (monthName == "May") {
            month = 5;
        } else if (monthName == "June") {
            month = 6;
        } else if (monthName == "July") {
            month = 7;
        } else if (monthName == "August") {
            month = 8;
        } else if (monthName == "September") {
            month = 9;
        } else if (monthName == "October") {
            month = 10;
        } else if (monthName == "November") {
            month = 11;
        } else if (monthName == "December") {
            month = 12;
        }

        StringBuilder shortDate = new StringBuilder();
        shortDate.append(month);
        shortDate.append("/");
        shortDate.append(day);
        shortDate.append("/");
        shortDate.append(year);

        return shortDate.toString();
    }

    protected String editMonth(String m) {

        // asks person to try again if month is not capitalized and spelled properly
        if (m != "January" && m != "February" && m != "March" && m != "April" && m != "May" && m != "June" && m != "July" && m != "August" && m != "September" && m != "October" && m != "November" && m != "December") {
            m = Input.getString( "Invalid month. Please type the month again." );
            return m;
        } else
            return m;
    }
}
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 1
    In addition to Matt's answer about the month, please note that `editDay` and `editYear` cannot change the values of `d` and `y`, since parameters are passed by value. – ajb Mar 11 '14 at 00:02

4 Answers4

4

There's nothing in the constructor of LongDate which sets the fields (monthName, day, and year) that getDate() reads.

I assume that the Date#editDay() and Date#editYear() functions look similar to LongDate#editMonth(). Note that editMonth() does not assign a value to the monthName field!

Wyzard
  • 33,849
  • 3
  • 67
  • 87
Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • But won't editMonth() change the value if it is incorrect or return the same value if it is the same? – JPstinger1113 Mar 11 '14 at 00:04
  • @JPstinger1113 You need to read the inheritance tutorial that goes over field hiding, linked in [answers to your previous copy of this question](http://stackoverflow.com/a/22312974/616460). You may also wish to read [this tutorial on variable scope](http://www.java-made-easy.com/variable-scope.html), which is applicable to your `editMonth()` method. – Jason C Mar 11 '14 at 00:08
  • 1
    @JPstinger1113, it *returns* a value to the caller, but it doesn't touch the object's `monthName` field. And when the constructor calls `editMonth`, it ignores the return value. – Wyzard Mar 11 '14 at 00:09
3

You should compare your strings with equals() and not ==. The equals() method compares string values, whereas == compares object references, which is not what you want here. So change:

if (monthName == "January") {

to:

if (monthName.equals("January")) {

and similarly for the other comparisons.

Martin Dinov
  • 8,757
  • 3
  • 29
  • 41
  • 1
    And, also, change `!=` to `!monthName.equals("January")` – oconnor0 Mar 10 '14 at 22:46
  • 2
    Although this *is* a potential issue, it is not the primary one. String interning would render this a non-issue in this specific example. Also this does not address the `day` and `year` issues; the constructor simply doesn't set the values correctly. – Jason C Mar 10 '14 at 22:48
3

Couple of issues. First:

public LongDate(String m, int d, int y) {       
    super.day(d);
    super.year(y);
    editMonth(m);    
}

You don't show Date so it is unclear to us what day() and year() are supposed to do, but regardless:

public class LongDate extends Date {     
    private String monthName;
    private int day;
    private int year;
    ... 
}

Your declarations of these fields are hiding any similar fields that the base presumably has. In any case, at no point in your constructor are you setting this.day or this.year to anything, and so, of course, they remain at their initial value of 0.

You need to clean up your code a bit. Either refer to the correct day and year, or make sure you are setting and getting the base class' version of those fields instead of redeclaring them in the subclass (again, not sure what your base implementation does).

You may want to have a look at the official tutorial on Inheritance. It's concise and well-written and covers topics like overriding methods, hiding fields, etc. I think it will give you a good starting point for solving your issues here.

And, of course, comparing strings with == here will lead to other issues in the future: How do I compare strings in Java?

Community
  • 1
  • 1
Jason C
  • 38,729
  • 14
  • 126
  • 182
  • Note: OP renamed `Date.day()` and `Date.year()` in an edit but the general answer is still the same. – Jason C Mar 10 '14 at 23:44
0

Your editMonth method returns a string, instead it should set the month:

monthName = m;

Another option is to keep the editMonth method the same, but in your constructor put:

monthName = editName(m);
PlasmaPower
  • 1,864
  • 15
  • 18