• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

API/UML Question

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here is the API and UML for the current project I'm working on.



So my question is, is the order/structure of what I have right now correct according to the API/UML provided?


 
Saloon Keeper
Posts: 9742
80
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
  • The constructor is like the one you commented out.
  • countOccurrences computes a count by scanning 'data' for 'word'.
  • What is a 'stem'?
  • No need for a main().

  •  
    Jon Fil
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

  • The constructor is like the one you commented out.
  • countOccurrences computes a count by scanning 'data' for 'word'.
  • What is a 'stem'?
  • No need for a main().



  • Oh okay, so it's supposed to be like this?


    -But then why are there two "String[] data" for? One with private and one in constructor?
    -For getStems if you created the class with the following data: “This”, “Data”, “Long” and then passed in a value of 3 for len it should return: “Thi”, “Dat”, “Lon” as an array of Strings.  If instead I passed in a value for 5 for len it should return: “This”, “Data”, “Long”
    -I only added main for whenever I do a quick test
     
    Carey Brown
    Saloon Keeper
    Posts: 9742
    80
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Looks good.

    You have two variables named 'data', because this is how a typical constructor looks, using parameter names that are identical to field names.
     
    Jon Fil
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:Looks good.

    You have to variables named 'data', because this is how a typical constructor looks, using parameter names that are identical to field names.



    But what's the difference between the two?  I know with public NLP(String[] data) I won't be able to create instance of NLP in main().
    And also, I only need NLP(String[] data) and not NLP(), correct?
     
    Carey Brown
    Saloon Keeper
    Posts: 9742
    80
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jon Fil wrote:

    Carey Brown wrote:Looks good.

    You have to variables named 'data', because this is how a typical constructor looks, using parameter names that are identical to field names.

    But what's the difference between the two?  I know with public NLP(String[] data) I won't be able to create instance of NLP in main().
    And also, I only need NLP(String[] data) and not NLP(), correct?


    Your UML doesn't call for a no-parameter constructor.

    You should be able, from main(), to do
     
    Jon Fil
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Carey Brown wrote:

    Jon Fil wrote:

    Carey Brown wrote:Looks good.

    You have to variables named 'data', because this is how a typical constructor looks, using parameter names that are identical to field names.

    But what's the difference between the two?  I know with public NLP(String[] data) I won't be able to create instance of NLP in main().
    And also, I only need NLP(String[] data) and not NLP(), correct?


    Your UML doesn't call for a no-parameter constructor.

    You should be able, from main(), to do



    Omg wow!  I never thought of that.  I only tried expecting that it'll grab stuff from NLP(String[] data)...  But wow, everything in my code all makes sense now! Thank you!

    I will be posting my code soon
     
    Jon Fil
    Ranch Hand
    Posts: 36
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Here is the instruction.





    Here's my completed(I think) code.



    @Anyone,
    Feedback would be greatly appreciated, thanks!
     
    Sheriff
    Posts: 17357
    300
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    The order that methods appear in source doesn't really have to match the order they appear in the UML diagram. On the other hand, there's no harm in matching the order either but I wouldn't take any points off for not matching the order. That would be silly, in my opinion.

    This:

    can be written as

    or

    Most importantly, keep display separate from other concerns. In both countOccurrences() and getStems() you have calls to StdOut.println(). The names of these methods do not suggest that they should be doing any kind of display to the console. They should only be doing what they claim to do, which is to count occurrences of a word and return the "stems" as a String array. Display of those results should be done elsewhere in the program. This will keep your methods focused on doing only one thing and doing it well.

    The calls to StdOut.println() can be moved to the main() method or in the case of the display logic in getStems(), to a different method whose job is only to display the strings in data[].
     
    Carey Brown
    Saloon Keeper
    Posts: 9742
    80
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    getStems() will overwrite the data array field. I'm guessing that what is really needed is that a new String[] is allocated and the stems are put in there and then the new array is returned, thus leaving the original 'data' array intact.
     
    Carey Brown
    Saloon Keeper
    Posts: 9742
    80
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

     
    If you like strawberry rhubarb pie, try blueberry rhubarb (bluebarb) pie. And try this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic