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

Add code to the following method

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have written a method called 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.

I dont know how to do the rest of the question, 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.


the code for spinbody:

 
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why doesn't your stick figuree have methods to widen and narrow it? Rather than writing xyz.setWidth(xyz.getWidth() − 2); you should be writing xyz.getNarrowerBy(2); One of the principles of object‑orientation is that the object does the work; you don't have to make your method do the work (it's part of the Single Responsibility Principle). Similarly it should probably have it moveRight() and moveLeft() methods.

Yuo may be able to implement moveLeft(10) as moveRight(−10), but that doesn't look elegant.
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would also suggest your method might well read like thiis:-As (??)Junilu told you on Monday, you want to divide the task into smaller tasks. Now you are doing the four operations in four methods; each method does one thing only, which makes for easily maintainable code.
Another maintenance problem: your indentation is inconsistent. It is you who will have the most trouble reading your own code, so make sure to indent it correctly. Also don't doble‑space it.

How is your delay() method implemented? Does 20 mean 20ms?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the code above is how I had to write it. I showed it to my tutor and its correct as its a mock question and answer.
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes 20 means 20ms. Also thank you to all those who have helped and are helping. much appreciated
 
Marshal
Posts: 6670
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't double-space your code.

Review HowToFormatCode (that's a link).

Line 7 won't compile.

Does this.body have setColour() and getColour() methods?  How would you use these to flip the color?

You have saved bodyWidth before your first while loop.  How would you use this in the condition of the second while loop?
 
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lilly Rankine wrote:

Line 5 doesn't do anything, 'bodywidth' is never used.
Line 7 won't compile.
All the uses of 'this' are unnecessary because it is a non-static method.
Where is the method to draw the body after it has been resized or moved?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It compiles in BlueJ and I cant change that. I just need to add to it and have no clue how.. if(this.body =(OUColour.RED()); is what I have started with but its wrong
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

lilly Rankine wrote:It compiles in BlueJ and I cant change that. I just need to add to it and have no clue how.. if(this.body =(OUColour.RED()); is what I have started with but its wrong


I don't understand. You can't fix the fact that it won't compile? Or that 'this' is useless?
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it compiles. Line 7 is not compiling for you because there is a / which shouldnt be there. I must have put it there by accident. oops! but I am very stuck with this one. I dont know how to change the colour from red to yellow once the class object 'stickFigure' turns and widens to its full shape, changing colour at the same time.
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Knute Snortum
Marshal
Posts: 6670
177
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Does this.body have setColour() and getColour() methods?  How would you use these to flip the color?

 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
would it be somthing like if(this.body.colour = OUColour.RED){
     this.body.setColour(OUColour.YELLOW)}
     else{
        this.body.set
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(this.body.colour = OUColour.RED){
     this.body.setColour(OUColour.YELLOW);
  }
  if (this.body.colour = OUColour.YELLOW){
  }
     else {
        return this.body.colour = OUColour.RED;
     }
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone correct me please. As I have tried at least
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to remember the difference between  == and =, but if you are writing pseudo‑code, you can use the symbols = and :=.
You can't return something from one path of execution and not return the same sort of thing from a different path. You either return the same sort of thing from all paths of execution or you return nothing from any of them (void method).
Why have you got a nested if in the second part of the code, rather than an else if?
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how would I write it then? that is what i am stuck on. If you can show me please
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   if(this.body.colour =OUColour.RED){
     this.body.setColour(OUColour.YELLOW);
  }
 else
  {    
  this.body.setColour(OUColour.RED);
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is not a boolean expression. The result of this expression is Colour. This will give you a compile error.
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if(this.body == OUColour.RED)

error incomparable types triangle and ou.OUColour
 
Campbell Ritchie
Marshal
Posts: 67533
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need colour to the left of the == possibly as ...body.colour

Couldn't you have worked that out without asking? You had ...body.colour in an earlier post.
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok so there is no error now. But when I execute the following code. The body shrinks but does not expand back to its original width??

 
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As it is shrinking, what colour is it? Then look at your logic.

If you don't clean up your indentation you'll end up misinterpreting your logic.
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i managed to sort that out!
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
NOPE NOW its not spinning!
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
lilly Rankine
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
its not expanding back!

public void spinBody()
        {
        int bodyWidth = this.body.getWidth();
         while (this.body.getWidth() > 0)
      {  
       this.body.setWidth(this.body.getWidth() - 2);
          this.body.setColour(OUColour.RED);
       this.body.setXPos(this.body.getXPos() + 1);
         this.delay(50);}

  while (this.body.getWidth() < 0)// part (d)(ii) complete the while loop condition here to expand the body )
      {
     
        this.body.setWidth(this.body.getWidth() + 2);
           this.body.setColour(OUColour.YELLOW);
        this.body.setXPos(this.body.getXPos() - 1);
        this.delay(50);
      }      
   }
 
Carey Brown
Saloon Keeper
Posts: 6796
64
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
 
Cob is sand, clay and sometimes straw. This tiny ad is made of cob:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!