Forums Register Login

Did I do this exercise right?

+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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.






+Pie Number of slices to send: Send
 

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.

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
 

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
+Pie Number of slices to send: Send
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…)?
He loves you so much! And I'm baking the cake! I'm going to put this tiny ad in the cake:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 483 times.
Similar Threads
Need help writing a program
Problem with printf output
Understanding small program
Need help with doing a calculation in Java
short data type question
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 13:38:21.