Computer Science APEA 15-100, Summer 2009
Lab 5
Read these instructions first!
- The homework collaboration policy does not apply to labs. In
labs, you may work alone or in small groups. And you may show each
other your code, and help each other in any manner. This is
encouraged, in fact!
- That said, the participation policy is still in effect -- you must
not only attend labs, but actually work during the labs. If you have
completed the assigned work, then you should either help others, if they
request it, or you should delve deeper into the assigned material.
But you must use the lab time exclusively to explore the material covered in
that lab.
- In particular, you may not use lab time to do your homework.
- There is nothing to submit for labs.
Note: You may not use Java concepts we have not yet covered, including loops
(do/while/for), conditionals ("if" statements or tertiary operators (?:),
for part 1 -- conditionals are ok for part 2),
arrays, or methods from any classes in java.util.* to solve these problems.
While they may be helpful, every problem here is solvable without them.
- Writing Methods without Conditionals
(continued)
- nthFibonacci
- dayOfWeek
- xIntercept
- xInterceptOfParabola
- Writing Methods with Conditionals
- isFactor
- isPerfectSquare
- isWeekday
- numberOfInterceptsOfParabola
- signum
- Writing Methods without Conditionals
(continued)
- nthFibonacci
class MyCode {
// This method takes an int n and returns the nth Fibonacci number.
// The Fibonacci numbers are 1, 1, 2, 3, 5, 8, 13, 21, 34, ....
// As you can see, each number is the sum of the previous two.
// From the great website mathforum.org, we see that the formula
// for the nth Fibonacci number is:
// phi^(n+1) / sqrt(5) - (1-phi)^(n+1) / sqrt(5)
// where phi is the Golden Ratio:
// phi = (1+sqrt(5))/2)
// Due to roundoff error of doubles, you may need to round the result,
// which can be done by addition and casting (truncation), or
// by a suitable Math method (though we've not covered that yet!).
public static int nthFibonacci(int n) {
return 42;
// replace this with your answer!
}
public static void testNthFibonacci() {
System.out.print("Testing nthFibonacci... ");
assert(nthFibonacci(0) == 1);
assert(nthFibonacci(1) == 1);
assert(nthFibonacci(2) == 2);
assert(nthFibonacci(3) == 3);
assert(nthFibonacci(4) == 5);
assert(nthFibonacci(5) == 8);
assert(nthFibonacci(6) == 13);
assert(nthFibonacci(7) == 21);
assert(nthFibonacci(8) == 34);
System.out.println("Passed all tests!");
}
public static void main(String[] args) {
testNthFibonacci();
}
}
- dayOfWeek
class MyCode {
// This method takes a date represented by three integers,
// the month (1-12), the day (1-31), and the year, and returns an
// integer representing the day-of-week for that date, where
// Sunday is 1, Monday is 2, and so on, and Saturday is 7.
// While there are several ways to do this, you must use
// this formula (from the most-excellent web site mathforum.org):
// N = d + 2m + [3(m+1)/5] + y + [y/4] - [y/100] + [y/400] + 2
// Then the remainder when you divide N by 7 is the day-of-week,
// where Saturday is 0 and Friday is 6. Note that these values for
// the days are not quite the same as those returned by this method.
// Here is mathforum's description of the formula:
// "d is the number or the day of the month, m is the number
// of the month, and y is the year. The brackets around the
// divisions mean to drop the remainder and just use the
// integer part that you get.
// Also, a VERY IMPORTANT RULE is the number to use for the
// months for January and February. The numbers of these months
// are 13 and 14 of the PREVIOUS YEAR. This means that to find
// the day of the week of New Year's Day [of 1998], 1/1/98,
// you must use the date 13/1/97."
// Note: you must make the adjustment to the month and year when
// appropriate. So, for example, the date of New Year's Day for
// 1998 would be obtained in the natural way: dayOfWeek(1, 1, 1998).
// You may ignore the cases where the month, day, or year are out of bounds.
public static int dayOfWeek(int month, int day, int year) {
return 42;
// replace this with your answer!
}
public static void testDayOfWeek() {
System.out.print("Testing dayOfWeek... ");
// On 2/5/2006, the Steelers won Super Bowl XL on a Sunday!
assert(dayOfWeek(2, 5, 2006) == 1);
// On 6/15/1215, the Magna Carta was signed on a Monday!
assert(dayOfWeek(6, 15, 1215) == 2);
// On 3/11/1952, the author Douglas Adams was born on a Tuesday!
assert(dayOfWeek(3, 11, 1952) == 3);
// on 4/12/1961, Yuri Gagarin became the first man in space, on a Wednesday!
assert(dayOfWeek(4, 12, 1961) == 4);
// On 7/4/1776, the Declaration of Independence was signed on a Thursday!
assert(dayOfWeek(7, 4, 1776) == 5);
// on 1/2/1920, Isaac Asimov was born on a Friday!
assert(dayOfWeek(1, 2, 1920) == 6);
// on 10/11/1975, Saturday Night Live debuted on a Saturday (of course)!
assert(dayOfWeek(10, 11, 1975) == 7);
System.out.println("Passed all tests!");
}
public static void main(String[] args) {
testDayOfWeek();
}
}
- xIntercept
class MyCode {
// This method takes 2 doubles representing the line y=mx+b,
// and returns the value of the x-intercept. You are assured
// that the line has an x-intercept.
public static double xIntercept(double m, double b) {
return 42.0;
// replace this with your answer!
}
public static void testXIntercept() {
System.out.print("Testing xIntercept... ");
assert(almostEqual(xIntercept(3,0), 0)); // y=3x, x-int at x=0
assert(almostEqual(xIntercept(2,-4), 2)); // y=2x-4, x-int at x=2
assert(almostEqual(xIntercept(3,-4), 1.3333333)); // y=3x-4, x-int at x=4/3=1.333...
System.out.println("Passed all tests!");
}
public static boolean almostEqual(double d1, double d2) {
double epsilon = 0.0001;
return (Math.abs(d1 - d2) < epsilon);
}
public static void main(String[] args) {
testXIntercept();
}
}
- xInterceptOfParabola
class MyCode {
// This method takes 3 doubles representing the parabola y=ax^2+bx+c,
// and returns the value of the SMALLER x-intercept (using the quadratic
// formula). You are assured the line has at least one x-intercept.
public static double xInterceptOfParabola(double a, double b, double c) {
return 42.0;
// replace this with your answer!
}
public static void testXInterceptOfParabola() {
System.out.print("Testing xInterceptOfParabola... ");
assert(almostEqual(xInterceptOfParabola(1,0,0), 0)); // y = x^2 has a zero at x=0
assert(almostEqual(xInterceptOfParabola(1,0,-4), -2)); // y = x^2-4 has a zero at x=-2
assert(almostEqual(xInterceptOfParabola(4,0,-4), -1)); // y = 4^2-4 has a zero at x=-1
System.out.println("Passed all tests!");
}
public static boolean almostEqual(double d1, double d2) {
double epsilon = 0.0001;
return (Math.abs(d1 - d2) < epsilon);
}
public static void main(String[] args) {
testXInterceptOfParabola();
}
}
- Part 2: Writing Methods with
Conditionals
- isFactor
class MyCode {
// This method takes two ints, f and n, and returns true
// if and only if f is a factor of n.
public static boolean isFactor(int f, int n) {
return false; // replace this with your answer!
}
public static void testIsFactor() {
System.out.print("Testing isFactor... ");
assert(isFactor(1, 21) == true);
assert(isFactor(2, 21) == false);
assert(isFactor(3, 21) == true);
assert(isFactor(21,21) == true);
assert(isFactor(22,21) == false);
assert(isFactor(42,21) == false);
assert(isFactor(-3,21) == true); // negatives are also factors
assert(isFactor(10, 0) == true); // any non-zero number is a factor of 0
assert(isFactor(0, 21) == false); // 0 is not a factor of any number!
assert(isFactor(0, 0) == false); // not even itself...
System.out.println("Passed all tests!");
}
public static void main(String[] args) {
testIsFactor();
}
}
- isPerfectSquare
Write your own tests! This method should take an integer n and
return true if n is a perfect square, and false otherwise.
Hint: on this and all problems where you write your own tests,
you should be thorough in testing all reasonable boundary cases.
Discuss what this means with your CA!
- isWeekday
Write your own tests! This method should take the same parameters
as dayOfWeek (from above), only here you should return true if the given
date is a weekday and false otherwise (if it is a weekend day -- Saturday or
Sunday). This method should call the dayOfWeek method you previously
wrote!
- numberOfInterceptsOfParabola
Write your own tests! This method takes the same parameters as
xInterceptOfParabola (from above), only here you should return the number of
x intercepts (that is, the number of real roots). This will be one of
0 (if it never crosses the x axis), 1 (if its maximum or minimum lies
exactly on the x axis), or 2 (if it crosses the x axis twice). If
there is a tricky part here, it is ensuring that your test for the middle
case -- exactly one root -- works in the face of the approximate nature of
doubles in Java.
- signum
Write your own tests! This method takes a double and returns an
integer indicating the sign of the parameter. The method returns +1
if the parameter is positive, 0 if it is 0, and -1 if it is negative.
Again, you should deal with the approximate nature of doubles in Java, so
"very nearly zero", to within some reasonable epsilon value that you choose,
is close enough.
Note: even though this method takes a double, of course you can
call it with an integer value which will simply be converted into a double.
Carpe diem!