CS201 - Spring 2014 - Class 8

  • Variable declarations
       - When we write Shape s = new Square(4), we're actually doing two things:
          1. Declaring a new variable s of type Shape
          2. Creating a new Square object and assigning it to the s variable
       - In fact, we can break these statements into two steps explicitly:
          Shape s;
          s = new Square(4);
       - By declaring a variable to be of a particular type, you are defining
          1) the types of things you can assign to that variable and
          2) the types of things you can do with that variable (really, with the things that variable references)
          - For example, if you declare a variable:
             String someString;

             you're saying that anything that you assign to that variable must have the functionality of a String, things like:
             - length()
             - substring()
             - ...
       - During assignment, Java checks to make sure that the object that you're assigning to it meets the requirements of the variable
          - What would happen if it didn't?
             String s = new Card(2, "clubs"); // this doesn't make sense!
             s = s.substring(2, 4); // this definitely doesn't make sense!

       - Assignment and declaration are different things!
          - In a given scope, you can only declare a variable once, e.g.
             Shape s;
             Shape s;

             doesn't make sense
          - You can assign to a variable as many times as you want
             String s;
             s = "this is a string"
             s = "this is another string"
             s = "this is another string"

             - or more reasonably, something like the concat function from ArrayExamples code

  • In class exercise/exercise

  • the Java class hierarchy
       - *everything* in Java is a subclass of some class (except for 1 class in Java)
       - you can only inherit from one class
       - if you don't inherit from a class, by default, you inherit from the class Object
       - You can see the object hierarchy at: http://docs.oracle.com/javase/7/docs/api/overview-tree.html

  • Look at ShapeMaker class in Shapes code
       - What does the getShapes method do?
          - prompts the user for a number
          - then prompts them for that number of shapes
          - if the user enters a rectangle or square it creates one
          - otherwise creates a generic shape
       - Where does it store these shapes?
          - in an array of Shapes
       - Why does that work?
          - Rectangles and Squares are both shapes!
       - What does the printShapes method do?
          - goes through the Shape array and prints each shape out (i.e. calls the toString method)

  • Unit testing
       - What is a test? In computer science/programming terms?
          - A way for checking if some code is working correctly
       - There are many different types of tests
       - A "unit" test is a test that test a small unit of functionality
          - of a single method/function
          - or even single working case of a method/function

  • JUnit
       - Each language has different kinds of support for unit testing
       - In Java, the most popular unit testing framework is called JUnit
       - It is NOT technically part of Java, but is instead an extra library that was written
          - It's a library add-on
          - See http://junit.org/ for more information

  • Writing tests in JUnit