cworner1
cworner1

Reputation: 451

My toString() method isn't overriding the Calendar toString() method

Here is my code, pretty simple really. Its not homework, I'm teaching myself java through an education book:


import java.util.*;

/** @author Chris */

public class Exercise9_9 extends GregorianCalendar
{   
    public static void main(String[] args)
    {
        String[] stringList = {"s", "c", "b", "f", "e", "q", "g", "w", "i", "o"};
        Integer[] integerList = {5, 7, 8, 9, 6, 5, 4, 1, 2, 3};
        Calendar[] calendarList = new Calendar[10];

        for (int a = 0; a < calendarList.length; a++)
        {
            calendarList[a] = new GregorianCalendar();
            calendarList[a].set(Calendar.YEAR, ((int)Math.random()* 1000));
        }

        System.out.println("Largest String: " + max(stringList));
        System.out.println("Largest int: " + max(integerList));
        **System.out.println("Largeset date: " + (max(calendarList)).toString());**
    }

    public static Object max(Object[] a)
    {
        Arrays.sort(a);
        return a[a.length-1];
    }

    **@Override
    public String toString()**
    {
        return "Test";
    }  
}

The question is to produce three arrays: int, String and Calendar type. Then pick out the biggest out of each category (and display the answers).

This class extends the GregorianCalendar class which means I have access to the Calendars toString() which I'm trying to override. However it doesn't work. Its like the toString() method isn't overriding because I'm getting the default toString() output. However, I'm using Netbeans and it acknowledges the override and even takes me to to Calendar.toString() when I click the override link. So I'm stuck, any help would be appreciated.

Upvotes: 2

Views: 1048

Answers (1)

king_nak
king_nak

Reputation: 11513

It's because you are not using your class, but GregorianCalendar:
calendarList[a] = new GregorianCalendar();

change this to
calendarList[a] = new Exercise9_9 ();

Upvotes: 9

Related Questions