Challenge: Weekday or Weekend
Completed
Write an enum Day with MON, TUE, WED, THU, FRI, SAT, SUN. Read a day name in capitals, turn it into a Day with valueOf, and print <DAY> is day <n> and a <weekday/weekend>, where n starts at 1 for MON.
Input
One word, e.g. SAT.
Output
For SAT: SAT is day 6 and a weekend
Example
Input
SAT
Output
SAT is day 6 and a weekend
Editor
Java
Hints
Day.valueOf("SAT") gives Day.SAT.
ordinal() starts at 0, so add 1.
You can compare enum values with ==.
Solution explanation
Enum values are unique objects, so == is the right way to compare them.
Show solution
import java.util.Scanner;
enum Day {
MON, TUE, WED, THU, FRI, SAT, SUN
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Day d = Day.valueOf(sc.next());
String kind = (d == Day.SAT || d == Day.SUN) ? "weekend" : "weekday";
System.out.println(d + " is day " + (d.ordinal() + 1) + " and a " + kind);
}
} Output
Answer
You can practice without logging in. Log in to save completion.
Save your progress
You're practising as a guest, so your progress won't be saved.
- Track every lab you complete
- Earn verifiable lab certificates
- See your progress in every module