<< Chapter < Page Chapter >> Page >

The value returned from the Math.sqrt method can be considered to be either positive or negative. Only the positivevalue is returned by this method. When the returned value is considered to be positive and the results are plotted, one-half of a circle is displayed as shownby the BLUE line in Figure 10 . Similarly, when the returned value is considered to be negative and the results are plotted, the other half of the circle isdisplayed as shown by the GREEN line in Figure 10 .

Listing 10 . The method named function for Circle01. double function(double rVal,double xVal){ double yVal = Math.sqrt(rVal*rVal - xVal*xVal + 0.0000000001);return yVal; }//end function

If the expression passed to the sqrt method is negative, the value returned by that method will not be valid. Because the computations inthis program are performed as type double , and because all floating point computations are only estimates of the truth, the computed differencebetween the radius squared and the x-value squared can actually be an extremely small negative value when it should be zero. A small positive fudge factor wasadded to prevent that value from going negative due to small floating point computational errors. (This sort of thing is often required when doing a lot of floating point computations. There are various ways to do it and there may bebetter ways than the one used here.)

Draw half the circle in BLUE

The code in Listing 11 draws the BLUE half of the circle shown in Figure 10 .

Listing 11 . Draw half the circle in BLUE. for(int cnt=0; cnt<=100;cnt++,xVal += xInc){ //Get a y-value for the given x-value.yVal = function(rVal,xVal); //Apply the offsets and scale the resultscol = (int)((xOff+xVal)*xScale); row = (int)((yOff+yVal)*yScale);//Move to the first point without drawing a line because the// pen is not down. Translate the origin to the center in the // process.turtle.moveTo(col + world.getWidth()/2, row + world.getHeight()/2);//Lower the pen in order to draw a line from each point to the // next point.turtle.penDown(); }//end for loop

Having initialized xVal and xInc on the basis of the radius in Listing 9 , the code in Listing 11 is essentially the same as the code in the earlier programs in this lesson.

Draw the other half of the circle in GREEN

The code that draws the GREEN half of the circle is shown in Listing 18 . It is essentially the same as the code in Listing 10 except that the sign on yVal is flipped from positive to negative as discussed above .

Different line width

One other difference that I haven't mentioned yet is trivial but interesting. You may have noticed that the line width in Figure 10 is about twice that in Figure 9 . I purposely did that to illustrate that one of the useful features of graphing functions with a turtle is that you can control the width of the line.The importance of that capability will become apparent in the next section.

That concludes the interesting differences between this program and the previous programs.

Questions & Answers

how to study physic and understand
Ewa Reply
what is conservative force with examples
Moses
what is work
Fredrick Reply
the transfer of energy by a force that causes an object to be displaced; the product of the component of the force in the direction of the displacement and the magnitude of the displacement
AI-Robot
why is it from light to gravity
Esther Reply
difference between model and theory
Esther
Is the ship moving at a constant velocity?
Kamogelo Reply
The full note of modern physics
aluet Reply
introduction to applications of nuclear physics
aluet Reply
the explanation is not in full details
Moses Reply
I need more explanation or all about kinematics
Moses
yes
zephaniah
I need more explanation or all about nuclear physics
aluet
Show that the equal masses particles emarge from collision at right angle by making explicit used of fact that momentum is a vector quantity
Muhammad Reply
yh
Isaac
A wave is described by the function D(x,t)=(1.6cm) sin[(1.2cm^-1(x+6.8cm/st] what are:a.Amplitude b. wavelength c. wave number d. frequency e. period f. velocity of speed.
Majok Reply
what is frontier of physics
Somto Reply
A body is projected upward at an angle 45° 18minutes with the horizontal with an initial speed of 40km per second. In hoe many seconds will the body reach the ground then how far from the point of projection will it strike. At what angle will the horizontal will strike
Gufraan Reply
Suppose hydrogen and oxygen are diffusing through air. A small amount of each is released simultaneously. How much time passes before the hydrogen is 1.00 s ahead of the oxygen? Such differences in arrival times are used as an analytical tool in gas chromatography.
Ezekiel Reply
please explain
Samuel
what's the definition of physics
Mobolaji Reply
what is physics
Nangun Reply
the science concerned with describing the interactions of energy, matter, space, and time; it is especially interested in what fundamental mechanisms underlie every phenomenon
AI-Robot
what is isotopes
Nangun Reply
nuclei having the same Z and different N s
AI-Robot
Got questions? Join the online conversation and get instant answers!
Jobilize.com Reply

Get Jobilize Job Search Mobile App in your pocket Now!

Get it on Google Play Download on the App Store Now




Source:  OpenStax, Object-oriented programming (oop) with java. OpenStax CNX. Jun 29, 2016 Download for free at https://legacy.cnx.org/content/col11441/1.201
Google Play and the Google Play logo are trademarks of Google Inc.

Notification Switch

Would you like to follow the 'Object-oriented programming (oop) with java' conversation and receive update notifications?

Ask