Important Notice:

Practice-It will be discontinued as of November 1st, 2024. After this date, the website will remain online for a transitional period, but login will be restricted to University of Washington NetID authentication. This marks the next phase towards the platform's full retirement. Thank you for your use and support of the application over the years.

If you are looking for an alternative, a similar tool, CodeStepByStep, was developed independently by the original author of Practice-It, and is available at codestepbystep.com**

logo Practice-It logo

printSum

Language/Type: Java
Author: Stuart Reges (on 2016/11/14)

Write a static method called printSum that takes an integer n and integers low and high as parameters and that produces a series of n integers between low and high inclusive, printing various information about the integers. The method should construct and use a Random object to generate the numbers. For example, if you make the following call:

printSum(15, 4, 8);

the method should produce three lines of output like the following:

sum 15 numbers 4 to 8
6 + 6 + 4 + 6 + 6 + 8 + 8 + 7 + 8 + 4 + 8 + 7 + 6 + 6 + 8 = 98
max = 98

The first line indicates that the method is going to find the sum of 15 numbers between 4 and 8 inclusive (as indicated by the parameters in the call). The second line shows the 15 numbers randomly selected by the method and their sum. The third line reports the highest partial sum for the series. A partial sum is the value obtained by adding up a limited number of terms (in the example above, a sum of 6 for one term, a sum of 12 for two terms, a sum of 16 for three terms, and so on). In this case, the maximum sum is the overall sum, but that won't always be the case. For example, if the call instead had been:

printSum(10, -2, 2);

the output would look like the following:

sum 10 numbers -2 to 2
-1 + 2 + 2 + 2 + -1 + -1 + -2 + 2 + 0 + -1 = 2
max = 5

Here the maximum sum occurs with the first four numbers that add up to 5. You are to exactly reproduce the format of these logs. You may assume that n is greater than or equal to 1 and that low is less than or equal to high.

Type your solution here:


This is a method problem. Write a Java method as described. Do not write a complete program or class; just the method(s) above.

You must log in before you can solve this problem.


Log In

If you do not understand how to solve a problem or why your solution doesn't work, please contact your TA or instructor.
If something seems wrong with the site (errors, slow performance, incorrect problems/tests, etc.), please

Is there a problem? Contact a site administrator.