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

28
Chapter 9 Moving Beyond Robots to Objects

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

Page 1: 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.

Chapter 9

Moving Beyond Robots to Objects

Page 2: 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.

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

Page 3: 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.

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

Page 4: 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.

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

Page 5: 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.

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

Page 6: 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.

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

Page 7: 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.

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);

Page 8: 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.

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.

Page 9: 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.

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( “ “)

Page 10: 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.

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 stringSystem.out.println( someString );Or, equivalentlySystem.out.println( someString.toString() );

Page 11: 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.

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”;

Page 12: 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.

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

Page 13: 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.

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

Page 14: 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.

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”

Page 15: 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.

9.3 String

• We can look at the individual char(acters)– someString.charAt(someIntValue)

• Each char is stored in a numbered slot – start with zeroaString.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

Page 16: 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.

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

Page 17: 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.

9.3 String

• We can also “add” (concatenate) Stringsfor 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 ?????

Page 18: 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.

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.

Page 19: 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.

9.4 Parameters for Robots

• When we ask an object to perform a service, we need to send additional information.aString.charAt(whichCharacter);

• or

Page 20: 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.

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 moveSeveralBlockskarel.moveSeveraBlocks(4); // moves karel 4 corners

karel.moveSeveraBlocks( someIntValue() );

Page 21: 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.

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!

Page 22: 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.

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.

Page 23: 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.

9.5 Other Classes

• Math class has something likepublic class Math

{public static int abs(int x) {…}

}

• This allows calls likeint absoluteValue = Math.abs(-12);

Page 24: 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.

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!

Page 25: 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.

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

Page 26: 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.

9.8 Java Arrays

Racer[] racers = new Racer[20];– creates an array but doesn’t create any Robots

Page 27: 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.

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.

Page 28: 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.

9.9 Important Ideas From This Chapter

• String

• array

• static method, static variable

• final class

• final method

• final