CSCI 201 002, 5 December

The class header

Abstract example

optional import statements
optional package declaration
public classname optional extends {
  fields, methods, constructors, etc.

Concrete example

import java.awt.geom ;
public  Circle {
  fields, methods, constructors, etc.

The Java program must be stored in the file where ClassName is the name of the class.

Fields or instance variables

Fields hold the state of the class. Generally, fields should be declared as private variables to allow the class code to verify that reasonable values are assigned to fields.

It is a good idea to put all your field definitions at the beginning of the class.

Semi-concrete example

import java.awt.geom ;
public  Circle {
  double x ;        // fields for the center of the circle
  double y ;
  double radius ;   // field for the radius

  methods, constructors, etc.

Accessor and Modifier methods

It is a good idea to add public methods to read and write your fields. The accessor method returns the value of a field. The methods return type should be the same as the field variable's type.

The modifier method assigns a new value to the field. Since it returns no value, its return "type" should be void. It should have a single argument that has the same type as the field variable.

Be prepared to cut-and-paste when creating these methods.

Accessor and Modifier methods for the radius

  public double getRadius() {
    return radius ;

  public void setRadius(double radius) {
    this.radius = radius ;      // Notice the use of "this"


You should define constructors to create class objects with reasonable values. Be sure to have a default no-arg constructor which provides reasonable initial values. Also, provide other constructors that you believe would be useful for "users" of your class.

In writing your constructor, it's a good idea to make use of your modifier methods.

Two Constructor methods

  public Circle() {             // no-arg constructor
    setX(0.0) ;                 // "return" a unit circle
    setY(0.0) ;                 //    at the origin
    setRadius(1.0) ;            

  public Circle(double x, double y, double radius) {
    setX(x) ;
    setY(y) ;
    setRadius(radius) ;

Useful methods

Now you can define some useful methods and exotic constructors.

Some geometry methods

  // computes the area of a circle
  public double getArea() {
    return Math.PI * radius / 4 ;

  // checks if a point is inside the Circle
  public boolean contains(double x, double y) {
    double distSquared = (this.x-x)*(this.x-x) + (this.y-y)*(this.y-y) ;
    return distSquared <= radius*radius ;

Methods and constructors using point objects

  // accessor for the origion
  public Point2D getOrigin() {
    return new Point2D.double(x, y) ;

  // modifier for the origion
  public void setOrigin(Point2D origin) {
    setX(origin.getX()) ;
    setY(origin.getY()) ;

  // constructor with points
  public Circle(Point2D origin, double radius) {
    super(origin.getX(), origin.getY(), radius) ;

  // checks if a point is inside the Circle
  public boolean contains(Point2D p) {
    return contains(p.getX(), p.getY()) ;