C++ Design and implement the class Day that implements the day of the week in a
ID: 3767287 • Letter: C
Question
C++ 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 4 days, the day to be returned in Friday.
G. Add the appropriate constructors.
willl rate, make sure it compiles thank you!
Explanation / Answer
Day.java
public class Day {
private String[] days = {"SUN", "MON", "TUE", "WED" ,"THU" , "FRI", "SAT"};
private String day;
public Day() {
}
public void setDay(String day) {
this.day = day;
}
public String getDay() {
return this.day;
}
public void printDay(String day) {
System.out.println("The day is " + day);
}
public String nextDay() {
int i=0;
String nextDay = "";
for(String d: days) {
i++;
if(i==7)
i=0;
if(d.equalsIgnoreCase(day)) {
nextDay = days[i];
}
}
return nextDay;
}
public String previousDay() {
int i=-1;
String prevDay = "";
for(String d: days) {
i++;
if(d.equalsIgnoreCase(day)) {
if(i==0) {
i = 6;
prevDay = days[i]
}
else {
prevDay = days[i-1];
}
}
}
}
public String getDay(int value) {
int newIndex =0;
for(int i=0; i < days.length; i++) {
if(days[i].equals(day)) {
newIndex = i + value;
while(newIndex >= 7) {
newIndex %= 6;
}
}
}
return days[newIndex];
}
}
DayTest.java
class DayTest
{
public static void main(String[] args)
{
Day d = new Day();
d.setDay("WED");
d.printday(d.getDay());
d.printday(d.nextDay());
d.printday(d.previousDay());
d.printday(d.getDay(2));
d.printday(d.getDay(-2));
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.