Finding the Code Responsible for Behavior

(Difference between revisions)
Jump to: navigation, search
Line 17: Line 17:
 
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!
 
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 ==
 +
# 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 it call according the to API?
 +
# 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?
 +
# Let's navigate to the ArrayList class. What class does ArrayList inherit its toString method from? This is the toString method we want to look at.
  
=== Deliverables: ===
 
What will the student hand in?
 
  
=== Assessment: ===
+
== Part 2 - Look at the Source Code for Java 7 ==
How will the activity be graded?
+
# If you do not already have it downloaded, discover how you can download the source code for Java 7.
+
# Understand the sub-directory structure. How does this relate to package/import statements?
How will learning will be measured?
+
# 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] 
  
Include sample assessment questions/rubrics.
+
== Part 3 - Find the code ==
 +
# Find the toString in the Java source code that is executed to produce the output [a, b, c, d]
 +
# Read and understand this method
 +
# Copy and paste the code in the method, and explain the method in English.
 +
##Indicate the line of code that produces the  "[".
 +
##Indicate the line of code that produces the  "]". 
 +
##Indicate the line of code the places the comma and space between the elements in the ArrayList.
  
=== Comments: ===
 
What should the instructor know before using this activity?
 
  
What are some likely difficulties that an instructor may encounter using this activity?
 
  
 +
=== Deliverables: ===
 +
A document with answers to questions
 +
 +
=== Assessment: ===
 +
TBD
 +
 +
=== Comments: ===
 +
None
 
=== Additional Information: ===
 
=== Additional Information: ===
 
{| border="1"
 
{| border="1"
 
|-  
 
|-  
|'''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?
+
|'''Knowledge Area/Knowledge Unit''' || PL
 
|-
 
|-
|'''Topic''' || What specific topics are addressed? The Computing Curriucula 2013 provides a list of topics - https://www.acm.org/education/CS2013-final-report.pdf
+
|'''Topic''' || PL/Object Oriented Programming
 
|-
 
|-
|'''Level of Difficulty''' || Is this activity easy, medium or challenging?
+
|'''Level of Difficulty''' || Easy
 
|-
 
|-
|'''Estimated Time to Completion''' ||  How long should it take for the student to complete the activity?
+
|'''Estimated Time to Completion''' ||  20 minutes
 
|-
 
|-
|'''Materials/Environment''' || What does the student need?  Internet access, IRC client, Git Hub account, LINUX machine, etc.?
+
|'''Materials/Environment''' || Access to Java SE JDK 7 and access to Java 7 API
 
|-
 
|-
|'''Author''' || Who wrote this activity?
+
|'''Author''' || E Brannock
 
|-
 
|-
|'''Source''' || Is there another activity on which this activity is based?  If so, please provide a link to the original resource.
+
|'''Source''' || N/A
 
|-
 
|-
 
|'''License''' || Under which license is this material made available? (http://creativecommons.org/licenses/)
 
|'''License''' || Under which license is this material made available? (http://creativecommons.org/licenses/)

Revision as of 14:56, 28 May 2015

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
Prerequisite Knowledge Invocation of Java methods (especially System.out.println and overloading), some inheritance concepts, some familiarity with the toString() method, .
Learning Objectives 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

Background:

Students should be able to create an ArrayList and it them out to the console without utilizing a for loop. Students should also be able to read the Java API and understand inheritance.

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 it 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 look at.


Part 2 - Look at the Source Code for Java 7

  1. If you do not already have it downloaded, discover how you can download the source code for Java 7.
  2. Understand the sub-directory structure. How does this relate to package/import statements?
  3. 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.


Deliverables:

A document with answers to questions

Assessment:

TBD

Comments:

None

Additional Information:

Knowledge Area/Knowledge Unit PL
Topic PL/Object Oriented Programming
Level of Difficulty Easy
Estimated Time to Completion 20 minutes
Materials/Environment Access to Java SE JDK 7 and access to Java 7 API
Author E Brannock
Source N/A
License Under which license is this material made available? (http://creativecommons.org/licenses/)


Suggestions for the Open Source Project:

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
Namespaces
Variants
Actions
Events
Learning Resources
HFOSS Projects
Evaluation
Navigation
Toolbox