nsc010
nsc010

Reputation: 385

Java - calculating the sum of a series of numbers

i am trying to calculate the sum of the first 16 elements of the series 1, 3, 9, 27, 81.... The code does so by first CREATING a suitable instance of Geometric that will be of TYPE Seq.

public class Geometric implements Seq {

private double b;
public Seq s;
public double sum;

public Geometric(double a) {

    this.b = a;
}


public double valAtIndex(int i) {
    // TODO Auto-generated method stub
    return Math.pow(b, i);
}

public double total() {

    s = new Geometric(3.0);
    for (int a = 0; a < 16; a++) {

        int c = -1;
        sum = sum + s.valAtIndex(c = c + 1);
        c++;

    }
    return sum;
}

public double getSum() {

    return sum;
}

public static void main(String[] args) {

    System.out.println(sum);
}

}

Not sure if i am doing this the long way round? It isnt working yet, eclipse is saying i need to change the modifier of sum to static?

Upvotes: 0

Views: 10720

Answers (4)

Alex Kalicki
Alex Kalicki

Reputation: 1533

Seems like you made this a bit more complicated then it needs to be. Here's how I'd do it without using another class (the code can go in any class where you need to compute the sum):

int currentNumber = 1;
int sum = 0;
for (int i = 0; i < 16; i++)
{
    sum += currentNumber;
    currentNumber *= 3;
}

The value of the sum of the terms in the geometric sequence will be stored in the variable sum. Alternatively use the closed form for the sum of the terms in a geometric sequence:

int firstNumber = 1;
int multFactor = 3;
int numTerms = 16;
int sum = firstNumber * ((1 - multFactor ^ numTerms) / (1 - multFactor));

or in your case:

int sum = 1 * ((1 - 3^16) / (1 - 3));  // returns 21523360, the number you were trying to calculate

Upvotes: 1

sedran
sedran

Reputation: 3566

it wants sum to be static because you are trying to reach sum inside a static method, main. You can't reach non-static fields inside static methods. You should create an instance of Geometric inside main, then you should call the variable sum.

public static void main(String[] args) {
    Geometric geo = new Geometric(some_number);
    System.out.println(geo.sum);
}

Also, your way of doing things is not a good way of object-oriented programming concept.

You are returning the total of the series from a method, but you are also storing it in a field called sum, and you are wanting sum before calculating it. Instead, you should get total from the method.

public static void main(String[] args) {
    Geometric geo = new Geometric(3.0);
    System.out.println(geo.total());
}

Since the method total is specified for instances of this class, it shouldn't create a new instance to calculate the total, it should calculate the current instance's total.

public double total() {
    double temp = 1;
    double sum = 0;
    for (int a = 0; a < 16; a++) {
        sum += temp;
        temp *= this.b;
    }
    return sum;
}

Upvotes: 4

Antimony
Antimony

Reputation: 39451

total and sum look like they're supposed to be static. I have no idea what getSum is for. But the real question is, why aren't you just adding up the numbers directly? You don't need a class for everything.

Upvotes: 0

Jon Lin
Jon Lin

Reputation: 143856

eclipse is saying i need to change the modifier of sum to static?

Because you are printing out sum in a static method main(String[]). You can't access an instance field from a static method. You need to create an instance of Geometric and get the sum from that instance.

Upvotes: 0

Related Questions