CSCI 201 Lab 11 -- Interfaces

As in previous labs, download the Lab11 project in jar format and extract its contents. Check to make sure that the Lab11 folder/directory exists and contains files before continuing.

Oval Descriptions & Initial Project Execution

Start JGrasp. Open the Lab11 project. Make sure the project includes the .java files and the .htm file. Compile the .java files, and run Mathfun as an applet. As you can see, the program generates a face which helps students practice their math skills. Initially, the width to length ratio  and the area [PI * (width/2) * (length/2)] of the ovals are shown. A clever student yearning to practice his or her algebra skills could use this information to solve for the width and length of the oval. Since the clever algebra student has a younger sibling who wants to practice math skills too, "oval" and "circle" labels are used to identify  oval and circle shapes so that the tot may practice shape recognition skills.


The purpose of this lab is to introduce interfaces; an interface is similar to a class, but it has some restrictions.

An interface may contain constants, which are available to the class or classes implementing the interface.

A class implements an interface by providing method implementations for all of the abstract methods defined by the interface. In this manner, the interface guarantees that the class implements certain methods, but the interface does not prevent the class from implementing additional methods. Multiple classes can implement the same interface, yet provide different definitions for the same methods. Furthermore, a class may implement more than one interface.

Program Description

The program consists of two classes, Mathfun and Ovalcalc, as well as an interface, Shapecalc. Mathfun is an applet, and its job is to display some form of art, in this case a face, and text describing the ovals in the face. Take a look at the interface, Shapecalc:

public interface Shapecalc extends Applet 
	public double area();
	public String shape();
The interface defines two abstract methods: area and shape. The methods, area() and shape(), are specified to return a double and a String, respectively. Neither method definition includes parameters. Now take a look at part of the Ovalcalc class which implements the Shapecalc interface.
public class Ovalcalc implements Shapecalc 
{   .... 
    //Method returns a double - the area of the oval.
    public double area()
    {   double a;
        a = (width/2.0) * (height/2.0) * Math.PI;
        return a;
    //Method returns String - either "circle" or "oval".
    public String shape()
    {  String geometry;
          if (width == height)
             geometry = "circle";
             geometry = "oval";
          return geometry;
Ovalcalc also defines another method ratio(), not defined by the interface.

Methods defined by an interface are always public. Notice that the method return types in Shapecalc and Ovalcalc are identical. If one of the abstract methods defined in Shapecalc had included formal parameters, the parameter types in Ovalcalc would need to be the same type. For example, if a method were defined in an interface as:

    public void setComplexity (int complexity);

The method could appear in a class implementing the interface as:

    public void setComplexity (int announce);

The parameter types must match, but the parameter names can differ. The interface defining the announce method is discussed in Lewis and Loftus on pp. 294-299.

Take a look at the applet Mathfun. The following code shows how each shape is created. The object face is an instantiation of Ovalcalc.

//Draws face. Creates text specifying face shape, width/length ratio, and area.
elpsWdth = 250;
elpsHt = 300;
g.fillOval(100, 100, elpsWdth, elpsHt); //face
Ovalcalc face = new Ovalcalc(elpsWdth, elpsHt);
String faceString = "Face is " + face.shape() + ". W/L=" + face.ratio() + " A=" + face.area();

The String faceString describes the oval's shape, width to length ratio and area. Notice how the methods shape(), ratio(), and area() defined in Ovalcalc are called in the statement creating faceString.

Part 1 - Create Rectcalc

A clever student needs more practice with his algebra skills. The clever tot needs more practice identifying shapes. Your first task is to create a new class, Rectcalc. Write a method, requiring no parameters, to calculate the perimeter of a rectangular shape. Define the instance variables and constructor as follows:

     private int width, height;
     public Rectcalc(int x, int y) 
     {   width = x;
         height = y;

Now modify the Mathfun class to display text stating the perimeter of each rectangle.

Lab Instructor checkoff

Show your instructor the output of your modified program. Your display should still show the face with text describing the ovals, as in the initial execution. Text stating the perimeter of each rectangle should also be visible.

Part 2 - Implement the Interface with Rectcalc

To complete your task, implement the Shapecalc interface with the Rectcalc class. Modify the Mathfun class to display text which also states the area  and identifies the shape as "Rectangle" or "Square" of each rectangle in the figure.

Lab Instructor checkoff

Show your instructor the enhanced display created by your program. This display should contain, not only all of the art and text visible for Check-off 1, but also the text stating the area of each rectangle and its shape, "Rectangle" or "Square."