This is a beta version of Practice-It. Give us feedback

logo Practice-It logo

printSum

Language/Type: Java
Author: Stuart Reges (on 2018/05/01)

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.