Write a Java application to solve this question: 1. Ask the user to enter the nu
ID: 3580012 • Letter: W
Question
Write a Java application to solve this question:
1. Ask the user to enter the numeric value for a day of the week (where '1' is Sunday, '2' is Monday, etc.). Report whether that day is a weekday or if it falls on a weekend. If an invalid number is entered, such as a '0' or a '9', then report "That is not a valid day".
Here is a sample run of the program:
Enter a day of the week (1 to 7): 3
That is a weekday.
Here is another run of the program:
Enter a day of the week (1 to 7): 7
That is a weekend.
And another run of the program:
Enter a day of the week (1 to 7): 9
That is not a valid day.
Explanation / Answer
Solution:
/* Ask the user to enter the numeric value for a day of the week (where '1' is Sunday, '2' is Monday, etc.). Report whether that day is a weekday or if it falls on a weekend. If an invalid number is entered, such as a '0' or a '9', then report "That is not a valid day". **/
=============================
import java.util.*;
public class Day{
Scanner scan=new Scanner(System.in);
int dayNumber;
public void input(){
try{
System.out.println("Enter a day of the week (1 to 7): ");
dayNumber=scan.nextInt();
}
catch(final NumberFormatException e){
System.out.println("Error Code: "+e);
System.exit(0);
}
}
public void compute(){
switch(dayNumber){
case 1: sunday(); break;
case 2: monday(); break;
case 3: tuesday(); break;
case 4: wednesday(); break;
case 5: thrusday(); break;
case 6: friday(); break;
case 7: saturday(); break;
default: System.out.println("That is not a valid day.");
}
}
public void sunday(){
System.out.println("That is a weekday");
}
public void monday(){
System.out.println("That is a weekday");
}
public void tuesday(){
System.out.println("That is a weekday");
}
public void wednesday(){
System.out.println("That is a weekday");
}
public void thrusday(){
System.out.println("That is a weekday");
}
public void friday(){
System.out.println("That is a weekday");
}
public void saturday(){
System.out.println("That is a weekend");
}
public static void main(String[] args){
Day week=new Day();
week.input();
week.compute();
}
}
Sample output:
Here is a sample run of the program:
Enter a day of the week (1 to 7): 3
That is a weekday.
Here is another run of the program:
Enter a day of the week (1 to 7): 7
That is a weekend.
And another run of the program:
Enter a day of the week (1 to 7): 0
That is not a valid day.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.