Sum of Integers
Easy
110
3
79.7% Acceptance
In this lab, you are tasked with implementing a method named sumOfIntegers
in the Main
class. This method should calculate the sum of all integers from 1 up to and including a given positive integer n
.
Requirements
- Create a method named
sumOfIntegers
in theMain
class. - The method should take one integer parameter
n
. - It should return the sum of all integers from 1 to
n
. - Consider edge cases, such as when
n
is 0.
Edge Cases
- When
n
is 0: The method should return 0, as there are no numbers to add. - Positive
n
: For any positive integern
, the method returns the sum of all integers from 1 ton
. - Invalid
n
: Ensure your method handles unexpected inputs gracefully.
Examples
- sumOfIntegers(10): This should return
55
, as the sum of numbers from 1 to 10 is 55. - sumOfIntegers(7): For
n = 7
, the expected output is28
, the sum of 1, 2, 3, 4, 5, 6, and 7. - sumOfIntegers(0): If
n
is 0, the method should return0
, since there are no numbers to sum up.
Follow these guidelines to implement your method, and test it against the challenges provided to ensure it meets all the requirements.
Challenges Information
Challenge 1: Validate Method Name and Return Type
- Objective: Ensure the method
sumOfIntegers
is correctly defined in theMain
class. - Criteria:
- The method must be named
sumOfIntegers
. - It should accept a single
int
parameter. - The return type of the method should be
int
.
- The method must be named
Challenge 2: Correct Value for n = 10
- Objective: Test if the
sumOfIntegers
method returns the correct sum whenn
is 10. - Criteria:
- Invoke
sumOfIntegers(10)
. - The method should return
55
, the sum of integers from 1 to 10.
- Invoke
Challenge 3: Correct Value for n = 0
- Objective: Ensure the
sumOfIntegers
method correctly handles the case wheren
is 0. - Criteria:
- Invoke
sumOfIntegers(0)
. - The expected return value should be
0
, as the sum from 1 to 0 is 0.
- Invoke
Challenge 4: Correct Value for n = 7
- Objective: Check if the
sumOfIntegers
method returns the correct sum forn
equal to 7. - Criteria:
- Invoke
sumOfIntegers(7)
. - The expected return value should be
28
, the sum of integers from 1 to 7.
- Invoke
Challenge 5: Correct Value for n = 18
- Objective: Validate the correctness of the
sumOfIntegers
method forn
equal to 18. - Criteria:
- Invoke
sumOfIntegers(18)
. - The expected return value should be
171
, the sum of all integers from 1 to 18.
- Invoke