Design and implement the class Day that implements the day of the week in a prog
ID: 657949 • Letter: D
Question
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.
Alright, so this is my code. It is PERFECT except for one small detail, I need to get user input to SET a day and then have the previous, next day follow accordingly.I have bolded the area that needs to be fixed.
import java.awt.*; // GUI import
import java.util.*;
import javax.swing.*;
class Day
{
public static int day;
public static String days;
public Day()
{
setDay(1); // 1 = sunday
}
public Day (int day)
{ //begin if else set day
if( (day >0) &&(day <8))
{
this.day=day;
setDay(day);
}
else
{
day =(day%7);
if(day ==0)
day =7;
this.day =day;
setDay(day);
}//end of if else block
}
public String toString()
{
return (days);
}
public void setDay(int daynumber)
{ // setting days to a numerical value
if (daynumber == 1)
days = "Sunday";
if (daynumber ==2)
days = "Monday";
if (daynumber ==3)
days = "Tuesday";
if (daynumber ==4)
days = "Wednesday";
if (daynumber ==5)
days = "Thursday";
if (daynumber ==6)
days = "Friday";
if (daynumber ==7)
days = "Saturday";
}
public Day setNameofDay(String day)
{
days = day;
return this;
}
public void setDay()
{
if (day <8)//check if day is between 1 to 7
System.out.print(days); //print statement
}
public void nextDay()
{
int tomorrow =day;
tomorrow++;
if (tomorrow <8)
setDay(tomorrow);
else
{
setDay(tomorrow-7);
}//end of else
}
public void dayBefore()
{
int yesterday=day;
yesterday--;
if(yesterday <1)
{
yesterday = 7;
}
setDay(yesterday);
}
public void findDay()
{
int calculate = 0;
String str;
int addDays =0;
str =JOptionPane.showInputDialog("Enter number of days to add");
calculate =Integer.parseInt(str);
addDays = day +calculate;
addDays = addDays %7;
if(addDays ==0)
addDays = 7;
setDay(addDays);
setDay();
}
}
public class DayTester
{
static Scanner console = new Scanner(System.in);
public static void main(String[] args)
{
Day myDay = new Day(1);
// currentley the numerical value is set to one which is Sunday. I am able to change this to any day from within the program, however I want the user to input what day they want to set! Please help
System.out.print("Today is: ");
myDay.setDay();
System.out.println();
System.out.print("Tomorrow is: ");
myDay.nextDay();
myDay.setDay();
System.out.println();
System.out.print("Yesterday was: ");
myDay.dayBefore();
myDay.setDay();
System.out.println();
myDay.findDay();
System.out.println();
}
}
Explanation / Answer
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;
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 (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.