Methods for Area and Circumference of a Circle

 Working With Circles.  Much of what is of interest with respect to circles can be determined if the radius is known.  This Java applet will compute the area = πr2 circumference = 2πr We will develop a method to compute each of these that is passed the radius as input. This first set of code is the program for CircleInfo.java.

 Before we discuss the program you should develop the HTML file called CircleInfo.html.



 The results of the program look like the following when computing the area and circumference of a circle with radius 5.40.

 The code can be described as the following. The first section imports the necessary classes from the appropriate packages all of the swing package the Container classes from the package awt - abstract windowing toolkit The inclusive class is defined as extending JApplet from the swing package Variables and objects are declared and initialized PI is declared in the overall class and is thus global, that is, available to all methods in the class the JTextArea is also declared in the overall class and is also global a string for output the variables for the inputs are declared within the init( ) method and is thus local to it the variable used to accumulate the output string is declared within the init( ) method and is thus local to it a boolean variable goodInput is initialized as true an object Container c is developed using the awt package the JTextArea outputArea is added to the Container called c double precision variables for the area and the circumference are declared a try and a catch block are used to give some error trapping for the input from the user the radius is parsed to be double precision if the input is acceptable we pass the radius to the circleArea( ) method and return the area as a double we pass the radius to the circleCircumference( ) method and return the area as a double Each result and the input that drove it are added to the output string using control characters for display the outputArea's contents are set to the the output string   the method public double circleArea( double r ) is declared and defined notice that r is a local double precision variable used only within the method notice that a double is returned   the method public double circleCircumference( double r ) is declared and defined notice that r is a local double precision variable used only within the method notice that a double is returned Make sure to notice that our methods are all delimited the same way.