Computer Science 15-100 (Sections T & U), Fall 2007
Homework 4
Due: Fri 28-Sep-2007 at 8:30am (email copy) and at recitation (physical
copy)
- Be sure to include your name, your Andrew ID, and your section (T or
U) clearly on the top of your assignment (both written and electronic)!
- We will use the same submission process this week. One
electronic copy and an identical physical copy should be submitted.
- Remember that you are also being graded on style!
- Note that you do not have to worry about overflow for any of these
exercises.
- Also note: for full credit, your output must match the given
output exactly.
- Important Note: you must use exactly the signatures given here.
These programs will be automatically graded (at least in part -- your CA's
will still look at all your code for style and correctness!), and the robot
grader will not be able to grade your work unless you use these signatures
as given. Using the wrong signatures will result in a 50% penalty on
each exercise where this occurs.
- Also: include all your non-graphics exercises in a single file,
Hw4.java, with a single public class, Hw4. All your non-graphics
methods must be placed in that one class in that one file. For
graphics exercises, create the Hw4Graphics.java file as described below.
- Your electronic submission will include both the Hw4.java and the
Hw4Graphics.java files.
- Write a method with this signature:
public static boolean isPrime(int n)
This method takes an integer and returns true if that integer is prime and
false otherwise.
Also include a method with this signature:
public static void checkIsPrime()
This method uses your isPrime() method and should work like this:
Enter an integer:
12
12 is not prime.
And like this:
Enter an integer:
11
11 is prime.
Of course, your program should work for any legal input!
Remember to exactly match the output!
Also note that 1 is not prime, so 2 is the smallest prime number.
Finally, note that you do not have to be clever in your prime test.
Your for loop can run from 2 to n-1 and need not only check odd numbers.
If it works, you get full credit on this assignment, regardless of its
efficiency.
- Write a method with this signature:
public static int sumOfProperDivisors(int n)
This method takes a
positive integer and returns the sum of that integer's proper divisors.
These are the integers between 1 (inclusive) and n (exclusive) that evenly
divide n. So, for example, the proper divisors of 6 are 1, 2, and 3
(which sum to 6), and the proper divisors of 8 are 1, 2, and 4 (which sum to
7).
Also include a method with this signature:
public static void checkSumOfProperDivisors()
This method uses
your sumOfProperDivisors() method and should work like this:
Enter
a positive integer: 8
The sum of
the proper divisors of 8 is 7.
And like this:
Enter an integer:
6
The sum of the proper divisors of 6 is
6.
- Write a method with this signature:
public static boolean isPerfect(int n)
This method takes a
positive integer and returns true if that number is perfect and false
otherwise, where a perfect number is a number that equals the sum of its
proper divisors. As you hopefully surmised, this method must use your
sumOfProperDivisors() method from above.
Also include a method with this signature:
public static void checkIsPerfect()
This method uses your
isPerfect() method and should work like this:
Enter
a positive integer: 8
8 is not
perfect.
And like this:
Enter an integer:
6
6 is perfect.
- Write a method with this signature:
public static boolean hasZeros(double a, double b, double c)
This
method takes three doubles a, b, and c, which describe the quadratic
equation f(x) = ax2 + bx + c, and returns true if this
parabola crosses the x axis -- that is, if it has any zeros (values where
f(x) equals 0). Recall that a quadratic equation has at least one zero
if its discriminant is non-negative, where the discriminant is (b2
- 4ac).
Also include a method with this signature:
public static void checkHasZeros()
This method uses your hasZeros()
method and should work like this:
Enter
three doubles, a, b, and c: 8 2 1.5
The equation f(x) = 8.0x^2 + 2.0x + 1.5 does NOT have any zeros.
And
like this:
Enter
three doubles, a, b, and c: 0.5 2 2
The equation f(x) = 0.5x^2 + 2.0x + 2.0 DOES have at least one zero.
4B: Bonus (optional): Change your UI to print the
numbers in a more natural way so that integer-valued doubles do not include
the decimal, as such:
Enter
three doubles, a, b, and c: 8 2 1.5
The equation f(x) = 8x^2 + 2x + 1.5 does NOT have any zeros.
- Write a method with this signature:
public static double smallerZero(double a, double b, double c)
This method takes three doubles a, b, and c, which describe the quadratic
equation f(x) = ax2 + bx + c which is guaranteed to have at
least one zero, and returns the smaller of the one or two values of x
where f(x) equals 0. To do this, you will use the quadratic formula,
and replace the +/- with minus (this is how you get the smaller zero).
Also include a method with this signature:
public static void checkSmallerZero()
This method uses both
your hasZeros() method and your smallerZero() method and should work
like this:
Enter
three doubles, a, b, and c: 8 2 1.5
The equation f(x) = 8.0x^2 + 2.0x + 1.5 does NOT have any zeros.
And
like this:
Enter
three doubles, a, b, and c: 0.5 2 2
The equation f(x) = 0.5x^2 + 2.0x + 2.0 has a zero at x = -2.000.
Note that you should print the value of the zero rounded to the nearest
thousandth.
Note: If you did the bonus for 4b, then you can use that form for the
output here, as in:
Enter
three doubles, a, b, and c: 0.5 2 2
The equation f(x) = 0.5x^2 + 2x + 2 has a zero at x = -2.000.
This
does not get bonus here (you got that already!), but also does not get
penalized.
- Write a method with this signature:
public static boolean isPalindrome(String s)
This method takes a
non-null string and returns true if it is a palindrome, and false otherwise,
where a palindrome is the same backwards as forwards. To solve this,
you must use this approach: using a for loop, run an index from
0 to s.length()-1. For each value of the index, use charAt to compare
the character at that index with the character that far from the end.
If they differ, return false, because the string is not a palindrome.
If you complete the for loop, then return true.
Also include a method with this signature:
public static void checkIsPalindrome()
This method uses your
isPalindrome() method and should work like this:
Enter
a string: abba
The string abba IS a
palindrome!
And like this:
Enter
a string: abbab
The string abbab IS NOT a
palindrome!
- Write a method with this signature:
public static String getTime(String city)
This method takes a
non-null string that is a city name and returns a string containing the
current time in that city. Note that the city must be one of:
"Boston", "Pittsburgh", "Chicago", "Denver", or "Seattle", and your
method must be case-insensitive (so "BOSTON" or "boston" must also work).
To solve this, get the current time (as described below), which will be the
time in Pittsburgh. Then, subtract one hour for Chicago, two hours for
Denver, or three hours for Seattle. Be careful, though, as this can
change am to pm or vice versa. For example, if it is 2pm here in
Pittsburgh, it is 11am in Seattle.
How do you get the current time? To do that, you need to use the
GregorianCalendar class that Java supplies. First, you need these
imports:
import java.util.Calendar;
import java.util.GregorianCalendar;
Next, use the following code to get the current hour (in 12-hour format) and
minute and am-or-pm value for your current locale (which is presumed to be
Pittsburgh):
int hour12 = cal.get(Calendar.HOUR);// 0..11
int min = cal.get(Calendar.MINUTE); // 0..59
int ampm = cal.get(Calendar.AM_PM); // 0=AM, 1=PM
Now it's on to constructing the time string. For this, you must use
String.format to construct the string. Make the hour and minute both
have 2 digits always, with a leading zero if the number is less than 10.
Here are some sample formats as expected:
10:53am
05:02pm
12:01am
Also include a method with this signature:
public static void checkGetTime()
This method uses your getTime()
method and should work like this:
Enter
a city (one of Boston, Pittsburgh, Chicago, Denver, or Seattle): Boston
The current time in Boston is 06:21pm.
And like this:
Enter
a city (one of Boston, Pittsburgh, Chicago, Denver, or Seattle): CHICAGO
The current time in CHICAGO is 05:21pm.
And like this:
Enter
a city (one of Boston, Pittsburgh, Chicago, Denver, or Seattle): Erie
Sorry, but Erie is not in our time zone database.
- Rewrite your graphics programs from last week. To do this, copy
BasicGraphics.java and rename it to Hw4Graphics.java (be sure to also rename
the class name in the file to Hw4Graphics). Then, draw all 3 of the
Panama, Grenada, and European Union flags in the same window,
arranged as such:


Note that you must include a method with this signature:
public static Polygon makeStar(int centerX, int centerY, int outerRadius)
This method creates and returns a new Polygon object representing a
5-pointed star with the given center and outerRadius. You must use a
for loop to construct the star. Then, add this method:
public static void fillStar(int centerX, int centerY, int outerRadius,
Graphics page)
This method takes the same arguments as makeStar, plus a "page" object.
It calls makeStar() to make the star, and then calls the star's "fill"
method (which requires the "page" argument) to actually paint the filled
star. Note that all your stars must be drawn using this fillStar
method
Note: for top-down design (which we will discuss in more detail soon),
you should supply methods that draw each of the flags, as such:
public static void drawPanamaFlag(int left, int top, int width, int height,
Graphics page)
public static void drawGranadaFlag(int left, int top, int width, int height,
Graphics page)
public static void drawEuropeanUnionFlag(int left, int top, int width, int
height, Graphics page)
Your outermost paint method will call each of these methods (providing
appropriate left/top/width/height bounds to properly place the flags as
noted above).
Note: when possible, you must use for loops to arrange your stars.
In particular, the horizontal stars in the Panama flag must use a for loop,
and all the stars in the European Union flag must also use a for loop.
Note: You should borrow liberally from your own code from the previous
assignment, making this a relatively easy question. The point of this
question, especially combined with last week's version, is to firmly
establish how convenient and powerful for loops and methods are.
- BONUS (optional): Another Flag
Add another flag to the lower-right corner of your previous problem.
The more interesting/artistic/challenging the flag, the more bonus you will
receive, up to 5 points (or 1/2 letter grade). To receive 5 points,
though, you would have to be truly inspired! But a few points should
be relatively easy to obtain with reasonably interesting work. To find
flags, check out the CIA World Factbook's
Flags of the World page.
Carpe diem!