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:
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;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?
It would just be a typo?
while(on == true);
. That’s why it’s better to do the right thing:while(on);
.– Maniero
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?
– Marcus Vinicius
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.– Maniero
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.
– Marcus Vinicius
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.– Maniero
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
– Marcus Vinicius
Just a suggestion, in
turnOnFireplace()
you could pass the amount of firewood to burns with that number make afor
for it to generate the 'puffs of smoke' after this callturnOffFireplace()
. One hour the fire ends naturally xD haha.– rray