Question
Using Java. Re-do this code on the bottom the day of the week, except this time put it into a class named MyDate (there is
Using Java.
Re-do this code on the bottom the day of the week, except this time put it into a class named MyDate (there is already a Java class named Date). The MyDate class should have:
1) Private instance variables of type int to store the month, day, and year that the date represents
2) A default constructor that initializes the date to 10-31-2017
3) Another constructor that sets the day, month, and year to parameters that are sent in
4) Public accessor methods to return the month, day, and year
5) Public mutator methods to set the month, day, and year
6) A public method that returns the full date of the variables in the object as a String in the format: DayOfWeek, MonthName DayNumber, Year. For example, a MyDate object set to 10-31-2017 would be returned as "Tuesday, October 31, 2017" if this method were called.
7) All other methods used by your class should be declared as private. You can make any additional private methods that you like.
Also create a separate class with a main method that tests the MyDate class by creating several MyDate objects with different dates (at least 3 of them) and outputting their values and full date strings.
import java.util.Scanner;
public class DayOfWeek {
public static boolean isLeapYear(int year) { if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) return true; else return false; }
public static int getCenturyValue(int year) { int firstTwoDigits = year / 100; int remainder = firstTwoDigits % 4; return (3 - remainder) * 2; }
public static int getYearValue(int year) { int lastTwoDigits = year % 100; int result = lastTwoDigits / 4; return (result + lastTwoDigits);
}
public static int getMonthValue(int month, int year) { switch (month) { case 1: if (isLeapYear(year)) return 6; else return 0; case 2: if (isLeapYear(year)) return 3; else return 2; case 3: return 3; case 4: return 6; case 5: return 1; case 6: return 4; case 7: return 6; case 8: return 2; case 9: return 5; case 10: return 0; case 11: return 3; case 12: return 5; } return -1; }
public static void main(String[] args) { int m, d, y; Scanner keyboard = new Scanner(System.in); System.out.println("Enter a numeric month."); m = keyboard.nextInt(); System.out.println("Enter a numeric day."); d = keyboard.nextInt(); System.out.println("Enter a numeric year."); y = keyboard.nextInt();
int sum = d + getCenturyValue(y) + getYearValue(y) + getMonthValue(m, y); String day = ""; switch (sum % 7) { case 0: day = "Sunday"; break; case 1: day = "Monday"; break; case 2: day = "Tuesday"; break; case 3: day = "Wednesday"; break; case 4: day = "Thursday"; break; case 5: day = "Friday"; break; case 6: day = "Saturday"; break; } System.out.println("That day is a " + day);
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started