This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

The following code is not working properly

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So I have to
Uncomment the provided method spinBody() which narrows the body in a loop till it has width 0, centring it on each iteration, before expanding it again to its original size.

Add code to this method to:

• change the colour of the body to yellow if it is red, or red if it is yellow once it reaches its minimum width

• complete the second while loop condition to restore the stick figure’s body to its width at the start of the method

You may assume in writing this method that the body of a StickFigure will always have a width that is an even number.


Now when i execute the code below it does not expand back to its orignal size! please help I have been at this for days.





full code for reference:

   
 
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please format your code before posting it. I have done it for you:



Unfortunately, your code does not compile at all for me to help you. What is "body" and OUColour ?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
body is the variable and oucolour is basically the colour
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that still does not help ! you have not provided the full code.

By the way, I looked at your code and this is what I understand...
Lets say width is 20 pixels to begin with (I am assuming pixels since you didnt provide any information)

your first while loop says until it's greater than 0, keep decrementing it by 2 till its less than 0 ...
so, your width now gets 18, 16, 14 ..... 0, -2

now, with width as -2, you are again running a while loop that increments it by 2 till it's less than 0
so, your width is now 0

Is that what you want to achieve ?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public Triangle()
  {
     super();
     this.colour = OUColour.RED;
     this.xPos = 0;
     this.yPos = 0;
     this.width = 20;
     this.height = 50;
  }

the width is 20 originally
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so basically its a varible body which is represented by a triangle that spins and when execute it turns but stops instead of going back to its original position. back to a triangle
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's my Short, Self Contained, Correct (Compilable), Example (SSCCE) for your code:



You can run my program directly without requiring any changes. It contains the basic code where you're stuck and nothing more !!
Can you work out what is the issue with the above code ?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
basically I need it to be 20 again
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nope! Im so stuck :'o
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, always reduce your problem to just the basic steps and nothing more. The Triangle class, spinMethod() are all unnecessary details that do not demonstrate the actual problem you are facing.

Are you able to understand the code I provided ?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes so the body spins half way. when its about to turn it does not expand back just stops
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have to use the code as it was provided by my tutor
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lilly Rankine wrote:I have to use the code as it was provided by my tutor

It does not matter, if you're able to spot the issue with my provided code, you should be able to fix yours.
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
while (bodyWidth < 0)... that part? before it was >0 but its still not working :/
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can you please point it out. I dont know what to do. i have an hour to g. been trying to figure it out for 3 days
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, the issue lies with your second while loop condition. In plain english it says :Increase the value of bodyWidth till it reaches zero.
What it should say is : Increase the value of bodyWidth till it reaches the original width value.

Can you fix the issue now ?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did that too.. so I did
while (this.body.getWidth() <= bodyWidth)
 
salvin francis
Bartender
Posts: 2472
108
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Does it work after your changes ? At this point I'd suggest you need to look at how loops and variables work. Merely guessing wont help you. I'd suggest keeping off animating stuff and studying the basics of core java first. Here's a good link to begin with : https://docs.oracle.com/javase/tutorial/

 
Saloon Keeper
Posts: 6772
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This topic is a continuation of THIS topic. Some of these issues have already been addressed there.
 
I'm all tasted up for a BLT! This tiny ad wants a monte cristo!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!