5 Replies - 556 Views - Last Post: 27 October 2011 - 07:27 PM Rate Topic: -----

#1 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

HashCode HashSet with objects question/problem

Posted 25 October 2011 - 11:45 AM

The assignment specifications are:
Chapter 9 presented an Instructor class that holds information about an instructor. (See Section 9-7--Aggregation) Modify the Instructor class by overriding the hashCode and equals method. Then write a class that stores several Instructor objects in a HashSet. The class should be able to display all of the instructor in the set and allow the user to search for an instructor. Demonstrate the class in an application.


So the code from that project was:

/**
This class stores data about an instructor.
*/

public class Instructor
{
	private String lastName; //last name
	private String firstName; //first name
	private String officeNumber; //office number
	
	/**
	Constructor initializes the last name,
	first name, and office number.
	@param lname the instructor's last name
	@param fname the instructor's first name
	@param office the office number
	*/
	
public Instructor(String lname, String fname, String office)
{
	lastName = lname;
	firstName = fname;
	officeNumber = office;
}

/**
Constructor initializes object as a copy of another instructor object.
@param object2 the object to copy
*/

public Instructor(Instructor object2)
{
	lastName = object2.lastName;
	firstName = object2.firstName;
	officeNumber = object2.office;
}

/**
The set method sets a value for each field
@param lname the instructor's last name
@param fname the instructor's first name
*/
public void set(String lname, String fname, String office)
{
	lastName = lname;
	firstName = fname;
	officeNumber = office;
}

/**
toString method
@return a string containing the instructor's information
*/

public String toString()
{
	//create a string representing the object
	String str = "Last Name: "+ lastName +
	"\nFirst Name: " + firstName + 
	"\nOffice Number: "+ officeNumber;
	
	//return the string
	return str;
}
}
}


I'm having trouble understanding what the assignments specs mean when it says "modify the instructor class by overriding the hashCode and equals method". What is that referring to? Neither of those methods are in the code above... Please help, thanks

Is This A Good Question/Topic? 0
  • +

Replies To: HashCode HashSet with objects question/problem

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10561
  • View blog
  • Posts: 39,083
  • Joined: 27-December 08

Re: HashCode HashSet with objects question/problem

Posted 25 October 2011 - 11:48 AM

They are inherited from the Objet class. You will have to override them, or re-define them in your Instructor class. When doing such, two Instructors such that a.equals©, a.hashCode().equals(c.hashCode()) should also return true. That is what your instructor is asking you to do.
Was This Post Helpful? 1
  • +
  • -

#3 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • View blog
  • Posts: 3,002
  • Joined: 30-January 11

Re: HashCode HashSet with objects question/problem

Posted 25 October 2011 - 11:50 AM

The hashCode and equals methods are inherited from the base class Object, and are present in every class.

To override the methods, simply create a method with the same signature as that in the Object class and provide your own implementation.

You should also read up on the equals, and hashCodemethods.

Good luck.
Was This Post Helpful? 1
  • +
  • -

#4 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

Re: HashCode HashSet with objects question/problem

Posted 27 October 2011 - 05:28 PM

Well I'm still stuck. I've been reading the chapter over and over trying to understand how to implement this, but it's just not working. I've been trying to use examples out of the book, but all im getting are errors..

can anyone post an example of what it should look like please?

cheers
Was This Post Helpful? 0
  • +
  • -

#5 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

Re: HashCode HashSet with objects question/problem

Posted 27 October 2011 - 05:36 PM

It should be something like...

        public boolean equals(Object obj){
         if(obj == this){
             return true;
         }
         if(obj == null || obj.getClass() != this.getClass()){


right?
Was This Post Helpful? 0
  • +
  • -

#6 Lusent  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 05-April 11

Re: HashCode HashSet with objects question/problem

Posted 27 October 2011 - 07:27 PM

I was able to resolve the problems.

I was not using the right syntax.

This guide helped a lot
http://www.angelikal...enericsFAQ.html

Thanks for your responses.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1