Computer Science APEA 15-100, Summer 2009
Lab 4
Read these instructions first!
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();
}
}
Carpe diem!