Results 1 to 9 of 9
  1. #1
    DooScooby is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default 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;

    }

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with Arrays

    I do not understand how to compare the characters of the SimpleString object arugement with the receiver
    Is the equals() method definition you posted from the SimpleString class?
    Why does it take an array for its argument?
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DooScooby is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help with Arrays

    Quote Originally Posted by Norm View Post
    Is the equals() method definition you posted from the SimpleString class?
    Why does it take an array for its argument?
    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.

  4. #4
    DooScooby is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default 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?

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default Re: Help with Arrays

    how to compare the two objects
    What is stored in the class when it is created?
    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#code
    If you don't understand my response, don't ignore it, ask a question.

  6. #6
    DooScooby is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help with Arrays

    Quote Originally Posted by Norm View Post
    What is stored in the class when it is created?
    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.
    BB Code List - Java Programming Forum
    I do not understand the question. What is stored in the class when it is created. The constructor is
    Java 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);
    		
    		}
    		
    	}
    I do not understand much of the point of the question but the Key point is below
    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.

  7. #7
    DooScooby is offline Member
    Join Date
    Jul 2012
    Posts
    5
    Rep Power
    0

    Default Re: Help with Arrays

    I figured it out.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,578
    Rep Power
    25

    Default 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.

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,423
    Rep Power
    20

Similar Threads

  1. Copying Single Arrays to 2-D Arrays
    By jmscarlet9 in forum New To Java
    Replies: 7
    Last Post: 04-02-2012, 11:17 PM
  2. Casting Enum Type arrays to object type arrays
    By nmvictor in forum Advanced Java
    Replies: 4
    Last Post: 02-17-2012, 12:49 PM
  3. arrays and multidimensional arrays
    By belfast09 in forum New To Java
    Replies: 5
    Last Post: 06-14-2011, 01:28 PM
  4. store array of arrays in array of arrays
    By joost_m in forum New To Java
    Replies: 4
    Last Post: 04-19-2010, 10:32 AM
  5. Arrays.sort... why sorting all arrays in class?
    By innspiron in forum New To Java
    Replies: 6
    Last Post: 03-23-2010, 01:40 AM

Tags for this Thread

Posting Permissions

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