Programming and Problem Solving with Java

(やまだぃちぅ) #1
473

5.Write a code segment that reads and sums values until it has summed ten data
values or until a negative value is read, whichever comes first. Use a doloop for
your solution.
6.Rewrite the following code segment using a doloop instead of a whileloop.
response = Integer.parseInt(inData.readLine());
while(response >= 0 && response <= 127 )
{
response = Integer.parseInt(inData.readLine());
}
7.Rewrite the following code segment using a whileloop.
inInt = Integer.parseInt(inData.readLine());
if(inInt >= 0 )
do
{
System.out.println(""+ inInt);
inInt = Integer.parseInt(inData.readLine());
} while(inInt >= 0 );
8.Rewrite the following code segment using a forloop.
sum = 0 ;
count = 1 ;
while(count <= 1000 )
{
sum = sum + count;
count++;
}
9.Rewrite the following forloop as a whileloop.
for(m = 93 ; m >= 5 ; m--)
outData.println( m + " "+ m * m);

10.Rewrite the following forloop as a doloop.


for(k = 9 ; k <= 21 ; k++)
outData.println( k + " "+ 3 * k);

11.Write a value-returning method that accepts two intparameters,baseand ex-
ponent, and returns the value of baseraised to the exponentpower. Use a forloop
in your solution.

Free download pdf