Puzzler.java

You are to reproduce the behavior of this Python program in Java using String methods.


##This program has only a main routine.  You will see 
#all of the problems solved in Python.  Your job is to make
#the program Puzzler.java, put a main method in it, and
#translate puzzler.py into Puzzler.java.  Use the String 
#documentation to solve these problems.
def main():
    s = "caterpillar"
    ##print a string containing the first and last letters of s
    print(s[0] + s[-1])
    ##print the substring between teh first and last 'a' in s.
    start = s.find("a") + 1
    end = s.rfind("a") 
    print(s[start:end])
    ##print s in upper-case
    print(s.upper())
    ##print True if "cat" is a substring of s.
    print("cat" in s)
    ##print False if "iar" is not a substring of s.
    print("iar" in s)
    t = "CaTerPIllAR"
    ## print True if s and t are the same string, case INsensitive
    #do this TWO different ways.
    print(s.upper() == t.upper())
    #print 50 stars
    print("*"*50)
    ##print u  with all whitespace removed from the right.
    u = "            paddded               "
    print(u.rstrip())
    ##print u  with all whitespace removed from the right.
    print(u.lstrip())
    ##print u  with all whitespace removed both sides.
    print(u.strip())
    ##replace every a in s with an A and print.
    print(s.replace("a", "A"))
    ##replace "cat" in s with "dog"
    print(s.replace("cat", "dog"))

main()

You can download this file on the left. Also on the left is shell code for Puzzler.java. As you progress, replace the commented-out Python code with java code. Compile and run as you go! Early and often!