Lecture 8-3: Encapsulation, this

Slides:



Advertisements
Similar presentations
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, toString reading: self-checks: #13-18,
Advertisements

Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Encapsulation, this, Subclasses.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2010 by Pearson Education 1 Assignment 11: Critters reading: HW11 assignment spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-x: Critters reading: HW9 Spec.
Object Oriented Programming Concepts OOP – reasoning about a program as a set of objects rather than as a set of actions Object – a programming entity.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Constructors and Encapsulation reading: self-checks: #10-17.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Encapsulation, this reading: self-checks: #13-17 exercises:
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 19: encapsulation, inheritance reading: (Slides adapted from Stuart.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
1 Object state: fields. Fields field: A variable inside an object that represents part of its internal state.  Each object will have its own copy of.
Copyright 2010 by Pearson Education Homework 9: Critters (cont.) reading: HW9 spec.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Copyright 2010 by Pearson Education Building Java Programs Homework 8: Critters reading: Critters Assignment Spec.
Copyright 2010 by Pearson Education Homework 8: Critters reading: HW8 spec.
Copyright 2008 by Pearson Education Building Java Programs Chapter 9 Lecture 9-2: Interacting with the Superclass ( super ); Discussion of Homework 9:
Copyright 2009 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-3: More Critters, static.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
Copyright 2008 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Methods and Constructors reading: self-checks: #1-12.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-3: Constructors; Encapsulation reading: self-checks: #13-18,
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Adapted from slides by Marty Stepp and Stuart Reges
Object Oriented Programming
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
Lecture 11: More on Classes
Homework 8: Critters reading: HW8 spec.
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
HW11 Assignment Specification
Classes and Objects, State and Behavior
Building Java Programs
Object initialization: constructors
Topic 28 classes and objects, part 2
Building Java Programs
CSE 142 Critters (IPL) behavior: Ant Bird Hippo Vulture
Topic 29 classes and objects, part 3
Building Java Programs
Adapted from slides by Marty Stepp and Stuart Reges
The software crisis software engineering: The practice of developing, designing, documenting, testing large computer programs. Large-scale projects face.
Building Java Programs
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Lecture 12: Classes II Building Java Programs: A Back to Basics Approach by Stuart Reges and Marty Stepp Copyright (c) Pearson All rights reserved.
Building Java Programs
Building Java Programs
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Building Java Programs
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
Building Java Programs
reading: 8.6 self-check: #18, exercises: #9, 14
Building Java Programs
Chapter 11 Inheritance and Encapsulation and Polymorphism
Topic 29 classes and objects, part 3
Building Java Programs
Topic 29 classes and objects, part 3
Presentation transcript:

Lecture 8-3: Encapsulation, this CSE 142, Spring 2013 Chapter 8 Lecture 8-3: Encapsulation, this reading: 8.5 - 8.6 self-checks: #13-17 exercises: #5

Abstraction Don't need to know this Can focus on this!!

tells Java how to convert an object into a String The toString method tells Java how to convert an object into a String Point p1 = new Point(7, 2); System.out.println("p1: " + p1); // the above code is really calling the following: System.out.println("p1: " + p1.toString()); Every class has a toString, even if it isn't in your code. Default: class's name @ object's memory address (base 16) Point@9e8c34

toString syntax public String toString() { code that returns a String representing this object; } Method name, return, and parameters must match exactly. Example: // Returns a String representing this Point. return "(" + x + ", " + y + ")";

Private fields A field can be declared private. No code outside the class can access or change it. private type name; Examples: private int id; private String name; Client code sees an error when accessing private fields: PointMain.java:11: x has private access in Point System.out.println("p1 is (" + p1.x + ", " + p1.y + ")"); ^

Accessing private state We can provide methods to get and/or set a field's value: // A "read-only" access to the x field ("accessor") public int getX() { return x; } // Allows clients to change the x field ("mutator") public void setX(int newX) { x = newX; Client code will look more like this: System.out.println("p1: (" + p1.getX() + ", " + p1.getY() + ")"); p1.setX(14);

Encapsulation encapsulation: Hiding implementation details of an object from its clients. Encapsulation provides abstraction. separates external view (behavior) from internal view (state) Encapsulation protects the integrity of an object's data.

Point class, version 4 // A Point object represents an (x, y) location. public class Point { private int x; private int y; public Point(int initialX, int initialY) { x = initialX; y = initialY; } public double distanceFromOrigin() { return Math.sqrt(x * x + y * y); public int getX() { return x; public int getY() { return y; public void setLocation(int newX, int newY) { x = newX; y = newY; public void translate(int dx, int dy) { x = x + dx; y = y + dy;

Client code, version 4 public class PointMain4 { public static void main(String[] args) { // create two Point objects Point p1 = new Point(5, 2); Point p2 = new Point(4, 3); // print each point System.out.println("p1: (" + p1.getX() + ", " + p1.getY() + ")"); System.out.println("p2: (" + p2.getX() + ", " + p2.getY() + ")"); // move p2 and then print it again p2.translate(2, 4); } OUTPUT: p1 is (5, 2) p2 is (4, 3) p2 is (6, 7)

CSE 142 Critters behavior: Ant Bird Hippo Vulture Husky (creative) eat eating food fight animal fighting getColor color to display getMove movement toString letter to display

A Critter subclass public class name extends Critter { ... } public abstract class Critter { public boolean eat() public Attack fight(String opponent) // ROAR, POUNCE, SCRATCH public Color getColor() public Direction getMove() // NORTH, SOUTH, EAST, WEST, CENTER public String toString() }

How the simulator works "Go" → loop: move each animal (getMove) if they collide, fight if they find food, eat Simulator is in control! getMove is one move at a time (no loops) Keep state (fields) to remember future moves Next move? %

Development Strategy Do one species at a time in ABC order from easier to harder (Ant → Bird → ...) debug printlns Simulator helps you debug smaller width/height fewer animals "Tick" instead of "Go" "Debug" checkbox drag/drop to move animals

Critter exercise: Cougar Write a critter class Cougar: Method Behavior constructor public Cougar() eat Always eats. fight Always pounces. getColor Blue if the Cougar has never fought; red if he has. getMove Walks west until he finds food; then walks east until he finds food; then goes west and repeats. toString "C"

Ideas for state You must not only have the right state, but update that state properly when relevant actions occur. Counting is helpful: How many total moves has this animal made? How many times has it eaten? Fought? Remembering recent actions in fields is helpful: Which direction did the animal move last? How many times has it moved that way? Did the animal eat the last time it was asked? How many steps has the animal taken since last eating? How many fights has the animal been in since last eating?

Cougar solution import java.awt.*; // for Color public class Cougar extends Critter { private boolean west; private boolean fought; public Cougar() { west = true; fought = false; } public boolean eat() { west = !west; return true; public Attack fight(String opponent) { fought = true; return Attack.POUNCE; ...

Cougar solution ... public Color getColor() { if (fought) { return Color.RED; } else { return Color.BLUE; } public Direction getMove() { if (west) { return Direction.WEST; return Direction.EAST; public String toString() { return "C";