• Post Reply Bookmark Topic Watch Topic
  • New Topic

Did I do this exercise right?  RSS feed

 
David Holcomb
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi. I'm currently learning Java from a book called "Introduction to Java Programming 9th Edition." I'm only on the first chapter so I only learned how to display text. I'd like to know if I got the answer to an exercise correct, because I cannot view answers to odd numbered exercises.

Here's the question:


Here's my solution:

Here's a solution that I found online:


Are either right or wrong? How would you approximate pi using just the System.out.println function?
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, when you divide ints the result of the division is also an int.

What does your program print out when you run it?
 
Stefan Evans
Bartender
Posts: 1837
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My suggestion: Write a java program to execute this line of code. Run your program. See what it outputs.

Does it output something that looks remotely like the value of pi?
Does the solution you found online get a different answer?
Given what you know about java thus far, can you think of any reason to explain these results?
What is the difference in java between 1 and 1.0 ?
1/3 and 1.0/3 ?

How would I do it?
Using just the System.out.println you have done about as good as you can (unless you want to keep typing more terms into that expressoin)

Note that that sequence is infinite - you can keep adding/subtracting terms on that expression, and you will get closer and closer to the true value of pi.
So rather than using one println, I would use a for loop. However I doubt you have covered loops in chapter one of your textbook. Presumably that solution will come later.






 
David Holcomb
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:In Java, when you divide ints the result of the division is also an int.

What does your program print out when you run it?


NVM, my program isn't correct.

My program prints out 4, which is over 3.14.

 
Liutauras Vilda
Sheriff
Posts: 4922
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you counted on a paper, what result do you expect? Then, have you tried to run the code and see what result you get? Did they match?

In fact, the answer is YES and NO. Since you're doing division of int type numbers, the floating point part is discarded. If you're not declaring variables before calculations, then only the one way to get right results is divide them in the way its done in your found online example.
 
David Holcomb
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stefan Evans wrote:
However I doubt you have covered loops in chapter one of your textbook. Presumably that solution will come later.



Yeah, that's coming later. The first chapter was basically about the history of Java, types of errors, and computer parts(CPU, Memory, output, input, etc).

Here's a new solution that I've created based off of the online solution I found:


The output is: 3.017071817071818 which is closer to pi than the online example I found.

Edit:

As I learn Java, I'll be able to easily do something like this in the future. Thanks for all the help everyone
 
Campbell Ritchie
Marshal
Posts: 56562
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The formula is not a single line. It is an infinite progression. You have to keep the formula running until the successive terms are so small that they make no difference to the calculated result.
The formula for π only converges very slowly, so you will need several thousand terms before they get so small that they vanish from the calculations. You probably cannot achieve that untli you can write a loop.

Have you worked out why you got 4 in the first instance? Did you realise that is 4 × (1 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0 + 0…)?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!