Methods (toString, equals)

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

CS 206 Introduction to Computer Science II 09 / 05 / 2008 Instructor: Michael Eckmann.
Chapter 1 Object-Oriented Concepts. A class consists of variables called fields together with functions called methods that act on those fields.
Strings An extension of types A class that encompasses a character array and provides many useful behaviors Chapter 9 Strings are IMMUTABLE.
The Fundamental Rule for Testing Methods Every method should be tested in a program in which every other method in the testing program has already been.
1. 2 Introduction to Methods  Type of Variables  Static variables  Static & Instance Methods  The toString  equals methods  Memory Model  Parameter.
Java Syntax Primitive data types Operators Control statements.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors reading:
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Sadegh Aliakbary Sharif University of Technology Fall 2010.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Arrays of Objects 1 Fall 2012 CS2302: Programming Principles.
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
SE-1020 Dr. Mark L. Hornick 1 Inheritance and Polymorphism.
Some Standard Classes Goals The Object class The String class Wrapper classes The Math class Random Numbers.
CSC 142 Computer Science II Zhen Jiang West Chester University
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
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.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
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,
2 Arrays Array is a data structure that represents a collection of the same type of data. A "fixed length list".
Copyright 2010 by Pearson Education Building Java Programs Chapter 8 Lecture 8-2: Object Behavior (Methods) and Constructors, Encapsulation, this reading:
Objects Real and Java COMP T1 3
Object-Oriented Concepts
Object-oriented Programming in Java
CSC 205 Java Programming II
Lecture 17: Polymorphism (Part II)
Continuing Chapter 11 Inheritance and Polymorphism
CS 302 Week 11 Jim Williams, PhD.
Recitation 6 Inheritance.
CSC240 Computer Science III
Lecture 8-3: Encapsulation, this
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
Building Java Programs
CSC 113: Computer programming II
Building Java Programs
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Building Java Programs
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Defining Classes and Methods
Building Java Programs
Feedback from Assignment 2
Building Java Programs
Building Java Programs
Lecture 18: Polymorphism (Part II)
Kinds of methods accessor: A method that lets clients examine object state. Examples: distance, distanceFromOrigin often has a non-void return type mutator: A.
If-statements & Indefinite Loops
CSE 142 Lecture Notes Defining New Types of Objects, cont'd.
Building Java Programs
Special instance methods: toString
Chapter 11 Inheritance and Polymorphism Part 2
Building Java Programs
Building Java Programs
Building Java Programs
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
CSE 143 Lecture 24 Inheritance and the Object class; Polymorphism
Lecture 8-2: Object Behavior (Methods) and Constructors
Building Java Programs
CSE 143 Lecture 23 Inheritance and the Object class; Polymorphism
Subtype Substitution Principle
reading: 8.6 self-check: #18, exercises: #9, 14
Presentation transcript:

Methods (toString, equals) CS163 Fall 2018

The toString() method tells Java how to convert an object into a String called when an object is printed or concatenated to a String: Point p = new Point(7, 2); System.out.println(”p: " + p); Same as: System.out.println("p: " + p.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) hash-code: Point@9e8c34

toString() implementation public String toString() { code that returns a suitable String; } Example: toString() method for our Student class: public String toString(){ return ”name: " + name+ "\n" + ”id: " + id + "\n" + ”average: " + average; // SHOW Eclipse example of Student class

toString in ArrayLists and other collections call toString automatically ArrayList<Student> students = new ArrayList<>(); … System.out.println(students); println(students) calls students.toString(), which automatically calls s.toString() for every point s // SHOW Eclipse example of Student class

Primitive Equality Suppose we have two integers i and j How does the statement i==j behave? i==j if i and j contain the same value

Object Equality Suppose we have two pet instances pet1 and pet2 How does the statement pet1==pet2 behave?

Object Equality Suppose we have two pet instances pet1 and pet2 How does the statement pet1==pet2 behave? pet1==pet2 is true if both refer to the same object The == operator checks if the addresses of the two objects are equal May not be what we want!

Object Equality - extended If you want a different notion of equality define your own .equals() method. Use pet1.equals(pet2) instead of pet1==pet2 The default definition of .equals() is the value of == but for Strings the contents are compared

.equals for the Pet class public boolean equals (Object other) { if (!other instanceof Pet) { return false; } Pet otherPet = (Pet) other; return ((this.age == otherPet.age) &&(Math.abs(this.weight – otherPet.weight) < 1e-8) &&(this.name.equals(otherPet.name))); // SHOW ECLIPSE EXAMPLE OF Equals code. Show the equals method of the Student class.