Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm not able to get the getDayOfWeek() method to run in my code. The while loop which checks for equality is what I think is

I'm not able to get the getDayOfWeek() method to run in my code. The while loop which checks for equality is what I think is messing it up. But any suggestion would be great. My entire code is below, i have a test code that I am using to run all of these. Thank you!

import java.util.TimeZone;

/*Gordon Thompson Lab 1_COSC241 */

public class Date { private final int January = 1; private final int Feburary = 2; private final int December = 12; private static final String[] dayOfWeek = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; private int year; private int month; private int day; //Constructs a new Date representing the given year, month, and day. public Date(int Y, int M, int D){ this.year = Y; this.month = M; this.day = D;

if (year < 1753) { throw new IllegalArgumentException(this + " year too small: " + year); } if (month < January || month > December) { throw new IllegalArgumentException(this + " month out of range: " + month); } if (day < 1 || day > this.DaysinMonth()) { throw new IllegalArgumentException(this + " day out of range: " + day); } } // Helping method; returns the number of days since the epoch (1/1/1730). public static int getDaysSinceEpoch() { return (int) ((System.currentTimeMillis() + TimeZone.getDefault().getRawOffset()) / 1000 / 60 / 60 / 24); } public Date(){ this(1970, 1, 1); int daysSinceEpoch = getDaysSinceEpoch(); for (int i = 0; i < daysSinceEpoch; i++) { this.nextDay(); } } //Returns this Date's day of the month, between 1 and the number of days in //that month (which will be between 28 and 31). public int getDay(){ return day; } //Returns this Date's month of the year, between 1 and 12. public int getMonth(){ return month; } //Returns this Date's year. public int getYear(){ return year; }

//Returns a String representing what day of the week this Date falls on. public String getDayOfWeek(){ int numday = 1; Date temp = new Date(1753, January, 1); while (!temp.equals(this)){ temp.nextDay(); numday = (numday + 1) % 7; } return dayOfWeek[numday]; } //Returns whether this Date's year is a leap year. public boolean isLeapYear(){ return year%4==0; } //Advances this Date to the next day after its current day. public void nextDay(){ day++; if (day > DaysinMonth()) { month++; day = 1; if (month > December) { year++; month = January; } } } //helps the above with the number of days in months private int DaysinMonth() { int numDays; if(month==2){ if(isLeapYear()) numDays = 29; else numDays = 28; } else if(month==4||month==6||month==9||month==11) numDays = 30; else if(month==1||month==3||month==5||month==7||month==8||month==10|| month==12) numDays = 31; else numDays = 00; return numDays; } public String toString(){ return year + "/" + month + "/" + day; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Learning PostgreSQL

Authors: Salahaldin Juba, Achim Vannahme, Andrey Volkov

1st Edition

178398919X, 9781783989195

More Books

Students also viewed these Databases questions

Question

Ability to work comfortably in a team environment

Answered: 1 week ago

Question

Exposure to SQL desirable but not required

Answered: 1 week ago