reading: 8.6 self-check: #18, exercises: #9, 14

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

1 Building Java Programs Chapter 8: Classes These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Copyright 2008 by Pearson Education Building Java Programs Chapter 8: Classes Lecture 8-1: Intro to Classes and Objects reading:
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
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:
ITEC 320 Lecture 16 Packages (1). Review Questions? –HW –Exam Nested records –Benefits –Downsides.
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:
Objects.
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.
CSCI 162 Classes.
CSC 142 Computer Science II Zhen Jiang West Chester University
CS305j Introduction to Computing Classes 1 Topic 23 Classes – Part I "A 'class' is where we teach an 'object' to behave." -Rich Pattis Based on slides.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
ITEC 320 Lecture 9 Nested records / Packages. Review Project ?’s Records Exam 1 next Friday.
Copyright 2006 by Pearson Education 1 Building Java Programs Chapter 8: Classes and Objects.
Attribute - CIS 1068 Program Design and Abstraction Zhen Jiang CIS Dept. Temple University SERC 347, Main Campus 12/24/2016.
Topic 27 classes and objects, state and behavior Copyright Pearson Education, 2010 Based on slides bu Marty Stepp and Stuart Reges from
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
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:
1 reading: 3.3 Using objects. Objects So far, we have seen: methods, which represent behavior variables, which represent data (categorized by types) It.
Adapted from slides by Marty Stepp and Stuart Reges
Object Oriented Programming
Building Java Programs
Lecture 11: More on Classes
CSC240 Computer Science III
Classes and Objects, State and Behavior
Lecture 8-3: Encapsulation, this
Building Java Programs
Building Java Programs
Object initialization: constructors
Building Java Programs
Methods (toString, equals)
Adapted from slides by Marty Stepp and Stuart Reges
Building Java Programs
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Classes and objects Readings: 8.1.
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
CSE 142 Lecture Notes Defining New Types of Objects
Building Java Programs
Building Java Programs
Lecture 11: Intro to Classes
Using Objects (continued)
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
Building Java Programs
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Special instance methods: toString
Building Java Programs
Classes and objects Readings: 8.1.
Building Java Programs
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
CPSC 233 Tutorial 13 March 11/12th, 2015.
Presentation transcript:

reading: 8.6 self-check: #18, 20-21 exercises: #9, 14 The toString method reading: 8.6 self-check: #18, 20-21 exercises: #9, 14

Printing objects By default, Java doesn't know how to print objects: Point p = new Point(10, 7); System.out.println("p: " + p); // p: Point@9e8c34 We can print a better string (but this is cumbersome): System.out.println("p: (" + p.x + ", " + p.y + ")"); We'd like to be able to print the object itself: // desired behavior System.out.println("p: " + p); // p: (10, 7)

The toString method tells Java how to convert an object into a String called when an object is printed/concatenated to a String: Point p1 = new Point(7, 2); System.out.println("p1: " + p1); If you prefer, you can write .toString() explicitly. System.out.println("p1: " + p1.toString()); Every class has a toString, even if it isn't in your code. The default is the class's name and a hex (base-16) number: Point@9e8c34

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

Client code // This client program uses the Point class. public class PointMain { public static void main(String[] args) { // create two Point objects Point p1 = new Point(7, 2); Point p2 = new Point(4, 3); // print each point System.out.println("p1: " + p1); System.out.println("p2: " + p2); // compute/print each point's distance from the origin System.out.println("p1's distance from origin: " + p1.distanceFromOrigin()); System.out.println("p2's distance from origin: " + p1.distanceFromOrigin()); // move p1 and p2 and print them again p1.translate(11, 6); p2.translate(1, 7); // compute/print distance from p1 to p2 System.out.println("distance from p1 to p2: " + p1.distance(p2)); }