AP Computer Science A · Unit 3 · 10-18% of the exam · about 80 minutes

Unit 3: Class Creation

By the end: Design and write a complete Java class: choose its private data, write constructors that set it up, write methods that return or change it, and trace exactly what happens with object references, static variables, scope and this.

Mr. Thompkins
  1. 3.1 Abstraction and program design · 8 min

    Design before you type

    • Attributes (what an object knows) become private instance variables. Behaviors (what it does) become methods.
    • A class diagram lists the name, the attributes with types, and the methods with parameter and return types. Minus means private, plus means public.
    • Data abstraction: treat related data as one object. Procedural abstraction: use a method knowing what it does, not how.
  2. 3.2 Impact of program design · 6 min

    Code that real people depend on

    • Reliable means it works under all conditions it will meet. Raise reliability by testing typical, boundary and unusual cases.
    • Check both the return value and the object's state after each test.
    • Code is intellectual property. Use others' code only with permission (a license), and give credit.
  3. 3.3 Anatomy of a class · 9 min

    Inside a class: private data, public methods

    • Skeleton: public class header, private instance variables, public constructors, then methods.
    • Encapsulation: data is private and reachable only through the methods the author offers.
    • Clients use public methods; private helper methods serve the class's own code.
  4. 3.4 Constructors · 10 min

    Constructors: every object starts ready

    • A constructor has the class's name and no return type. It sets the initial state of every instance variable.
    • Overloaded constructors have different parameter lists. Java picks the one that matches the arguments.
    • Unassigned instance variables keep defaults: 0, 0.0, false, null. Java gives a no-parameter constructor only when you write none.
  5. 3.5 Methods: how to write them · 10 min

    Methods: give back, or change

    • Header: access, return type, name, parameters. void returns nothing; a non-void method returns a value of its type.
    • Accessors report without changing. Mutators change instance variables, and may also return something.
    • return ends the method at once. Every path of a non-void method must end in a return.
  6. 3.6 Passing and returning object references · 9 min

    References: two names, one object

    • Object variables hold references. Assigning one to another copies the reference and creates an alias.
    • A method that receives an object can change it through mutators, and the caller sees the change.
    • Assigning a new object to a parameter changes only the method's copy of the reference.
  7. 3.7 Class variables and methods · 8 min

    static: one copy for the whole class

    • static variable: one copy shared by the whole class. Use it for counters and shared values.
    • static final: a class constant that can't be reassigned.
    • Call static methods on the class name: Order.isValid(7), Math.sqrt(2).
  8. 3.8 Scope and access · 7 min

    Scope: where a variable lives

    • Local variables and parameters exist only inside their block or method.
    • Instance variables exist as long as the object and are usable in all its instance methods.
    • A local variable or parameter with an instance variable's name shadows it inside that method.
  9. 3.9 this keyword · 8 min

    this: the object that is running

    • this is a reference to the object whose method or constructor is running.
    • this.x = x; assigns the parameter x to the instance variable x.
    • c.addMember(this) passes the current object; return this; returns it.
  10. Unit 3 wrap-up · 12 min

    Put it together