Please heeeelp :(Using java


Create an array of strings that contains the names of the days of the week: {“Sunday”,”Monday”,”Tuesday”,”Wednesday”,”Thursday”,“Friday”, ”Saturday”}

a. In your main, create a program that asks the user for an integer between 1 and 7, inclusive. Using the array, your program should print out the day of the week corresponding to the number the user entered. Your program must REJECT any number that’s outside this range.

b. On the same line that prints the day’s name, modify your code to also declare if it is or if it is not a weekend.

An example printed output for Q1 could be: “Please enter a number: 4

The day you chose is Wednesday and it is not a weekend.”

“Please enter a number: 7

The day you chose is Saturday and it is a weekend.”

Using java
Create an array of strings that


Answer:

The java program for the given scenario is shown below.

import java.util.Scanner;

import java.util.*;

public class Test

{

   //declaration and initialization of array`

   static String[] days = new String[]{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};

   static int n;

   static Scanner sc = new Scanner(System.in);

public static void main(String[] args) {

    //loop runs till valid user input received

    do{

     System.out.print("Enter any number from 1 to 7: ");

     n=sc.nextInt();

     if(n<1 || n>7)

         System.out.println("Invalid number.");

    }while(n<1 || n>7);

    //print message depending on user input

 switch(n)

 {

     case 1: System.out.println("The day you chose is Sunday and it is a weekend."); break;

     case 2: System.out.println("The day you chose is Monday and it is not a weekend."); break;

     case 3: System.out.println("The day you chose is Tuesday and it is not a weekend."); break;

     case 4: System.out.println("The day you chose is Wednesday and it is not a weekend."); break;

     case 5: System.out.println("The day you chose is Thursday and it is not a weekend."); break;

     case 6: System.out.println("The day you chose is Friday and it is not a weekend."); break;

     case 7: System.out.println("The day you chose is Saturday and it is a weekend."); break;

     default: break;

 }

}

}

Explanation:

1. An array of size seven is declared and initialized with all the seven days in a week.

2. An integer variable is declared to store user input.

3. An object of Scanner class is created to enable user input.

4. All the above are declared outside main() and hence, declared as static.

5. Inside main, inside a do-while loop, user input is taken. The loop executes until the user enters a valid input.

6. Once a valid user input is obtained, switch case begins.

7. Inside switch case, each number from 1 to 7 is associated with a weekday.

8. The message is displayed to the user based on the corresponding user inputted number.

9. The switch case also includes default case.

10. All the code is written inside class named Test.

11. The program can be tested for any size of the array and the corresponding elements of the array.

12. The output is attached in an image.






Rate answer
Wrong answer?

If your question is not fully disclosed, then try using the search on the site and find other answers on the subject Computers and Technology.

Find another answers

Load image