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.

Slides:



Advertisements
Similar presentations
10 Copyright © 2005, Oracle. All rights reserved. Reusing Code with Inheritance and Polymorphism.
Advertisements

ThinkPython Ch. 10 CS104 Students o CS104 n Prof. Norman.
The ArrayList Class and the enum Keyword
This is Java Jeopardy.
The Random and String Classes The import statement.
MSc IT Programming Methodology (2). number name number.
Java Programming Strings Chapter 7.
Chapter 10 Review. Write a method that returns true is s1 and s2 end with the same character; otherwise return false. Sample Answer: public boolean lastChar(String.
Written by: Dr. JJ Shepherd
©2004 Brooks/Cole Chapter 7 Strings and Characters.
CSCI 1100/1202 April 3, Testing A program should be executed multiple times with various input in an attempt to find errors Debugging is the process.
© 2006 Pearson Education Chapter 4: Writing Classes Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by John Lewis,
CIT 590 Intro to Programming Java lecture 4. Agenda Types Collections – Arrays, ArrayLists, HashMaps Variable scoping Access modifiers – public, private,
10-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Programming with Java, for Beginners
Arrays Liang, Chpt 5. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Arrays Horstmann, Chapter 8. arrays Fintan Array of chars For example, a String variable contains an array of characters: An array is a data structure.
Constants Variables change, constants don't final = ; final double PI = ; … area = radius * radius * PI; see Liang, p. 32 for full code.
Chapter 3 Using Classes and Objects. 2 Creating Objects  A variable holds either a primitive type or a reference to an object  A class name can be used.
1 Programming section 3 2 Importing classes purpose packages CLASSPATH creating and using a package.
Working with the data type: char  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
Chapter 2 storing numbers and creating objects Pages in Horstmann.
School of Computing Science CMT1000 Ed Currie © Middlesex University Lecture 4: 1 CMT1000: Introduction to Programming Ed Currie Lecture 5a: Input and.
Introduction to Computers and Programming Strings Professor: Evan Korth New York University.
28-Jun-15 Using Objects. 2 Overview In this presentation we will discuss: Classes and objects Methods for objects Printing results.
29-Jun-15 Using Objects. 2 Classes and objects The type of an object is the class that describes that object If we say int count, the type of count is.
Week 10 - Monday.  What did we talk about last time?  Method overloading  Lab 9.
Java 2 More Java Then Starbucks ;-). Important Terms Primitive Data – Basic, built-in values (characters & numbers) Data Type – Used to talk about values.
SE-1010 Dr. Mark L. Hornick 1 Some Java Classes using & calling methodsS.
5 BASIC CONCEPTS OF ANY PROGRAMMING LANGUAGE Let’s get started …
Strings Carol Yarbrough AP Computer Science Instructor Alabama School of Fine Arts.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Chapter 7: Characters, Strings, and the StringBuilder.
Chapter 6 Introduction to Defining Classes. Objectives: Design and implement a simple class from user requirements. Organize a program in terms of a view.
CSCI 1100/1202 April 1-3, Program Development The creation of software involves four basic activities: –establishing the requirements –creating.
Computer Science 111 Fundamentals of Computer Programming I Working with our own classes.
Working With Objects Tonga Institute of Higher Education.
String Class. Variable Holds a primitive value or a reference to an object. Holds a primitive value or a reference to an object. A reference lets us know.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Week 3 - Friday.  What did we talk about last time?  Operations on boolean values  !, &&, ||  Operations on char values  +, -  Operations on String.
Chapter 2 Input, Variables and Data Types. JAVA Input JAVA input is not straightforward and is different depending on the JAVA environment that you are.
CSC 212 – Data Structures Lecture 2: Primitives, References, & Classes.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Java Classes Chapter 1. 2 Chapter Contents Objects and Classes Using Methods in a Java Class References and Aliases Arguments and Parameters Defining.
Road map char data type Reading –Liang 5: Chapter 2: 2.7.4; 2.9; –Liang 6: Chapter 2: 2.7.4; 2.9 –Liang 7: Chapter 2: 2.7.4; 2.9.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
ITK 168 – More Variables 10/13/05. Another example of using instance variables and constants  Go through SimpleBot  Modify SimpleBot to “teleport”
1 Predefined Classes and Objects Chapter 3. 2 Objectives You will be able to:  Use predefined classes available in the Java System Library in your own.
Strings, Characters, and Regular Expressions Session 10 Mata kuliah: M0874 – Programming II Tahun: 2010.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Eastside Robotics Alliance / Newport Robotics Group 1 T/Th, 6:30 – 8:30 PM Big Picture School Day 3 · 10/9/2014.
3.1 Objects. Data type. Set of values and operations on those values. Primitive/Built-In types. n Usually single values n Can build arrays but they can.
COP Topics Java Basics - How to write & call your own method - Math methods (static methods) - String methods (instance methods) - Errors Classes.
String class.
Foundations of Programming: Arrays
String Handling in JAVA
String Objects & its Methods
Lesson 2: Building Blocks of Programming
Chapter 7: Strings and Characters
Creating Objects A variable holds either a primitive value or a reference to an object A class name can be used as a type to declare an object reference.
Outline Creating Objects The String Class The Random and Math Classes
AP Java Unit 3 Strings & Arrays.
String Class.
What is a String? String s = "compsci"; s c o m p s i
More on iterations using
String Objects & its Methods
Presentation transcript:

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 isnt 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 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

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 dont 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 dont 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 doesnt 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