Results 1 to 8 of 8
Thread: what is wrong with my code
- 02-10-2011, 10:14 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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 10:22 PM.
- 02-10-2011, 10:44 PM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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; }
- 02-10-2011, 11:00 PM #3
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?
- 02-10-2011, 11:26 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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"
- 02-10-2011, 11:30 PM #5
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 02-10-2011, 11:51 PM #6
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); }
- 02-11-2011, 12:04 AM #7
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
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
- 02-11-2011, 05:10 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 75
- Rep Power
- 0
I have implement it may be with slight differnt way but i works
Main: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
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
-
What is wrong with this code?
By Mythreadings in forum New To JavaReplies: 38Last Post: 11-19-2010, 12:43 AM -
What's wrong with my code?
By Isong in forum AWT / SwingReplies: 1Last Post: 11-16-2010, 06:00 PM -
What Could be Wrong with This code????
By Manfizy in forum New To JavaReplies: 9Last Post: 08-22-2010, 11:28 AM -
what is wrong in dis code?
By jitun2004 in forum New To JavaReplies: 8Last Post: 04-15-2009, 09:30 AM -
what's wrong with this code?
By agenteleven in forum Advanced JavaReplies: 5Last Post: 10-07-2008, 11:26 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks