Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

Move all parts of the Snowman with setLocation (Wheels)  RSS feed

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Assignment
Modify the snowman so it can be placed at a valid position in the window. Do this by implementing a setLocation() method in the Snowman class which moves all the components of the snowman, including Hat and SpeechBubble (look at the Hat class for an example how this can be done). Do not forget to modify the constructor so that you specify the starting position of the Snowman using your new function.  
----------------------------------------------------------------------------------------------------------------------------------

Hello there
I have some trouble with implementing a setLocation() that moves the Snowman +  Hat + SpeechBubble.
I got setLocation for  the parts of the snowman working, eye, body etc with this code:
Snowman.java


All the parts of the snowman are then moved with this code:
SnowCartoon.java


However i don't understand how to get the hat and the speech-bubble to move with the snowman.  
My guess is that i should in the snowman.java class but eclipse says that _hat cant be resolved to a variable.
When i create hat in the SnowCartoon.java class there's no problem. But then i don't know how to make them all move together.

Here's the whole code:
----------------------------------------------------------------------------------------------------------------------------------
Hat.java


Snowman.java


SnowCartoon.java
 
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That suggests to me that you want to have all the part as fields in the Snowman class. Now you can implement setLocation() by moving from the old location and implement move() methods on all the parts.
 
Danielx Anderssonx
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That suggests to me that you want to have all the part as fields in the Snowman class. Now you can implement setLocation() by moving from the old location and implement move() methods on all the parts.


Yes i solved the problem by creating hat and bubble in the snowman class instead.

 
Danielx Anderssonx
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:That suggests to me that you want to have all the part as fields in the Snowman class. Now you can implement setLocation() by moving from the old location and implement move() methods on all the parts.


But my solution is bad because the next part of the assignment wants you to create a new snowman, but with a different text in the speech-bubble. Hmmm...
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you will have to pass the speech bubble text to a constructor or setXXX() method.
 
Danielx Anderssonx
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Maybe you will have to pass the speech bubble text to a constructor or setXXX() method.


I'm going to pass the speech bubble to a constructor. But could you show me how that code would look?
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Danielx Anderssonx
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:




When defining the text with:
How can i change the code below to respond to that?
 
Danielx Anderssonx
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:


i figured it out.


 
Sheriff
Posts: 5458
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good job figuring it out!

Just one point: using an underscore (_) in variable names is frowned upon and, I believe, is now deprecated. And either way, using any character to signal that a variable is a field is frowned upon.
 
Sheriff
Posts: 12748
210
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Knute Snortum wrote:Just one point: using an underscore (_) in variable names is frowned upon and, I believe, is now deprecated. And either way, using any character to signal that a variable is a field is frowned upon.


The tutorial for Java 8 has this:

Java 8 Tutorial wrote:The convention, however, is to always begin your variable names with a letter, not "$" or "_". Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged.



The "_" may become a keyword in future versions of Java. Starting in Java 9, you cannot specify an underscore by itself as an identifier name and your code will not compile if you try to do so.

People who use a special marker character to designate a member variable will often claim that the practice is useful because you can immediately distinguish member variables from local variables. Many Android apps use "m" to designate member variables and in fact, that seems to be a convention. My personal opinion is that it's an unfortunate precedent that has now become accepted convention.

The argument against that convention is that if you cannot easily discern local from non-local variables, then maybe your methods and/or your classes are too big and you've violated Kent Beck's 4th Rule for Simple Design: Code is small (classes, methods, scope, etc.)  If you keep methods and classes small, it's not that hard to see which variables are declared locally.

(Hungarian) Name warts like "m" and "_" also violate one rule of Clean Code and good naming: Choose names that are pronounceable.
 
Ranch Hand
Posts: 355
8
BSD Debian Open BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

...Additionally, the dollar sign character, by convention, is never used at all. You may find some situations where auto-generated names will contain the dollar sign, but your variable names should always avoid using it. A similar convention exists for the underscore character; while it's technically legal to begin your variable's name with "_", this practice is discouraged.



$ and sometimes _ are currently generated/used from tools that's why is discouraged use them explicitly
 
Campbell Ritchie
Marshal
Posts: 61766
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it was in Java10, but _ has since become a keyword. Like const and goto, it is never used.

By the way: while on about new keywords, I know var isn't a keyword, but is it a reserved word?
 
Harry Kar
Ranch Hand
Posts: 355
8
BSD Debian Open BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie.... [/tt wrote:isn't a keyword, but is it a reserved word?


In Java keyword Vs reserved word have the semantics as in common knowledge? mean (i use a "recursive" definition)
  • Keywords: are actual(=part of the actual languages syntax) reserved words
  • Reserved words: aren't  keywords
  • ?
     
    Master Rancher
    Posts: 3002
    105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Danielx
    Does your _bubble with TAIL_DIR_LEFT always fit on screen? If not, do you have also a TAIL_DIR_RIGHT, and if so, shouldn't the location of it depend on the message?
     
    Campbell Ritchie
    Marshal
    Posts: 61766
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Harry Kar wrote:. . . In Java keyword Vs reserved word have the semantics as in common knowledge? . . .

    No.

    The keywords are all devoid of semantic content, but they have syntactic significance and may (eight of them) determine types of things.
    The three traditional reserved words had semantic content but didn't determine syntax. All the keywords were also reserved. There are some languages which have non‑reserved keywords.
     
    Harry Kar
    Ranch Hand
    Posts: 355
    8
    BSD Debian Open BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Campbell Ritchie wrote:

    Harry Kar wrote:. . . In Java keyword Vs reserved word have the semantics as in common knowledge? . . .

    No.

    The keywords are all devoid of semantic content, but they have syntactic significance and may (eight of them) determine types of things.
    The three traditional reserved words had semantic content but didn't determine syntax. All the keywords were also reserved.


    with mine

    ... have the semantics as in common...


    meant significance, meaning

    your

    The keywords are all devoid of semantic content, but they have syntactic significance


    corresponds exactly to mine

    Keywords: are actual(=part of the actual languages syntax) reserved words


    or I miss something?

    and may (eight of them) determine types of things


    what are they 8?

    There are some languages which have non‑reserved keywords.


    such as...?
     
    Campbell Ritchie
    Marshal
    Posts: 61766
    193
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Harry Kar wrote:. . .

    and may (eight of them) determine types of things


    what are they 8?

    boolean byte,, char, double, float, int, long and short.

    There are some languages which have non‑reserved keywords.


    such as...?

    Old Fortran, for one.

    Maybe I was mistaken about all keywords; this and super probably have semantic content.
     
    Harry Kar
    Ranch Hand
    Posts: 355
    8
    BSD Debian Open BSD
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Ah yes i forgot about Fortran some time has passed back then
    Thanks Campbell
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!