CS 305j Discussion Assignment - Loops and File Processing

1. Consider the following method. Indicate the output for each method call below.

public static void mystery(int x)
{
int y = 0;
while(x % 2 == 0)
{
y++;
x /= 2;
}
System.out.println(x + " " + y);
}

Method calls:

mystery(19);

mystery(42);

mystery(40);

2. For the method calls below, indicate what value is returned:

public static int mystery2(int x, int y)
{
while(x != 0 && y  != 0)
{
if(x < y)
y -= x;
else
x -= y;

}
return x+y;
}

Method call                Value Returned

mystery2(3, 3);   ____________________________________

mystery2(5, 3);   ____________________________________

mystery2(12, 18);  ___________________________________

3. Write a static method named evenNumbers that accepts a string of text as a parameter. Assume that the text is a series of integers, and process this text and report various statistics about the integers. Report the total number of integers, the sum of the integers, the number of even integers and the percent of even integers. For example, if the string is:

"5 7 2 8 9 10 12 98 7 14 20 22"

Your method should produce this output:

Total number  = 12
Sum of integers = 214
Number of evens = 8
Percent of evens = 66.666666666667