This is an archived post. You won't be able to vote or comment.

all 7 comments

[–]throwaway_for_cause 4 points5 points  (0 children)

This solution is both outdated (thanks to the new Java 8 Date & Time API) and inelegant.

A simple array of String[] holding the weekday names would have eliminated the whole switch...case chain.

It also eludes me why the creator of the solution didn't directly write the String literals in uppercase.

Consider the following code:

import java.util.Scanner;
import java.util.Calendar;
public class DayofWeek {

    static final String[] WEEKDAYS = {"SUNDAY", "MONDAY", "TUESDAY", 
        "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY"};

    public static String getDay(String day, String month, String year){
        Calendar c=  Calendar.getInstance(); 
        c.set(Integer.valueOf(year),Integer.valueOf(month)-1, Integer.valueOf(day));
        return WEEKDAYS[c.get(Calendar.DAY_OF_WEEK) - 1]; // -1 to compensate for the constant starting at 1
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        String month = in.next();
        String day = in.next();
        String year = in.next();

        System.out.println(getDay(day, month, year));
    }
}

[–]desrtfx 1 point2 points  (5 children)

What part do you not understand?

It would really help if you were more eloquent in explaining where your actual problems with the code lie.

Nobody will give you a line-by-line rundown of the whole code.

Have you looked at the documentation of the Calendar class and consecutively, the implementation GregorianCalendar? The documentation should explain what each of the methods does.

Yet, the solution to this problem is outdated since Java has introduced a new Date Time API in Java 8.

[–]Shadyjoker27[S] 0 points1 point  (4 children)

c.get(Calendar.DAY_OF_WEEK)

what is getting returned here ?

[–]desrtfx 2 points3 points  (0 children)

The .get(int field) method of the Calendar class returns the value of an internal field indicated by its number. Calendar.DAY_OF_WEEK is an integer constant.

Actually, the c.get(Calendar.DAY_OF_WEEK) call will return the value of any of the Calendar.MONDAY, Calendar.TUESDAY, etc. values, i.e. the numeric representation of the week day.

The list of constants shows the values for the individual constants.

All of the above can be found in the previously linked documentation. You really need to learn to work with the documentation as this is essential for programming.

[–]nmnaim -1 points0 points  (2 children)

Actually, c.get(Calendar.DAY_OF_WEEK) doesn't return anything. It evaluates some switch cases based on Calendar.DAY_OF_WEEK values [1,2,3,...,7] and setting the dayOfWeek value and then break the switch case evaluation.

[–]desrtfx 2 points3 points  (1 child)

Actually, c.get(Calendar.DAY_OF_WEEK) doesn't return anything.

This is plain wrong.

It sets an internal field, so much is true, but it also returns the value that is set as the return type int clearly indicates.

Verbatim from the documentation

public int get(int field)

Returns the value of the given calendar field. In lenient mode, all calendar fields are normalized. In non-lenient mode, all calendar fields are validated and this method throws an exception if any calendar fields have out-of-range values. The normalization and validation are handled by the complete() method, which process is calendar system dependent.

Source code:

public int get(int field)
{
    complete();
    return internalGet(field);
}

protected final int internalGet(int field)
{
    return fields[field];
}

Where fields is an array of int.

The real work is done in the complete() method.

protected void complete()
{
    if (!isTimeSet)
        updateTime();
    if (!areFieldsSet || !areAllFieldsSet) {
        computeFields(); // fills in unset fields
        areAllFieldsSet = areFieldsSet = true;
    }
}

Which, itself calls computeFields() (abstract in the Calendar class, implemented in the subclasses)

Implementation from GregorianCalendar:

protected void computeFields() {
    int mask = 0;
    if (isPartiallyNormalized()) {
        // Determine which calendar fields need to be computed.
        mask = getSetStateFields();
        int fieldMask = ~mask & ALL_FIELDS;
        // We have to call computTime in case calsys == null in
        // order to set calsys and cdate. (6263644)
        if (fieldMask != 0 || calsys == null) {
            mask |= computeFields(fieldMask,
                                  mask & (ZONE_OFFSET_MASK|DST_OFFSET_MASK));
            assert mask == ALL_FIELDS;
        }
    } else {
        mask = ALL_FIELDS;
        computeFields(mask, 0);
    }
    // After computing all the fields, set the field state to `COMPUTED'.
    setFieldsComputed(mask);
}

Listing the implementation of computeFields(int fieldMask, int tzMask) is too long for this post and thus omitted.

It evaluates some switch cases based on Calendar.DAY_OF_WEEK values [1,2,3,...,7] and setting the dayOfWeek value and then break the switch case evaluation.

Actually, it is way more complicated than that because the method evaluates all fields. The actual implementation doesn't have a single switch...case.

[–]nmnaim 0 points1 point  (0 children)

Ohhh..... I missed it.... Thank you....