User Story
As a developer, I need to implement an Apex class containing a void method that calculates the sum of integers from 1 to 10. This will enable efficient computation within the Salesforce environment.
Key Takeaways
- Class & Methods
- Variables
- For Loop
Codes
public class SumCalculator {
public static void calculateSumOfIntegers() {
Integer sum = 0;
for (Integer i = 1; i <= 10; i++) {
sum += i;
}
System.debug('The sum of the numbers from 1 to 10 is ' + sum + '.');
}
}
Test Class
@isTest
public class SumCalculatorTest {
@isTest
static void testSumOfIntegers() {
Test.startTest();
SumCalculator.calculateSumOfIntegers();
Test.stopTest();
}
}