Q BgQuestion:

      
Novice
Karma Points: 35
Respect (100%):
posted by  freekina191 on 11/2/2009 11:15:59 PM  |  status: Closed  |  Earned Karma: 35

java methods?

Course Textbook Chapter Problem Needs by
Software Design N/A N/A N/A 11/3/2009 at 2:00:00 PM
Question Details:
1. Fill in the code for the following class methods.
    1. The following method returns the position of the first occurrence of a given character in a given string. The position starts from 0 for the first character in the string. The method should return -1 if the character does not appear in the string. For example, the call indexOf('t',"iteration") would return 1, and the call indexOf('s',"iteration") would return -1. (You are not allowed to use the String indexOf method.)
      public static int indexOf(char c, String str)
      {
      // fill this in
      }
    2. Given two String parameters, the following method returns true if they are the same string of characters, and false otherwise. Note: You cannot use the equals String method; in other words, you actually have to write the code that compares the two strings character by character.
      public static boolean stringsAreEqual(String s1, String s2)
      {
      // fill this in
      }
IF YOU TRY AND PRACTICE ENOUGH, YOU TRULY CAN ACHIEVE ANYTHING!!
Bonus Point Alert! Earn +7 additional karma points for helping this gold member.

AAnswers:

Answer Question Ask for clarification
posted by Reena on 11/2/2009 11:54:21 PM  |  status: Live
Asker's Rating: Lifesaver   
Response Details:
Dear...
Here is the code...
1)    public static int indexOf(char c,String str)
    { 
          int i;
          for(i=0;i<str.length();i++
          {
             if(str.charAt(i)==c)
                return i;
          }
          return -1;
     }
2)    public static boolean stringsAreEqual(String s1, String s2)
     { 
        if(s1.equals(s2))
             return true;
        else
           return false;
     }
Hope this will help you...
Oracle
Karma Points: 18,830
posted by Marth on 11/3/2009 6:44:01 PM  |  status: Live
Asker's Rating: Helpful   
Response Details:
1.

public static int indexOf(char c, String str)
{
      for(int i = 0; i < str.length(); i++)
      {
            // char found
            if(str.charAt(i) == c)
            {
                  return i;
            }
      }
      // char not found
      return -1;
}

2.

public static boolean stringsAreEqual(String s1, String s2)
{
      // check length first
      if(s1.length() != s2.length())
      {
            return false;
      }

      // check each char in both strings
      for(int i = 0; i < s1.length(); i++)
      {
            // if the characters at the same index position do not match
            if(s1.charAt(i) != s2.charAt(i))
            {
                  return false;
            }
      }

      // all checks have been passed
      return true;
}


Thank you for rating my response. Feel free to PM me if you have further questions.

Java Genius

"Lois Lane is falling, accelerating at an initial rate of 32ft per second per second. Superman swoops down to save her by reaching out two arms of steel. Ms. Lane, who is now traveling at approximately 120 miles per hour, hits them, and is immediately sliced into three equal pieces." - Sheldon
Answer Question Ask for clarificarion

Join Cramster's Community

Cramster.com brings together students, educators and subject enthusiasts in an online study community. With around-the-clock expert help and a community of over 100,000 knowledgeable members, you can find the help you need, whenever you need it. Join for free today » How Cramster is different from tutoring »