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

Calculate the Product of an Array

 
Greenhorn
Posts: 10
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I have an exercise where the goal is to output 15 integers into a 5x3 array, Output the values in chart format and calculate and output the product of each row beside the appropriate row. I am having trouble with getting and outputting the product for each row; as of now my code (below) can output the product for ALL 15 integers entered, but not for each row. Code is below, any help is appreciated!


 
Saloon Keeper
Posts: 6243
58
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • You are not resetting the product before processing each row.
  • You are not printing the product after processing each row.
  •  
    Mona Smith
    Greenhorn
    Posts: 10
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you so much! It works now!
     
    Carey Brown
    Saloon Keeper
    Posts: 6243
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
     
    Mona Smith
    Greenhorn
    Posts: 10
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Here is the correct code:


     
    Sheriff
    Posts: 6268
    167
    Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good job!  I have only a couple of comments.

    * The naming convention for a non-constant variable is just lowercase letters.  So PRODUCT would be product.  Uppercase letters are only used if the value is constant.
    * Comments are good, but be careful of comments that do nothing other than state what can be seen clearly from the code.  For instance:
    It's obvious from the code that you are printing numbers.  Leave that comment out.
     
    Marshal
    Posts: 14053
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Good job on working out the solution.

    I'd like to point out something about code comments though. Having all those comments like that is ok as a beginner. Your teacher might even encourage to write comments like that. However, they are very noisy and do nothing other than to obscure the code and make it way more difficult to read.

    Comments like this are pointless and should really be deleted:

    So is this:

    Comments are often code smells. Avoid comments that try to explain WHAT the code is doing or HOW it's doing it. Strive to make your code more expressive and self-explanatory instead. You're an author, so make your code tell a story that others can follow along with and actually enjoy reading. The best comments are the ones that tell you WHY you're doing something.

    This is a particularly bad kind of comment:

    How does this line "loop until condition is met"? The comment oversteps its bounds and only causes the astute reader consternation.

    Lastly, this is one of the worst kinds of comments because it's both pointless and redundant:

    Proper indentation should make it clear what statement this closing brace terminates. There's no need to harp on about it in a comment.

    Again, I realize that this is probably something you've been encouraged to do by your instructor. If not, then you've gone overboard. If it is, then just play along but know that most coding standards call for meaningful comments only. That is, comments that explain why something is being done, not what or how.

    Example of good comment:
     
    Carey Brown
    Saloon Keeper
    Posts: 6243
    58
    Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The Java compiler permits the brackets to appear after the variable name. This happened early on in the development of the Java compiler because some of the syntax conventions were borrowed from other languages. Today, this is frowned upon and the brackets should immediately follow the type, as in:
     
    Mona Smith
    Greenhorn
    Posts: 10
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you all for the help! And yes, my teacher requires that we comment on at least every other line of code to make sure we understand what we are doing. And thank you, I will put the square brackets immediately following the type from now on.
     
    Junilu Lacar
    Marshal
    Posts: 14053
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Mona Smith wrote:And yes, my teacher requires that we comment on at least every other line of code to make sure we understand what we are doing.


    That's quite unfortunate. Again, I understand why you might want to do that as a beginner but it's quite ironic that your instructor thinks he/she is "making sure you understand" by telling you to write comments that make the code more difficult to understand.
     
    Junilu Lacar
    Marshal
    Posts: 14053
    234
    Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One thing I'm happy to say though, Mona, is that your code is one of the rare examples we see around here of very well formatted and indented code. Keep up the good work on that. It may seem like a trivial thing but trust us when we say, it's VERY important to format and indent your code properly. So thank you.

    In fact, I think that deserves a cow, so have one on me.

    And definitely, showing effort to figure things out and fix your code, then sharing what you did deserves praise.
     
    Mona Smith
    Greenhorn
    Posts: 10
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Wow thank you so much! It means a lot, I really needed the motivation!
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!