• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem with java code - just can not seem to get the answer I want

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This programs adds values to a variable called money, which is initially set at 0, depending on what number is generated (prime, composite, square, or other).

I know if I have two numbers that are the same then it produces 0 all the time.
I am almost certain the multiples of 11, the composites, and the primes are always producing correct values.

For example, when I get the random numbers of 52 (composite), 73 (prime), 13 (prime), 56 (composite), 64 (square), and 23 (prime) I get a returned value of 708, but the answer is supposed to be 716.

52 + 100 + 100 +56 = 308 * 2 = 616 + 100 = 716

There must be something wrong with the loop for the squares, can anybody see anything that might not produce a correct result? Thank you!




(Moderator edit: broke up long lines.)
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You think the calculation should be like this? Is that what your instructions say?

52 + 100 + 100 +56 = 308 * 2 = 616 + 100 = 716



But your code doesn't do that. First it adds all the 100's for the primes... that's 300. Then it does all the * 2's for the squares... that's 600. Then it adds all the composite numbers 52 and 56... that's 708.

It seems like your calculation should go through the six numbers and apply the prime/square/composite rule for each number sequentially, instead of what you have.

You might find it a lot easier if you put the six numbers into an array, that would simplify a lot of your code. It would also make it a lot easier to write the loop which I just suggested.

And welcome to the Ranch!
 
Nick Emm
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you so much! I understand now! You are absolutely right, I am going to change this right now.
 
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just out of curiosity, I have a question about this code:

Does your instructor teach/ask you to write comments like this? If not, did you see this style in a book somewhere? If not, how did you come to this way of commenting code?

I ask because, frankly, these comments are just crud, IMO. They clutter the view of the code and serve no purpose whatsoever. If your code is consistently and properly formatted, the statements to which the open and closed braces belong should be unmistakably clear. That is, the code below is just as clear as the code above, and it's much easier to read, at least it is for me.

Personally, I prefer opening braces to be on the same line as the start of the statement.

Again, the key is consistency and an almost obsessive attention to proper alignment.
 
Nick Emm
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Every Java teacher of mine has stressed that you need comments so I try to put them everywhere necessary. And when you put loops and ifs together it sometimes makes it real hard to tell if you have the correct number of brackets or not. But in this case yours is definitely alot better then mine because there is only like two pairs of brackets I just got into the habit of doing //close if alot. I never thought about the alignment either with each House Number, putting it on a new line that is. I will try to do it like that more often.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick wrote:Every Java teacher of min has stressed that you need comments


Well that's discouraging. I wish these teachers would teach how to write proper comments when it's appropriate and only where necessary. Code that is properly formatted and properly organized should be self-documenting. This approach is becoming more and more the de facto "standard" especially with more progressive development organizations that emphasize Agile development and clean code. It's really easy to find good guidelines for writing good comments.

Another guideline for good comments is that, when absolutely necessary, comments should tell you WHY you are doing something. The WHAT and HOW should be evident when you read the code.

Here's a quick example of code that's not as clear as it can be:

Even without comments, you know which open brace matches which close brace because the code is properly formatted and aligned. However, the code is cryptic - it doesn't TELL you what it's really trying to do. One way to fix that is to add a ton of comments. Another, more superior, approach is to refrain from writing comments and make the code more expressive and self-documenting so that its intent is evident. Code like that practically tells you what's happening.

Compare the code above to the same, only more self-documenting, code below.

With a proper choice of names, the code now explains what it does as well. No comments are necessary because, assuming you are familiar with how many communities assign house numbers, one side of the street has odd numbers and the other side has even numbers, and it is fairly evident which side is which here just from reading the code itself.

Imagine not being the original developer and being asked to find the particular part of the code that determines which houses in the neighborhood are on the north side of the street vs the south side of the street. Which version do you think will help you do that more easily and more quickly?

[ 5th and final edit! ] And here's an example of a good comment for the above code:
The comment reveals how this functionality fits into the "big picture" and helps explain why the code does what it does.
 
Junilu Lacar
Sheriff
Posts: 17644
300
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Nick Emm wrote:I try to put them everywhere necessary.


Just a few more points and I promise I'll stop bugging you about this

It's just that this is one of my pet peeves so please accept my apologies if this comes across as (quite?) a bit overbearing.

As I alluded to earlier, there are times when comments are absolutely necessary to explain why a choice was made to do something. This helps other people who were not privy to the decision-making process for making that choice be able to understand and accept an otherwise questionable aspect of the code and/or design.

However, in my experience (20+ years of professional programming), many programmers just write comments because they are told that they must. Comments that are written to satisfy someone's demand to write them so that code is "well documented" usually just end up defeating that purpose. "Code with lots of comments" is not the same as "well-documented code." On the other hand, self-documenting code usually is.

So, when you find yourself thinking that you need to write a comment, first ask yourself this: "What can I change in my code so that I won't have to write a comment? How can I rewrite the code so that it explains itself more clearly?" If you can learn the different ways you can do this —and there are many— you'll be a better programmer than most.

Good luck.
 
catch it before it slithers away! Oh wait, it's a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic