8-4 Computer Programming: pleas write a java program in response to the followin
ID: 664292 • Letter: 8
Question
8-4 Computer Programming:
pleas 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.
Thank you very much for your help!
Explanation / Answer
import java.util.*;
import java.lang.*;
import java.io.*;
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;
public int day;
public Day(int day)
{
this.day = day;
}
public void setDay(int day)
{
this.day = day;
}
public int getDay()
{
return day;
}
public void print()
{
System.out.println(this.toString());
}
public int nextDay()
{
int next;
if (day == 6)
next = 0;
else
next = day + 1;
return next;
}
public int previousDay() {
int prevDay;
if (day == 0)
prevDay = 6;
else
prevDay = day - 1;
return prevDay;
}
public int addDays(int days)
{
return (day + days) % 7;
}
public String toString()
{
switch (this.day)
{
case SUNDAY:
return "Sunday";
case MONDAY:
return "Monday";
case TUESDAY:
return "Tuesday";
case WEDNESDAY:
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.println();
System.out.print("Set day: ");
Day d = new Day(SUNDAY);
d.print();
System.out.print("Next day: ");
d.setDay(d.nextDay());
d.print();
System.out.print("Previous day: ");
d.setDay(d.previousDay());
d.print();
System.out.print("After 4 days: ");
d.setDay(d.addDays(4));
d.print();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.