To expand on what others have mentioned. Yes, you can create a class to run the total sum. I believe your question is actually about Fibonacci sequence. In Fibonacci sequence each value is the sum of all previous. For example, 1 1 2 3 5 8 13...n.
Most likely you will need to use recursion to generate the expected result. Here is some code in java:
public int fibonacci(int n) {
if (n < 2) { //If n=1 or lower we return n
return n; //This is our base case.
}
else {
return fibonacci(n-1)+fibonacci(n-2);
//else, we use the code above
//example, if n = 3, then fibonacci(3-1) + fibonacci(3-1);
//(fibonacci(2) = fibonacci(2-1) + fibonacci(2-2)) + fibonacci(1)
//The above statement now moves into the base case
//1+0+1 = 2, remember these numbers are added because of the
//else statement.
}
}
We can conclude that when we are out of the base case that we now have to follow the recursion until it reaches the base case and bring all of the returned information back into the recursive statment.