Problem Set #1: Getting Comfy with Java

The following are Python programs that do some stuff. Try to translate these programs into Java code. Remember that Java is object-oriented, which means you must use classes to encapsulate your methods and instance variables.

  • This program prints numbers 1 through 20. Next, for any given integer x, print numbers 1 through x. When you're done, try using a while loop instead and putting the Java code in a method called printXNumbers.
for i in range(1, 21):  
    print i
  • This program prints pairs of jazz songs.
jazzSongs = ["Strange Fruit", "Lush Life",  
             "God Bless the Child", "How High the Moon",
             "Mack the Knife (in Berlin)", "At Last",
             "What a Wonderful World", "My Fun Valentine",
             "Girl from Ipanema", "Fever"]
jzLen = len(jazzSongs)  
for i in range(0, jzLen, 2):  
    print "%s <=> %s" % (jazzSongs[i], jazzSongs[i+1])
# for i in range(jzLen / 2):
#    print "%s <=> %s" % (jazzSongs[i], jazzSongs[-i-1])
  • Some more questions possibly made up on the fly...