Comparable Interface - Code Reading-Activity

From Foss2Serve
Revision as of 18:46, 16 August 2016 by Darci.burdge (Talk | contribs)
Jump to: navigation, search

Directions for Using This Template

The template below contains sections which describe the items that should be included when creating an activity. To use this template:

  • Create a new page with the word "Activity" in the name. Please try to use a short, descriptive name.
  • Fill in the sections below according to the instructions. Feel free to copy the source for this template into your new page.
  • Categorize the page as "Category:Learning Activity". Look at the source for an existing activity to see the syntax.
  • Also categorize the page under one or more of the subcategories found at
  • Note that feedback to the author(s) of the activity regarding usage or suggestions for enhancements can be included via the Discussion tab (upper left of the page).


Title Comparable Interface - Code Reading Activity
Overview Students learn about the Comparable interface in Java by looking at the code of OpenMRS and the actual implementation of the compareTo method in the String class of Java API.
Prerequisite Knowledge Students should be familiar with the String class and string comparison in Java. They should understand the concept of classes and objects.
Learning Objectives Understand the concept and purpose of the Comparable interface. Understand the benefits of defining classes that implement the Comparable interface.


  • This activity could be done as a homework assignment (either done individually or by pairs of students), or as an inclass (lecture or recitation) activity.
  • This activity could be done either early in CS2 course or as a late semester CS1 activity.
  • This activity is designed to use OpenMRS, but there is nothing specific to OpenMRS. The instructor can use any open source project that has sufficient examples of use of Comparable interface.


  1. Go to openMRS-core GitHub repository at and search compareTo
  2. Locate 4 different examples of a call to the compareTo method, for each:
    1. specify the filename and the line # on which the call occurs and write the line that contains the call
    2. identify the type of the object the method is called on
    3. locate the class associated with this datatype and
      1. write the header for this class
      2. locate the implementation of the compareTo method and
        • identify the parameter type
        • identify the return type
  3. Recall that you have used the compareTo method in the String class. Below is a partial class definition:
    public final class String 
       implements, Comparable<String>, CharSequence
    public int compareTo(String anotherString) { 
            int len1 = value.length;
            int len2 = anotherString.value.length;
            int lim = Math.min(len1, len2);
            char v1[] = value;
            char v2[] = anotherString.value;
            int k = 0;
            while (k < lim) {
                char c1 = v1[k];
                char c2 = v2[k];
                if (c1 != c2) {
                    return c1 - c2;
            return len1 - len2;
    1. What is the type of the parameter?
    2. What is the return type?
    3. given the following strings, determine the result of the method call String str1 = "hello"; String str2 = "help"; Call --> int result = str1.compareTo(str2);
  4. Look at the class headers, what do they all have in common?
  5. In general, what can be said about the parameter type for each of the compareTo methods
  6. What can be said about the return type for all implementations of the compareTo methods
  7. All of the classes that you looked at so far, implement the Comparable interface. Look at the API page for Comparable interface WARNING: you will not understand everything on that page - do not worry about it just yet, just try to read through it and find the answers to the following questions.
    1. The first senctence of the descriptions states that "This interface imposes a total ordering on the objects of each class that implements it." What do you think is meant by "total ordering"?
    2. The first sentence of the second paragraphs states that "Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort)." What do you think is meant by "automatically"?
    3. Locate the Method Detail for the compareTo method. Describe in your own words what is the meaning of the value that is returned by this method?
    4. If a call is made to Arrays.sort, what do you think will happen to an array of strings?
    5. Assume the compareTo method in the String class is modified as follows. What do you think the result of calling Arrays.sort on an array of strings will be now?
       public int compareTo(String anotherString) { 
              int len1 = value.length; 
              int len2 = anotherString.value.length;
              int lim = Math.min(len1, len2);
              char v1[] = value;
              char v2[] = anotherString.value;
              int k = 0;
              while (k < lim) {
                  char c1 = v1[k];
                  char c2 = v2[k];
                  if (c1 != c2) {
                      return c2 - c1; ** modified
              return len2 - len1; ** modified
  8. What does the class implementing the Comparable interface have to provide?
  9. What is the benefit of defining a class that implements the Comparable interface?


Student will submit a document containing answers to the above questions.


Criteria Level 1 (fail) Level 2 (pass) Level 3 (good) Level 4 (exceptional)
Does the student understand what it means to implement an interface?
Does the student understand what the compareTo method returns?
Does the student understand what the parameter for the compareTo method should be?
Does the student understand how the compareTo method is used to impose ordering on objects of the specified type?


What should the instructor know before using this activity?

What are some likely difficulties that an instructor may encounter using this activity?

Additional Information:

ACM Knowledge Area/Knowledge Unit What ACM Computing Curricula 2013 knowledge area and units does this activity cover? ACM_Body_of_Knowledge
ACM Topic What specific topics are addressed? The Computing Curriucula 2013 provides a list of topics -
Level of Difficulty Is this activity easy, medium or challenging?
Estimated Time to Completion How long should it take for the student to complete the activity?
Materials/Environment What does the student need? Internet access, IRC client, Git Hub account, LINUX machine, etc.?
Author(s) Who wrote this activity?
Source Is there another activity on which this activity is based? If so, please provide a link to the original resource.
License Under which license is this material made available? (

Suggestions for Open Source Community:

Suggestions for an open source community member who is working in conjunction with the instructor.

This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License

CC license.png

Personal tools
Learning Resources
HFOSS Projects