Dana Vrajitoru
C101 Computer Programming

C101/I210 Homework 10

Due date: Wednesday, April 15, 2020.

Objective: To write the switch instruction and static class attributes in Java.

In this homework you will write a program that works with the days of the week.

Ex. 1. Days of the Week

Create a project in Eclipse called hw10 and create a class called WeekDays in it, containing the main function. Add a scanner as a static class attribute, just like in the lab.

a. Names of Days

Write a static method that takes one parameter which is an integer number between 1 and 7 and returns a string representing the day of the week. We will use the convention that 1 = Sunday, 2 = Monday, and so on. Implement this method using a switch statement, with a case for each value between 1 and 7 where you return the day as a string, and a default case where you return an empty string. You can use the method monthName from the lab as an example. Here is the header of this method:

static String dayName(int day)

In the main, declare an integer variable for the day, and ask the user to enter a value for it, then input it. Declare a String variable for the name, then assign it a value by calling the method dayName with the integer you just input. Then output the value of the name.

b. Week-end

Write a static method that takes in a string that represents a day name and outputs a message telling us if it's a business day or a week-end day. The method doesn't need to return anything, so it will be of type void. Use another switch statement in this method where you do the same thing for all days from Monday to Friday, and another thing for Saturday and Sunday. In the default case you can output that the day name is not valid. You can use the method numberOfDays from the lab as a model.

static void isWeekEnd(String name)

In the main, use the string name to call this function.

c. Next Day

Write a method that takes in a string that represents a day name and outputs another string that represents the next day. For example, if the parameter value is "Monday", the method should return "Tuesday". Implement this method using another switch statement using the value of the parameter, and with a case for each of the days. In the default case, return an empty string. The header of the function should be

static String nextDay(String name)

In the main, call this method on the day name you got from calling the first method and output the result.

Turn in:

Upload the lab and homework .java files: Month.java and WeekDays.java, to Canvas in Assignments - Homework 10.