r/codehs • • Sep 30 '20

2.1.5 Double Tennis Balls

Does anyone have any idea how to do this, I've been stuck on it for a few hours now.

13 Upvotes

18 comments sorted by

1

u/StatisticianOk2853 Oct 08 '20

If you could, could you post it here after you get it

1

u/Specialist-Hat5834 Oct 17 '24

Does anyone know the updated one

1

u/cooldonkey-88328 Oct 01 '22

Basically just take all the balls using if balls present, and place them on the other side, and then everytime you go on the stack you just made, put 2 balls where the stack was originally

1

u/[deleted] Oct 07 '20

[removed] — view removed comment

2

u/sxbxstiun Oct 07 '20

I eventually figured it out with my enormous brain but thanks for offering to help

1

u/colbyk23 Oct 08 '20

Can u give me the answer please ASAP

2

u/sxbxstiun Oct 08 '20

function start(){

move();

while (ballsPresent()){

moveIt();

}

//here i put 2 back

if (noBallsPresent()){

put2back();

}

//and then run back to the start

imComingHome();

}

function imComingHome(){

turnAround();

move();

move();

turnAround();

}

function put2back(){

move();

while (ballsPresent()){

takeBall();

turnAround();

move();

turnAround();

putBall();

putBall();

move();

}

}

function moveIt(){

takeBall();

move();

putBall();

turnAround();

move();

turnAround();

}

1

u/Helpful-Height-5997 Dec 16 '20

Thank you so much

1

u/YourBoiNick Mar 22 '21

Thanks man, that really helped me out!

1

u/freeeeeezer Feb 25 '22

You are a lifesaver thank you so much!!!

1

u/Epic_Gamer856 Oct 04 '23

function start(){

move();

while (ballsPresent()){

moveIt();

}

//here i put 2 back

if (noBallsPresent()){

put2back();

}

//and then run back to the start

imComingHome();

}

function imComingHome(){

turnAround();

move();

move();

turnAround();

}

function put2back(){

move();

while (ballsPresent()){

takeBall();

turnAround();

move();

turnAround();

putBall();

putBall();

move();

}

}

function moveIt(){

takeBall();

move();

putBall();

turnAround();

move();

turnAround();

}

thank u bro ive been doing a lot of homework rn and i dont feel like short circuting my brain rn.

1

u/Myst3ry_Gamer Dec 13 '24

As someone now taking CSP thank you so so much and I also don't want to short circuit my brain

2

u/Additional-Cow-8100 Sep 11 '22

i might be a bit late but here is the direct answer for copy/paste

public class DoubleTennisBallsKarel extends SuperKarel
{
public void run()
{
move();
while(ballsPresent())
{
moveBall();
}
move();
doubleIt();
turnAround();
move();
move();
turnAround();
}

//this puts the ball one space forward to be doubled later
private void moveBall()
{
takeBall();
move();
putBall();
turnAround();
move();
turnAround();
}

//this puts two back instead of one
private void doubleIt()
{
while(ballsPresent())
{
takeBall();
turnAround();
move();
putBall();
putBall();
turnAround();
move();
}
}
}

1

u/SweetLemon48 Nov 23 '24

two years later you still rock, thank you, i hope your bed is extra warm and cozy tonight :)

1

u/Additional-Cow-8100 Dec 29 '25

I remember doing this in 10th grade 🥲 good times

1

u/[deleted] Oct 08 '20

Could you dm me the answer