Bird Watcher, ejercicio 4

Hello everyone, I have some questions about how to properly answer exercise 4 (Bird Watcher), task 3. Below is the context and task 3:

Instructions:
You are an avid bird watcher who keeps track of how many birds have visited your garden in the last seven days.

This is the int array you will be working with:

int birdsPerDay = new int {0, 2, 5, 3, 7, 8, 4};

You have six tasks, all related to the number of birds that visited your garden.
Task 3: Increment today’s count
Implement the BirdWatcher.incrementTodaysCount() method to increment today’s count:

int birdsPerDay = { 2, 5, 0, 7, 4, 1 };
BirdWatcher birdCount = new BirdWatcher(birdsPerDay);
birdCount.incrementTodaysCount();
birdCount.getToday();
// => 2

Note: in task 2, which was successfully completed, today is the last day, i.e., the last value in the int array, which is 4, equivalent to 4 birds. Task 3 asks me to increase today’s count by 1, which would give 5, but I’m not sure how to approach it.

This is the method for task 3:

class BirdWatcher {
private final int birdsPerDay;

public BirdWatcher(int birdsPerDay) {
this.birdsPerDay = birdsPerDay.clone();
}

//task 3 method
public void incrementTodaysCount(){

//my approach
birdsPerDay[birdsPerDay.length - 1];
incrementBird = incrementBird + 1;
}
}
In the comments “my approach,” I posted my answer, but it is not correct. It should be noted that the method does not return values.

Can anyone help me?

What error message are you getting?

Hi @AndyCodePractica, to make it the code easier to read in the forum post, I’d highly recommend using triple backticks (```) to create a code block. Applying it, the code can be formatted much nicer and easier to read in the post, like this:

public void incrementTodaysCount(){

    //my approach
    birdsPerDay[birdsPerDay.length - 1];
    incrementBird = incrementBird + 1;
}

As for the problem with your code, as BNAndras has asked above, what error are you getting?

You’re right the method does not return a value. I suppose the question is what do you with this value? For that, I’d start by looking at the test. What does the test expect?

Task 3 wants to you modify the birdsPerDay variable.

Here you are getting the value of the today’s count but you are not doing anything with it.

And here, instead of modifying the birdsPerDay variable, you are referencing a nonexistant incrementBird variable.

We try to teach students to read the tests and understand the test output so they can better learn how to do test driven development. Showing people the exact error skips a very useful part of the learning process.

2 Likes

Okay, buddy, here’s the error message I’m getting:

@Test
@Tag(“task:3”)
@DisplayName(“The incrementTodaysCount method correctly increments today's counts”)
public void itIncrementTodaysCount() {
    birdWatcher.incrementTodaysCount();
    assertThat(birdWatcher.getToday()).isEqualTo(TODAY + 1);
}
**Test Failure**
Message: 
expected: 5
 but was: 4
Exception: org.opentest4j.AssertionFailedError: 
expected: 5
 but was: 4
    at java.base/jdk.internal.reflect.DirectConstructorHandleAccessor.newInstance(DirectConstructorHandleAccessor.java:62)
	at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:502)
    at BirdWatcherTest.itIncrementTodaysCount(BirdWatcherTest.java:46)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
    at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)

//my implemented code

class BirdWatcher {
    private final int[] birdsPerDay;

    public BirdWatcher(int[] birdsPerDay) {
        this.birdsPerDay = birdsPerDay.clone();
    }
    //Task 3
    public void incrementTodaysCount(){
        int incrementAve = 0;
        incrementAve = birdsPerDay[birdsPerDay.length - 1];
        incrementAve = incrementAve + 1;
    }
}

There’s just two lines in the test:

  • The first line calls the incrementTodaysCount, which you’re trying to figure out what it needs to do.
  • The second line calls another method and checks the value.

The error message contains a couple of pieces of information:

  • The first tells you the test is expecting a specific value, but it got a different value.
  • The second part is the stack trace at the bottom, starting with Exception: org.opentest4j.AssertionFailedError. This tells you what error has occurred. It also contains a number of lines that start with at .... This tells you where which lines of code was running when the error occurred.

A number of the lines hit the Java library code (they are generally the ones that are at java.base). But the most interesting one is the at BirdWatcherTest.itIncrementTodaysCount(BirdWatcherTest.java:46). The BirdWatcherTest.java:46 part tells you which file and line it failed on. I’d expect this to be the second line since the error is an assertion and the test has just one assertion (this test happens to have just one, but tests can have more, so this would tell you which assertion is failing when there is more than one).

Coming back to the second line of the test and error message:

  • Can you see what value the method that the test is calling needs to return?
  • Based on that, can you see what incrementTodaysCount needs to do in order for that other method to return the correct value?