Recitation 2

Download Report

Transcript 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);
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
Arrays
1. 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.
2. The call to Arrays.sort() will sort our string array in
alphabetical order. Why is this necessary?
3. 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.
4. The new method will return void instead of int. Why?
public int uniqueCount(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++;
}
}
}
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
Local Variables
• Consider the following code:
public class Frequencies {
public static void main(String[] args) {
public static int uniqueCount(String[] list) {
int count = 1;
Arrays.sort(list);
String[] list = {“apple”, “banana”, “apple”, pear”};
String last = list[0];
uniqueCount(list);
for (int k=1; k<list.length; k++) {
if (!list[k].equals(last)) {
}
}
last = list[k];
count++;
}
}
return count;
}
• 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()?
Local Variables
• Now consider the following slightly modified code:
public static void main(String[] args) {
public class Frequencies {
int count = 1;
public static int uniqueCount(String[] list) {
String[] list = {“apple”, “banana”, “apple”, pear”};
Arrays.sort(list);
uniqueCount(list);
String last = list[0];
for (int k=1; k<list.length; k++) {
}
}
if (!list[k].equals(last)) {
last = list[k];
• What is the scope of the k variable?
count++;
• If you type this code in Eclipse it will complain that “k cannot
be resolved to a variable”. Why?
}
}
System.out.println(last[k]);
return count;
}
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 (int i=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.
Instance Variables
• Instance variables have a scope of an entire instance of a class.
• Example:
public class Node {
private int value;
public int printValue() {
System.out.println(“Value is “ + value);
}
public void changeValue(int newValue) {
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.
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.
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)
list
other
List
Empty
ArrayList
References
• Go to this link to submit your answers: http://goo.gl/kbsjVq
References
• Consider the following code:
public class Rec2Example {
private int myA;
private int myB;
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);
}
}
1. How many times is the Rec2Example constructor called in the
main method?
a)
b)
c)
d)
0
1
2
-23.157
2. What is the value of ex2.myA at the end of main?
a)
b)
c)
d)
33
44
7
24,102.15034
References
• Consider the following code:
public static void randomFunction() {
ArrayList<Integer> list = new ArrayList<>();
ArrayList<Integer> otherList = list;
list.add(44);
otherList.add(55);
}
3.
What does otherList contain at the end of
randomFunction()?
a) [44]
b) [55]
c) [44, 55]
d) [“Sup”, “brah”, “lel”, “#yol0sw4G1~”]
References
• Consider the following code:
public static void randomFunction2() {
String a = “Hello”;
String b = “Goodbye”;
b = a;
a.concat(“ CS201”);
}
4. What is true at the end of randomFunction2?
a)
b)
c)
d)
b is “Hello CS201”
b is “Goodbye CS201”
b is “Hello” and a is “Hello CS201”
b is “Hello” and a is “Hello”
5. Explain your answer to #4.
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…
Have a good weekend!
Make sure to submit your answers to get credit for today…