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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started