5 Replies - 603 Views - Last Post: 20 September 2013 - 01:23 PM Rate Topic: -----

#1 drocktapiff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-September 13

HELP... Accessing elements in Array of classes..

Posted 20 September 2013 - 11:03 AM

Hi guys okay so i have a contact manager assignment and I'm having some difficulties,
I'll give you the run down on exactly what we have to do, what i've done and where I'm having trouble.
Okay so we have to make a contact list, and make different classes one for the contact(name,middle,last,phone#,socialnetworking site,address) and pretty much do the same for two other classes for address and phone number. I've done that thus far, although he wants us to be able to add a contact so this is my code for adding a contact thus far.
        private String firstName;
	private String middleName;
	private String lastName;
	private Address[] addressList;
	private PhoneNumber[] phoneList;
	private SocialNetwork[] socialList;
	private int maxContact = 10;
	
	
	public void addContact(String firstName, String middleName, String lastName, Address[] addressList, PhoneNumber[] phoneList, SocialNetwork[] socialList){
		this.firstName = firstName;
		this.lastName = lastName;
		this.middleName = middleName;
		this.addressList = addressList;
		this.phoneList = phoneList;
		this.socialList = socialList;
		
		String checkfirstName;
		private void checkContact(String firstName, String middleName, String lastName){



What I'm having trouble doing is making sure two contacts are the same, so he wants us to make verify theres no duplicates. I've started a for loop but im not necessarily sure if thats the right thing to do... I need help,
I've tried doing if this.firstName.equals( <- thats where i dont know to finish .... im going crazy, please help..

Is This A Good Question/Topic? 0
  • +

Replies To: HELP... Accessing elements in Array of classes..

#2 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: HELP... Accessing elements in Array of classes..

Posted 20 September 2013 - 11:10 AM

I suggest you write an equals() method for your Contact class, then use it to determine if a new contact object is the same as an already existing object. You may have criteria that allows a new object to be "equal" to an existing, even if all fields are NOT the same, suggesting the new object is an update. If a possible update, you may want to give the user the option to update the existing with any new information contained in the proposed new object or continue adding the new object as different.

You can find a lot about writing an equals() method by searching something like, "java equals method".
Was This Post Helpful? 1
  • +
  • -

#3 drocktapiff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-September 13

Re: HELP... Accessing elements in Array of classes..

Posted 20 September 2013 - 11:14 AM

View PostGregBrannon, on 20 September 2013 - 11:10 AM, said:

I suggest you write an equals() method for your Contact class, then use it to determine if a new contact object is the same as an already existing object. You may have criteria that allows a new object to be "equal" to an existing, even if all fields are NOT the same, suggesting the new object is an update. If a possible update, you may want to give the user the option to update the existing with any new information contained in the proposed new object or continue adding the new object as different.

You can find a lot about writing an equals() method by searching something like, "java equals method".


Well in this program there is no userinput we pretty much make up the contacts ourselves, but overriding and equals method does sound like a good idea, so doing a equals method within my addContact method? But my only question is how would I compare this.firstName, with another within the array of contacts?.. thank you so much by the way.
Was This Post Helpful? 0
  • +
  • -

#4 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: HELP... Accessing elements in Array of classes..

Posted 20 September 2013 - 11:25 AM

The equals method would be called thusly:

boolean result = firstContact.equals( secondContact );

where firstContact and secondContact are both Contact objects, and the result is a boolean returned by the equals() method. firstContact and secondContact may be elements of a Contact array[] so that the call might look like:

boolean result = contactArray[0].equals( contactArray[1] );

which will determine if the second element of the Contact array[] is the same as the first. However, I recommend you determine equality before adding the object to the array. Why check afterwards? That's messy and complicated.

Then, accessing the fields of the two objects in the equals() ignores that they may be elements of an array. It's irrelevant. The comparison of the two object's fields in the equals() method will look something like:

if ( this.firstName.equals( object2.firstName ) {}

and so on for the comparisons you do to determine equality.
Was This Post Helpful? 0
  • +
  • -

#5 drocktapiff  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 19
  • Joined: 20-September 13

Re: HELP... Accessing elements in Array of classes..

Posted 20 September 2013 - 11:30 AM

View PostGregBrannon, on 20 September 2013 - 11:25 AM, said:

The equals method would be called thusly:

boolean result = firstContact.equals( secondContact );

where firstContact and secondContact are both Contact objects, and the result is a boolean returned by the equals() method. firstContact and secondContact may be elements of a Contact array[] so that the call might look like:

boolean result = contactArray[0].equals( contactArray[1] );

which will determine if the second element of the Contact array[] is the same as the first. However, I recommend you determine equality before adding the object to the array. Why check afterwards? That's messy and complicated.

Then, accessing the fields of the two objects in the equals() ignores that they may be elements of an array. It's irrelevant. The comparison of the two object's fields in the equals() method will look something like:

if ( this.firstName.equals( object2.firstName ) {}

and so on for the comparisons you do to determine equality.




Okay so this is what i have so far and i have a bunch of errors to work out, but I'm just curious am I on the right path? sorry to bother again..
private void checkContact(String firstName, String middleName, String lastName){
			for(int i = 0; i < maxContact; i++){
				ContactManager[(i)].Contact(firstName) = F;
				ContactManager[(i)].Contact(middleName) = M;
				ContactManager[(i)].Contact(lastName) = L;
				
					private boolean equals(String F, String M, String L){
						if (this.firstName.equals(F) && this.middleName.equals(M) && this.lastName.equals(L)
							return false
					}


Was This Post Helpful? 0
  • +
  • -

#6 GregBrannon  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2250
  • View blog
  • Posts: 5,340
  • Joined: 10-September 10

Re: HELP... Accessing elements in Array of classes..

Posted 20 September 2013 - 01:23 PM

You've either gone in a different direction or I just don't understand what you're up to. What does checkContact() do? Why have you (apparently) defined the method equals() inside another method? (That's not allowed.)

Maybe what you posted was thinking out loud, but I'm still curious what checkContact() is supposed to do.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1