One method stopping the loop of another method

Asked

Viewed 286 times

2

I have four objects (already instantiated) triangle, square, circle and a last picture, responsible for invoking both methods of each class of the respective objects, in order to draw a house, a sun, a chimney, a window and a roof, and put them in an orderly way to give life to the image.

Before you forget to mention, for teaching purposes, my teacher is using the blueJ - according to him the tool provides better understanding at IOO. So far it’s all working perfectly.

Although we’re starting in object orientation, we’ve already passed through primitive language disciplines, so you can deduce that I still know little about the subject.

My difficulties consist of:

  1. I need to call a method "turnOnFireplace" as the name already says light the chimney, causing smoke to come out of the chimney in animation;

  2. Create another "turnOffFireplace" method to erase the chimney, causing it to stop smoking.

What I did:

I thought of creating a boolean type variable to represent the state of the chimney (lit/erased), I make a loop do-while to create the circle (representing the smoke), I make it move as if it were coming out of the chimney, disappear and return the starting point, doing everything again until the method is invoked turnOffFireplace to change the value of the on variable to false by giving a "break" in the loop. is stopping the execution of the animation, however when I invoke the 'turnOffFireplace' method, by which, should change the value of the variable on to false, and stop loop, breaking it, it does not happen... the loop continues.

I’ll act better with the code below.

 public void turnOnFireplace()
{
    smoke = new Circle();

    on = true;

    do
    {
        smoke.changeColor("black");
        smoke.slowMoveVertical(100);
        smoke.makeInvisible();
        smoke.move(-100);
    }while(on = true);
}
public void turnOffFireplace()
{   
    on = false;
}

The variable I mentioned was declared at the beginning of the class Picture.

What happens is that the loop does not stop. I tried other variations of this logic but did not solve it. How to solve this?

  • 2

    It would just be a typo? while(on == true);. That’s why it’s better to do the right thing: while(on);.

  • You could be a little clearer, because if I put change the value of the variable from true to false... if I understand it, it will stop. in the first right loop?

  • 2

    Could you be clearer in your question? I’m having to imagine things since you don’t have all the information on how this mechanism works. You are causing each passage through the loop the variable to be true, then it never ends. If you want to use the variable without assigning a meaningless value to it, it should work. If it doesn’t work it has problems in other places that you can’t notice with the code posted.

  • Ok! the turnOnFireplace method will be responsible for starting the loop, as this method the variable on ta already receiving true, the loop will start, right? So, already in the turnOffFireplace method modifies the variable on to false, in my reasoning since, the on had its value changed, the loop is broken.

  • 1

    The initial problem is another. Another problem that may be happening is that turnOffFireplace() is never being called. If so, your question should show this.

  • The ok, our fault, the bluej does it graphically, in the project, I create the objects, write the algorithm in each one, and create an object that will start say "the program" whereas, in a pop-up menu opens and in it I choose the methods I will call. type as if I simulate the actions of the project

  • 1

    Just a suggestion, in turnOnFireplace() you could pass the amount of firewood to burns with that number make a for for it to generate the 'puffs of smoke' after this call turnOffFireplace(). One hour the fire ends naturally xD haha.

Show 2 more comments

1 answer

3


From the little bit of code that was placed, you can’t tell if you have an engineering error of the whole class. I will consider that there is something that changes the state of the variable on for false at some point. This will probably happen by calling the method turnOffFireplace(). Whether this occurs concurrently or not is a question that we can not know by the exposed in the code. If the method is not called to change the state of the variable on in another thread, safely I hope, so the only way to change the state of the variable within the loop is by calling the method directly or indirectly.

It’s not directly being called, you can see. I think it’s unlikely that it’s being called indirectly. All methods called do not seem to be suitable for calling turnOffFireplace(), after all are methods that should only trace a circle.

If you don’t solve this there is no solution. When you should call I don’t know, there is nothing in the question that makes it clear. Even if Bluej does it, it’s not clear how.

An alternative is to have an event system controlling the state of the variable and the smoke display must occur by event controlled by the main loop in the game. But there is an engineering work that goes beyond the scope of the question.

Typo

There is another serious problem in the code and even if it solves this engineering issue, it still won’t work. The code:

}while(on = true);

is wrong, what you’re doing there is assigning the value true for the variable on, porting the while will always be true, even if the value of on has been changed elsewhere, it will be wrong every time you go through the loop. This line should be:

} while (on);

If Bluej turns to change the state of the variable somehow, then this should solve the problem.

That’s why I say it’s terrible to use variavel == true. There are people who argue that this is more readable. For me it is less readable and induces typos, like what happened.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.