Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inheritance and Polymorphism assignment!!

  1. #1
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Inheritance and Polymorphism assignment!!


    I think i got most of this done but im stuck on one thing right now:
    this is what i have so far:
     
    public class Employee {
     
        private String name;
        private static double annualSalary;
        private String yearHired;
        private String socialSecurityNumber;
        private String GregorianCalender;
     
        public Employee(){
     
            super();
            name = "no name";
            annualSalary = 0.0;
            yearHired = "January 1, 1900";
            socialSecurityNumber = "999999999";
     
        }
     
        public Employee(String name, double salary, GregorianCalender year, String ssn){
     
            annualSalary = salary;
            yearHired = GregorianCalender;
            socialSecurityNumber = ssn;
     
        }
     
        public Employee(Employee emp){
     
            annualSalary = emp.getAnnualSalary();
            //yearHired = emp.getYearHired();
            socialSecurityNumber = emp.getSocialSecurityNumber();
     
        }
     
        public double getAnnualSalary(){
     
        return annualSalary;
    }
     
        public GregorianCalender getYearHired(GregorianCalender date){
            return date;
        }
     
        public String getSocialSecurityNumber(){
     
            return socialSecurityNumber;
        }
     
        public void setAnnualSalary(double salary){
            annualSalary = salary;
        }
     
        public void setYearHired(GregorianCalender date){
            yearHired = GregorianCalender;
        }
     
        public void setSocialSecurityNumber(String ssn){
     
            socialSecurityNumber = ssn;
        }
     
        public String toString(){
     
            return("Name:" + name + "/n Year Hired: " + yearHired + "/n Annual Salary: " +
                    annualSalary + "/nSocial Security Number: " + socialSecurityNumber);
     
        }
     
        public boolean equals(Object obj){
     
     
        }
     
     
    }

    thats one class I have one more other class and a main class
    public class Person {
    	//instance field for the name of the Person
    	private String name;
     
    	/**
    	 * Default constructor
    	 */
    	public Person() {
    		name= "no name";
    	}
     
    	/**
    	 * Constructor that set the name to what is passed in
    	 * @param n The name of the Person you are creating
    	 */
    	public Person(String n) {
    		name = n;
    	}
     
    	/**
    	 * Returns the name of the person
    	 * @return The name of the person
    	 */
    	public String getName() {
    		return name;
    	}
     
    	/**
    	 * Set the Person's name to name that is passed in
    	 * @param n Name that you want to set the Person's name to.
    	 */
    	public void setName(String n){
    		name = n;
    	}
     
    	/**
    	 * overrides default toString() method, that returns the Person in string form
    	 * @return The Person as a string
    	 */
            @Override
    	public String toString() {
    		return("Person's name is " + getName());
    	}
    }

    and this is the main class that im using to test
    import java.util.GregorianCalendar;
     
    public class TestEmployee
    {
        public static void main(String[] args) {
    	Employee e1 = new Employee();
    	Employee e2
    	    = new Employee("Bob Smith", 45000.5,
    			   new GregorianCalendar(2009, 0, 1), "123456789");
    	Employee e3 = new Employee(e2);
    	System.out.println(e1.toString());
    	System.out.println(e2.toString());
    	System.out.println(e3.toString());
    	if (e2.equals(e3))
    	    System.out.println(new StringBuilder().append(e2.getName()).append
    				   (" is the same as ").append
    				   (e3.getName()).toString());
    	else
    	    System.out.println(new StringBuilder().append(e2.getName()).append
    				   (" is NOT the same as ").append
    				   (e3.getName()).toString());
    	System.out.println();
    	e3.setName("Test Person");
    	e3.setSocialSecurityNumber("134256987");
    	e3.setAnnualSalary(65000.63);
    	e3.setYearHired(new GregorianCalendar(2009, 9, 15));
    	System.out.println(e1.toString());
    	System.out.println(e2.toString());
    	System.out.println(e3.toString());
    	if (e2.equals(e3))
    	    System.out.println(new StringBuilder().append(e2.getName()).append
    				   (" is the same as ").append
    				   (e3.getName()).toString());
    	else
    	    System.out.println(new StringBuilder().append(e2.getName()).append
    				   (" is NOT the same as ").append
    				   (e3.getName()).toString());
        }
    }

    this is that its asking me to do for this part
    public boolean equals(Object obj){
     
        }
    public boolean equals (Object obj)
    Method that returns true if the invoking object and the passed argument are exactly equal, otherwise returns false.


  2. #2
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance and Polymorphism assignment!!

    not sure what the question is..
    but if you mean to implement equal, then you'll want to test for instanceof to see if the object is the same as the one that
    invoked the method, and then just test to see that all the elements are equal..
    example :
    public boolean equals(Object obj)
    {
       if (this instanceof Person && obj instanceof Person && this.getName() == obj.getName()) return true;
       else
       return false;
    }

  3. #3
    Member
    Join Date
    Sep 2012
    Posts
    41
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default Re: Inheritance and Polymorphism assignment!!

    so instanceof is suppose to be as i understood from this :
    getSocialSecurity and getAnnualSalary
    am I correct?

Similar Threads

  1. Polymorphism Question
    By BuhRock in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2012, 11:09 AM
  2. Polymorphism.
    By TP-Oreilly in forum Java Theory & Questions
    Replies: 2
    Last Post: March 11th, 2012, 09:28 AM
  3. assignment troubles polymorphism (guide for assignment included)
    By tdawg422 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 8th, 2011, 10:01 AM
  4. Programming assignment using inheritance
    By Slypher in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 4th, 2011, 05:42 PM
  5. Polymorphism test
    By speedycerv in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 29th, 2011, 07:15 AM