Exercises that will help you study for the Midterm:

Exercise #1

Consider this loop:

   for (int z =10; z<30;z++)
   {
      System.out.println("The number is " + z);
    }
a. How many times will this run?
b. How would you change this to count by 2's?
c. How would you change this to count by 5's?
d. How would you add up all these numbers and just print the total?
e. How would you get the average of all the numbers printed?
f. How would you make a point of x = the number in the loop and y = the same number in the loop and then print out the point?
g. How would you find the distance between the origin and the point you created and print that each time in the loop?
h. How would you print as many stars as the number inside the loop? (So when z was 10, print 10 stars and when z was 11, print 11 stars, making a figure that grew as it went down.)
i. How would you add another inner loop to print twice as many dashes as you did stars?

Exercise #2

Write a program to ask the user their name and then ask for a low number and then a high number. It should print the name in uppercase and then the numbers from low to high separated by a plus sign, and then print the total of the numbers. Ask the user the high and low numbers and print the answering equation 5 times. Then give the total of all the answers and the average of all the answers. (Creating and using one method in your program is required.)

A samples session would be:

What is your first name? Kris
What is the low number? 5
What is the high number? 10
KRIS, 5+6+7+8+9+10 = 45
What is the low number? -10
What is the high number? -5
KRIS, -10+-9+-8+-7+-6+-5=-45
. . . (repeat 3 more times)
The total of all ranges is 999
The average of all ranges is 199.8
---------------------------------
Questions to ask yourself:
What building blocks do I need? Make a block of anything complex. Make a block of anything repeated.
What will each block print? What will each block return? What does each block need to take in to do its job?
In building each block, and the main program, ask:
What do I need next? What variables and literals are available to me right now.

Some Help for Exercise #2:
Write a method named printRange that takes two integers as parameters, a low and a high, and the name and that prints out the name in uppercase and all integers in the range from low to high separated by a plus sign. Then, print = and the total of the numbers.

For example, the call:
printRange(5, 10, "kris");

should produce the following output:
Kris, 5+6+7+8+9+10 = 45

Your method should produce a complete line of output. If low and high are equal, the output should be a list with just one number. You can assume the low number is smaller than the high number. Your method should also work properly for negative integers. For example, the following series of calls:

printRange(3, 3,"Sally");
printRange(-10, -5,"James");

should produce the following lines of output:
Sally 3 = 3
James -10+-9+-8+-7+-6+-5=-45

Once this method works, create a main routine to ask for the two numbers and give the result 5 times.

Once that main routine works, change it to give the total of all the ranges, and the average total.