Can someone help me with computer science homework?

Can someone help me with computer science homework?




Loop A

final int LIMIT = 100; // setup

int count = 1;



while (count <= LIMIT) // condition

{ // body

System.out.println(count); // -- perform task

count = count + 1; // -- update condition

}

// Program continues here after loop terminates

Loop B

final int LIMIT = 16;

int count = 1;

int sum = 0;

int nextVal = 2;



while (sum < LIMIT)

{

sum = sum + nextVal;

nextVal = nextVal + 2;

count = count + 1;

}



System.out.println("Had to add together " + (count-1) + " even numbers " +

"to reach value " + LIMIT + ". Sum is " + sum);

Loop C

int sum = 0; //setup

char keepGoing = 'y';

int nextVal;



while (keepGoing == 'y' || keepGoing == 'Y') // (Do you recall the '||' operator?)

{

System.out.print("Enter the next integer: "); //do work

nextVal = kbd.nextInt();

sum = sum + nextVal;



System.out.println("Type y or Y to keep going"); //update condition

keepGoing = kbd.nextChar();

}



System.out.println("The sum of your integers is " + sum);







1. First, go to this web page to find out how to handle error-laden loops in BlueJ.

2. In Loop A above, the println statement comes before the value of count is incremented. What would happen if you reversed the order of these statements so that count was incremented before its value was printed? Would the loop still print the same values? Explain.

3. Consider the second loop, Loop B, above.

a. Trace this loop; that is, in a table show values for variables nextVal, sum and count at each iteration. As you work through the iterations by hand, update the values in the table everytime a variable's value changes. Whenever there is output, copy it into the next row in the OUTPUT column in the table. When evaluating a line of code (such as "sum=sum+nextVal"), use the table to determine what the current value of each of the variables is to determine the effect of the code.

nextVal sum count OUTPUT



b. Note that when the loop terminates, the number of even numbers added together before reaching the limit is count-1, not count. How could you modify the code so that when the loop terminates, the number of things added together is simply count?

4. Write a while loop that will print "I love computer science!!" 100 times. Is this loop count-controlled?

5. Add a counter to the third example loop, Loop C, above (the one that reads and sums integers input by the user). After the loop, print the number of integers read as well as the sum. Just note your changes on the example code. Is your loop now count-controlled?

6. The code below is supposed to print the integers from 10 to 1 backwards. What is wrong with it? (Hint: there are two problems!) Correct the code so it does the right thing.

7. count = 10;

8. while (count >= 0)

9. {

10. System.out.println(count);

11. count = count + 1;

12. }





No Answers Posted Yet.