Computer Science 15-110, Lecture 9 (Sections M-Q), Fall 2009
Homework 4
Due: Thu 1-Oct-2009 at 11:59pm (email copy to your CA)
(no late submissions accepted).
Read these instructions first!
- The hw instructions from hw1 all apply here.
In particular:
- This is not a lab and this is not
collaborative!
- You must work alone on this assignment
(except for help from CA's or the course instructor, who you are always
welcome to consult).
- Submit all your source (.java) file(s) all in a single zipped folder,
hw4-<andrewId>.zip, sent by email as an attachment to your CA. For
example, I would submit hw4-koz.zip (you should replace "koz" with your
andrewId).
- Do not submit .java~ files (with the tilde
(~) at the end). These are DrJava backup files. They are not
useful.
- Do not submit .class files. These are
compiled bytecode files. They also are not useful.
- Do not submit project files or any other
files. Just .java files
and your one free-response file.
Programming guidelines:
- Style counts!!! Use well-named variables,
proper indenting, reasonable commenting, etc.
- Write test methods for every non-graphical
method you write!!!
- Note: You may use conditionals ("if" statements
or ternary operators (?:)) and loops. However, you may not use
Java concepts we have not yet covered, including arrays, or methods from any classes in java.util.* (besides Scanner or
others we explicitly use) to solve these problems (which isn't a problem for
most of you, seeing as we have not yet covered these!). While they may be
helpful, every problem here is solvable without them, and they are not
permitted for now.
- nthEmirpPrime
- mostCommonVowel
- "Loopy" Animation
-
Bonus/Optional:
longestCommonSubstringLength
-
Bonus/Optional: Simple
Invaders Game
- nthEmirpPrime
In the file Hw4.java, write the method:
public static int
nthEmirpPrime(int n)
This method takes an int n and returns the nth "Emirp prime",
where an Emirp prime is a prime number which becomes a different prime
number when its decimal digits are reversed. For example, 13 is an
Emirp prime because when we reverse the digits we get 31, which is also
prime. 2, 3, 5, 7, and 11 are all examples of primes that are not
Emirp primes because they are the same prime when the digits are
reversed. Here are the first several Emirp primes:
13, 17, 31, 37, 71, 73, 79, 97, 107, 113, 149, 157, 167, 179, 199,...
So if n==1, your method should return 13. If n==2, it should return
17. And so on. If n is less than 1, your method should return
-1.
Aside: in case you missed it, emirp is prime spelled
backwards. Also, we didn't make this up (look up "emirp" on
Wikipedia)!
- mostCommonVowel
In the file Hw4.java, write the method:
public static char mostCommonVowel(String s)
This method takes one string and returns the lowercase vowel ('a',
'e', 'i', 'o', or 'u') that occurs most often in the string, regardless of
case (so "Aha!" contains two a's). In the case of a tie, the first
vowel alphabetically is returned (so "Yippee, I say!" would return 'e', even
though it also has 2 i's). If the string is null or does not contain a
vowel, the method should return ((char)0), which is the character with
unicode value 0 (which no character has!).
- "Loopy" Animation
First, take a close look at the (updated)
class notes on nested loops and then, especially, the
class notes on nested loops in graphics. Compile that code, run
it, be sure you thoroughly understand it, because you're going to need it!
While you may not discuss your solutions to the homework, you may
discuss the class notes with each other, with your CA's (of course), with
anyone else. Do not start this problem until you understand those
notes very well!
Then, in the file
Hw4Animation.java, write this "loopy" animation:
Hw4AnimationDemo.jar
(also available in Hw4AnimationDemo.zip)
Download and run the demo program (Hw4AnimationDemo.jar), which shows
how this program should work once it is completed. Carefully review
the program so you do not omit any details (such as pausing, or resizing
with arrows, or clicking to move the selection).
Hint: divide-and-conquer! That is, solve this problem in small,
independent sub-problems. Also, use top-down design, which basically
means to solve each sub-problem using plenty of well-chosen helper methods.
-
Bonus/Optional:
longestCommonSubstringLength
In the file Hw4.java, write the method:
public static int
longestCommonSubstringLength(String s1, String s2)
This method takes two strings and returns the length of the longest
string s3 such that both s1 and s2 contain s3. If no such string
exists (say, if either s1 or s2 is null), then your method should return 0.
Note: your method must not create any new strings
(because that would be very inefficient!). The string "s3" in the
description is just to help you understand the problem. You may not
create such an "s3" in your solution, and in fact you should not have any
variables of type String except s1 and s2, which you should not modify.
Also: the only methods in the String class you may use are charAt()
and length().
-
Bonus/Optional: Simple
Invaders Game
In the file Hw4BonusInvaders.java, write this
simple version of a SpaceInvaders-like game:
Hw4InvadersDemo.jar
(also available in Hw4InvadersDemo.zip)
Download and run the demo program (Hw4SimpleInvadersGame.jar), which shows
how this program should work once it is completed.
Hint: you may want to use Math.random() here. Or you can read
about using the Random class. Your choice.
Carpe diem!