Finding the Code Responsible for Behavior

From Foss2Serve
(Difference between revisions)
Jump to: navigation, search
(Created page with "__NOTOC__ {| border="1" |- |'''Title''' || Finding the Code Responsible for Behavior |- |'''Overview''' || High level description of what the student will do |- |'''Prerequi...")
 
 
(27 intermediate revisions by 5 users not shown)
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
{| border="1"
 
|-
 
|'''Title''' || Finding the Code Responsible for Behavior
 
|-
 
|'''Overview''' || High level description of what the student will do
 
|-
 
|'''Prerequisite Knowledge''' || What topics and tools does the student need to know prior to beginning this activity?
 
|-
 
|'''Learning Objectives''' || What should the student be able to do after completing completed this activity?
 
|}
 
  
=== Background: ===
+
{{Learning Activity Overview
Is there background reading material?
+
|title=
 +
Finding the Code Responsible for Behavior
 +
|overview=  
 +
Java learners often wonder why certain behaviors are exhibited when a method is invoked.
 +
They tend to view the entire mechanism as "magic".
 +
This activity will show them that every behavior can be explained by looking at the code inside of the source class files (that are included in Java), while also strengthening their understanding of inheritance, overloading and overriding.
 +
|prerequisites=
 +
Invocation of Java methods (especially System.out.println and overloading), some inheritance concepts, some familiarity with the toString() method and overriding.
 +
|objectives=
 +
# Fully understand the interaction of the println method with the toString method
 +
# Inheritance
 +
# How to locate the code responsible for the behavior of a method
 +
# what an overload is (println) 5) what an override is (toString)
 +
|process skills=
 +
}}
  
Are there other activities the student should have done first?
+
=== Background ===
  
What is the rational for this activity?
+
Students should be able to create an ArrayList and print it to the console (without a for loop). Students should also be able to read the Java API and understand inheritance at a high level.
  
Include helpful hints to faculty here.
+
=== Directions ===
  
=== Directions: ===
+
I can easily create an ArrayList of String named al, and add "a", "b", "c" and "d" to it. Then I can quickly print it out using the line of code System.out.println(al); This produces the output [a, b, c, d].
What should the student do?
+
Have you ever wondered why the output looks like this? Why are those "weird" brackets included in the output? Why are there commas between each String? Let's discover the answer!
  
=== Deliverables: ===
+
== Part 1 - Read the API ==
What will the student hand in?
+
  
=== Assessment: ===
+
# Find the println method in the API. There are many println methods. Which overloaded println is used when you println an ArrayList? What method does the println call according the to API?
How will the activity be graded?
+
# Since println invokes String.valueOf, navigate to this method. What method does String.valueOf invoke?
+
# Now you can see that String.valueOf invokes the toString method of the object. What class does al belong to? Isn't it ArrayList?
How will learning will be measured?
+
# Let's navigate to the ArrayList class. What class does ArrayList inherit its toString method from? This is the toString method we want to dissect.
  
Include sample assessment questions/rubrics.
+
== Part 2 - Look at the Source Code in OpenJDK for Java  ==
  
=== Comments: ===
+
# OpenJDK (Open Java Development Kit) is a free and open source implementation of the Java programming language.
What should the instructor know before using this activity?
+
# If you do not already have it downloaded, discover how you can download the source code for Java from OpenJDK.
 +
# Understand the sub-directory structure. How does this relate to package/import statements?
 +
# Open the java source code for the class that you determined contains the toString that is executed to produce the output [a, b, c, d].
  
What are some likely difficulties that an instructor may encounter using this activity?
+
== Part 3 - Find the code ==
  
=== Additional Information: ===
+
# Find the toString in the Java source code that is executed to produce the output [a, b, c, d].
{| border="1"
+
# Read and understand this method.
|-
+
# Copy and paste the code in the method, and explain the method in English.  
|'''Knowledge Area/Knowledge Unit''' || What ACM Computing Curricula 2013 https://www.acm.org/education/CS2013-final-report.pdf knowledge area and units does this activity cover?
+
## Indicate the line of code that produces the  "[".
|-
+
## Indicate the line of code that produces the  "]".  
|'''Topic''' || What specific topics are addressed? The Computing Curriucula 2013 provides a list of topics - https://www.acm.org/education/CS2013-final-report.pdf
+
## Indicate the line of code the places the comma and space between the elements in the ArrayList.
|-
+
|'''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''' || 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? (http://creativecommons.org/licenses/)
+
|}
+
  
 +
== Part 4 - Open source vs. licensed code ==
  
=== Suggestions for the Open Source Project: ===
+
# Oracle makes this source code available. However, it is licensed and owned by Oracle. Therefore, can you modify this code? Is this open source??
Suggestions for an open source community member who is working in conjunction with the instructor.
+
# What would be the proper way to change the way an ArrayList is output to a console.  
  
  
--------------------
+
=== Deliverables ===
This work is licensed under a
+
[http://creativecommons.org/licenses/by-sa/4.0/ Creative Commons Attribution-ShareAlike 4.0 International License]
+
  
[[File:CC_license.png]]
+
A document with answers to questions
  
[[Category: Learning_Activity]]
+
=== Assessment ===
[[Category: Coding_and_Style]]
+
 
 +
TBD
 +
 
 +
=== Comments ===
 +
 
 +
I used an in-class activity based on this description for two semesters now.
 +
Here is the link to the specific activity description and worksheet: https://goo.gl/6R6c2B.
 +
The students work in small groups during the recitation.
 +
 
 +
=== Additional Information ===
 +
 
 +
{{Learning Activity Info
 +
|acm unit=
 +
PL
 +
|acm topic=
 +
PL/Object Oriented Programming
 +
|difficulty=
 +
easy
 +
|time=
 +
20 minutes
 +
|environment=
 +
Access to Java OpenJDK 7 or 8  and access to Java 7 or 8 API
 +
|author=
 +
E. Brannock
 +
|source=
 +
N/A
 +
|license=
 +
{{License CC BY SA}}
 +
}}
 +
 
 +
=== Suggestions for the Open Source Project ===
 +
 
 +
N/A
 +
 
 +
[[Category:Learning Activity]]
 +
[[Category:Coding and Style]]
 +
[[Category:CS2]]
 +
[[Category:Good Draft]]

Latest revision as of 22:22, 7 September 2018


Title

Finding the Code Responsible for Behavior

Overview

Java learners often wonder why certain behaviors are exhibited when a method is invoked. They tend to view the entire mechanism as "magic". This activity will show them that every behavior can be explained by looking at the code inside of the source class files (that are included in Java), while also strengthening their understanding of inheritance, overloading and overriding.

Prerequisites

Invocation of Java methods (especially System.out.println and overloading), some inheritance concepts, some familiarity with the toString() method and overriding.

Learning
Objectives
After successfully completing this activity, the learner should be able to:
  1. Fully understand the interaction of the println method with the toString method
  2. Inheritance
  3. How to locate the code responsible for the behavior of a method
  4. what an overload is (println) 5) what an override is (toString)
Process Skills
Practiced


Background

Students should be able to create an ArrayList and print it to the console (without a for loop). Students should also be able to read the Java API and understand inheritance at a high level.

Directions

I can easily create an ArrayList of String named al, and add "a", "b", "c" and "d" to it. Then I can quickly print it out using the line of code System.out.println(al); This produces the output [a, b, c, d]. Have you ever wondered why the output looks like this? Why are those "weird" brackets included in the output? Why are there commas between each String? Let's discover the answer!

Part 1 - Read the API

  1. Find the println method in the API. There are many println methods. Which overloaded println is used when you println an ArrayList? What method does the println call according the to API?
  2. Since println invokes String.valueOf, navigate to this method. What method does String.valueOf invoke?
  3. Now you can see that String.valueOf invokes the toString method of the object. What class does al belong to? Isn't it ArrayList?
  4. Let's navigate to the ArrayList class. What class does ArrayList inherit its toString method from? This is the toString method we want to dissect.

Part 2 - Look at the Source Code in OpenJDK for Java

  1. OpenJDK (Open Java Development Kit) is a free and open source implementation of the Java programming language.
  2. If you do not already have it downloaded, discover how you can download the source code for Java from OpenJDK.
  3. Understand the sub-directory structure. How does this relate to package/import statements?
  4. Open the java source code for the class that you determined contains the toString that is executed to produce the output [a, b, c, d].

Part 3 - Find the code

  1. Find the toString in the Java source code that is executed to produce the output [a, b, c, d].
  2. Read and understand this method.
  3. Copy and paste the code in the method, and explain the method in English.
    1. Indicate the line of code that produces the "[".
    2. Indicate the line of code that produces the "]".
    3. Indicate the line of code the places the comma and space between the elements in the ArrayList.

Part 4 - Open source vs. licensed code

  1. Oracle makes this source code available. However, it is licensed and owned by Oracle. Therefore, can you modify this code? Is this open source??
  2. What would be the proper way to change the way an ArrayList is output to a console.


Deliverables

A document with answers to questions

Assessment

TBD

Comments

I used an in-class activity based on this description for two semesters now. Here is the link to the specific activity description and worksheet: https://goo.gl/6R6c2B. The students work in small groups during the recitation.

Additional Information

ACM BoK
Area & Unit(s)

PL

ACM BoK
Topic(s)

PL/Object Oriented Programming

Difficulty

easy

Estimated Time
to Complete

20 minutes

Environment /
Materials

Access to Java OpenJDK 7 or 8 and access to Java 7 or 8 API

Author(s)

E. Brannock

Source

N/A

License

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

CC license.png


Suggestions for the Open Source Project

N/A

Personal tools
Namespaces
Variants
Actions
Events
Learning Resources
HFOSS Projects
Evaluation
Navigation
Toolbox