CS201 - Spring 2014 - Class 4

  • Admin
       - assignment 1 part 2

  • problems for the day

  • Quick recap: take a look at the EvenBetterCard class
       - three key parts of a class
          - instance variables (and CONSTANTS)
          - methods
             - accessor methods ask questions about the object
             - mutator methods change the object
          - constructor(s)
       - private/public
       - final
       - static
       - public static void main(String[] args)

  • In addition to checking that the card number passed to the constructor is correct, we'd also like to check the suit to make sure it's a valid suit
       - how can we do this?
          - check to make sure that the string is one of the 4 possible suits

  • look at isValidSuit method in EvenBetterCard class in ClassBasics code
       - notice anything unusual?

  • == vs. equals
       - when you declare a variable in Java it makes space for it in memory
       - if the variable is a primitive type (one of the 8 lowercase ones, like int, double, etc.) then the value that the variable is holding is the actual value (e.g. the number)
       - if the variable is not a primitive type (e.g. String, BasicCard, etc.), then the variable holds a *reference* to the object
          - for example, we can print out the BetterCard variable to see what is stored in it:
             - you see the type
             - followed by an @
             - followed by a number. This number is actually a memory location (sort of)
       - when you ask if two variables are ==, you're asking if the two values where the variables are stored are the same
          - for primitive types, this will do the intuitive thing
          - for non-primitive types, they will only be equal if they reference the same object!

             BetterCard card = new BetterCard(10, "heart")
             BetterCard card2 = card;

             System.out.println(card == card2);

             - this will give us true

             BetterCard card = new BetterCard(10, "heart")
             BetterCard card2 = new BetterCard(10, "heart")

             System.out.println(card == card2);

             - this will give us false!
       - If you want to ask if two object are equal (that is equivalent), then you you need to call the equals method
          - note that this is a method that YOU must implement if you're creating a new class
             - more on this later!

  • how do we check if two cards are equal?
       - check to see that their the same suit and same number
       - public or private?
          - public!

  • look at the Card class in ClassBasics code
       - I've added an equals method to check if two cards are equal
       - what does other.getNumber() == number check?
          - checks to make sure that *this* objects number is the same as the one that's been passed in

  • toString
       - It's kind of annoying always having to call the getNumber and getSuit method when we want to print out a card
       - If we try and print out a card right now, though, it prints out the memory location
       - We can override this behavior by defining a toString method, specifically:
          public String toString()

          - the method is called on the object and returns a String representation of that context
       - Anytime an object is used in a String context (e.g. for printing), then this method is called
       - Look at the toString method in the Card class
          - returns a new String that is the number plus the suit
       - Now, if we try and print out a card:
          Card c = new Card(10, "hearts");
          System.out.println(card)

          Java calls the toString method and we get a much better printout!
       - In fact, any time it's used in a string context, e.g.
          Card c = new Card(10, "hearts");
          String s = "The card is: " + c;
          System.out.println(s);

          the toString method is called!

  • Aliasing
       - What will be the value of x and y after the following statements:
          int x = 10;
          int y = x;
          x = 20;

          - x will be 20
          - y will be 10
       - assignment copies the value stored at the variable into the other variable
       - Look at CardTests class in ClassBasics code
          - What will be printed if we run aliasing1()?
             - For objects, what's stored in the variables is a *reference* to the object
             - = still copies the value stored in the variable, but that happens to be a reference!
          - What will be printed if we run aliasing2()?

  • testing our equals method
       - look at the testEquals method in the CardTests class in ClassBasics code