Question: Write a program that reads int daynum from the console (use StdIn.readInt() ). (The starting code does this for you.) Then print out the corresponding

Write a program that reads int daynum from the console (use StdIn.readInt()). (The starting code does this for you.) Then print out the corresponding String day name in two different ways: once using if-else if-..., and again by using daynum to index into the given String array DAYS, where DAYS[daynum] is the String correspondence given below.

Use the correspondence: 1->Sunday, 2->Monday, ...., 7->Saturday. Also note how the empty String "" is stored in DAYS[0], allowing you to directly index DAYS using daynum. This approach allows you to eliminate long chained conditionals.

You may assume that daynum is in the correct range 1..7.

Starting code:

public class Weekdays

{

public String[] DAYS =

{ "", "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

public static void main(String[] args)

{

// read int N

StdOut.print("Enter N between 1 and 7: ");

int dayNum = StdIn.readInt();

StdOut.printf("You entered %d ", dayNum);

// print day equivalent of N using if..else if

// if (N==1)

// StdOut.println ("Sunday");

// else if (N==2)

// print day equivalent using array indexing into DAYS

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!