I have my main class Date.Java
Date.Java does:
-sets month date and year, and makes sure their valid inputs
-determines how many days are in the month you enter
-determines how many days have passed since the day you enter
-determines how many days are left from the day you enter
-determines if the year is a leap year or not
I also have a testing class TestDate.java
-It asks the user for the inputs and sends them to Date.java
My issue is I can only get the program to print the date the user enters. How would I go about calling the other methods and having them print what they are returning?
Date.java:
public class Date
{
int Day;
int Month;
int Year;
int numberOfDays;
int daysPassed;
int daysRemaining;
int M;
int D;
int Y;
public Date (int Day, int Month, int Year)
{
setDate(Month, Day, Year);
}
public void setDate (int Day, int Month, int Year)
{
setMonth(Month);
setDay(Day);
setYear(Year);
}
//-------------------SETTERS----------------------
public void setMonth(int Month)
{
M = ((Month>0&&Month<13) ?Month:1); //conditional statement that checks to see if the Month is valid
}
public void setDay(int Day)
{
D = ((Day>=1&&Day<=365) ?Day:1); //conditional statement that checks to see if the day is valid
}
public void setYear(int Year)
{
Y = ((Year>=1000&&Year<=9999) ?Year:1900); //conditional statement that checks to see if the Year is valid
}
//-------------------GETTERS----------------------
public int getMonth()
{
return M;
}
public int getDay()
{
return D;
}
public int getYear()
{
return Y;
}
public String toString()
{
return String.format("%d-%d-%d", getMonth(), getDay(), getYear());
}
public static boolean isLeapYear(int getYear)
{
if (getYear%4 == 0)
return true;
else
return false;
}
public int daysOfMonth(int getMonth, int numberOfDays, boolean isLeapYear)
{
if (getMonth==1) //jan
numberOfDays = 31;
//---------------------------------------Feb
if (isLeapYear == true) //feb
{
if (getMonth==2)
numberOfDays = 29;
}
if (isLeapYear == false) //feb
{
if (getMonth==2)
numberOfDays = 28;
}
//---------------------------------------Feb
if (getMonth==3) //march
numberOfDays = 31;
if (getMonth==4) //april
numberOfDays = 30;
if (getMonth==5) //may
numberOfDays = 31;
if (getMonth==6) //june
numberOfDays = 30;
if (getMonth==7) //july
numberOfDays = 31;
if (getMonth==8) //august
numberOfDays = 31;
if (getMonth==9) //sept
numberOfDays = 30;
if (getMonth==10) //oct
numberOfDays = 31;
if (getMonth==11) //nov
numberOfDays = 30;
if (getMonth==12) //dec
numberOfDays = 31;
return numberOfDays;
}
public int daysPassedInYear(int getMonth, int Day, int getDay, int numberOfDays, boolean isLeapYear)
{
if (getMonth==1)
Day = getDay-31;
if (isLeapYear = true)
{
if (getMonth==2)
daysPassed = (numberOfDays-getDay)-60;
if (getMonth==3)
daysPassed = (numberOfDays-getDay)-91;
if (getMonth==4)
daysPassed = (numberOfDays-getDay)-121;
if (getMonth==5)
daysPassed = (numberOfDays-getDay)-152;
if (getMonth==6)
daysPassed = (numberOfDays-getDay)-182;
if (getMonth==7)
daysPassed = (numberOfDays-getDay)-213;
if (getMonth==8)
daysPassed = (numberOfDays-getDay)-244;
if (getMonth==9)
daysPassed = (numberOfDays-getDay)-274;
if (getMonth==10)
daysPassed = (numberOfDays-getDay)-305;
if (getMonth==11)
daysPassed = (numberOfDays-getDay)-335;
if (getMonth==12)
daysPassed = (numberOfDays-getDay)-366;
}
if (isLeapYear = false)
{
if (getMonth==2)
daysPassed = (numberOfDays-getDay)-59;
if (getMonth==3)
daysPassed = (numberOfDays-getDay)-90;
if (getMonth==4)
daysPassed = (numberOfDays-getDay)-120;
if (getMonth==5)
daysPassed = (numberOfDays-getDay)-151;
if (getMonth==6)
daysPassed = (numberOfDays-getDay)-181;
if (getMonth==7)
daysPassed = (numberOfDays-getDay)-212;
if (getMonth==8)
daysPassed = (numberOfDays-getDay)-243;
if (getMonth==9)
daysPassed = (numberOfDays-getDay)-273;
if (getMonth==10)
daysPassed = (numberOfDays-getDay)-304;
if (getMonth==11)
daysPassed = (numberOfDays-getDay)-334;
if (getMonth==12)
daysPassed = (numberOfDays-getDay)-365;
}
return daysPassed;
}
public int daysRemainingInYear(int dayspassed, boolean isLeapYear, int daysRemaining)
{
if (isLeapYear = true)
daysRemaining = (366 - dayspassed);
if (isLeapYear = false)
daysRemaining = (365 - dayspassed);
return daysRemaining;
}
}
TestDate.java:
import javax.swing.JOptionPane;
public class TestDate {
public static void main(String[] args) {
int month =Integer.parseInt(JOptionPane.showInputDialog("What month do you want(in number form ex. Jan = 1?"));
int day =Integer.parseInt(JOptionPane.showInputDialog("What day do you want within the month?"));
int year =Integer.parseInt(JOptionPane.showInputDialog("What year do you want"));
Date setDateObject = new Date(month, day, year);
System.out.println(setDateObject.toString());
}
}
You can call the other methods on your Date object and print their return value.
Date date = new Date(1, 2, 2003);
System.out.println(date.getYear());
System.out.println(date.isLeapYear());
The way you use parameters is weird.. Parameters like getMonth, numberOfDays, isLeapYear are used to pass information into the function. getMonth and isLeapYear are essential to the function working correctly, but numberOfDays is not essential. You can declare the function like so:
public int daysOfMonth(int getMonth, boolean isLeapYear)
{
if (getMonth==1) //jan
return 31;
//---------------------------------------Feb
if (isLeapYear && getMonth==2)
return 29;
}
if (/* !isLeapYear && */ getMonth==2)
return 29;
}
//---------------------------------------Feb
if (getMonth==3) //march
return 31;
if (getMonth==4) //april
return 30;
if (getMonth==5) //may
return 31;
if (getMonth==6) //june
return 30;
if (getMonth==7) //july
return 31;
if (getMonth==8) //august
return 31;
if (getMonth==9) //sept
return 30;
if (getMonth==10) //oct
return 31;
if (getMonth==11) //nov
return 30;
if (getMonth==12) //dec
return 31;
return -1; // error
}
Then you can create a date like so and call the daysOfMonth function.
Date date = (2, 29, 2004);
System.out.println(date);
System.out.println("Is this date a leap year? " + date.isLeapYear());
System.out.println("How many days in this month? " + date.daysOfMonth(date.getMonth(), date.isLeapYear());
Do
System.out.println(yourobject.yourMethod());
This will print what ever your method yourMethod() is returning.
Also you can print the values inside any function.
public void yourMethod(){
System.out.println(yourAtrribute);
}
For your class you may do as following:
Date setDateObject = new Date(month, day, year);
//do other stuffs here
System.out.println(setDateObject.getYear());
This will print year that your getYear() method is returning.
Related
I want to fix the date to month/day/year in my class (Gregorian Calendar) so it works with my tester program. I get errors when running my tester program, I think it has to do with my string toString() method but I have tried to fix it but keep getting errors. I do not understand how having my string output to month + day + year would not work correctly in outputting mmmm/dd/yyyy. Thank you for your help.
Errors:
Exception in thread "main" java.lang.IllegalArgumentException
at Date.<init>(Date.java:15)
at Assign8B.main(Assign8B.java:14)
Class
public class Date {
private int day, month, year;
public Date() {
this.day = 1;
this.month = 1;
this.year = 1970;
}
public Date(int year, int month, int day) {
if (year < 1582) {
throw new IllegalArgumentException();
} else if (month <= 0 && month > 12) {
throw new IllegalArgumentException();
} else if (!isLeapYear(year) && (month == 2 && day == 29)) {
throw new IllegalArgumentException();
} else {
this.day = day;
this.month = month;
this.year = year;
}
}
public void addDays(int days) {
int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int step = 1;
if(days < 0)
step = -1;
if(isLeapYear(year))
daysOfMonth[1] = 29;
int d = 0;
while(d < days){
d++;
day += step;
if(day > daysOfMonth[month-1]){
day = 1;
month++;
if(month > 12){
year++;
month = 1;
if(isLeapYear(year))
daysOfMonth[1] = 29;
else
daysOfMonth[1] = 28;
}
}
else if(day < 1) {
month--;
if(month == 0) {
month = 12;
year--;
if(isLeapYear(year))
daysOfMonth[1] = 29;
else
daysOfMonth[1] = 28;
}
day = daysOfMonth[month-1];
}
}
}
public void addWeeks(int weeks) {
addDays(weeks * 7);
}
public int getDay() {
return day;
}
public void setDay(int day) {
this.day = day;
}
public int getMonth() {
return month;
}
public void setMonth(int month) {
this.month = month;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public boolean isLeapYear() {
return isLeapYear(this.year);
}
public boolean isLeapYear(int year) {
return(year % 400 == 0 || (year % 4 == 0 && year % 100 != 0));
}
public int daysTo(Date other) {
int days = 0;
int d1, m1, y1, d2, m2, y2;
int sign = 1;
if(this.toString().compareTo(other.toString()) > 0){
d1 = other.day;
m1 = other.month;
y1 = other.year;
d2 = day;
m2 = month;
y2 = year;
sign = -1;
} else {
d1 = day;
m1 = month;
y1 = year;
d2 = other.day;
m2 = other.month;
y2 = other.year;
}
int[] daysOfMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if(isLeapYear(y1))
daysOfMonth[1] = 29;
while(d1 != d2 || m1 != m2 || y1 != y2){
days++;
d1++;
if(d1 > daysOfMonth[m1-1]){
d1 = 1;
m1++;
if(m1 > 12){
y1++;
m1 = 1;
if(isLeapYear(y1))
daysOfMonth[1] = 29;
else
daysOfMonth[1] = 28;
}
}
}
days = days * sign;
return days;
}
public String longDate() {
String[] months = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
return months[month-1] + " " + day + ", " + year;
}
public String toString() {
String s = month + "/" + day + "/" + year;
return s;
}
public static int daysTo(Date one, Date two) {
return one.daysTo(two);
}
}
Tester Program
public class Assign8B {
// Part of the main method I'll use to test your class
// NO imports allowed from the JAVA API
public static void main(String[] a) {
Date one = new Date(10,15,1582); // start of Gregorian
Date two = new Date(1,28,2020); // 2020 is a leap year
one.addDays(1); // advance one day (negative subtracts days)
one.addWeeks(10); // advance one week (negative allowed, yes)
System.out.println(two.daysTo(one)); // -159645 days (negative)
System.out.println(one.getDay()); // day is now the 25th (advanced)
System.out.println(one.getMonth()); // returns 12, January is 1
System.out.println(one.getYear()); // still 1582 from start
System.out.println(one.isLeapYear()); // false for 1582
System.out.println(one.toString()); // style is 12/25/1582
try {
Date three = new Date(12,33,1956); // obviously illegal
Date four = new Date(2,29,2013); // illegal leap year
three.setDay(31); // fixes that day of month, OK
four.setMonth(3); // fixes the month, year still wrong
four.setYear(1929); // fixes the year, code not reached
} catch (IllegalArgumentException e) {
System.out.println("Illegal day attempted");
}
// Use UNIX zero of 01/01/70 for default, and create "longDate" output
// I thought a long date was dinner with a person you don't like?
Date five = new Date();
System.out.println(five.longDate()); // January 1, 1970
// Finally, let's understand what static methods are most commonly used for:
System.out.println(Date.daysTo(one, two)); // still 159645 days (positive)
}
}
As #madprogrammer mentioned in the comments, you have to change your Date call because of the order of year month day in your own class constructor, and then look at your add days function and change how you add years in your code.
How does the compareTo() method for Dates work here in java? I know that when you compare two dates the result will always be 0 if equal, 1 if the date being
compared inside the compareTo() parameter is older, and -1 if the date inside the parameter is more recent.
//Just an example
String[] da = {"01/14/1975", "08/20/1975", "08/20/1975"};
SimpleDateFormat f = new SimpleDateFormat("MM/dd/yyyy");
Date d1 = new Date();
Date d2 = new Date();
//this outputs 1 because d2 is older than d1
d1 = f.parse(da[1]);
d2 = f.parse(da[0]);
System.out.println(d1.compareTo(d2));
//this outputs 0 because dates are the same
d1 = f.parse(da[1]);
d2 = f.parse(da[2]);
System.out.println(d1.compareTo(d2));
//this outputs -1 because d2 is more recent than d1
d1 = f.parse(da[0]);
d2 = f.parse(da[1]);
System.out.println(d1.compareTo(d2));
Now I want to compare dates without using compareTo() method or any built-in method in java. As much as possible I want to use just the basic operators in java.
What is the computation or the algorithm of the compareTo() method in comparing dates that enable it to return -1, 0, and 1?
Edit:
In the case at the sample problem at my book, using java.util.Date is forbidden, what is supposed to be done is to create your own date object like this:
public class DatesObj
{
protected int day, month, year;
public DatesObj (int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() { return month; }
public int getDay() { return day; }
public int getYear() { return year; }
}
Now how do I compare this as if like they're int and determine which is old and which is newer??
If you want to compare two dates just as if they were just plain-old integers, you must first turn each date into a plain-old integer. The easiest way to turn a year/month/day representation of a date into a plain-old integer, that can be effectively compared with plain-old integers from other dates, is to line the pieces up in exactly that order: year first, month next, day last:
// in DateObj class....
public int getDateInt() {
return (yyyy * 10000) + (mm * 100) + dd;
}
So for March 19, 2019, you get 20190319, and for December 7, 1941 you get 19411207; by comparing the "integerized" versions of the dates you can see that:
19411207 < 20190319, just as December 7, 1941 is earlier than March 19, 2019;
20190319 > 19411207, just as March 19, 2019 is later than December 7, 1941;
19411207 != 20190319, just as December 7, 1941 and March 19, 2019 are different dates
You're limited to dates within the Common Era and no more than about 200,000 years into the future with this particular implementation. But with a little tweaking, you could easily easily handle dates outside these ranges, an exercise that I will, as the textbooks so often say, leave as an exercise for the reader.
Implement Comparable and override compareTo().
class DatesObj implements Comparable<DatesObj>{
protected int day, month, year;
public DatesObj(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
#Override
public int compareTo(DatesObj o) {
int diff = this.year - o.year;
if(diff != 0) {
return diff;
}
diff = this.month - o.month;
if(diff != 0) {
return diff;
}
return this.day - o.day;
}
}
Compare the years. If the years of both the dates are same, compare the months.
If the months are same, compare the dates.
public int compareDate(DatesObj d) {
if (this.year != d.year) {
if (this.year > d.year)
return 1;
else
return -1;
}
if (this.month != d.month) {
if (this.month > d.month)
return 1;
else
return -1;
}
if (this.day != d.day) {
if (this.day > d.day)
return 1;
else
return -1;
}
return 0;
}
Ref : https://docs.oracle.com/javase/8/docs/api/java/lang/Comparable.html
Ref : https://developer.android.com/reference/java/util/Calendar
create own class with Interface Comparable
class DateCompare implements Comparable<Date>
{
protected int day, month, year;
public DateCompare(int mm, int dd, int yyyy) {
month = mm;
day = dd;
year = yyyy;
}
#Override
public int compareTo(Date o) {
Calendar cal = Calendar.getInstance();
cal.setTime(o);
int diff = this.year - cal.get(Calendar.YEAR);
if(diff != 0) {
return diff;
}
diff = this.month - cal.get(Calendar.MONTH);
if(diff != 0) {
return diff;
}
return this.day - cal.get(Calendar.DAY_OF_MONTH);
}
public int getMonth() {
return month;
}
public int getDay() {
return day;
}
public int getYear() { return year; }
}
And Other More Helpful
https://gist.github.com/Ashusolanki/fed3b6a680092985ac0ab93ed70fcd7c
private String postTime(Date date)
{
long postTime = date.getTime();
long atTime = System.currentTimeMillis();
long diff = atTime - postTime;
long sec = TimeUnit.SECONDS.convert(diff, TimeUnit.MILLISECONDS);
if (sec >= 60) {
long minit = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if (minit >= 60) {
long hours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if (hours >= 24) {
long days = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
return days + " Days Ago";
} else {
return hours + " Hours Ago";
}
} else {
return minit + " Minutes Ago";
}
} else {
return sec + " Secounds Ago";
}
}
I have an Event class which uses the PriorityQueue and a Time class that I defined myself to use with the Event class.
static class Event implements Comparable<Event>{
Customer customer;
Time eventTime;
char eventType;
public int compareTo(Event e){
int n = e.eventTime.compareTo(eventTime);
int compare = 0;
if(n < 0){
compare = -1;
}
else if(n == 0){
compare = 0;
}
else if(n > 0){
compare = 1;
}
return compare;
}
}
class Time{
private int hour;
private int minute;
private boolean isMorning;
public Time(){
hour = 0;
minute = 0;
isMorning = true;
}
public Time(int h, int m, boolean morning){
hour = h;
minute = m;
isMorning = morning;
}
public void setTime(int h, int m, boolean morning){
hour = h;
minute = m;
isMorning = morning;
}
public int getHour(){
return hour;
}
public int getMinute(){
return minute;
}
public boolean isAM(){
return isMorning;
}
public String toString(){
String AM = "";
String min = "";
if(minute < 10){
min = ("0" + minute);
}
else{
min = ("" + minute);
}
if(isMorning){
AM = "AM";
}
else{
AM = "PM";
}
return ("" + hour + ":" + min + " " + AM);
}
public Time plus(int n){
Time newTime = new Time();
boolean newMorning = false;
int minutes = minute + n;
int newHour = minutes/60;
int newMinutes = minutes%60;
hour = hour + newHour;
if(hour > 12){
hour = hour - 12;
if(isMorning){
newMorning = false;
}
else{
newMorning = true;
}
}
newTime.setTime(hour, newMinutes, newMorning);
return newTime;
}
public int timeDifference(Time t){
int n = totalMinutes();
int m = t.totalMinutes();
return m - n;
}
public int compareTo(Time t){
int n = totalMinutes();
int m = t.totalMinutes();
int compare = 0;
if(n < m){
compare = -1;
}
else if(n == m){
compare = 0;
}
else if(n > m){
compare = 1;
}
return compare;
}
private int totalMinutes(){
int tempMinute = 0;
if(!isMorning){
if(hour == 12){
}
else{
hour = hour + 12;
tempMinute = (hour*60) + minute;
}
}
else{
if(hour == 12){
tempMinute = minute;
}
else{
tempMinute = (hour*60) + minute;
}
}
return tempMinute;
}
}
This isn't all of my code as I have others just holding the values that will later be inserted into the Event queue. When I check the time outside of the Event queue it matches the time that it should be, say I have a Time object as 11:22 AM, but when I insert it into the Event queue my time changes to 23:22 AM. For some reason it is adding 12 hours within the Event queue and I don't understand why.
Figured it out the totalMinutes() method was messing with the hours because it was being called when using compareTo() or timeDifference() implicitly. Thank you for the help!
First, totalMinutes() messes with the hour field when it shouldn't. It should use a local variable.
Second, your entire Event.compareTo(Event e) method can be reduced to
return e.eventTime.compareTo(eventTime);
which is back to front, unless you want the most recent times first. Try
return eventTime.compareTo(e.eventTime);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have to modify my class so that it passes JUnit test cases. It seems like everything should be working fine but it keeps failing. It is giving me a java.lang.AssertionError.
Here is my class
package dayofweek;
/**
* Method to find out what day of the week a certain date was
*
* Output determines what day of the week this certain date was
*
*/
public class DayOfWeek {
private int myMonth = -1, myDayOfMonth = -1, myYear= -1, myAdjustment= -1, numericDayOfWeek= -1, remainderSeven= -1;
private static final int NO_VALUE = -1;
/**
* #param what the date was
*/
public DayOfWeek(int month, int dayOfMonth, int year){
myMonth = month;
myDayOfMonth = dayOfMonth;
myYear = year;
remainderSeven = 0;
if(myMonth==1){
myAdjustment = 1;
if(myYear%4==0){
myAdjustment=0;
}
}
if(myMonth==2){
myAdjustment = 4;
if(myYear%4==0){
myAdjustment=3;
}
}
if(myMonth==3){
myAdjustment = 4;
}
if(myMonth==4){
myAdjustment = 0;
}
if(myMonth==5){
myAdjustment = 2;
}
if(myMonth==6){
myAdjustment = 5;
}
if(myMonth==7){
myAdjustment = 0;
}
if(myMonth==8){
myAdjustment = 3;
}
if(myMonth==9){
myAdjustment = 6;
}
if(myMonth==10){
myAdjustment = 1;
}
if(myMonth==11){
myAdjustment = 4;
}
if(myMonth==12){
myAdjustment = 6;
}
int fourDivides = myYear / 4;
numericDayOfWeek = myAdjustment + myDayOfMonth + (myYear-1900) + fourDivides;
remainderSeven = numericDayOfWeek % 7;
}
/**
* #return the month in a string
*/
public String getMonthString(){
String[] arrayOfMonths = new String[12];
arrayOfMonths[0] = "January";
arrayOfMonths[1] = "February";
arrayOfMonths[2] = "March";
arrayOfMonths[3] = "April";
arrayOfMonths[4] = "May";
arrayOfMonths[5] = "June";
arrayOfMonths[6] = "July";
arrayOfMonths[7] = "August";
arrayOfMonths[8] = "September";
arrayOfMonths[9] = "October";
arrayOfMonths[10] = "November";
arrayOfMonths[11] = "December";
if (this.myMonth > 0 && this.myMonth <=12){
return arrayOfMonths[this.myMonth-1];
}
else{
return null;
}
}
/**
* #return what day of the month it was
*/
public int getDayOfMonth(){
if(myDayOfMonth != NO_VALUE){
return myDayOfMonth;
}
else{
return NO_VALUE;
}
}
/**
* #return what year it was
*/
public int getYear(){
if(myYear != NO_VALUE){
return myYear;
}
else{
return NO_VALUE;
}
}
/**
* #return the numeric day of the week
*/
public int getNumericDayOfWeek(){
return remainderSeven;
}
/**
* returns what day of the week it was
*/
public String getDayOfWeek(){
String[] arrayOfDays = new String[7];
arrayOfDays[0] = "Saturday";
arrayOfDays[1] = "Sunday";
arrayOfDays[2] = "Monday";
arrayOfDays[3] = "Tuesday";
arrayOfDays[4] = "Wednesday";
arrayOfDays[5] = "Thursday";
arrayOfDays[6] = "Friday";
if( myMonth != NO_VALUE && myDayOfMonth != NO_VALUE && myYear != NO_VALUE){
return arrayOfDays[remainderSeven];
}
else{
return null;
}
}
/**
* #return the month in an int
*/
public int getMonth(){
if(myMonth != NO_VALUE){
return myMonth;
}
else return NO_VALUE;
}
}
Here is the JUnit test class
package dayofweektesting;
import dayofweek.DayOfWeek;
import static org.junit.Assert.*;
import org.junit.Test;
/**
* Test cases for "Day of week" lab assuming requirements set
* at top of DayOfWeek class
*
* Test valid leap years
*
*
*/
public class TestValidLeapYears {
/**
* Test valid 2/29/XXXX dates
*/
#Test
public void firstValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1904);
assertTrue(dow.getDayOfWeek().compareTo("Monday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 2);
}
#Test
public void secondValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1908);
assertTrue(dow.getDayOfWeek().compareTo("Saturday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 0);
}
#Test
public void thirdValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1912);
assertTrue(dow.getDayOfWeek().compareTo("Thursday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 5);
}
#Test
public void fourthValidLeapYear() {
DayOfWeek dow = new DayOfWeek(2, 29, 1916);
assertTrue(dow.getDayOfWeek().compareTo("Tuesday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 3);
}
/**
* Test boundary dates of2/28/XXXX and 3/1/XXXX for valid leap years
*/
#Test
public void firstValidLeapYearBoundaries() {
DayOfWeek dow = new DayOfWeek(2, 28, 1904);
assertTrue(dow.getDayOfWeek().compareTo("Sunday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 1);
dow = new DayOfWeek(3, 1, 1904);
assertTrue(dow.getDayOfWeek().compareTo("Tuesday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 3);
}
#Test
public void secondValidLeapYearBoundaries() {
DayOfWeek dow = new DayOfWeek(2, 28, 1908);
assertTrue(dow.getDayOfWeek().compareTo("Friday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 6);
dow = new DayOfWeek(3, 1, 1908);
assertTrue(dow.getDayOfWeek().compareTo("Sunday") == 0);
assertTrue(dow.getNumericDayOfWeek() == 1);
}
}
Here is the line the error is pointing to, in the secondValidLeapYearBoundaries()
assertTrue(dow.getDayOfWeek().compareTo("Friday") == 0);
does anyone know how to fix all these problems?
How to fix it? Print out the return value of dow.getDayOfWeek() for your failing case (or examine it in a debugger) to see what it is giving you, then single-stepping through the function to find out why. In this particular case, it's probably the constructor rather than getDayOfWeek since the latter is simply giving you information calculated by the former.
That's how you solve almost any problem, in coding and most other fields that are deterministic. Confirm that there's a problem then reexamine the steps to see where the problem was introduced.
What I'm trying to do is access an object, in this case date1 which has 3 attributes day, month and year. I'm attempting to make a method called showTomorrow() which will display the objects information 1 day infront in String format. This means I cannot alter the attributes of the original object.
I've written the Data.java program and it's shown below, if someone could point me in the right direction or show me what it would be really helpfull.
This is what I'd essentially be running on my main method I believe.
**Date date1 = new Date(30, 12, 2013)** // instantiate a new object with those paramaters
**date1.showDate();** // display the original date
**date1.tomorrow();** // shows what that date would be 1 day infront
The problem is right now it's not displaying anything. I thought that by saying dayTomorrow = this.day++; I was adding it's default value + 1 day to the variable dayTomorrow.
public class Date
{
private int day;
private int month;
private int year;
private int dayTomorrow;
private int monthTomorrow;
private int yearTomorrow;
public Date()
{
day = 1;
month = 1;
year = 1970;
}
public Date(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
}
public void setDate(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
}
public String getDate()
{
String strDate;
strDate = day + "/" + month + "/" + year;
return strDate;
}
public String getTomorrow()
{
String strTomorrow;
strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
return strTomorrow;
}
public String tomorrow()
{
dayTomorrow = this.day++;
monthTomorrow = this.month;
yearTomorrow = this.year;
if(dayTomorrow > 30)
{
dayTomorrow = 1;
monthTomorrow = this.month++;
}
if(monthTomorrow > 12)
{
monthTomorrow = 1;
yearTomorrow = this.year++;
}
return getTomorrow();
}
public void showDate()
{
System.out.print("\n\n THIS OBJECT IS STORING ");
System.out.print(getDate());
System.out.print("\n\n");
}
public void showTomorrow()
{
System.out.print("\n\n THE DATE TOMORROW IS ");
System.out.print(getTomorrow());
System.out.print("\n\n");
}
public boolean equals(Date inDate)
{
if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
{
return true;
}
else
{
return false;
}
}
}
You just need to use ++this.day, ++this.month and ++this.year. When you use this.day++ it returns the previous date value, not the new. Putting the ++ in the front solves the problem. Also, it changes the day value... you might want to change that to this.day + 1.
Are You calling showDate() after date1.tomorrow() to show your output?
or instead of date1.tomorrow(); call date1.showTomorrow();
Have a look at this : http://docs.oracle.com/javase/tutorial/java/nutsandbolts/op1.html
post incremention ...
You could use the native date support in java but I figured you are just practicing right?
This should do the trick:
public class Date {
private int day = 1;
private int month = 1;
private int year = 1970;
private int dayTomorrow = day+1;
private int monthTomorrow;
private int yearTomorrow;
public Date()
{
tomorrow();
}
public Date(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
tomorrow();
}
public void setDate(int inDay, int inMonth, int inYear)
{
day = inDay;
month = inMonth;
year = inYear;
}
public String getDate()
{
String strDate;
strDate = day + "/" + month + "/" + year;
return strDate;
}
public String getTomorrow()
{
String strTomorrow;
strTomorrow = dayTomorrow + "/" + monthTomorrow + "/" + yearTomorrow;
return strTomorrow;
}
public void tomorrow()
{
monthTomorrow = this.month;
yearTomorrow = this.year;
if(dayTomorrow > 30)
{
dayTomorrow = 1;
monthTomorrow = this.month++;
}
if(monthTomorrow > 12)
{
monthTomorrow = 1;
yearTomorrow = this.year++;
}
}
public void showDate()
{
System.out.print("\n\n THIS OBJECT IS STORING ");
System.out.print(getDate());
System.out.print("\n\n");
}
public void showTomorrow()
{
System.out.print("\n\n THE DATE TOMORROW IS ");
System.out.print(getTomorrow());
System.out.print("\n\n");
}
public boolean equals(Date inDate)
{
if(this.day == inDate.day && this.month == inDate.month && this.year == inDate.year)
{
return true;
}
else
{
return false;
}
}
}
Look carefully for any changes i've made ;)
Here's the main:
public static void main(String[] args) {
Date d = new Date();
d.showDate();
d.showTomorrow();
}