Suppose we attend a party. To be sociable, we will shake hands with everyone else. Write a fragment of code using a for statement that will compute the total number of handshakes that occur. (Hint: Upon arrival, each person shakes hands with everyone that is already there. Use the loop to find the total number of handshakes as each person arrives.)
What will be an ideal response?
```
int numberAttending = 8;
int handShakes = 0;
for( int person=1; person <= numberAttending; person++){
handShakes += (person - 1);
// When person k arrives, they will
//shake hands with the k-1 people already there
}
System.out.println("The sum is " + sum);
```
This code is in Fragments.java.
You might also like to view...
Which of the following products can be used to encrypt hard drives for an organization that uses portable computers?
A) Bit Locker B) Check Point Software C) Vera Crypt D) All of the above
A ____ holds drawing objects, shapes, and other items.
a. stack b. layer c. fill d. cluster
In both the iterative and the recursive binary search, explain why the condition for halting the routine with the variable found assigned false is first > last.
What will be an ideal response?
Write a recursive method that will find and return the largest value in an array of integers. Hint: Split the array in half and recursively find the largest value in each half. Return the larger of those two values.
What will be an ideal response?