0

I need to create a method with the signature public double totalRent()

This method accesses each Property object within the array properties and sums up the property rent and returns the total amount. Returns:total rent

So heres my code:

public double totalRent()
{

    for(int index = 0; index < property.length; index++)
    {

        int total =+ property[index];
    }
    return totalRent();
} 

Is this correct? It keeps saying the total variable is not used, so I'm not sure if I'm doing it right.

ordonezalex
  • 2,645
  • 1
  • 20
  • 33
myst
  • 29
  • 3
  • 1
    Read the message, `total` is created on each iteration, then immediately destroyed - it's useless. Also, it is `+=` and a `Property` object can't be added to an integer primitive... – Andrew Li Oct 13 '16 at 01:20
  • Like @AndrewL. has pointed out, your `total` variable is in the `for` loop. This is an issue of "scope". Variables *defined* (like `total` in this case) in a `for` loop are destroyed at the end of the loop. So, you're assigning a value (`property[index]`) to a new variable (`total`) every time, and then getting rid of that variable (`total`). See this other question: http://stackoverflow.com/q/8803674 – ordonezalex Oct 13 '16 at 01:24
  • In java 8, `long total = Arrays.stream(property).sum();` – Jude Niroshan Oct 13 '16 at 01:27

2 Answers2

2

Your logic / understanding of the code is a bit off, let's try to fix it:

public double totalRent() {

    int total = 0;

    for(int index = 0; index < property.length; index++){

        total += (property[index]).getRent(); //assuming the getter here
    }

    return total;
}

The first problem here was that you were returning a call to the method itself, thereby creating a recursion, and an infinite one at that, since you had no exit condition.

What you want to return are the values, not the function itself. The function defines how you do something, the logical algorithm.

As you also stated, your IDE tried to warn you that you were not using the value of total. That is because within its scope (you can google this for tutorials and docs, or go here ) you never used it, so it was useless.

The solution is, you declare the variable outside of the for loop, so that it is in scope and can be returned. Inside of the for loop, you progressively sum all the values, then return.

You will also notice I added .getRent() right next to the call to each element of the array. That is because you cannot sum objects directly with + , you're gonna need to sum the correct field of each one. You can either get the field directly if it is declared as public inside of the Property class, or with a getter (as I did), ie. A method that specifically accesses and returns a value from an object.

You can understand more about getters and why they're useful by leraning the basic principles of encapsulation, or directly from the oracle tutorials here.

Of course, as some comments suggested, there are other ways to sum all the values in the array, however since this question was specifically about the mistakes you might have made, this is more than enough to learn.

Community
  • 1
  • 1
Athamas
  • 609
  • 5
  • 16
0

Well, you are re-creating the same total variable each iteration. The total should be initialized out of the loop, so it doesn't fall out of scope.

Also, you are using =+, but I think you mean +=. += is used to add the latter amount to the former variable.

Also, you are calling the function instead of returning. return totalRent() just calls the function again. You should return the variable. (in this case, total)

Something like this:

int total = 0;
for(int index = 0; index < properties.length; index++){
  //do stuff
}
return total;

Hope this helped, and happy coding!

a.deshpande012
  • 715
  • 7
  • 18