Java Reference
In-Depth Information
prev = next;
next = console.nextInt();
// Point D
}
// Point E
return count;
}
Categorize each assertion at each point with ALWAYS, NEVER, or SOMETIMES:
next == 0
prev == 0
next == prev
Point A
Point B
Point C
Point D
Point E
Exercises
1. Write a method called showTwos that shows the factors of 2 in a given integer. For example, consider the following
calls:
showTwos(7);
showTwos(18);
showTwos(68);
showTwos(120);
These calls should produce the following output:
7 = 7
18 = 2 * 9
68 = 2 * 2 * 17
120 = 2 * 2 * 2 * 15
2. Write a method called gcd that accepts two integers as parameters and returns the greatest common divisor (GCD)
of the two numbers. The GCD of two integers a and b is the largest integer that is a factor of both a and b .
One efficient way to compute the GCD of two numbers is to use Euclid's algorithm, which states the following:
GCD ( a , b )
GCD ( b , a % b )
GCD ( a , 0)
Absolute value of a
3. Write a method called toBinary that accepts an integer as a parameter and returns a String containing that
integer's binary representation. For example, the call of printBinary(44) should return "101100" .
 
 
Search WWH ::




Custom Search