CS201 - Spring 2014 - Class 5

  • Quiz!

  • Admin
       - assignment 1: how did it go?
       - assignment 2, part 1 posted later today
          - due on Friday (even though we don't have class)
       - assignment 2, part 2 also posted later today

  • programming tips
       - build up your code incrementally, testing as you go
       - use print statements (i.e. System.out.println() to understand what's going on with your code
          - remove these before submitting!

  • arrays
       - Java has many ways to store multiple items in a single variable
       - The most fundamental way is to use an array
       - Arrays:
          - hold multiple values **of the same type**
          - have a fixed length
             - to get the length, access using .length (not a method call!)
          - are indexed using []
          - similar to Lists in python
             - except they're not extensible, they have a fixed length
             - don't have all of the fancy functionality
             - can't slice them
       - to create a new array:
          type[] variable = new type[size]

          for example:
             // creates an array of 10 integers
             int[] numbers = new int[10];

             // creates an array of 50 Strings
             String[] strings = new String[50];

       - once you've created an array you can index them to either put things in them or read data from them
          numbers[0] = 10;
          System.out.println(numbers[0])

          - Array indexing starts at 0!
       - arrays define they're own type, e.g.
          - int[] -> array of integers
          - String[] -> array of Strings
          - boolean[] -> array of booleans

  • look at sumArray and sumArray2 functions in ArrayExamples.java in ArrayExamples code
       - both iterate through an array of integers and sum them up
       - arrays are indexed starting at 0
       - notice the .length call

  • "for each" loop
       - For some types of things (like arrays) that store multiple things of the same type, there is a special way to iterate over them
       - the "for each" loop allows you to do this:

          for( <type> <temp_variable>: <array> ){

          }

          - <type> is a type (e.g. int, double) and should be the same type as the things stored in the array
          - <temp_variable> is a temporary variable that is available only inside the for loop
          - <array> is the array to iterate over
       - The for loop block is run one time for each item in the array
       - For each iteration, the temp_variable is assigned the next value in the array
          - the first time through the loop it gets the first value (i.e. at index 0)
          - second time through, the second value (i.e. at index 1)
          - ...
       - This is very similar to the loop structure in python

  • What does the longerThan function do?
       - Iterates over an array of strings
       - counts how many of them are greater than or equal to the minimum length

  • concatenating strings
       - when used with a string, the '+' operator concatenates
       - For example:
          - "bananas" + " and " + " apples"
          gives us "bananas and apples"
          
          - 10 + " bananas"
          gives us "10 bananas"
             - Java converts the other items to a String if one of the arguments is a string

  • What does the concat function do?
       - takes a list of strings and concatenates them together into one big string with spaces in between
       - trim removes leading and trailing whitespace (look at the documentation)

  • What does getNumbers do?
       - generates a new array of size number
       - puts the numbers from 1 to number in the array
       - what would be stored in numbers[10]
          - 11!
          - indexed starting at 0   

  • talk about main (specifically String[])

  • I want to create a class that represents a deck (or multiple decks) of cards that allows me to deal cards
       - What functionality (i.e. methods might I want)?
          - get the next card
          - are the more cards left?
          - shuffle the cards
          - create a new deck of cards
       - What state should I be saving?
          - the deck of cards
          - which card we're on
          
  • look at CardDealer class in ArrayExamples code
       - Two constants
          - you can declare an array on the fly using curly braces {}
       - Two instance variables
          - array of cards
          - where we are in dealing the cards
             - anything different here?
             - if we know the initial value of an instance variable without needing input (i.e. from the constructor), then we can initialize it directly
       - What does the constructor do?
          - constructs all the cards in the deck
          - constructs multiple decks of cards
          - notice that when we construct the array of cards, we need to specify the length
          - what is cardIndex?
             - used to keep track of which card we're on
       - How do we get the next card?
          - give the current card
          - increment our position counts
       - How do we tell if we still have cards left?
          - if the position counts is less than the number of cards (i.e. cards.length)
       - How do we shuffle the cards?
          - many ways!
          - one way, for each card, pick another card in the deck and swap with it
          - there is a class called Random that has support for generating random numbers
             - see the documentation for all of the different types of methods
             http://docs.oracle.com/javase/6/docs/api/java/util/Random.html
          - A package is a collection of classes and is defined by a directory in our project
          - Any class NOT inside the package must be imported (i.e. brought in)
          - The Random class is in the java.util package. To bring it in we put:
             import java.util.Random

          - import statements should go at the top of your file *outside* of the class definition