CS 100, Summer 2001 Thursday, 7/12 Lecture 10 ----------------------------------------------------------------------- Announcements: + Assignment 3 due 7/13 + drop deadline is Friday + did you check the posted grades? + reading: Savitch 5.4 - also, go back now and re-read 5.1, 5.2, 5.3 - they are certain to make more sense now! ----------------------------------------------------------------------- Topics: + classes + objects + references + encapsulation (public, private, static) ----------------------------------------------------------------------- ---------------------------------------------------------------------- Classes: (section 4.3) + the code that you write that combines variables and methods + general syntax: modifier class classname optionalstuff { -> identity variables; -> data, state methods; -> how to use, create (behavior) } + examples used so far: System.out, Math, SavitchIn, your main classes + concept: blueprint/pattern for creating objects that you can use + like adding a type to a language - this is very powerful! EXAMPLES: BunchOfGrapes.java, Spaceship.java ---------------------------------------------------------------------- Programming with Classes: + put classes together in same project/folder/dir - Java SDK wants 1 public class per file (and so do we, for style) - can have more than 1 class in file if only 1 is public (CodeWarrior allows you to break this rule!) - classes in same project can "see" each other (think scope) + How to use classes? - put vars and methods inside other classes - use dot (.) operator to access a method or var var: identifier.identifier (can have more dots) method: identifier.identifier(arguments) (can have more dots) - 2 Programming Paradigms (1) Procedural programming: make everything static inside class you wish to use ex) Math.random(), Math.PI This is what we've been doing so far. (2) OOP: use specific instances of class called OBJECTS (class methods are NOT static) - "update" methods (updating values) - "accessor" methods (getting values) + You should refer to BunchOfGrapes.java when working on Spaceship methods. EXAMPLES: BunchOfGrapes.java, Spaceship.java + What does STATIC really mean? - implies "unchanging" or "the same for all objects" - it makes a variable or method exist at the CLASS level - all objects of the class have access - and more importantly, you don't need an object to get access to them. - EXAMPLE: Spaceship.Earth - don't need a Spaceship to know where Earth is. + What if a variable or method is not marked static? - these are referred to as INSTANCE VARIABLES or methods - they require an object to be created before using/accessing them - EXAMPLE: Spaceship.move() - doesn't make sense to say move() unless you have created a Spaceship that you wish to move + Note that there is an important difference between "static" and "final" - static: same for all objects of the class type, but could be changed (and then every object in the class would get an updated version) - final: cannot be changed (constant) - so you can have "static final" variables, as well as just "static" ones. (e.g., Earth is static and final because we don't want anyone to change it, and we want all Spaceships to know where Earth is) ---------------------------------------------------------------------- Objects: + think back: how to use a variable? type varname; varname = value; + specific instance of a class + declaring and assigning objects: classname var; var = new classname(arguments); + we've already seen examples of this, with String objects String s = "Hello!" is the same as String s = new String("Hello!"); - This is a shortcut that only works for strings: Java automatically interprets any quoted string as new String(that_string); --------------------------------------------------------------------- + References - create objects by INSTANTIATION new Classname(arguments) - gives the ADDRESS IN MEMORY (location) of the NEWLY CREATED OBJECT - location is called a REFERENCE - Why? objects too "large" to move, so instead, just pass address - If you looked at the "variables as cups" example, references were described as "remote controls" - this also explains why we can't do == to check if they're the same; == will compare whether the OBJECTs the references point to are the same object. - to compare the objects, use .equals() - see p. 270 in Savitch + Draw some pictures! (these should be in your notes) + assignment - int x = 3; int y = 2; y = x; y = 5; // what is x? - x doesn't change! - BUT.... Spaceship s1 = new Spaceship(); // at (5,5) Spaceship s2 = s1; // reference to s1 (5,5) s2.move(-1, 2); // now s2 is at (4,7) // but so is s1! (They're the same spaceship) - references "point" at objects. - but I can also do s2 = new Spaceship(2,2); // at (2,2). s1 is still at (4,7)! - now s2 "points" somewhere else. --------------------------------------------------------------------- + Encapsulation - more keywords: (Savitch p. 247) - public: visible to anyone - private: visible only to other methods in the same class - style: you should always make instance variables "private" - methods can be public/private, too: this specifies where the method can be called from (anywhere, or just from inside the class) - so how do you get the value of a private instance variable? - use an "accessor" method EXAMPLES: Position.getX(), Spaceship.getFuel() - and how do you change the value of a private variable? - use an "update" method (Savitch says "mutator" method) EXAMPLES: Spaceship.move(), Spaceship.hyperjump() - But what's the point? Why not just make xPosition and yPosition public and let the programmer update them directly? - "update" methods allow you to check to make sure the programmer isn't giving you invalid input - what if the programmer set xPosition and yPosition to -1, -1?? - by defining an "update" method, you can control what kind of changes you'll allow to those variables. + Encapsulation: hiding the details, separating the interface from the implementation - interface: what a programmer needs to know, to be able to use your class - implementation: how you actually wrote the class (all the details that whoever is using your class need not know) - see Encapsulation guidelines (style rules!) on p. 257-8. + Motivation: 1) Protection (don't let adversarial programmers wreck your spaceship!) 2) Abstraction (other programmers don't need (or want) to know about the gritty details --------------------------------------------------------------------- PICK UP Graded Exercise 5