-2

So this is my first time asking on stackoverflow but i need help with this problem. I have to use enums that includes constructors, methods, and static methods.

So everything in the main was already written and rest I wrote it but I am lost now.

The question is to solve this half written code and have the output. The output is suppose to look like this:

Today's a weekday. 
The abbreviation for MONDAY is M
The abbreviation for MONDAY is T
The abbreviation for MONDAY is W
The abbreviation for MONDAY is R
The abbreviation for MONDAY is F
The abbreviation for MONDAY is S
The abbreviation for MONDAY is Y
SUNDAY
MONDAY
WEDNESDAY
THURSDAY
FRIDAY
SATURDAY
SUNDAY
true
true
true
true

My code so far:

import java.util.List;

public class DayOfWeekDriver
{
    public enum DayOfWeek{
        MONDAY("M"), TUESDAY("T"), WEDNESDAY("W"), THURSDAY("R"), FRIDAY("F"), SATURDAY("S"), SUNDAY("S");

        static{
            MONDAY.next = TUESDAY;
            TUESDAY.next = WEDNESDAY;
            WEDNESDAY.next = THURSDAY;
            THURSDAY.next = FRIDAY;
            FRIDAY.next = SATURDAY;
            SATURDAY.next = SUNDAY;
            SUNDAY.next = MONDAY;
        }

        private DayOfWeek next;

        public DayOfWeek nextDay(){
            return next;
        }

        private String abbreviation;

        private String day;

        DayOfWeek(String abbreviation) {
            this.abbreviation = abbreviation;
        }
        public String getAbbreviation() {
            return abbreviation;
        }  

        public String getDays(){
            return day;
        }  
    }

    public char getLetter(){

    }



    public static void main(String[] args)
    {
        DayOfWeek today = DayOfWeek.FRIDAY;

        if (today == DayOfWeek.SATURDAY ||  today == DayOfWeek.SUNDAY)
        {
           System.out.println("Today's a weekend.");
        }
        else
        {
            System.out.println("Today's a weekday.");
        }

        for (DayOfWeek day : DayOfWeek.values())
        {
           System.out.printf("The abbreviation for %s is %c \n",
                             day, day.getLetter());
        }

        for (Character c : "YMWFTRS".toCharArray())
        {
            System.out.println(DayOfWeek.toDayOfWeek(c));
        }

        List<DayOfWeek> list = DayOfWeek.getDays();

        System.out.println(list.get(0) == DayOfWeek.values()[0]);

        System.out.println(DayOfWeek.MONDAY.next() == DayOfWeek.TUESDAY);

        System.out.println(today.next() == DayOfWeek.SATURDAY);

        System.out.println(DayOfWeek.SUNDAY.next() == DayOfWeek.MONDAY);

    }
}

And also if I can get suggestions where to learn more of java that would be great! Thank you.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • 1
    What is your actual question? Please add a problem statement to your question. – Tim Biegeleisen May 10 '18 at 06:19
  • What is the question? – Tom W May 10 '18 at 06:19
  • the question is to solve this half written code and have the output and the output is suppose to look like this. Today's a weekday. The abbreviation for MONDAY is M The abbreviation for MONDAY is T The abbreviation for MONDAY is W The abbreviation for MONDAY is R The abbreviation for MONDAY is F The abbreviation for MONDAY is S The abbreviation for MONDAY is Y SUNDAY MONDAY WEDNESDAY THURSDAY FRIDAY SATURDAY SUNDAY true true true true – joseph park May 10 '18 at 06:52
  • Welcome to Stack Overflow. What makes you think you are missing something? Please be much more explicit and precise about what is missing, so we may know with what to help you. – Ole V.V. May 10 '18 at 15:08
  • Please make the distinction between the question you have been asked and the question you are asking us. I think I can see only the former until now? Also adding supplementary information is very welcome, only please do it in the question so we have everything in one place, not in a comment. You have an [edit](https://stackoverflow.com/posts/50266684/edit) link under the question for that. – Ole V.V. May 10 '18 at 15:15

1 Answers1

0

An enum is a special type of class. Write it as a stand-alone class. It makes the rest of your program easier to maintain, read, and reason about. Since the elements within the enum are constants, use all uppercase names for maximum readability.

public enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;
};

Now that you have a class, you want to add a feature, a "single letter" day representation. You do that for a class by adding an attribute. Enums are both constants and classes, so we will add the "single letter" day representation to the enum by leveraging it's class-like features.

public enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;

    private String shortName;

    public String getShortName() {
        return shortName;
    }
};

But, how do we set this shortName? The answer is that, since enum is a class, we use the constructor. enum constructors exist, but they are constrained. They must be private to prevent new instances from being constructed (a feature of enums).

public enum Day {
    SUNDAY,
    MONDAY,
    TUESDAY,
    WEDNESDAY,
    THURSDAY,
    FRIDAY,
    SATURDAY;

    private String shortName;

    private Day(String abbreviation) {
       shortName = abbreviation;
    }

    public String getShortName() {
        return shortName;
    }
};

Which leads to the last question, how do we declare the value to be passed into the constructor. Well, you do it when you declare the static value, like so

public enum Day {
    SUNDAY("S"),
    MONDAY("M"),
    TUESDAY("T"),
    WEDNESDAY("W"),
    THURSDAY("R"),
    FRIDAY("F"),
    SATURDAY("Y");

    private String shortName;

    private Day(String abbreviation) {
       shortName = abbreviation;
    }

    public String getShortName() {
        return shortName;
    }
};

This places the "single letter" representation within the thing it represents, providing good code locality and ease of maintenance.

To use this in your code

 if ( day == Day.MONDAY ) { // Typical enum comparison

 System.out.println(day.getShortName()); // prints the day's short name

When learning Java, it is difficult to start off with good object-orientation. Your question was basically one of "how to I add something to an enum" but because you used your enum, embedded into another class, it was being solved in the wrong class. You started programming "how can I make this class give me the right answer for that enum". Don't program that way, if you can avoid it. Instead write your code such that the "other object" gives you the answer.

enum in Java is a port of an idea in another language. Java did a great job of both making the enum look like C's enum, and making it behave like an integrated component of the Java language. That said, people initially are exsposed to C's enum syntax, which means they are exposed to C's enum maintenance problems. So, your approach is understandable for a person learning Java and Java's enums. That said, using the enum as an object will make your code faster, easier to read, and easier to reason about. Now that you've seen a different approach, use it when it is appropriate.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138
  • /** Accessor to the abbreviation for this day. **return char the one-letter abbreviation public char getLetter() /** Accessor to a List of days, in order. **return List list of the days. */ public static List getDays() – joseph park May 10 '18 at 17:16
  • /** Get the day corresponding to the specified letter. ** param letter an abbreviation for a day. ** return DayOfWeek the day which matches the given letter. */ public static DayOfWeek toDayOfWeek(char letter) /** Get the day following this one. * @return DayOfWeek day that follows this one in the defined sequence. */ public DayOfWeek next() – joseph park May 10 '18 at 17:18
  • and srry about the spacings how do i enter down for next sentense cuz that looks really messy – joseph park May 10 '18 at 17:18
  • @josephpark Don't worry about it. The comments aren't formatted the same as other things. I'm used to reading with the bad spacing – Edwin Buck May 10 '18 at 17:40
  • @josephpark The extensions you intend to add are easy, but for clarity's sake, as it is a second question; please ask it as a second question, with source code that is "almost there", after you apply the changes in this question. "How would I return a List of this enum?" sounds like a good title for that question. – Edwin Buck May 10 '18 at 17:51
  • @josephpark https://stackoverflow.com/q/13109497/302139 and please don't forget to upvote this answer if it provided you help, and / or give it a green check mark if you think it's the correct answer to your problem. – Edwin Buck May 10 '18 at 17:54