280 likes | 289 Views
Learn about electronic objects, variables in Java, strings, and classes for creating objects. Understand the concepts through examples and practical applications.
E N D
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 • Act more like people & cats than rocks & freshman
9.1 Objects • Most objects are passive • only active when asked to do something • when asked to do something that isn’t appropriate, it is an error
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
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
9.1 Objects • In Java • variable can refer to other things besides objects • some variables don’t reference things, but hold things • e.g., int size = 30; • creates and assigns without using new • Variable that isn’t a reference to an object • can only hold a value of its own precise type
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);
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.
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( “ “)
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() );
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”;
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
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
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”
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()<= n
9.3 String String word1 = “The cat”; word1.charAt(0) returns the char ‘T’ word1.charAt(4) returns the char ‘c’ word1.charAt(5) returns the char ‘a’ word1.charAt(7) is an error because word1.length() <= 7
9.3 String • We can also “add” (concatenate) Strings for example: String more = “help me”; String stillMore = more + “Please”; stillMore.length() returns ???? stillMore contains what sequence ????? more = “someone” + more; more.length() returns ???? more contains what sequence ?????
9.3 String • In Java, Strings don’t behave polymorphically • because the class is declared as final • prohibits you from building a subclass of String • i.e., you can not extend String • methods can also be final • can not be overridden • A final class means all its methods are final.
9.4 Parameters for Robots • When we ask an object to perform a service, we need to send additional information. aString.charAt(whichCharacter); • or
9.4 Parameters for Robots • if we want karel to move several blocks. public void moveSeveralBlocks(int howManyBlocks){ for(int i = 0; i <howManyBlocks; i++) move(); } • calls to moveSeveralBlocks karel.moveSeveraBlocks(4); // moves karel 4 corners karel.moveSeveraBlocks( someIntValue() );
9.4 Parameters for Robots • It is possible to define several methods in the same class with the same name, provided: • they DIFFER in “number and type” • Compare these 4 examples – which could be in the same class??? public void move(int distance) {…} public void move(double distance) {…} public boolean move(int dist) {…} public void move(int distance, char ch) {…} • Different return type only is NOT sufficient!
9.5 Other Classes • Math is a special type of class • Certain messages can be sent to classes/methods. • These are called static methods • A static member of a class is shared by all objects in the class and, if public, shared everywhere.
9.5 Other Classes • Math class has something like public class Math { public static int abs(int x) {…} } • This allows calls like int absoluteValue = Math.abs(-12);
9.5 Other Classes • Variables can also be static (or shared) • Static variables don’t have separate copies in each object of the class • Instead, the one variable is shared by ALL objects of the class!
9.6 Still more Java System.out.println(“The robot has + karel.numBeepers() + “ beepers”); • notice the mixed use of the + operator with Strings and ints
9.8 Java Arrays Racer[] racers = new Racer[20]; • creates an array but doesn’t create any Robots
9.8 Java Arrays for(int i = 0; i < racers.length; i++) racers[i] = new Racer(i, 1, East, 0); • creates the Robots and stores their reference in the racers array. • Slots are numbered from 0 just like Strings • length is used without the () • accessing an index out of range causes an ArrayIndexOutOfBoundsException run-time exception.
9.9 Important Ideas From This Chapter • String • array • static method, static variable • final class • final method • final