Model code for COM 1201

There are certain expectations that I have for your code. This document should make it clear just what those expectations are. You should use this model code for your homework and exams.

A function

When I ask you to write a function, the context is not as important as the content. In Java code, the context for a function must be a class, as all Java code must be contained in a class. On an exam, even when you are writing Java code, you can ignore the code needed to establish the context. Specifically, you must write the function, but not the class that encloses it.

Assume the following task description:

You must write a function named daysInYear that takes a parameter named year of type int and returns the number of days in that year, A.D., taking into account leap days. A year must include a leap day if it meets the following criteria:

Write your method below:

Such a task description requires you to write a function whose documentation includes example input and output. The model code is below:

/**
 * Returns the number of days in the given year.
 *
 * @example daysInYear(2004) returns 366
 * @example daysInYear(2100) returns 365
 * @example daysInYear(2000) returns 366
 * @example daysInYear(2003) returns 365
 * @param year year for which to calculate days
 */
public int daysInYear(int year) {
    if (year % 400 == 0)
        return 366;

    if (year % 100 == 0)
        return 365;

    if (year % 4 == 0)
        return 366;

    return 365;
}

Notice the following things in particular:

A class

When I ask you to write a class, all of its contents must be present. In this course we write classes that implement data structures, which means that it is nearly impossible to write examples as we do in the model code for a single function. Nearly everything else is the same, however.

Assume the following task description:

Write a class named Circle that represents a circle as you know it from geometry class. Your class must implement the following interface:

/** Interface specifying the required behavior for a geometrical shape. */
public interface Shape {

    /** Returns the area of this. */
    public double area();

    /**
     * Returns whether or not this contains the point
     * with the given coordinates.
     */
    public boolean contains(double x, double y);
}

You may choose whatever data definition you like. Write your method below:

The model code is below:

/**
 * Represents a circular shape.
 * @author Jeff Raab
 */
public class Circle implements Shape {

    /** X coordinate of the center of this. */
    protected double x = 0.0;

    /** Y coordinate of the center of this. */
    protected double y = 0.0;

    /** Radius of this. */
    protected double radius = 0.0;

    /**
     * Constructs a circle with a center at the given x and y coordinates
     * and the given radius.
     *
     * @param anX x coordinate for the circle
     * @param aY y coordinate for the circle
     * @param aRadius radius for the circle
     */
    public Circle(double anX, double aY, double aRadius) {
        this.x = anX;
        this.y = aY;
        this.radius = aRadius;
    }

    /**
     * Returns the area of this.
     *
     * @example (new Circle(0, 0, 0)).area() returns 0
     * @example (new Circle(0, 0, 1)).area() returns 3.14159...
     * @example (new Circle(0, 0, 2)).area() returns 4*3.14159...
     */
    public double area() {
        return (Math.PI * radius * radius);
    }

    /**
     * Returns whether or not this contains the point
     * with the given coordinates.
     *
     * @param x x coordinate for the point
     * @param y y coordinate for the point
     * @example (new Circle(0, 0, 0)).contains(0, 0) returns false
     * @example (new Circle(0, 0, 1)).contains(0, 0) returns true
     * @example (new Circle(0, 0, 1)).contains(1, 1) returns false
     */
    public boolean contains(double x, double y) {
        double dx = this.x - x;
        double dy = this.y - y;

        double distance = Math.sqrt(dx * dx + dy * dy);

        return (distance < radius);
    }
}

Except for the examples, all of the notes for a function apply. Notice also the following things:

If you meet these expectations, you are well along the way to becoming a good programmer, and getting a good grade in the course.