Constructors .equals method

Constructors .equals method

Hi, my two dates never match using .equals method and two different constructors.

o/p is like

[email protected] [email protected]

Code:

package examples;

public class Pdate{
    int Pday;
    int Pmonth;
    int Pyear;
    int d1,d2;
Pdate()
    {
    Pday=2;
    Pmonth=1;
    Pyear=1988;
    }
Pdate(int da,int mo,int ye){
    this.Pday=da;
    this.Pday=mo;
    this.Pday=ye;
    }

public static void main(String args[]){
    Pdate date1=new Pdate(2,1,1988);
    Pdate date2=new Pdate();

    {if (date1.equals(date2))
    System.out.println(date1+"Awesome date"+date2);
    else
    System.out.println(date1+"Blah date"+date2);
    }

}}
View Answers

May 31, 2012 at 3:38 PM

We have modified your code. It will check the two dates properly.

package examples;
import java.util.*;
public class Pdate extends Date{
    int Pday;
    int Pmonth;
    int Pyear;
    int d1,d2;
Pdate()
    {
    Pday=2;
    Pmonth=1;
    Pyear=1988;
    }
Pdate(int da,int mo,int ye){
    this.Pday=da;
    this.Pday=mo;
    this.Pday=ye;
    }

public static void main(String args[]){
    Pdate date1=new Pdate(2,1,1988);
    Pdate date2=new Pdate();

    if (date1.equals(date2))
    System.out.println(date1+"Awesome date"+date2);
    else
    System.out.println(date1+"Blah date"+date2);

}
}

May 31, 2012 at 3:38 PM

We have modified your code. It will check the two dates properly.

package examples;
import java.util.*;
public class Pdate extends Date{
    int Pday;
    int Pmonth;
    int Pyear;
    int d1,d2;
Pdate()
    {
    Pday=2;
    Pmonth=1;
    Pyear=1988;
    }
Pdate(int da,int mo,int ye){
    this.Pday=da;
    this.Pday=mo;
    this.Pday=ye;
    }

public static void main(String args[]){
    Pdate date1=new Pdate(2,1,1988);
    Pdate date2=new Pdate();

    if (date1.equals(date2))
    System.out.println(date1+"Awesome date"+date2);
    else
    System.out.println(date1+"Blah date"+date2);

}
}

May 31, 2012 at 7:13 PM

no it doesn't check the date properly still. both date1 and date2 are same, yet it still says "Blah date"..

also i don't understand how extending Date would be helpful, can u plz explain why?

Thanks..

Ads









Related Tutorials/Questions & Answers:
Constructors .equals method
Constructors .equals method  Hi, my two dates never match using .equals method and two different constructors. o/p is like [email protected] [email protected] Code: package examples; public class Pdate
Constructors .equals method
Constructors .equals method  Hi, my two dates never match using .equals method and two different constructors. o/p is like [email protected] [email protected] Code: package examples; public class Pdate
Advertisements
Equals()
Equals()  What is role of equals() method in camparator interface
constructors
constructors  package Test; class B { B() { } private B() { } } public class A { public static void main(String[] args) { B b1 = new B(); } } Error: Duplicate method B in type B. Trying to run
equals and hashcode
to the equals() method because it is inherited from the Object class. However, this default... can override the default implementation of the equals() method defined...equals and hashcode  Could you please explain me why equals
Constructors in Java
Constructors in Java  When do we use Constructors in JAva?   All object creation is done through constructors. If no constructor is specified, the compiler will supply a default empty constructor that basically does
constructors
How are this() and super() used with constructors?
How are this() and super() used with constructors?   Hi, How are this() and super() used with constructors? thanks
Overriding equals and hashCode in Java
Overriding equals and hashCode in Java  Overriding equals and hashCode in Java
1024 bytes equals to
1024 bytes equals to  1024 bytes equals to   1024 bytes equals to 1 KB
ModuleNotFoundError: No module named 'equals'
ModuleNotFoundError: No module named 'equals'  Hi, My Python... 'equals' How to remove the ModuleNotFoundError: No module named 'equals'... to install padas library. You can install equals python with following command
Difference between equals() and == ?
. But one more difference is that equals() is a method and "= ="... equals() method in java. The equals() method which is defined in object class. equals() method behave same as the equality operator but rather then checking
objective c if string equals
objective c if string equals   Checking if a string is equal to ' " '   In objective c, isEqualToString is a keyword that allows you to check if the string is equal to or not. if ([text isEqualToString
method
method   how and where, we can define methods ? can u explain me with full programme and using comments
method
method  can you tell me how to write an abstract method called ucapan() for B2 class class A2{ void hello(){ system.out.println("hello from A2"); }} class B2 extends A2{ void hello(){ system.out.println("hello from B2
why we use constructors instead of methods in java?
why we use constructors instead of methods in java?   why we use constructors instead of methods in java
What are constructors ? explain different types of constructor with example
What are constructors ? explain different types of constructor with example  Hi, What are constructors ? explain different types of constructor... constructor require. That's why the Constructors are used to initialize the instance
Version of ai.x>equals-ignoring-fields_2.11 dependency
List of Version of ai.x>equals-ignoring-fields_2.11 dependency
Maven Dependency equals-ignoring-fields_2.11 >> 1.0.1
You should include the dependency code given in this page to add Maven Dependency of ai.x >> equals-ignoring-fields_2.11 version1.0.1 in your project
Maven Dependency equals-ignoring-fields_2.11 >> 1.0.2
You should include the dependency code given in this page to add Maven Dependency of ai.x >> equals-ignoring-fields_2.11 version1.0.2 in your project
Maven Dependency equals-ignoring-fields_2.11 >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of ai.x >> equals-ignoring-fields_2.11 version1.0.0 in your project
Case-insensitive equals using Hibernate Criteria.
Case-insensitive equals using Hibernate Criteria.  How to use Case-insensitive equals with Hibernate Criteria?   The Case Insensitive ignores the case of the latter. It Selects all the records of the table related
Case-sensitive equals using Hibernate Criteria.
Case-sensitive equals using Hibernate Criteria.  How to check for case sensitive equals in Hibernate criteria?   package net.roseindia.main; import java.util.*; import net.roseindia.table.Employee; import
method inside the method??
method inside the method??  can't we declare a method inside a method in java?? for eg: public class One { public static void main(String[] args) { One obj=new One(); One.add(); private static void add
Exceptional Constructors - Java Tutorials
why should we over ride hashCode(), when equals was over ride()?
why should we over ride hashCode(), when equals was over ride()?  why should we over ride hashCode(), when equals was over ride
get method
get method   how to use get method: secure method is post method and most of use post method why use a get method
method question
method question  How do I figure out the difference in a method heading, a method body, and a method definition
Method
Method       In this section, we will explore the concept of method in the reference of object oriented... and behaviors are defined by methods. Method : An brief introduction Methods
Method Overloading
Method Overloading  In java can method be overloaded in different class
Method Overloading
Method Overloading  In java can method be overloaded in different class

Ads