Results 1 to 4 of 4

Thread: Need Help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Need Help

    How would I use the equal methods to compare my names. To tell if thier equal to each other or not equal to each other.

    Here is my Name.java and TestName.java

    Code:
    public class Name
    {
     	private String first;
    	private String middle;
    	private String last;
    	
    	public Name(String f, String m, String l){
    	first = f;
    	middle = m;
    	last = l;
    	
    	}
    	
    	public String getFirst(){
    	return first;
    	}
    	
    	public String getMiddle(){
    	return middle;
    	}
    	
    	public String getLast(){
    	return last;
    	}
    	
    	public String getFirstMiddleLast(){
    	return first + " " + middle + " " + last;
    	}
    	
    	public String getLastFirstMiddle(){
    	return last + ", " + first + " " + middle;
    	}
    	
    	public String getInitials(){
    	return first.substring(0, 1) + "." + middle.substring(0, 1) + "." + last.substring(0, 1) + ".";
    	}
    	
    	public int getLength(){
    	return first.length() + middle.length() + last.length();
    
    	}
    }
    Code:
    	public class TestName {
    		
    		public static void main (String[] args){
    			String First;
    			String Middle;
    			String Last;			
    			
    			Scanner scan = new Scanner(System.in);
    			
    			System.out.println("Please enter full name");
    
    			First = scan.next();
    			Middle = scan.next();
    			Last = scan.next();
    			
    			Name n1 = new Name(First, Middle, Last);
    													
    			System.out.println("Please enter full name2");
    			
    			First = scan.next();
    			Middle = scan.next();
    			Last = scan.next();	
    				
    			Name n2 = new Name(First, Middle, Last);
    					
    			System.out.println();
    			System.out.println("first : " + n1.getFirst());
    			System.out.println("middle : " + n1.getMiddle());
    			System.out.println("last : " + n1.getLast());
    			System.out.println("first-middle-last version : " + n1.getFirstMiddleLast());
    			System.out.println("last-first-middle version : " + n1.getLastFirstMiddle());
    			System.out.println("initials = " + n1.getInitials().toUpperCase());
    			System.out.println("length = " + n1.getLength());
    			System.out.println();
    			System.out.println("first : " + n2.getFirst());
    			System.out.println("middle : " + n2.getMiddle());
    			System.out.println("last : " + n2.getLast());	
    			System.out.println("first-middle-last version : " + n2.getFirstMiddleLast());
    			System.out.println("last-first-middle version : " + n2.getLastFirstMiddle());
    			System.out.println("initials = " + n2.getInitials().toUpperCase());
    			System.out.println("length = " + n2.getLength());
    			System.out.println();
    			
    			if(n1.equals(n2)){
    				System.out.println("They are equal");
    			}
    			else
    			{
    				System.out.println("They are not equal");
    			}
    
    	}
    }

  2. #2
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Need Help

    Code:
    if(n1.equals(n2)){
    This is calling the equals method of your Name class. You never made one... so you need to make an equals method in your Name class. It should take in a Name. Try that and if you have questions go ahead and ask.

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Sep 2006
    Posts
    19

    Re: Need Help

    It compiles, but it don't run like it supose to.

    Code:
    public class Name
    {
     	private String first;
    	private String middle;
    	private String last;
    	private String Name;
    	private String n1, n2;
    	
    	public Name(String f, String m, String l){
    	first = f;
    	middle = m;
    	last = l;
    	
    	}
    	
    	public String getFirst(){
    	return first;
    	}
    	
    	public String getMiddle(){
    	return middle;
    	}
    	
    	public String getLast(){
    	return last;
    	}
    	
    	public String getFirstMiddleLast(){
    	return first + " " + middle + " " + last;
    	}
    	
    	public String getLastFirstMiddle(){
    	return last + ", " + first + " " + middle;
    	}
    	
    	public String getInitials(){
    	return first.substring(0, 1) + "." + middle.substring(0, 1) + "." + last.substring(0, 1) + ".";
    	}
    	
    	public int getLength(){
    	return first.length() + middle.length() + last.length();
    	}
    	
    	public boolean equals(Name comp)
    	{
    		return Name.equals(comp.Name) && n1 == comp.n2;
    
    	}
    }

  4. #4
    Lively Member
    Join Date
    Dec 2005
    Posts
    68

    Re: Need Help

    Allright, you made some good progress. You made an equals method that takes in a Name as a parameter which is a great start.
    In order for two names to be the same, the first, middle, and last names stored in both Name Objects must be the same right?

    So, try something like this:

    Code:
    	public boolean equals(Name comp)
    	{
    		return comp.getFirst().equals(first) && 
                              comp.getMiddle().equals(middle) && 
                              comp.getLast().equals(last);
    	}
    This code checks to see if the first, middle, and last names of the incoming Name Object passed as a parameter are the same as the first middle and last names in the current object.

Posting Permissions

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



Click Here to Expand Forum to Full Width