I. Objects and Classes A. example introduction ------------------------------------------ SAMPLE DEVELOPMENT INTRODUCTION Vision: a calculator for numerical tasks. The system can store numbers in registers. The system can store formulas as "subroutines"; these can refer to the values in registers. Use cases for first iteration: Store a number in a register: The user tells the system they want to enter a number into a register. The user tells the system the number. The user tells the system what register to store the number in. The system remembers that the number is in the given register. Store a formula in a subroutine: The user tells the system they want to store a formula in a subroutine. The user tells the system the name of the subroutine. The user gives the system the formula. The system remembers that the formula has the given name. Evaluate a formula. The user tells the system they want to evaluate a given formula, supplying the name. The system tells the user the result. ------------------------------------------ B. modeling 1. Analysis What concepts do we have in the above use cases? How should we name registers? How should we name formulas? What attributes? associations? ------------------------------------------ |-----------------| | FormulaMap | |-----------------| | 1 | | Associates | | * |-----------------| | FormulaNaming | |-----------------| | name: String | |-----------------| | 1 | | Names | | 1 |-----------------| | Formula | |-----------------| | 1 | 1 | | | Left | Right | | | 1 | 1 |-----------------| | Registers | |-----------------| | vals: Array of | | Number | |-----------------| ------------------------------------------ 2. Design How should we implement the numbers? How should we implement the FormulaMap? ------------------------------------------ DESIGN DECISIONS Reuse from Java: double (instead of number) String (for names) From the domain analysis: Formula FormulaMap Registers ------------------------------------------ Why not just represent a formula as a String? Why do we need a class for FormulaMap if we are just going to use a java.util.HashMap? Why do we need a class Registers if we're just using an array? a. Kinds of data types illustrated ------------------------------------------ KINDS OF TYPES WE'VE SEEN Atomic types: double Product types: Homogeneous: double[] String java.util.HashMap ------------------------------------------ What kind of type is Formula? If we implemented FormulaNaming, what kind would it be? b. coding How can we be sure "left" and "right" are always initialized? C. Object semantics (model of how it works) 1. What happens when an object is created? ------------------------------------------ OBJECT CREATION SEMANTICS Formula f1 = new Formula(0,1); ------------------------------------------ 2. What happens when you assign an object to a variable ------------------------------------------ ASSIGNMENT MAKES ALIASES Variables in Java hold "references" (pointers to objects that may be null) Stack Heap f [ null] f1 [ *---]---> (...) Assignment copies the reference After running f = f1; f [ *---]-\ \ f1 [ *---]---> (...) Assignment never copies objects by itself, To make a copy, you can do: f = f1.clone(); ------------------------------------------ 3. What happens when a method is called ------------------------------------------ METHOD CALLS package tests; import calc.*; import junit.framework.TestCase; /** Tests of the calculator. * @author Gary T. Leavens */ public class TestCalc extends TestCase { public TestCalc(String name) { super(name); } /** * Test simple summations in the calculator. */ public void testSummation1() { Formula f1 = new Formula(0, 1); Formula f2 = new Formula(1, 2); Registers.put(0, 4.0); Registers.put(1, 8.0); Registers.put(2, -1.0); FormulaMap.put("add01", f1); FormulaMap.put("add12", f2); assertEquals(12.0, FormulaMap.get("add01").evaluate(), 0.01); assertEquals(7.0, FormulaMap.get("add12").evaluate(), 0.01); } } ------------------------------------------ D. classes So what does a class do? What properties do objects of a class share? 1. static (class members) vs. instance (object members) What do you call non-static methods? ------------------------------------------ STATIC VARIABLE REPRESENTATION ------------------------------------------ How many copies of a static variable are there in a program? 2. What happens when a class is loaded? How are static variables initialized? How is the code for a static method found? How does that differ from the way code for an instance method is found? II. abstraction and information hiding A. compilation units (CUs) 1. privacy levels support information hiding ------------------------------------------ PRIVACY LEVELS IN JAVA public = private = (default) = These can be applied to all top-level declarations: - fields - methods - classes and interfaces ------------------------------------------ ------------------------------------------ EXAMPLES OF FIELD PRIVACY package examples; public class Privacy { private int priv = 0; int pack = 1; public int pub = 3; } EXAMPLES OF METHOD PRIVACY package examples; class Privacy2 { private void priv() {} void pack() {} public void pub() {} } ------------------------------------------ ------------------------------------------ WHICH ARE LEGAL? import examples.*; public class Client2 { // note: some of this is illegal! public void testPrivacy() { Privacy o = new Privacy(); int i = 3; i = o.priv; o.priv = i; i = o.pack; o.pack = i; i = o.pub; o.pub = i; } public void testPrivacy2() { Privacy o = new Privacy2(); o.priv(); o.pack(); o.pub(); } } ------------------------------------------ What would you use private fields for? Should you ever use public fields? How do clients access hidden stuff? 2. specification vs. implementation ------------------------------------------ SPECIFICATION vs. IMPLMENTATION Def: The *specification* of an object is Def: The *implementation* of an object is ------------------------------------------ ------------------------------------------ package calc; /** Formulas for the calculator. * @author Gary T. Leavens */ public class Formula { /** The left register */ private /*@ spec_public @*/ int left; /** The right register */ private /*@ spec_public @*/ int right; /** * Initialize this Formula object to be the sum of the given registers. * @param left the left register's number. * @param right the right register's number */ //@ requires 0 <= left && left < Registers.NUM_REGS; //@ requires 0 <= right && right < Registers.NUM_REGS; //@ ensures this.left == left && this.right == right; public Formula(int left, int right) { this.left = left; this.right=right; } /** * Evaluate the formula and return its value. */ /*@ ensures (* \result is the sum of the value of @ the left and right register's values. *); @*/ public double evaluate() { return Registers.get(left) + Registers.get(right); } } ------------------------------------------ Why write the specification down? consider the constructor, would we be able to pass it -1 as a register number? 3. interfaces in java ------------------------------------------ INTERFACES RECORD OBJECT SIGNATURES (AND SPECIFICATIONS) package calc; /** Interface of Formulas * @author Gary T. Leavens */ public interface FormulaType { /** * Evaluate the formula and return its value. */ //@ ensures (* \result is the value of the formula. *); double evaluate(); } ------------------------------------------ ------------------------------------------ CLASSES IMPLEMENT INTERFACES package examples; /** A person class. */ public class Formula implements FormulaType { /* ... */ } ------------------------------------------ 4. correctness ------------------------------------------ CORRECT IMPLEMENTATION Def: A class C *correctly implements* an ADT if each object of class C: (i) has an interface that subsumes the specified interface (ii) has code that satisfies its contract ------------------------------------------