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.

 

import java.awt.Container;
import javax.swing.*;

public class CircleInfo extends JApplet
{

JTextArea outputArea = new JTextArea( );
double PI = 3.14159;

public void init()
{

String output = "";
String strRadius;
double radius = 0.0;
boolean goodInput = true;



// get the applet's GUI component display area
Container c = getContentPane( );

// attach output area to container c
c.add( outputArea );

double area;
double circumference;

strRadius = JOptionPane.showInputDialog("Please enter the radius of the circle");

try
{

radius = Double.parseDouble(strRadius);

}

catch (NumberFormatException nfeDouble)
{

JOptionPane.showMessageDialog(null, "You need to enter a\nnumber for the radius", "Input Error", JOptionPane.ERROR_MESSAGE);
goodInput = false;

}

if (goodInput)
{

area = circleArea( radius );
circumference = circleCircumference( radius );

output = output + "The radius of the circle is " + radius + "\n\n";
output = output + "The area of the circle is " + area + "\n\n";
output = output + "The circumference of the circle is " + circumference;

outputArea.setText(output);

}

}

// method to compute the area
// based on the radius

public double circleArea( double r )
{

return PI * r * r;

}

// method to compute the circumference
// based on the radius

public double circleCircumference( double r )
{

return 2.0 * PI * r;

}

}

 

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

 

<html>
<applet code="CircleInfo.class" width=300 height=150>
</applet>
</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.