Comparable Interface - Code Reading-Activity
Jklukowska (Talk | contribs) (Created page with "__NOTOC__ An "Activity" is the smallest unit of learning and could be an assignment, homework, in-class activity, or other learning activity. == Directions for Using This Tem...") |
|||
(33 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
__NOTOC__ | __NOTOC__ | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{Learning Activity Overview | |
− | + | |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. |
− | + | |prerequisites= | |
− | | | + | * Be familiar with the String class and string comparison in Java. |
− | + | * Understand the concept of classes and objects. | |
− | | | + | |objectives= |
− | + | * Understand the concept and purpose of the Comparable interface. | |
− | + | * Understand the benefits of defining classes that implement the Comparable interface. | |
+ | |process skills= | ||
+ | ''What process skills will the student practice while completing this activity?'' | ||
+ | }} | ||
=== Background: === | === Background: === | ||
− | |||
− | + | * 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. | ||
− | + | === Directions: === | |
+ | <ol> | ||
+ | <li> Go to openMRS-core GitHub repository at https://github.com/openmrs/openmrs-core and search compareTo </li> | ||
+ | <li> Locate 4 different examples of a call to the compareTo method, for each: | ||
+ | <ol type="a"> | ||
+ | <li> specify the filename and the line # on which the call occurs and write the line that contains the call | ||
+ | <li> identify the type of the object the method is called on | ||
+ | <li> locate the class associated with this datatype and | ||
+ | <ol type='i'> | ||
+ | <li> write the header for this class | ||
+ | <li> locate the implementation of the <code>compareTo</code> method and | ||
+ | <ul> | ||
+ | <li>identify the parameter type | ||
+ | <li>identify the return type <br> | ||
+ | </ul> | ||
+ | </ol> | ||
+ | </ol> | ||
− | + | <li> Recall that you have used the <code>compareTo</code> method in the <code>String</code> class. Below is a partial class definition: | |
+ | <pre> | ||
+ | public final class String | ||
+ | implements java.io.Serializable, Comparable<String>, CharSequence | ||
+ | </pre> | ||
− | === | + | <pre> |
− | What | + | 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; | ||
+ | } | ||
+ | k++; | ||
+ | } | ||
+ | return len1 - len2; | ||
+ | } | ||
+ | </pre> | ||
+ | <ol type='a'> | ||
+ | <li> What is the type of the parameter? | ||
+ | <li> What is the return type? | ||
+ | <li> given the following strings, determine the result of the method call | ||
+ | <tt>String str1 = "hello";</tt> | ||
+ | <tt>String str2 = "help";</tt> | ||
+ | Call --> <tt>int result = str1.compareTo(str2);</tt> | ||
+ | </ol> | ||
+ | <li> Look at the class headers, what do they all have in common? | ||
+ | <li> In general, what can be said about the parameter type for each of the compareTo methods | ||
+ | <li> What can be said about the return type for all implementations of the compareTo methods | ||
+ | <li> All of the classes that you looked at so far, implement the Comparable interface. Look at the API page for Comparable interface https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html. 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. | ||
+ | <ol type='a'> | ||
+ | <li> 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"? | ||
+ | <li> 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"? | ||
+ | <li> 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? | ||
+ | <li> If a call is made to <code>Arrays.sort</code>, what do you think will happen to an array of strings? | ||
+ | <li> Assume the <code>compareTo</code> method in the <code>String</code> class is modified as follows. What do you think the result of calling <code>Arrays.sort</code> on an array of strings will be now? | ||
+ | <pre> | ||
+ | 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 | ||
+ | } | ||
+ | k++; | ||
+ | } | ||
+ | return len2 - len1; ** modified | ||
+ | } | ||
+ | </pre> | ||
+ | </ol> | ||
+ | <li> What does the class implementing the Comparable interface have to provide?</li> | ||
+ | <li> What is the benefit of defining a class that implements the Comparable interface?</li> | ||
+ | </ol> | ||
=== Deliverables: === | === Deliverables: === | ||
− | + | Student will submit a document containing answers to the above questions. | |
=== Assessment: === | === Assessment: === | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
{| border="1" class="wikitable" | {| border="1" class="wikitable" | ||
! Criteria | ! Criteria | ||
Line 50: | Line 119: | ||
! Level 4 (exceptional) | ! Level 4 (exceptional) | ||
|- | |- | ||
− | | ''' | + | | '''Does the student understand what it means to implement an interface?''' |
| | | | ||
| | | | ||
Line 57: | Line 126: | ||
|- | |- | ||
− | | ''' | + | | '''Does the student understand what the compareTo method returns?''' |
| | | | ||
| | | | ||
Line 63: | Line 132: | ||
| | | | ||
+ | |- | ||
+ | | '''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?''' | ||
+ | | | ||
+ | | | ||
+ | | | ||
+ | | | ||
|} | |} | ||
Line 71: | Line 153: | ||
=== Additional Information: === | === Additional Information: === | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | {{Learning Activity Info | |
− | + | |acm unit= | |
− | + | Software Development Fundamentals / Fundamental Programming Concepts, Programming Languages / Object Oriented Programming | |
+ | |acm topic= | ||
+ | interfaces, code reading and understanding, object oriented design | ||
+ | |difficulty= | ||
+ | easy to moderate | ||
+ | |time= | ||
+ | 2-3 hours | ||
+ | |environment= | ||
+ | Any platform with a modern Internet browser. | ||
+ | |author= | ||
+ | Darci Burdge, Joanna Klukowska | ||
+ | |source= | ||
+ | This is not dependent on any other activity. | ||
+ | |license= | ||
+ | {{License CC BY SA}} | ||
+ | }} | ||
− | + | === Feedback: === | |
+ | 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). | ||
[[Category: Learning_Activity]] | [[Category: Learning_Activity]] | ||
− | [[Category: | + | [[Category: CS2]] |
+ | [[Category: Interfaces]] | ||
+ | [[Category: Ready to Use]] |
Latest revision as of 14:11, 15 October 2018
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. |
Prerequisites |
|
Learning Objectives |
After successfully completing this activity, the learner should be able to:
|
Process Skills Practiced |
What process skills will the student practice while completing this activity? |
Background:
- 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.
Directions:
- Go to openMRS-core GitHub repository at https://github.com/openmrs/openmrs-core and search compareTo
- Locate 4 different examples of a call to the compareTo method, for each:
- specify the filename and the line # on which the call occurs and write the line that contains the call
- identify the type of the object the method is called on
- locate the class associated with this datatype and
- write the header for this class
- locate the implementation of the
compareTo
method and- identify the parameter type
- identify the return type
- Recall that you have used the
compareTo
method in theString
class. Below is a partial class definition:public final class String implements java.io.Serializable, 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; } k++; } return len1 - len2; }
- What is the type of the parameter?
- What is the return type?
- given the following strings, determine the result of the method call String str1 = "hello"; String str2 = "help"; Call --> int result = str1.compareTo(str2);
- Look at the class headers, what do they all have in common?
- In general, what can be said about the parameter type for each of the compareTo methods
- What can be said about the return type for all implementations of the compareTo methods
- All of the classes that you looked at so far, implement the Comparable interface. Look at the API page for Comparable interface https://docs.oracle.com/javase/7/docs/api/java/lang/Comparable.html. 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.
- 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"?
- 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"?
- 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?
- If a call is made to
Arrays.sort
, what do you think will happen to an array of strings? - Assume the
compareTo
method in theString
class is modified as follows. What do you think the result of callingArrays.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 } k++; } return len2 - len1; ** modified }
- What does the class implementing the Comparable interface have to provide?
- What is the benefit of defining a class that implements the Comparable interface?
Deliverables:
Student will submit a document containing answers to the above questions.
Assessment:
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? |
Comments:
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 BoK Area & Unit(s) |
Software Development Fundamentals / Fundamental Programming Concepts, Programming Languages / Object Oriented Programming |
---|---|
ACM BoK Topic(s) |
interfaces, code reading and understanding, object oriented design |
Difficulty |
easy to moderate |
Estimated Time to Complete |
2-3 hours |
Environment / Materials |
Any platform with a modern Internet browser. |
Author(s) |
Darci Burdge, Joanna Klukowska |
Source |
This is not dependent on any other activity. |
License |
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License |
Feedback:
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).