Not too sure where I am going wrong. I need to sum the first 16 elements of the series: 1, 3, 9, 27, 81…. using the method total The code does so by creating an instance of Geometric1 that will be of typ Seq.
The total method prints a value 7174454.0 but i should be getting 21523360.
public class Geometric1 implements Seq{
private double b;
public static double result = 0.0;
public Geometric1(double b) {
this.b = b;
}
public double valAtIndex(int i) {
// TODO Auto-generated method stub
return Math.pow(b, i);
}
public static double total() {
Seq s = new Geometric(3.0);
for (int i = 0; i < 15; i++) {
result += s.valAtIndex(i);
}
return result;
}
public static void main(String[] args) {
System.out.println(total());}
}
You’re close. You just need to adjust your loop to run 16 times.
Keep result initialized to
0(you’ve edited the post multiple times), and change your loop as follows: