Suppose we want to design a class Day for working with calendar days (such as January 1, 2016). Specify methods for the following operations.1)A getMonth method that gets the month as an integer between 1 and 12.CheckShow answer2)A getMonthName method that gets the month as a string, such as "May".CheckShow answer3)An addDays method that returns the calendar day that is n days from this one.CheckShow answer4)A moveByDays method that moves this calendar day object by n days.CheckShow answer5)A constructor that constructs a Day with year y, month m (as an integer between 1 and 12), and day of month d.
Question
Suppose we want to design a class Day for working with calendar days (such as January 1, 2016). Specify methods for the following operations.1)A getMonth method that gets the month as an integer between 1 and 12.CheckShow answer2)A getMonthName method that gets the month as a string, such as "May".CheckShow answer3)An addDays method that returns the calendar day that is n days from this one.CheckShow answer4)A moveByDays method that moves this calendar day object by n days.CheckShow answer5)A constructor that constructs a Day with year y, month m (as an integer between 1 and 12), and day of month d.
Solution
Here is a basic design for the Day class in Java:
public class Day {
private int year;
private int month;
private int day;
// Constructor
public Day(int y, int m, int d) {
this.year = y;
this.month = m;
this.day = d;
}
// Method to get the month as an integer
public int getMonth() {
return this.month;
}
// Method to get the month as a string
public String getMonthName() {
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
return months[this.month - 1];
}
// Method to add days to the current day
public Day addDays(int n) {
// This is a simplified version and does not account for different month lengths or leap years
return new Day(this.year, this.month, this.day + n);
}
// Method to move the current day by a certain number of days
public void moveByDays(int n) {
// This is a simplified version and does not account for different month lengths or leap years
this.day += n;
}
}
Please note that the `
Similar Questions
Create a class named Year that contains a data field that holds the number of days in theyear. Include a get method that displays the number of days and a constructor that setsthe number of days to 365. Create a subclass named LeapYear. LeapYear’s constructoroverrides Year’s constructor and sets the day field to 366.Write a program that instan6ates one object of each class and displays their data.Add a method named daysElapsed() to the Year class you created in Exercise 5a. ThedaysElapsed() method accepts two arguments represen6ng a month and a day; it returnsan integer indica6ng the number of days that have elapsed since January 1 of the year.Create a daysElapsed() method for the LeapYear class that overrides the method in theYear class. Write a program that calculates the days elapsed on March 1 for a Year and fora LeapYear.
The Calendar class is an abstract class that provides methods for converting between a specific instant in time and a set of calendar fields such as YEAR, MONTH, DAY_OF_MONTH, HOUR, and so on, and for manipulating the calendar fields, such as getting the date of the next week.You are given a date. You just need to write the method, , which returns the day on that date. To simplify your task, we have provided a portion of the code in the editor.ExampleThe method should return as the day on that date.Function DescriptionComplete the findDay function in the editor below.findDay has the following parameters:int: monthint: dayint: yearReturnsstring: the day of the week in capital lettersInput FormatA single line of input containing the space separated month, day and year, respectively, in format.ConstraintsSample Input08 05 2015Sample OutputWEDNESDAYExplanationThe day on August th was WEDNESDAY.
Define the follow ing m ethods for MyDate class:1 . addDays – To add n day s to the date.2 . addMonths – To add n m onths to date.3 . addYears – To add n y ear s to date.4 . weekday – To r eturn w eekday of the date.5 . diffDates – To find differ ence betw een tw odates in term s of the y ear s, m onths, and day s.6 . futureDate – To find a futur e date after a giv ennum ber of day s, m onths, and y ear s.7 . pastDate – To find a date in the past befor e agiv en num ber of day s, m onths, and y ear s.
Which of the following creates an object of the Date class? A: public class Test { public Test() { new java.util.Date(); } } B: public class Test { public Test() { java.util.Date date = new java.util.Date(); } } Group of answer choicesABNeitherBoth A and B
Create a class called Date that includes three pieces of information as instance variables—a month, a day and a year. Your class should have a constructor that initializes the three instance variables after validating the given date. Provide a getDetails() member method to read the date. Provide a method displayDate() that displays the month, day and year separated by forward slashes(/). Write a C++ application and pseudocode that demonstrates classDate’s capabilities.Input : Read Month, Day and Year (Line by line)Output :Print Date that displays the month, day and year separated by forward slashes(/)Note: print "Invalid Date" other than the following2000 < Year < 20250 < Day <= 31 ( for the months Jan, March, May, July, August, October, December)0 < Day <= 30 ( for the months April, June, September, and November)0 < Day <= 28 ( for the month February ) and (0 < Day <= 29), if month = 2 and year = leap year0 < Month < = 12
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.