Java Reference
In-Depth Information
public double area()
that compute the circumference and area of a polygon. To compute the
perimeter, compute the distance between adjacent points, and total up the
distances. The area of a polygon with corners (x 0 , y 0 ),ș, (x nɨ1 , y nɨ1 ) is
1
2 x 0 y 0
(
+
x 1 y 2
+ւ +
x n ɨ1 y 0
ɨ
y 0 x 1
ɨ
y 1 x 2
ɨւ ɨ
y n ɨ1 x 0
)
As test cases, compute the perimeter and area of a rectangle and of a
regular hexagon. Note: You need not draw the polygonȌthat is done in
Exercise P7.15.
΢ Exercise P7.8. Write a program that reads a sequence of integers into an
array and that computes the alternating sum of all elements in the array. For
example, if the program is executed with the input data
1 4 9 16 9 7 4 9 11
then it computes
1 ɨ4 +9 ɨ16 +9 ɨ7 +4 ɨ9 +11 = ɨ2
328
329
PROGRAMMING EXERCISES
΢΢Exercise P7.9. Write a program that produces random permutations of the
numbers 1 to 10. To generate a random permutation, you need to fill an
array with the numbers 1 to 10 so that no two entries of the array have the
same contents. You could do it by brute force, by calling
Random.nextInt until it produces a value that is not yet in the array.
Instead, you should implement a smart method. Make a second array and
fill it with the numbers 1 to 10. Then pick one of those at random, remove
it, and append it to the permutation array. Repeat 10 times. Implement a
class Permutation Generator with a method
int[] nextPermutation
 
Search WWH ::




Custom Search