Days of the week

Report a typo

Your task is to implement the getDayOfWeekName method that converts the number of the day of the week to its short name. If the given number is incorrect, the method should throw an IllegalArgumentException.

Let's assume that a week starts from Monday:

  • 1 → "Mon";
  • 2 → "Tue";
  • 3 → "Wed";
  • 4 → "Thu";
  • 5 → "Fri";
  • 6 → "Sat";
  • 7 → "Sun".

Sample Input 1:

0

Sample Output 1:

java.lang.IllegalArgumentException

Sample Input 2:

1

Sample Output 2:

Mon
Write a program in Java 17
import java.util.*;

public class Main {

public static String getDayOfWeekName(int number) {
// write your code here
}

/* Do not change code below */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int dayNumber = scanner.nextInt();
try {
System.out.println(getDayOfWeekName(dayNumber));
} catch (Exception e) {
System.out.println(e.getClass().getName());
}
}
}
___

Create a free account to access the full topic