Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

im so confused. im trying to write a program in java. however, he made a due the

ID: 3637895 • Letter: I

Question

im so confused. im trying to write a program in java. however, he made a due the same day as a quiz. i need someone to do this for me i'll greatly appreciate it. its all done in eclipse. He sent this:

Create a Project and a class called StringMethods2, according to the specification.
NOTE: You may NOT use the lastIndexOf method from the String class.

SPECIFICATIONS FOR PROGRAM:

--Constructor Detail--
StringMethods2

public StringMethods2()
--------------------------------------------
--Method Detail--
isAllDigits

public static boolean isAllDigits(String s)

Method to determine if a string is all digits

Parameters:
s - the String to test
Returns:
true if all digits; false otherwise. An empty String is not all digits.

isTelephoneNumber

public static boolean isTelephoneNumber(String s)

Determines if a string is a proper telephone number in the format (770)-423-6039

Parameters:
s - String to test
Returns:
true if properly formatted telephone number; false otherwise

lastIndexOf

public static int lastIndexOf(String s,
char desiredChar)

Find the position of the last occurance of a character in a String

Parameters:
s - String to search
desiredChar - the desired character
Returns:
index of last occurance of the desired char in the String; otherwise -1
Throws:
IllegalArgumentException - if the String is empty

Explanation / Answer

Hi, I tried answering all your method questions - and without using the lastIndexOf method from the String class, I did use others though charAt() and Character .isDigit() mainly :) I put plenty of comments explaining the choices made in each method, so the code should be pretty easy to understand. Don't just copy-paste stuff and expect it to work, you really need to give it a read and test it a couple of times if you can to really get what's giong on - it'll help with the quiz too :) If anything is missing or not clear, send me a PM or comment on this response and we can look at it again. I hope this helps, pelase remember to rate :) Good luck! public class StringMethods2 {
   
    // constructor
    public StringMethods2()
    {
        // nothing here for now ... ?
        // unless you make it like StringMethods1 constructor
    }
   
    // the 3 static methods to implement
    public static boolean isAllDigits(String s)
    {
        if(s == null)
            return false;
        if(s.isEmpty())
            return false;
       
        // check if each character is a digit
        // as soon as tou find one that isn't, return false
        // else, if you make it through the whole string
        // then it's all digits, so return true at the end
        for(int i = 0; i < s.length(); i++)
        {
            if( Character.isDigit(s.charAt(i)) == false )
                return false;
        }
        return true;
    } // end of isAllDigits method
   
    public static boolean isTelephoneNumber(String s)
    {
        // return true if formatted like (770)-423-6039
       
       
        if(s == null)
            return false;
        if(s.isEmpty())
            return false;
       
        if(s.length() != 14) // 10 for digits + 4 for () - -
            return false;
       
       
        // check that () -- exist and in their proper positions
        // check that the rest are all digits
        // as soon as one of these isn't what it's supposed to be
        // return false
        // otherwise, if all is well the loop keeps going until done
        // and at the end it returs 'true'
        for(int i = 0; i < s.length(); i++)
        {
            if(i == 0) // check for (
            {
                if(s.charAt(i) != '(')
                    return false;
            }
            if(i == 4) // check for )
            {
                if(s.charAt(i) != ')')
                    return false;
            }
            if(i == 5 || i == 9) // check for '-'
            {
                if(s.charAt(i)!='-')
                    return false;
            }
            else // the others should be all digits
            {
                if(Character.isDigit(s.charAt(i)) == false)
                    return false;
            }
        } // end of for-loop
       
        return true;
    } // end of isTelephoneNumber method
   
    public static int lastIndexOf(String s, char desiredChar)
    {
        int index = -1;
        if(s.isEmpty())
            throw new IllegalArgumentException("String cannot be empty!");
       
        // loop through the string, everytime you find a match
        // for the character, replace the index by the current
        // one - at the end of the loop, you'll haev the last
        // index position of the character, or, if no matches were
        // found, it'll keep its initialization value of -1 and be
        // returned as it is
        for(int i = 0; i < s.length(); i++)
        {
            if(s.charAt(i) == desiredChar)
                index = i;
        }
       
        return index;
    }// end of lastIndexOf method } // end of StringMethods2 class