Implementation of Day Class

In this section, you will learn how to implement Day class.


Share on Google+Share on Google+

Implementation of Day Class


In this section, you will learn how to implement Day class.

Implementation of Day Class

Here we are going to design and implement the class Day. The class Day should store the days of the week. Then test the following operations through the object of the class Day:
1) Set the day.
2) Print the day.
3) Return the day.
4) Return the next day.
5) Return the previous day.
6) 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.

Here is the code:

public class Day {
	final static int SUNDAY = 0;
	final static int MONDAY = 1;
	final static int TUESDAY = 2;
	final static int WEDNESDAY = 3;
	final static int THURSDAY = 4;
	final static int FRIDAY = 5;
	final static int SATURDAY = 6;

	private int day;

	public Day(int day) { = day;

	public void setDay(int day) { = day;

	public int getDay() {
		return day;

	public void print() {

	public int nextDay() {
		int next;
		next = day + 1;
		return next;

	public int previousDay() {
		int prevDay;
		prevDay = day - 1;
		return prevDay;

	public int addDays(int days) {
		return (day + days) % 7;

	public String toString() {
		switch ( {
		case SUNDAY:
			return "Sunday";
		case MONDAY:
			return "Monday";
		case TUESDAY:
			return "Tuesday";
			return "Wednesday";
		case THURSDAY:
			return "Thursday";
		case FRIDAY:
			return "Friday";
		case SATURDAY:
			return "Saturday";
		return "";

	public static void main(String[] args) {
		System.out.println("******Test Day******");
		System.out.print("Set day: ");
		Day d = new Day(SUNDAY);
		System.out.print("Next day: ");
		System.out.print("Previous day: ");
		System.out.print("After 5 days: ");


*******Test Day*******

Set day: Sunday
Next day: Monday
Previous Day: Sunday
After 5 days: Friday


Posted on: June 16, 2010 If you enjoyed this post then why not add us on Google+? Add us to your Circles