public class Test4D extends Object
Modifier and Type | Method and Description |
---|---|
static boolean |
isSorted(List<Integer> t)
Returns true if the specified list is sorted in ascending
order and false otherwise.
|
static void |
moveFirstDown(List<Integer> t)
Moves the first element
f of t down the list
until it reaches the first location in the list where the next element is
greater than or equal to f . |
static int |
sum(int start,
int end)
Returns the sum of the integer values between start and end (including
start and end).
|
static int |
wheresWaldo(Waldo waldo,
int locA,
int locB)
Returns the location of Waldo given two guesses of Waldo's location.
|
public static int sum(int start, int end)
sum(5, 5) returns 5 sum(0, 1) returns 1 (0 + 1) sum(0, 2) returns 3 (0 + 1 + 2) sum(3, 6) returns 18 (3 + 4 + 5 + 6) sum(-100, 101) returns 101 (-100 + -99 + ... + 99 + 100 + 101)
start
- the number to start the sum atend
- the number to end the sum atpublic static boolean isSorted(List<Integer> t)
t isSorted(t) ------------------------- [] true [-5] true [8, 9] true [1, 2, 3] true [1, 1, 5] true [10, 4] false [1, 2, 3, 1] false
t
- a listpublic static void moveFirstDown(List<Integer> t)
f
of t
down the list
until it reaches the first location in the list where the next element is
greater than or equal to f
. For example:
if t is equal to then moveFirstDown(t) makes t equal to -------------------------------------------------------------------- [] [] [1] [1] [-1, 2] [-1, 2] [1, -2, 8] [-2, 1, 8] [4, -5, -9, 4] [-5, -9, 4, 4] [9, 1, 3, 2, 4] [1, 3, 2, 4, 9]
t
- the list to move the first element ofpublic static int wheresWaldo(Waldo waldo, int locA, int locB)
This method can find Waldo's location using 32 or fewer recursive calls (i.e., it does not check every possible location for Waldo).
waldo
- a reference to a Waldo objectlocA
- a guess of Waldo's locationlocB
- a guess of Waldo's location