Computer Science APEA 15-100, Summer 2009
Homework 5
Due: Tue 7-Jul-2009 at 8:59am (email copy only)
(no late submissions accepted).
Read these instructions first!
- Start from this file: Hw5.java
- Be sure to include your name, your Andrew ID, and your section clearly on the top of each file in your assignment.
- For non-programming problems:
- Place all your solutions to the non-programming problems in a single
file named Hw5 (with whatever extension is appropriate for the format you
choose, such as Hw5.txt or Hw5.html, etc). You must use one of these
file formats: plain text (txt), or RTF, or HTML, or Word (doc, not docx), or
PDF. No other file formats will be accepted.
- Show your work. Correct answers without supporting
calculations will not receive full credit.
- For programming problems:
- Try to use well-named variables, proper indenting, reasonable commenting,
etc.
- Note: You may not use Java concepts we have not yet covered,
including loops (do/while/for),
conditionals
("if" statements or tertiary operators (?:)), 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.
- What to submit
- Create a submission directory like this: "koz-hw5" (replace
"koz" with your andrew id)
- Place all the files you are submitting in that directory, zip that
directory, and submit the zipped directory
- How to submit
- Send an email with the zipped submission directory as an attachment
to your CA by the submission deadline. Do not miss the deadline,
even by one minute! Email problems are not a valid excuse
for late submissions.
- It is recommended
that you "cc" yourself in that email, too, just to confirm that you properly
sent the email.
- Note:
- Improper submissions will be penalized up to 10 points and may be
rejected.
- Late submissions will be rejected.
- Note: Conditionals are allowed from this assignment
onwards.
- Reading
Finish reading Chapter 2 carefully. Note that the next quiz
will cover all of
Chapter 2 (and then some!).
- Finish Lab 5
Finish all the examples from lab 5. Include the solutions in your
submission, all in one file named Lab5.java. Note: the lab collaboration
policy continues to apply to the lab problems. So you can collaborate at
will to solve these problems. This only applies to the lab problems -- all
other problems in this assignment are subject to the usual homework
collaboration policy.
-
digitAt
Write the following method:
public
static int digitAt(String
s, int index)
This method takes a String and an index and returns the integer value of the
digit at the given index. For example, given "84+23" and
index 0, the method returns the value 8. If the index is
not in bounds or there is not a digit at that location of the String, the
method should return -1.
Hint: you should not use the number 48 in your solution.
Instead, use '0', which means "the Unicode value of the character '0'",
which happens to be 48.
Here is a test method for you:
public static void
testDigitAt() {
System.out.print("Testing
digitAt... ");
assert(digitAt("84+23",-1) == -1);
assert(digitAt("84+23",
0) == 8);
assert(digitAt("84+23",
1) == 4);
assert(digitAt("84+23", 2) == -1);
assert(digitAt("84+23",
3) == 2);
assert(digitAt("84+23",
4) == 3);
assert(digitAt("84+23", 5) == -1);
System.out.println("Passed
all tests!");
}
- quadrantOfIntersection
Write the following method:
public static int
quadrantOfIntersection(double m1, double b1, double m2, double b2)
This method takes four doubles representing the two lines y=m1*x+b1
and y=m2*x+b2 and returns an int value representing the quadrant where these
two lines intersect, or -1 if they do not intersect in a single point
(either because they are parallel or they are in fact the same line).
Quadrants are numbered normally, so 1 is the top-right, 2 top-left, 3
bottom-left, and 4 bottom-right. If the two lines intersect at (or
"very nearly" at) the origin, you should return a 0. If the intersect
directly on the x or y axis, but not at the origin, do something reasonable
(your choice, just be consistent and document your design). Also, you
should write your own test method for this problem:
public static void
testQuadrantOfIntersection()
Be thoughtful about your test cases, trying to test all the different
conditions that might arise. You will be graded both on your solution
and also on the completeness/robustness of your test cases.
Note: you do not need to use trigonometry to solve this problem!
- IsFibonacciNumber
Write the following method:
public static boolean
isFibonacciNumber(int n)
This method takes an integer n and returns true if it is a Fibonacci
number and false otherwise, where the Fibonacci numbers are obtained by
summing the two previous numbers, as such: 1, 1, 2, 3, 5, 8, 13,
21,... While there are several ways to solve this problem, your method
must use this approach: it turns out that for every Fibonacci number
z, one or both of 5z2+4 or 5z2-4 is a perfect square.
For example:
1: 5(1)2-4 = 5-4 = 1 = 12.
Also, 5(1)2+4 = 5+4 = 9 = 32.
2: 5(2)2-4 = 20-4 = 16 = 42.
Though, 5(2)2+4 = 20+4 = 24 (not a perfect square).
3: 5(3)2-4 = 45-4 = 41 (not a perfect
square). But, 5(3)2+4 = 45+4 = 49 = 72.
5: 5(5)2-4 = 125-4 = 121 = 112.
Though, 5(5)2+4 = 125+4 = 129 (not a perfect square).
Fascinatingly, it is also true that this pattern is only true for the
Fibonacci numbers!!! For example:
4: 5(4)2-4 = 80-4 = 76 (not a perfect
square). Also, 5(4)2+4 = 80+4 = 84 (not a perfect square).
6: 5(6)2-4 = 180-4 = 176 (not a perfect
square). Also, 5(6)2+4 = 180+4 = 184 (not a perfect
square).
So? With a little thought, we can use this amazing fact as a quick
test to determine if a number n is in fact a Fibonacci number. Simply
check whether 5n2+4 or 5n2-4 is a perfect square!
This is the approach your method must use. It must also call a
helper method, isPerfectSquare (which was covered in lab5), which takes
an integer and returns true if and only if it is a perfect square.
Also, you should write your own test method for this problem:
public static void
testIsFibonacciNumber()
Again, be thoughtful about your test cases, trying to test all the
different conditions that might arise. You will be graded both on your
solution and also on the completeness/robustness of your test cases.
Carpe diem!