1 / 17

Recitation 2

Recitation 2. James Wei Professor Peck 9/3/2013. Covered in this Recitation. Arrays Variable Scoping Local variables Instance variables Class variables References. Arrays. Consider the following code: public int uniqueCount (String[] list) { Arrays.sort (list);

carlo
Download Presentation

Recitation 2

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Recitation 2 James Wei Professor Peck 9/3/2013

  2. Covered in this Recitation • Arrays • Variable Scoping • Local variables • Instance variables • Class variables • References

  3. Arrays • Consider the following code: public intuniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; int count = 1; // Invariant: count is number of unique words in list[0…k] for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } } • The above code will correctly return the number of unique strings in an array

  4. Arrays • Why is count initialized to 1 instead of 0? Explain using the invariant shown in the code. An invariant is a condition that is reliably true during code execution. • The call to Arrays.sort() will sort our string array in alphabetical order. Why is this necessary? • Modify the code in Eclipse so that it prints out all the unique words in the list along with the number of appearances each word makes. You do not need to add any additional loops or create any new arrays to do this. • The new method will return void instead of int. Why? public intuniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; int count = 1; // Invariant: count is number of unique // words in list[0…k] for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } }

  5. Variable Scoping • All variables have some kind of scope that define the areas where they are accessible. Variables exist within their scope and are not visible outside of it. • Typically can see the scope of a variable by looking at the block of code it is defined in. A block could be a method, a class, a for loop, a while loop, etc. • Three types to cover: • Local variables • Instance variables • Class variables

  6. Local Variables • Consider the following code: public class Frequencies { public static intuniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } return count; } public static void main(String[] args) { int count = 1; String[] list = {“apple”, “banana”, “apple”, pear”}; uniqueCount(list); } } • What is the scope of the count variable? • If you type this code in Eclipse it will complain that “count cannot be resolved to a variable”. Why? • How would you fix this issue without creating any new variables or moving the declaration “int count = 1” in main()?

  7. Local Variables • Now consider the following slightly modified code: public class Frequencies { public static intuniqueCount(String[] list) { Arrays.sort(list); String last = list[0]; for (int k=1; k<list.length; k++) { if (!list[k].equals(last)) { last = list[k]; count++; } } System.out.println(last[k]); return count; } public static void main(String[] args) { int count = 1; String[] list = {“apple”, “banana”, “apple”, pear”}; uniqueCount(list); } } • What is the scope of the k variable? • If you type this code in Eclipse it will complain that “k cannot be resolved to a variable”. Why?

  8. Local Variables • Remember, you can usually identify a variable’s scope by checking the first pair of curly braces surrounding its declaration. Usually this will give you the block of code pertaining to its scope. • Here is an example using a for loop: for (inti=0; i<someArray.length; i++) { int j = 1; // some code } // the line below is invalid, i and j are accessed out of scope System.out.println(“i=“ + i + “, j=“ + j); • We can see that “i” is declared inside the declaration of the for loop, whilst “j” is declared inside the body of the for loop. Thus, the scope of these two variables is the for loop. • Note that the curly braces of the for loop have been bolded for emphasis.

  9. Instance Variables • Instance variables have a scope of an entire instance of a class. • Example: public class Node { private int value; public intprintValue() { System.out.println(“Value is “ + value); } public void changeValue(intnewValue) { value = newValue; } } • Notice that the variable “value” is declared outside of any methods in the Node class, but inside the class itself. This indicates that its scope is the entire class itself. Again, the corresponding curly braces have been bolded for emphasis.

  10. Class Variables • Class variables appear to be almost the same as instance variables, except they are declared with a “static” modifier. • Example: public class Node { private static int DEFAULT_VALUE = 0; private int value; public Node() { value = DEFAULT_VALUE; } } • Here “DEFAULT_VALUE” is a class variable because it has the static modifier, whereas “value” is an instance variable. • Don’t worry about what static means yet, just know that a class variable is static and that an instance variable is not.

  11. References • Java accesses objects by following references to those objects. • When I execute the line, “ArrayList<String> list = new ArrayList<>();”, this is what Java creates: • Now whenever we want to access the new ArrayList we just created, Java will look up the list variable, which points to the ArrayList object that resides in memory. “list” is a reference to the ArrayList object. • Whenever we create a variable and assign it a value with “=“, we are actually creating a new reference. So for instance, if we now say “ArrayList<String> otherList = list;”, we will get the following: • Now we have two references, “list” and “otherList”, which both reference the same object in memory. Thus, changing “list” will also change “otherList”, and vica versa. Memory (Heap) Empty ArrayList list otherList

  12. References • Go to this link to submit your answers: http://goo.gl/kbsjVq

  13. References • Consider the following code: public class Rec2Example { private intmyA; private intmyB; public Rec2Example(int a, int b) { myA = a; myB = b; } public void setA(int a) { myA = a; } public static void main(String[] args) { Rec2Example ex = new Rec2Example(33, 7); Rec2Example ex2 = ex; ex.setA(44); } } • How many times is the Rec2Example constructor called in the main method? • 0 • 1 • 2 • -23.157 • What is the value of ex2.myA at the end of main? • 33 • 44 • 7 • 24,102.15034

  14. References • Consider the following code: public static void randomFunction() { ArrayList<Integer> list = new ArrayList<>(); ArrayList<Integer> otherList = list; list.add(44); otherList.add(55); } • What does otherList contain at the end of randomFunction()? • [44] • [55] • [44, 55] • [“Sup”, “brah”, “lel”, “#yol0sw4G1~”]

  15. References • Consider the following code: public static void randomFunction2() { String a = “Hello”; String b = “Goodbye”; b = a; a.concat(“ CS201”); } • What is true at the end of randomFunction2? • b is “Hello CS201” • b is “Goodbye CS201” • b is “Hello” and a is “Hello CS201” • b is “Hello” and a is “Hello” • Explain your answer to #4.

  16. References • If you’re still a bit confused, don’t worry—Professor Peck will explain in more detail in lecture. • Just remember, Java remembers which objects are in memory by keeping references that point to those objects, and these references are assigned whenever you set something “=“ to something else. • You will learn more about how this works under the covers in CS250 if you take it…

  17. Have a good weekend! Make sure to submit your answers to get credit for today…

More Related