Results 1 to 9 of 9
Thread: Help with Arrays
- 07-01-2012, 11:54 PM #1
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
Help with Arrays
I have the following requirement to create a new equals method to compare arrays.
I have a new class called SimpleString the constructors looks like this.
public SimpleString(String aString)
{
super();
int len = aString.length();
theLetters= new char[len];
for (int i=0; i < len; i++)
{
theLetters[i]= aString.charAt(i);
System.out.print(theLetters[i]);
}
}
public SimpleString(char[] aString)
{
super();
int len = aString.length;
theLetters= new char[len];
for (int i=0; i < len; i++)
{
theLetters[i]= aString[i];
System.out.print(theLetters[i]);
}
}
I have to write a method called equals that when called as of below
new SimpleString("test").equals(new SimpleString("test"));
will return true or false.
This is what I have so far but I do not understand how to compare the characters of the SimpleString object arugement with the receiver I assume both of which are created in the theLetters array of the constructor above. This is what I have so far but it returns false everytime
new SimpleString("test").equals(new SimpleString("test"));
false
What changes should I make to this, I have highlighted where I think the comparison is failing?
public boolean equals (SimpleString[] input)
{
boolean match=true;
int i=0;
if (input.length==this.theLetters.length)
{
while ((i < input.length)&&(match))
{
if (!input[i].equals(this.theLetters[i]));
{
match = false;
}
i++;
}
}
else
{
match=true;
}
return match;
}
- 07-02-2012, 12:18 AM #2
Re: Help with Arrays
Is the equals() method definition you posted from the SimpleString class?I do not understand how to compare the characters of the SimpleString object arugement with the receiver
Why does it take an array for its argument?If you don't understand my response, don't ignore it, ask a question.
- 07-02-2012, 08:43 AM #3
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
Re: Help with Arrays
This was the question
"Write a public instance method for the SimpleString class called equals().
This method takes one argument of type SimpleString and returns a boolean
value.
The method should first check that the length of the receiver is the same as the
length of the argument. If the lengths are not the same, the method should return
false. If the lengths are the same, the method should then iterate over the arrays
referenced by the receiver’s and the argument’s theLetters instance variables,
checking that each character, at each index, is the same in both arrays. If any pair
of characters is different the method should return false. If all pairs of characters
are the same the method should return true.
You can test your method by executing the following
statements, one statement at a time:
new SimpleString("test").equals(new SimpleString("test"));
new SimpleString("test").equals(new SimpleString("tent"));
The first statement should return true, the second false. "Last edited by DooScooby; 07-02-2012 at 10:39 AM.
- 07-02-2012, 10:57 AM #4
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
Re: Help with Arrays
Yes the array as an argument maybe incorrect, it is this I do not understand, should it just be (SimpleString input) I am not sure how to compare the two objects in the new SimpleString("test").equals(new SimpleString("test")); The SimpleString gets created as an array theLetters in the constructor but how are the two held before they are compared?
- 07-02-2012, 01:43 PM #5
Re: Help with Arrays
What is stored in the class when it is created?how to compare the two objects
The equals() method should compare what is stored in the class.
Please edit your post with the code and wrap the code in code tags.
http://www.java-forums.org/misc.php?do=bbcode#codeIf you don't understand my response, don't ignore it, ask a question.
- 07-02-2012, 01:52 PM #6
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
Re: Help with Arrays
I do not understand the question. What is stored in the class when it is created. The constructor is
I do not understand much of the point of the question but the Key point is belowJava Code:* Default constructor for objects of class SimpleString * The constructor that takes a single argument of type String. * The constructor creates an array of char which is the same size as the constructor’s argument * and assign it to theLetters. Copy all the letters from the argument into the array referenced by * theLetters, one at a time, and in the same order. */ public SimpleString(String aString) { super(); int len = aString.length(); theLetters= new char[len]; for (int i=0; i < len; i++) { theLetters[i]= aString.charAt(i); } }
In this question you are going to pretend that the Java String class doesn’t exist and you
therefore need to write your own, called SimpleString. This will be a bit of a cheat as we
will still make use of an instance of String in one of the new class’s constructors but it
should nevertheless give you some insight into how parts of the String class might be
implemented.
In writing your code for this question, the only String messages you are allowed to
use are charAt() and length(). In addition, you cannot make use of the methods
from the Arrays and Array utility classes.
This is the problem how do I compare the receiver and the argument without using any String functions?
Java Code:public boolean equals(SimpleString a) { boolean match =true; int len1 = a.length(); int len2 = this.theLetters.length; int i=0; if (len1 == len2) { while ((i<len1) && (match)) { if(???) { match =false; } i++; } } else { match = false; } return match; }Last edited by DooScooby; 07-02-2012 at 01:56 PM.
- 07-02-2012, 02:35 PM #7
Member
- Join Date
- Jul 2012
- Posts
- 5
- Rep Power
- 0
Re: Help with Arrays
I figured it out.
- 07-02-2012, 02:37 PM #8
Re: Help with Arrays
The equals() method must look inside of the a object to get to what is stored inside of it in the theLetters array.
There are two arrays, one in each object. Compare the contents of the local array against the contents of the array in the a object.
BTW a is a poor name for a variable. It makes it hard to discuss the code when there is a variable named: a. a is too common a word and makes for ambiguity.If you don't understand my response, don't ignore it, ask a question.
- 07-03-2012, 04:31 AM #9
Re: Help with Arrays
Why do they call it rush hour when nothing moves? - Robin Williams
Similar Threads
-
Copying Single Arrays to 2-D Arrays
By jmscarlet9 in forum New To JavaReplies: 7Last Post: 04-02-2012, 11:17 PM -
Casting Enum Type arrays to object type arrays
By nmvictor in forum Advanced JavaReplies: 4Last Post: 02-17-2012, 12:49 PM -
arrays and multidimensional arrays
By belfast09 in forum New To JavaReplies: 5Last Post: 06-14-2011, 01:28 PM -
store array of arrays in array of arrays
By joost_m in forum New To JavaReplies: 4Last Post: 04-19-2010, 10:32 AM -
Arrays.sort... why sorting all arrays in class?
By innspiron in forum New To JavaReplies: 6Last Post: 03-23-2010, 01:40 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks