Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

llowing equation for x : - 7 ( x + 6 ) + 1 = 3 - 5 ( x - 4 )

Answered: 1 week ago

Question

Did the team members feel that their work mattered

Answered: 1 week ago

Question

3. What may be the goal of the team?

Answered: 1 week ago