Strobe_
Strobe_

Reputation: 515

Counting the number of functions an Object does

I have created this program/scenario.

Multiple Robots are created and then "escape" from the room that has been created. I have also created a counter to count the moves that the robots make and then form an average.

I have created all this it only wants to return 0 for some reason. It's throwing up no errors or the like so I feel like I'm missing something obvious.

Here are both parts of the code:

public static double countingMoves;

.
.
.

public void move() {
super.move();
countingMoves++;
}

public int getRobotMoves() {
return countingMoves;
}




int Counter = EscapeBot.countingMoves/10;

Upvotes: 0

Views: 48

Answers (1)

christopher
christopher

Reputation: 27356

int Counter = EscapeBot.countingMoves/10;

First Point

You're dividing two integers, which as stated in the comments, will result to 0 is the result is < 0. Cast one of these types to a double. This process is called Arithmetic Promotion, where every element in an expression has it's precision increased to the element with the highest precision. E.g:

int / int = int
double / double = double
int / double = double
int + String = String

For your code:

double Counter = EscapeBox.countingMoves/10.0;

Second Point

The Java naming convention states that the first word of variables or methods that are not constants must begin with a lower case letter.

Counter -> counter

Third and hopefully final point

If you look at where you are computing the average, 0.0 is actually correct. You compute the average at the start, before you have any moves.

double Counter = EscapeBot.countingMoves/10.0;
// When computed at the start, this equals:
// double Counter = EscapeBot.countingMoves(0)/10.0 = 0/10.0 = 0.0

Comes before any moves. By putting this at the end of your code, you should get a more accurate reading.

Upvotes: 5

Related Questions