Write a Java program in response to the following prompt Design and implement th
ID: 667798 • Letter: W
Question
Write a Java program in response to the following prompt
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 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 the day by adding certain days to the current day. For example, if the current day is Monday and we add four days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
G. Add the appropriate constructors.
H. Write the definitions of the methods to implement the operations for the class Day, as defined in A through G.
I. Write a program to test various operations on the class Day.
Explanation / Answer
Day.java
public class Day {
// Constants used to easily identify the days and their
integer equivalents
public static final int SUNDAY = 0;
public static final int MONDAY = 1;
public static final int TUESDAY = 2;
public static final int WEDNESDAY = 3;
public static final int THURSDAY = 4;
public static final int FRIDAY = 5;
public static final int SATURDAY = 6;
private static final int DAYS_IN_WEEK = 7;
private int dayInt; // Stores the current day as
an int
private String dayString; // Stores the current day as a
String value
// Default constructor, Calls setDay and sets the day to
Sunday (0)
public Day()
{
setDay(SUNDAY);
}
// Constructor that takes an int parameter that is passed
to setDay
public Day(int day)
{
setDay(day);
}
// Sets the dayInt value and dayString value
public final void setDay(int day)
{
this.dayInt = day;
this.dayString = convertDayToString(day);
}
// Returns the current value of dayInt as integer
public int getDay()
{
return dayInt;
}
// Returns the String value of dayString (replaces print
method)
@Override
public String toString()
{
return dayString;
}
// Returns a String of the next day after the current day
public String nextDay()
{
return calcDaysFrom(1);
}
// Returns a String of the previous day before the current
day
public String prevDay()
{
return calcDaysFrom(-1);
}
// Returns a String of the day X number of days after the
current day
public String calcDaysFrom(int numberOfDays)
{
int tempDay;
tempDay = (numberOfDays % DAYS_IN_WEEK) + this.dayInt;
tempDay = tempDay % DAYS_IN_WEEK;
return convertDayToString(tempDay);
}
// Takes an integer from 0 - 6 and returns its day of week
String value
private String convertDayToString(int day)
{
String tempDayString;
switch (day)
{
case SUNDAY:
tempDayString = "Sunday";
break;
case MONDAY:
tempDayString = "Monday";
break;
case TUESDAY:
tempDayString = "Tuesday";
break;
case WEDNESDAY:
tempDayString = "Wednesday";
break;
case THURSDAY:
tempDayString = "Thursday";
break;
case FRIDAY:
tempDayString = "Friday";
break;
case SATURDAY:
tempDayString = "Saturday";
break;
default:
tempDayString = "Sunday"; // default to Sunday
break;
}
return tempDayString;
}
}
DayTestProgram.java
public class DayTestProgram
{
public static void main(String[] args)
{
// Create the Day objects
Day testDayDefault = new Day();
Day testDay = new Day(Day.SATURDAY);
// Test the toString method (toString takes place of
print method)
System.out.println("Day with default constructor is "
+ testDayDefault.toString());
System.out.println("Day constructor with Day.SATURDAY
(6) as "
+ "parameter is " + testDay.toString());
// Test setDay method
testDay.setDay(Day.THURSDAY);
System.out.println("setDay with Day.THURSDAY (4) as
parameter is "
+ testDay.toString());
// Test the calcDaysFrom method
for (int i = 0; i <= 10; i++)
{
System.out.println(i + " days from " +
testDay.toString() + " is a "
+ testDay.calcDaysFrom(i));
}
for (int i = 0; i >= -10; i--)
{
System.out.println(i + " days from " +
testDay.toString() + " is a "
+ testDay.calcDaysFrom(i));
}
// Test the nextDay and prevDay method
System.out.println("Day after " + testDay.toString() +
" is "
+ testDay.nextDay());
System.out.println("Day before " + testDay.toString()
+ " is "
+ testDay.prevDay());
// Test the getDay method
System.out.println("The integer value of " +
testDay.toString() +
" is " + testDay.getDay());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.