Computer Science 15-100 (Sections S-V), Fall 2008
Homework 1
Practice
Due: Never. This is not
assigned work.
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.* to solve these problems. While they may be helpful, every problem here is solvable without them.
class MyCode {
// This method takes two positive numbers, x and y, and returns
// the remainder when you divide x by y.
// Of course, this is just (x % y), but the trick is that here
// you are not allowed to use the % operator.
// This can be done using the other arithmetic operators (+, -, *, /).
// You may ignore the cases where x is negative or y is zero or negative.
public static int modlessRemainder(int x, int y) {
return 42; // replace this with your answer!
}
public static void testModlessRemainder() {
System.out.print("Testing modlessRemainder... ");
assert(modlessRemainder(0,3) == 0);
assert(modlessRemainder(1,3) == 1);
assert(modlessRemainder(2,3) == 2);
assert(modlessRemainder(3,3) == 0);
assert(modlessRemainder(4,3) == 1);
assert(modlessRemainder(5,3) == 2);
assert(modlessRemainder(6,3) == 0);
assert(modlessRemainder(7,3) == 1);
assert(modlessRemainder(1437, 82) == (1437 % 82));
System.out.println("Passed all tests!");
}
public static void main(String[] args) {
testModlessRemainder();
}
}
class MyCode {
// This method takes a street number (which you may assume is positive),
// and returns the street number of the nearest bus stop, where buses
// stop on streets that are multiples of 8 (8th, 16th, 24th, etc).
// So it behaves as such:
// nearestBusStop(11) returns 8
// nearestBusStop(13) returns 16
// But what about 12th street? It is equally far from 8th and 16th
// streets, but riders generally head towards town (0th street), and
// so they prefer the lower bus stop. Hence:
// nearestBusStop(12) returns 8
// You can assume there is a bus stop on 0th street, and again you
// can ignore the case where the street is negative).
// Remember: do not use conditionals, loops, arrays, etc.
// This can be done (in one line of code!) using only what we
// have covered Week #1's notes (in fact, using just addition,
// division and multiplication of integers).
public static int nearestBusStop(int street) {
return 42; // replace this with your answer!
}
public static void testNearestBusStop() {
System.out.print("Testing nearestBusStop... ");
assert(nearestBusStop(0) == 0);
assert(nearestBusStop(4) == 0);
assert(nearestBusStop(5) == 8);
assert(nearestBusStop(8) == 8);
assert(nearestBusStop(11) == 8);
assert(nearestBusStop(12) == 8);
assert(nearestBusStop(13) == 16);
System.out.println("Passed all tests!");
}
public static void main(String[] args) {
testNearestBusStop();
}
}
class MyCode {
// This method takes three ints representing the lengths of the sides
// of a triangle, and returns true if such a triangle exists and false
// otherwise. Note from the triangle inequality that the sum of each
// two sides must be greater than the third side, and further note that
// all sides of a legal triangle must be positive.
public static boolean isLegalTriangle(int side1, int side2, int side3) {
return true; // replace this with your answer!
}
public static void testIsLegalTriangle() {
System.out.print("Testing isLegalTriangle... ");
assert(isLegalTriangle(3, 4, 5));
assert(isLegalTriangle(5, 4, 3));
assert(isLegalTriangle(3, 5, 4));
assert(!isLegalTriangle(3, 4, 7));
assert(!isLegalTriangle(7, 4, 3));
assert(!isLegalTriangle(3, 7, 4));
assert(!isLegalTriangle(5, -3, 1));
assert(!isLegalTriangle(-3, -4, -5));
System.out.println("Passed all tests!");
}
public static void main(String[] args) {
testIsLegalTriangle();
}
}
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();
}
}
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();
}
}
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();
}
}
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();
}
}
Carpe diem!