4 Replies - 277 Views - Last Post: 06 October 2013 - 04:51 PM Rate Topic: -----

#1 ahmadando  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-October 13

Java Comparing Objects Problem!

Posted 06 October 2013 - 04:28 PM

I have commented my program to some extent,where the problem lies.The problem lies with these Methods mentioned below.
Please don't include High Level Programming in your Answer!

Our Instructor has asked to use these methods:
Methods Detail:

boolean after(Date when)
Tests if this date is after the specified date.

boolean before(Date when)
Tests if this date is before the specified date.

int compareTo(Date anotherDate)
Compares two Dates for ordering.

boolean equals(Object obj)
Compares two dates for equality.

public class Date
{
	private int PDay;
	private int PMonth;
	private int PYear;

	public Date(int day,int month,int year)
	{
		setMonth(month);
		setYear(year);
		setDay(day);
	}

	public Date(int month,int year)
	{
		this(1,month,year);
	}

	public Date(int year)
	{
		this(1,1,year);
	}

	public void setYear(int year)
	{
		if(year>=1950 && year<=2049)
			PYear=year;
		else
			System.out.println("Year Invalid-Must be Between 1950 & 2049");
	}

	public int getYear()
	{
		return PYear;
	}

	public void setMonth(int month)
	{
		if(month>=1 && month<=12)
			PMonth=month;
		else
			System.out.println("Month Invalid-Must be Between 1 & 12");
	}

	public int getMonth()
	{
		return PMonth;
	}

	public void setDay(int day)
	{
		if(getMonth()==1 || getMonth()==3 || getMonth()==5 || getMonth()==7 || getMonth()==8 || getMonth()==10 || getMonth()==12)
		{
			if(day>=1 && day<=31)
				PDay=day;
			else
				System.out.println("Day Out of Bound Error!");
		}
		else if(getMonth()==4 || getMonth()==6 || getMonth()==9 || getMonth()==11)
		{
			if(day>=1 && day<=30)
				PDay=day;
			else
				System.out.println("Day-Month mismatch Error!");
		}
		else if(getMonth()==2)
		{
			if(getYear()%4==0 && getYear()%100!=0 || getYear()%400==0)
			{
				if(day>=1 && day<=29)
				{
					PDay=day;
					System.out.println("Note: "+getYear()+" is a leap year!");
				}
				else
					System.out.println("Day-Month mismatch Error!\n(February has 29 days in a leap year!)");
			}
			else
			{
				if(day>=1 && day<=28)
				{
					PDay=day;
					System.out.println("Note: "+getYear()+" is not a leap year!");
				}
				else
					System.out.println("Day-Month mismatch Error!\n(February has 28 days in a usual year!)");
			}
		}
		else
			System.out.println("Day Invalid-Must be Between 1 & 31");
	}	

	public int getDay()
	{
		return PDay;
	}

	public boolean equals(Date aDate)
	{
		//This works fine!
		if(getYear()==aDate.getYear() && getMonth()==aDate.getMonth() && getDay()==aDate.getDay())
			return true;
		else
			return false;
	}

	public boolean after(Date aDate)
	{
		//I am stuck at how to adjust the conditions so that if one of them is false,others don't get false.
		if(getYear()>aDate.getYear() && getMonth()>aDate.getMonth() && getDay()>aDate.getDay())
			return true;
		else
			return false;
	}

	public boolean before(Date aDate)
	{
		//I am stuck at how to adjust the conditions so that if one of them is false,others don't get false.
		if(getYear()<aDate.getYear() && getMonth()<aDate.getMonth() && getDay()<aDate.getDay())
			return true;
		else
			return false;
	}

	public int compareTo(Date o)
	{
		equals(o);
		after(o);
		before(o);

		// It won't allow me to execute so I randomly asked to return 5 as return type is integer.
		return 5;
	}

	public String toString()
	{
		return getDay()+"/"+getMonth()+"/"+getYear();
	}
}



Test Code is:
public class DateTest
{
	public static void main(String[] args)
	{
		Date newDate = new Date(26,2,2013);
		Date newDate1 = new Date(21,1,1991);
		System.out.println(newDate);
		System.out.println(newDate1);
		System.out.println("Dates are Equal: "+newDate.equals(newDate1));
		if(newDate1.after(newDate)==true)
			System.out.println(newDate1+" is after "+newDate+" : "+newDate1.after(newDate));
		else
			System.out.println(newDate1+" is after "+newDate+" : "+newDate1.after(newDate));
		if(newDate.before(newDate1)==true)
			System.out.println(newDate1+" is before "+newDate+" : "+newDate1.before(newDate));
		else
			System.out.println(newDate1+" is before "+newDate+" : "+newDate1.before(newDate));
	}
}


Output:

Note: 2013 is not a leap year!
26/2/2013
21/1/1991
Dates are Equal: false
21/1/1991 is after 26/2/2013 : false
21/1/1991 is before 26/2/2013 : true

With a different Argument!
public class DateTest
{
	public static void main(String[] args)
	{
		Date newDate = new Date(26,2,2013);
		Date newDate1 = new Date(21,1,2013);
		System.out.println(newDate);
		System.out.println(newDate1);
		System.out.println("Dates are Equal: "+newDate.equals(newDate1));
		if(newDate1.after(newDate)==true)
			System.out.println(newDate1+" is after "+newDate+" : "+newDate1.after(newDate));
		else
			System.out.println(newDate1+" is after "+newDate+" : "+newDate1.after(newDate));
		if(newDate.before(newDate1)==true)
			System.out.println(newDate1+" is before "+newDate+" : "+newDate1.before(newDate));
		else
			System.out.println(newDate1+" is before "+newDate+" : "+newDate1.before(newDate));
	}
}



Output:

Note: 2013 is not a leap year!
26/2/2013
21/1/2013
Dates are Equal: false
21/1/2013 is after 26/2/2013 : false
21/1/2013 is before 26/2/2013 : false

WHY IS IT NOT GIVING THE RIGHT OUTPUT!

Is This A Good Question/Topic? 0
  • +

Replies To: Java Comparing Objects Problem!

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: Java Comparing Objects Problem!

Posted 06 October 2013 - 04:38 PM

Quote

Note: 2013 is not a leap year!
26/2/2013
21/1/2013
Dates are Equal: false
21/1/2013 is after 26/2/2013 : false
21/1/2013 is before 26/2/2013 : false

WHY IS IT NOT GIVING THE RIGHT OUTPUT!



if(getYear()<aDate.getYear() && getMonth()<aDate.getMonth() && getDay()<aDate.getDay())


Because it is not the case that 2013 is less than 2013, to begin with. But if you fix that, it would still not be the case that 26 would be less than 26, so you'd fail on a comparison of 26/1/2013.before(21/2/2013) - this would return false because your expression requires ALL of those conditions to be true before it returns true.
Was This Post Helpful? 0
  • +
  • -

#3 ahmadando  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 02-October 13

Re: Java Comparing Objects Problem!

Posted 06 October 2013 - 04:42 PM

I totally understand what you said but what is the other way of accomplishing this task?
The Logic?

This post has been edited by andrewsw: 06 October 2013 - 04:45 PM
Reason for edit:: Removed unnecessary quote

Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8324
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Java Comparing Objects Problem!

Posted 06 October 2013 - 04:50 PM

calling a method could be an expensive process

       if(getMonth()==1 || getMonth()==3 || getMonth()==5 || getMonth()==7 || getMonth()==8 || getMonth()==10 || getMonth()==12)
        {


do it only once
    int m = getMonth();
    if(m==1 || m==3 || m==5 ....
        {


but in this case you can of over doing it... no need to call getMonth() within the class itself... the variable PMonth is directly available

A lot better to male an array of number of days in each mont

    private static int final nbDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};


then you can access it by your month number
Also respect Java convetions, variable names start by a lower case letter like: theMonth :)

For your boolean tests avoid those long if() with &&
just return when the codition is broken

public boolean before(Date otherDate) {
    // test year
    if(otherDate.year > year)
       return false;
    if(otherDate.year < year)
       return true;
    // test month
    if(otherDate.month > month)
       return true;
    if(otherDate.month < month)
       return false;
    // OK same year and month
    return otherDate.day < day;
}


if(otherDate/month >=
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7564
  • View blog
  • Posts: 12,681
  • Joined: 19-March 11

Re: Java Comparing Objects Problem!

Posted 06 October 2013 - 04:51 PM

Take it step by step. If this year is before that year, then this date is before that date: you can return true in that case. If this year is after that year, then this date is not before that year. In that case, you can return false.
If you haven't returned, then this year == that year, so we have to look at the month. Same procedure.


You can be a little more slick than that, but don't worry about being slick. Get it to work right, then think about being clever.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1