PLEASE READ CAREFULLY REQUIREMENTS AND COMPLETE ALL PARTS. ALSO WRITE COMMENTS F
ID: 3760939 • Letter: P
Question
PLEASE READ CAREFULLY REQUIREMENTS AND COMPLETE ALL PARTS. ALSO WRITE COMMENTS FOR EACH LINE OF CODE. THANK YOU!
Using jGrasp and the Software Development Kit, write a 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
import java.util.*;
public class MyDay
{
static Scanner readinput = new Scanner(System.in);
String day;
public MyDay(String day)
{
day = "Sunday";
}
public void setDay(String theDay)
{
day = theDays;
}
public String getDay()
{
return day;
}
public void printDay()
{
System.out.println(" your day is " + day);
}
public String getNextDay()
{
if((day.compareTo("Saturday") == 0) || (day.compareTo("Sat") == 0))
return ("Sunday");
else if((day.compareTo("Sunday") == 0) || (day.compareTo("Sun") == 0))
return ("Monday");
else if((day.compareTo("Monday") == 0) || (day.compareTo("Mon") == 0))
return ("Tuesday");
else if((day.compareTo("Tuesday") == 0) || (day.compareTo("Tue") == 0))
return ("Wednesday");
else if((day.compareTo("Wednesday") == 0) || (day.compareTo("Wed") == 0))
return ("Thursday");
else if((day.compareTo("Thursday") == 0) || (day.compareTo("Thu") == 0))
return ("Friday");
else if((day.compareTo("Friday") == 0) || (day.compareTo("Fri") == 0))
return ("Saturday");
else
return ("wrong entry lol!"");
}
public String getPreDay()
{
if((day.compareTo("Friday") == 0) || (day.compareTo("Fri") == 0))
return ("Thursday");
else if((day.compareTo("Thursday") == 0) || (day.compareTo("Thu") == 0))
return ("Wednesday");
else if((day.compareTo("Wednesday") == 0) || (day.compareTo("Wed") == 0))
return ("Tuesday");
else if((day.compareTo("Tuesday") == 0) || (day.compareTo("Tue") == 0))
return ("Monday");
else if((day.compareTo("Monday") == 0) || (day.compareTo("Mon") == 0))
return ("Sunday");
else if((day.compareTo("Sunday") == 0) || (day.compareTo("Sun") == 0))
return ("Saturday");
else if((day.compareTo("Saturday") == 0) || (day.compareTo("Sat") == 0))
return ("Friday");
else
return (""Opps Error lol!"");
}
public static void main (String args[])
{
// One of its: sun, Sunday, sunday, SuNdAy...
MyDay testday = new MyDay("Sun");
System.out.print("Enter day to set your today: ");
String storeday = readinput.nextLine();
testday.setDay(storeday);
System.out.println("Your day is " + testday.getDay());
testday.printDay();
System.out.println("Your previous day is " + testday.getPreDay());
System.out.println("Your next day is " + testday.getNextDay());
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.