Chapter 9 Moving Beyond Robots to Objects. 9.1 Objects Objects are electronic things Objects can do...

Click here to load reader

download Chapter 9 Moving Beyond Robots to Objects. 9.1 Objects Objects are electronic things Objects can do stuff –can remember stuff –can talk to other objects

of 28

  • date post

    26-Mar-2015
  • Category

    Documents

  • view

    215
  • download

    1

Embed Size (px)

Transcript of Chapter 9 Moving Beyond Robots to Objects. 9.1 Objects Objects are electronic things Objects can do...

  • Slide 1

Chapter 9 Moving Beyond Robots to Objects Slide 2 9.1 Objects Objects are electronic things Objects can do stuff can remember stuff can talk to other objects Act more like people & cats than rocks & freshman Slide 3 9.1 Objects Most objects are passive only active when asked to do something when asked to do something that isnt appropriate, it is an error Slide 4 9.1 Objects Objects in the programming world primarily robots corners are almost like objects we also used a random number generator object flipping a coin in RandomWalker Slide 5 9.1 Objects Some objects are things They have names They can also have aliases names are also called variables because the object they refer to can change In general, a variable of an object type/class can refer to any object of its type or any subtype Slide 6 9.1 Objects In Java variable can refer to other things besides objects some variables dont reference things, but hold things e.g., int size = 30; creates and assigns without using new Variable that isnt a reference to an object can only hold a value of its own precise type Slide 7 9.1 Objects What does this do? int size = 30; int bigger = size + 30; System.out.println(bigger); How about this? int size; int bigger = size + 1; System.out.println(bigger); Slide 8 9.2 Classes class is short for classificaton A class describes a set of objects of the same kind. A class describes what an object can do and/or remember A good way to think about a class is that it is a factory for creating objects of that type. Slide 9 9.2 Classes How do you make a robot remember its name? class BrainyRobot extends Robot{ public BrainyRobot(String name, ) { super (); myName = name; } public String name() { return myName; } private String myName; } A String is a sequence of characters, usually in double Quotes( ) Slide 10 9.2 Classes String is a Java class myName is an instance variable Usually, one initializes an instance variable in the constructor(s) It is called an instance variable because each instance of the class that was instantiated has its own instance of the variable(memory) In Java we can print out a string System.out.println( someString ); Or, equivalently System.out.println( someString.toString() ); Slide 11 9.3 String A String is a sequence of (any) characters A String object performs services for us e.g., remembers the sequence of characters tell us its length: someString.length(); return a new String: String more = someWord.concat(a word); Or, equivalently String more = someWord + a word; Slide 12 9.3 String Examples: String word1 = one Fish; word1.length() returns 8 String word2 = Fox in Socks; word2.length() returns 12 String word3 = word1.concat( two Fish); word3.length() returns 17 because word3 contains the String one Fish two Fish word1.length() still returns 8 because word1 has not been altered Slide 13 9.3 String String class has 30+ services but none change the characters because there are no modifier methods Strings are immutable a string variable can refer to different Strings Slide 14 9.3 String String word1 = The cat; String word2 = word1; word1 = word1 + in the Hat; word1.length() returns 18 because word1 contains the String The cat in the Hat However word2.length() returns 7 because word2 contains the String The cat Slide 15 9.3 String We can look at the individual char(acters) someString.charAt(someIntValue) Each char is stored in a numbered slot start with zero aString.charAt(0) represents/returns the first char aString.charAt(4) represents/returns the 5th char aString.charAt(n) is an error if aString.length()