Design and implement the class Day that implements the day of the week
in a program. The class Day should store the day, such as Sun for
Sunday. The program should be able to perform the following operations
on an object of the type Day:
a) Set the day
b) Print the day
c) Return the day
d) Return the next day
e) Return the previous day
f) Calculate and return a day by adding # of days to current day.
(ie. if current day is Monday and we add 3 days then new day
is Thursday)
g) Add the appropiate constructors // default & set constructors
h) Write the definitins of the methods to implement the
operations for the class Day as defined in a - g
i) Write a program to test various operations on the class Day
This is what i have so far:
Expand|Select|Wrap|Line Numbers
- public class Day {
- private int dow;
- int num = 10;
- private static String daysOfWeek[] = { "Sunday", "Monday", "Tuesday",
- "Wednesday", "Thursday", "Friday", "Saturday" };
- public Day(String strDay){
- for (int i = 0; i < Day.daysOfWeek.length; i++)
- if (strDay.equalsIgnoreCase(Day.daysOfWeek[i])){
- this.dow = i;
- return;
- }
- System.err.println("Bad Data in Constructor");
- dow = 0;
- }
- public Day(int dow) {
- this.dow = dow;
- }
- public int getDow() {
- return dow;
- }
- public void setDow(int dow) {
- this.dow = dow;
- }
- public int nextDay() {
- int nextDay;
- nextDay = this.dow + 1;
- return nextDay;
- }
- public int previousDay() {
- int prevDay;
- prevDay = this.dow - 1;
- return prevDay;
- }
- public int calcDay() {
- int newDay;
- this.dow = dow + num;
- newDay = this.dow % 7;
- return newDay;
- }
- }