Results 1 to 8 of 8
  1. #1
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default what is wrong with my code

    hi, in the following code i try to compare two objects from type string
    in other words , i try to implement a class that simulate (.equals)

    Java Code:
    public class Amr {
    
      private String word;
      
        public Amr(String str) 
            {
                setString(str);
            }
            
        private void setString (String str)
        {
            this.word = str;
        }
        
        public boolean IsPloyndrome(Amr string)
            {
              if ( this.getString().equals(string.getString()) )
                   return true;
              else 
                   return false;
            }
        
        public  Amr getString()
            {
              Amr obj = new Amr(word);
              return obj;
            }
    }

    main method:


    Java Code:
      public class Polyndrom
    {
        public static void main (String[] args)
        {
            Amr h = new Amr("hi");
            Amr b = new Amr("hi");
            
            if ( h.IsPloyndrome(b) )
                System.out.println ("equals");
                else System.out.println ("not equals");
        }
    }
    Last edited by amrmb09; 02-10-2011 at 11:22 PM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    You don't need a setter in the constructor, it has access to the private instance variables.

    getString also takes no argument, but uses an argument which isn't declared anywhere.

    Getters and Setters both also have access to private class variables

    in the comparison method you can also access the string field directly.

    so
    Java Code:
    getString() 
    
    should look like
    
    public String getString(){
      return word;
    }

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Why is your getString method returning a new Amr object? Since it does then your if statement becomes if Amr object equals Amr object. You don't have an equals method in your Amr class so it will use the equals method it inherits form object and 99.99999999% of the time it will not do what you want.

    So to cut a long story short, why doesn't your getString method just return word?

  4. #4
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default that's why

    many thanks for u all

    my getString doesnt return (word)
    beacuse i want to return an object

    i want to simulate the logic behind ".equals method"

  5. #5
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The way you are trying to do it requires you to override .equals() method. The getter should not work like that, try having it just return a string, then override .equals() which takes a Amr object as the argument and returns boolean, then you can compare the items however you want.

  6. #6
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by amrmb09 View Post
    many thanks for u all

    my getString doesnt return (word)
    beacuse i want to return an object
    I have several issues.

    The method is called getString but it returns an Amr and not a String.

    You say you want to return an object, well String is an object.

    To compare two Amr objects you call the getString method which creates a new Amr object each time it is called. You call it twice (once for each Amr object) and end up with 4 Amr objects just to compare if they contain the same String. A whole lot of nonsense. You code can be simplified to:
    Java Code:
    public boolean IsPloyndrome(Amr string)
        return word.equals(string.word);
    }

  7. #7
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    ohh, thank you so much
    i dont know why i have not though that simple
    anyway, i will apply this code and study it well

    many thanks for u all

  8. #8
    amrmb09 is offline Member
    Join Date
    Nov 2010
    Posts
    75
    Rep Power
    0

    Default

    I have implement it may be with slight differnt way but i works
    Java Code:
    public class Amr 
    {
    	private String word;
    
    			  public Amr(String text)
    			    {
    				    this.word = text;
    			    }
    			  public String getText()
    				{
    					return word;
    				}
      
    			  public boolean isEqual(Amr text)
    				{
    
    			  		return this.word.equals(text.getText());  		    
    				}
      
    			  public static boolean Ispoly(String str)
                    {
        	          int cnt=0,cnt2=0;
        	
        	            if ( str.length()%2 == 0 )
        		            {
    		                    for (int i=0;i<str.length()/2;i++)
        		     	             if  (str.charAt(i) == str.charAt( ((str.length()-1)-i) ) )
        			                          cnt++;
        	       
        	                         if ( cnt==str.length()/2 )     	        
        		                              return true;
        		                     else  
        		                              return false; 
        	                }//end if
        	
        	            else
        	    	        {    
    		                    for (int i=0;i<(str.length()-1)/2;i++)
    		    		   	         if  (str.charAt(i) == str.charAt( ((str.length()-1)-i) ) )
    			    			              cnt2++;
        	
        	                         if ( cnt2==(str.length()-1)/2 )     	        
        		                               return true;
        		                     else  
        		                     	       return false; 
        	                }//end else   
               
                     }// end ispoly
        
    }// end Class Amr
    Main:
    Java Code:
    public class Polyndrom
    {
    	public static void main (String[] args)
    	{
    		if ( Amr.Ispoly("abcddcba") )
    			 System.out.println ("polyndrom");
    			    else 
    			   	     System.out.println ("not polyndrom");
    			   	     
    	    Amr o1=new Amr("asd");
    	    Amr o2=new Amr("asd");
    	    
    	    if (o1.isEqual(o2))
    	    System.out.println ("equal strings");
    	        else   System.out.println ("not equal strings");
    	}
    }

Similar Threads

  1. What is wrong with this code?
    By Mythreadings in forum New To Java
    Replies: 38
    Last Post: 11-19-2010, 01:43 AM
  2. What's wrong with my code?
    By Isong in forum AWT / Swing
    Replies: 1
    Last Post: 11-16-2010, 07:00 PM
  3. What Could be Wrong with This code????
    By Manfizy in forum New To Java
    Replies: 9
    Last Post: 08-22-2010, 12:28 PM
  4. what is wrong in dis code?
    By jitun2004 in forum New To Java
    Replies: 8
    Last Post: 04-15-2009, 10:30 AM
  5. what's wrong with this code?
    By agenteleven in forum Advanced Java
    Replies: 5
    Last Post: 10-07-2008, 12:26 PM

Posting Permissions

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