Go Back   CodingForums.com > :: Server side development > Java and JSP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-23-2012, 03:11 AM   PM User | #1
newnew
New to the CF scene

 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
newnew is an unknown quantity at this point
Exclamation Help with Boolean and comparing?

"Handling names and phone numbers is a common task of many products today including contact list software and cell phones. In this assignment, you will write the class for a PhoneEntry that provide functionality for these types of programs."

Honestly, I don't know a lot of the basics. I think I have most of it done, I just don't understand everything I commented out. I attached snap shots of the assignment file, I feel confident with the rest of the code where its not commented out, anyway... Help? And thanks!
Code:
public class PhoneEntry 
{
	private String name;
	private String phone;
	
	public PhoneEntry(String newName, String newPhone)
	{
		name = newName;
		phone= newPhone;
		
	}
	
	public PhoneEntry(PhoneEntry other)
	{
		//in here should be "copy the contents of the parameter 
		//to the current object" but I don't know what that means........ 
	}
	
	public String getPhone()
	{ 
		return phone;
	}
	
	public String getName()
	{
		return name;
	}
	
	public String toString()
	{
		String result = name + "   "+ phone;
		return result;
	}

	
	public Boolean equals(String name)
	{ 
		/*public Boolean equals (String name) – If the names in the objects are 
the same, return true, otherwise return false. The phone numbers are not 
compared.*/
		//if(name=="?")?........ what do i compare it against? 
			
			return true;
		
		else 
		
			return  false; 
		
				
			
	}
	
	public Boolean equals(PhoneEntry other)
	{
		/*public Boolean equals (PhoneEntry other) – If the names and the 
phone numbers in the objects match, return true, otherwise return false*/
		//don't know if this is right.....?
		if(name==phone)
			
			return true;
		else
			
			return false;
		
	}
		
	
	public void setPhone(String newPhone)
	{
		phone= newPhone;
	
	}
	
	
	
}

Here's the test code.
Code:
package assign1;

public class Assign1 {

	// main method to test each of the public methods in the PhoneEntry class
	
	public static void main(String[] args) {
		
		System.out.println ("Test 1: PhoneEntry (String, String) and toString");
		System.out.println ("Expecting Joe 123-4567");
		
		PhoneEntry entry1 = new PhoneEntry ("Joe", "123-4567");
		System.out.println (entry1);
		
		System.out.println ();
		System.out.println ("Test 2: PhoneEntry (PhoneEntry) and toString");
		System.out.println ("Expecting Joe 123-4567");
		
		PhoneEntry entry2 = new PhoneEntry (entry1);
		System.out.println (entry2);
	
		System.out.println ();
		System.out.println ("Test 3: getPhone()");
		System.out.println ("Expecting 345-6789");

		entry2 = new PhoneEntry ("Fred", "345-6789");
		System.out.println (entry2.getPhone());

		System.out.println ();
		System.out.println ("Test 4: getName()");
		System.out.println ("Expecting Fred");

		System.out.println (entry2.getName());
		
		System.out.println ();
		System.out.println ("Test 5: equals(String)");
		System.out.println ("Expecting true");
		
		System.out.println (entry1.equals("Joe"));

		System.out.println ();
		System.out.println ("Test 6: equals(String)");
		System.out.println ("Expecting false");
		
		System.out.println (entry1.equals("Fred"));

		System.out.println ();		
		System.out.println ("Test 7: equals(PhoneEntry)");
		System.out.println ("Expecting false");
		
		System.out.println (entry1.equals(entry2));

		System.out.println ();
		System.out.println ("Test 8: constructor 2 and equals(PhoneEntry)");
		System.out.println ("Expecting true");
		
		System.out.println (entry1.equals(new PhoneEntry (entry1)));

		System.out.println ();
		System.out.println ("Test 9: setPhone(String)");
		System.out.println ("Expecting 345-6789");
		
		entry1.setPhone(entry2.getPhone());
		System.out.println (entry1.getPhone());
		
		System.out.println ();
		System.out.println ("Test 10: alias");
		System.out.println ("Expecting Joe 567-4444");
		
		entry2 = entry1;
		entry1.setPhone("567-4444");
		System.out.println (entry2);
	}
}
Attached Thumbnails
Click image for larger version

Name:	assign2.jpg
Views:	53
Size:	46.5 KB
ID:	11199   Click image for larger version

Name:	assign1.jpg
Views:	43
Size:	41.5 KB
ID:	11200  
newnew is offline   Reply With Quote
Old 05-23-2012, 03:25 AM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,741
Thanks: 4
Thanked 2,465 Times in 2,434 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This:
Code:
	public PhoneEntry(PhoneEntry other)
	{
		//in here should be "copy the contents of the parameter 
		//to the current object" but I don't know what that means........ 
	}
Is a copy constructor. It allows you to clone an existing object without making the class cloneable. Simply set the values of "this" to the values provided in "other". I won't read the assignment itself, but unless it says otherwise, I would presume a shallow copy is sufficient.

Equals is incorrect. Its definition should be:
Code:
public boolean equals(Object other);
Unless specified otherwise. This will override the function equals (which means you really *should* override the hashcode as well), so any functionality that uses equals for comparisons will be able to function properly. Such things like collections will do this.
This is slightly trickier. Notice the definition is only of type Object to override (although I haven't checked in Java 7 if its generic or not), so this is what you have to do:
  1. Set boolean bResult to false
  2. If other instanceof PhoneEntry then
    1. Execute .equals on name, and phone number to compare the results. A simple ternary operation consisting of an && between the two comparisons for assignment. Assign result to bResult
  3. Return bResult

No need to overload the equals.

Edit:
Also worth a mention; boolean != Boolean. One is a primitive and another is a class wrapper.
Fou-Lu is offline   Reply With Quote
Old 05-23-2012, 06:28 AM   PM User | #3
newnew
New to the CF scene

 
Join Date: Jan 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
newnew is an unknown quantity at this point
Thank you! Let's see if I can do this. :-)
newnew is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:28 PM.


Advertisement
Log in to turn off these ads.