• 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
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Knute Snortum
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Piet Souris
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Methods or constructor?

 
Ranch Hand
Posts: 263
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im doing a create a character exercise and have this class



are those fields best set using methods or a constructor?

am i right in saying that the constructor would do it immediately and methods could be called at anytime to initialise them.
can you give me a brief example of when you would use either.

 
Saloon Keeper
Posts: 6054
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some questions to ask yourself.
  • Does it make sense for this object to be immutable?
  • Is everything about the object knowable at time of construction?
  • Are there any fields that must be known at construction?
  • Are there any fields that may never change once an object is constructed?
  • Can you identify precisely which fields need to be modifiable after an object is constructed?
  • If a field is not assigned by the constructor is there a value that it can be initialized to that wouldn't cause errors if they never get modified?

  • In your post I might guess that 'name' must be known when the object is constructed and may not be modified thereafter.
     
    Marshal
    Posts: 65114
    247
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Give your packages names all lower‑case, please.
    Don't give a class the same name as an existing class in the java.lang package.
     
    Marshal
    Posts: 6974
    471
    Mac OS X VI Editor BSD Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wondering, what this variable with such data type represents?

     
    wayne brandon
    Ranch Hand
    Posts: 263
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes double silly of me
    Didnt even think of character keyword and packages are always lower case, amended.

    i didnt want to use whole numbers for specialAbility as it can build up slower 1 1,2 1,3 1,4 etc should that be a float?

     
    Saloon Keeper
    Posts: 10434
    223
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No. Floating points type should not be used to represent exact values. If your allowed values have a finite precision, you can use BigDecimal. If your allowed values are rational numbers with infinite precision, you will want to write a custom Fraction class.

    What does a value of 1.2 for specialAbility mean? Is it a score? Then you might want to add a suffix like Score to your variable name to make that clear.
     
    Sheriff
    Posts: 13570
    223
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you want special ability to grow more slowly, you can still use an int and a scaling factor, say 10, and use integer division.

    Say you use 10 as a scaling factor, then both 21 and 29 will give you 2 (using integer division, 21/10 and 29/10 both give 2) so special ability will have to be incremented 10 times before it goes from 2 to 3.
     
    wayne brandon
    Ranch Hand
    Posts: 263
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    This set method for the name of the character works



    am i going about it the right way, or is this ugly?
     
    Rancher
    Posts: 4190
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The UI should not be part of your character code.

    The setName method should simply take a name String and set the value in the class.
     
    wayne brandon
    Ranch Hand
    Posts: 263
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    firstly the method should be



    no need for String as its not returning anything.

    so should all input output be done in the main class?

     
    Dave Tolls
    Rancher
    Posts: 4190
    47
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    No, it should be done in one or more UI classes.
    That way, when you decide to change the type of UI (eg move from a console UI to a Swing or FX UI) you don't have to change the "business" logic of your code to any great extent.
     
    Carey Brown
    Saloon Keeper
    Posts: 6054
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    A setter for 'name' should follow this template by Java conventions:
    Notice, no user interface allowed. If you want a method the prompts the user for values then call it something else, perhaps
    Or even better, don't put UI code inside this class at all.
     
    wayne brandon
    Ranch Hand
    Posts: 263
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok all good thanks will rearrange

    what about calling the function to ask again for the name if the user says no, is that sloppy?

    lastly will a UI Class (i have created a UserInterface class)
    use static methods like my Keyboard class?
     
    wayne brandon
    Ranch Hand
    Posts: 263
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I have realised the main problem I'm having and if i can sort this out I think I will make huge headway

    I have the following classes:

    Main
    FantasyCharacter
    UserInterface
    KeyboardUtility

    and I dont know how to make them work together.

    I want the program to ask the user for a character name and print it out at the end (Along with other functionality I will add as I go along)

    the name instance variable is in the FantasyCharacter class



    and i have a setName method in the same class



    now you guys have told me to add a UserInterface class to handle my input calling and text display.

    but I cant work out how from the interface class I can get an answer into



    this is the major problem I'm having is the linking up and communication between objects methods etc etc
    well i'm sure this exercise is useful



     
    Carey Brown
    Saloon Keeper
    Posts: 6054
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    If you have a UI class you can design it like a utility with static methods. You might have a method
    As a user, I would be annoyed with a program that was constantly bugging me, "is this correct?". In the example above the character has a number of attributes, I'd have the program get them all, then print them all, and only then ask if the character is correct. Not for each individual attribute. If they say no, then repeat the questions using the previous input as the promptXXXWithDefault() value. This allows them to quickly step through the prompts hitting Enter until they get to the attribute they want to change. -- Having said all that, I think your first cut of the program should just assume all the entries to be correct, and add this additional logic after you've got all that debugged.
     
    wayne brandon
    Ranch Hand
    Posts: 263
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Yes I agree as usual, thanks Carey will continue with it tomorrow
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!