CS62 - Fall 2020 - Class 3
Example code in this lecture
Initialization
CardDealer
Lecture notes
Admin
- mentor/office hours posted
- lab 1 feedback sent
- how is assignment 1 going?
- assignment deadlines: 11:59 *any* time zone
- lab tomorrow
- lab exercise during lab
- quiz on Thursday
- assignment 2 out on Thursday (we'll cover some new material on Thursday... this is the last time we'll do this)
initializing variables
- When we declare a new variable, Java creates a new space in memory for the value
- if it's a primitive type, the value will go directly there
- if it's anything else, it will be a reference to an object
- We often will declare the variable and assign to it immediately:
int x = 2;
Card c = new Card(1, "hearts");
- However, we can also decouple these steps:
int x;
x = 2;
Card c;
c = new Card(1, "hearts");
- What would happen if we looked at/printed out the value of a variable *before* assigning to it? - In some languages, this isn't well defined!
- In Java, when you create a variable without assigning to it, it gets a default value
- For the primitive types, it's something reasonable (often 0)
- For everything else, we get "null"
- null is Javas way of indicating that this variable is not associated with a value.
look at Initialization.java in
Initialization code
- What would happen if we run test1()?
- Where is the constructor?!?
- If you don't specify a constructor, Java will automatically generate a zero parameter constructor for you
- We'll see:
2
4 of hearts
- What would happen if we run test2()?
- We haven't set the values, so we'll get the default values
0
null
- What would happen if we run test3()?
- try to call a method on an object that doesn't exist!
java.lang.NullPointerException
- You will get null pointer exceptions
- it just happens if you program long enough in Java
- The cause is that you tried to access some value as if it were an object, but it was actually null
What does the constructor do in FixedCardDealer in
CardDealer code
- What does the constructor do?
- creates a new array for number of decks * 13 * 4 entries
- adds each card for each deck to the array
- outer loop is over the number of decks
- second loop is over suits
- third loop is over numbers
initializing arrays
- often, we'll initialize an array and then fill it in, e.g.,
int[] x = new int[10];
x[0] = 2;
...
- sometimes (e.g., with constants), we'll want to initialize the array with values. To do this, use curly braces
int[] x = {1, 2, 3, 4, 5};
Look at the FixedCardDealer in
CardDealer code
- What does the shuffle method do?
- for each card, pick another random entry and swap the entries
- Where does Random come from?
- it's another class that we imported!
A package is a collection of classes
- usually they're related
- Any class inside a particular package may use any other class in the package without doing anything special
- If you want to use a class that is not inside your package, you need to import it
- To import a class:
import <package_name>.<class_name>
- sometimes, there are nested packages, e.g.
import <outer_package>.<inner_package>.<class_name>
- For example:
import java.util.Random
- imports the Random class from the java.util package
import java.util.Scanner
- imports the Scanner class from the java.util package
- To write your own package:
- Packages are indicated in two ways (both are required):
1) All classes inside a packages should be in a directory with the packages name
2) All classes inside a package should start with package <package_name>;
Look at the FixedCardDealer in
CardDealer code
- What does the printDeck method do?
- it's static, so it doesn't have access to the instance variables
- creates a new card dealer and then prints out the entire deck
- utilizes next and getNext
- both use position to keep track of where we are
- using this pair of methods is a common way to iterate through data. we'll see it a lot!
What if we wanted to add an addDeck method?
- We could do it, but it would be a bit painful
- have to create a new array that's one deck larger
- copy the existing cards over
- then add another deck
ArrayLists
- Arrays are of fixed length and are built into the language
- ArrayLists are built on top of arrays (underneath the covers, they are implemented using arrays), but they allow much more functionality than arrays
- they are much more similar to lists in python
- ArrayLists are a class, so to use them you create them by calling the constructor and then interact with them via methods
- Constructing a new ArrayList
ArrayList<type_of_items> name = new ArrayList<type_of_items>();
e.g.,
ArrayList<Card> cards = new ArrayList<Card>();
- This is calling the constructor with a bit of information about what type of thing we're going to be storing in the ArrayList
- ArrayLists have many methods, the most common we'll use now are:
- size()
- how many things are in the ArrayList
- get(index)
- get the value at index (will be of whatever type the ArrayList was initialized as)
- set(index, value)
- set entry at index to value. index must be between 0 and size()-1 and the value must be of the type that was initialized.
- add(value)
- add the value to the end of the ArrayList and increase the size by 1
- this is something we couldn't do easily with arrays!
Look at CardDealerArrayList class in
CardDealer code
- Almost identical to FixedCardDealer!
- Constructor is simpler since we don't have to precalculate the size or keep track of the current index
- shuffle uses get/set
- Can now support the addDeck functionality without too much difficulty
Look at CardDealer class in
CardDealer code
- any time you have repeated code you should consider making a helper method with the code and then calling it instead
- the addDeckWithoutShuffle method adds a single deck and we can add call it in both addDeck and the constructor