Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 Attached Thumbnails Help with Boolean and comparing?-assign2.jpg   Help with Boolean and comparing?-assign1.jpg  

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    Jan 2012
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you! Let's see if I can do this. :-)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •