Checking Equality of Reference Variables. Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Basic -2 Classes and Objects. Classes and Objects A class is a complex data TYPE An object is an instance of a class. Example: Class: Person Objects:
DATA STRUCTURES Lecture: Interfaces Slides adapted from Prof. Steven Roehrig.
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Inheritance and Polymorphism.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Immutable Objects and Classes.
Road Map Introduction to object oriented programming. Classes
Enhancing classes Visibility modifiers and encapsulation revisited
Terms and Rules Professor Evan Korth New York University (All rights reserved)
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Designing Data Types. 2 Object Oriented Programming Procedural programming. [verb-oriented] n Tell the computer to do this. n Tell the computer to do.
3.3 Designing Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · September.
MIT AITI 2002 Abstract Classes, Interfaces. Abstract Classes What is an abstract class? An abstract class is a class in which one or more methods is declared,
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Arrays (Part 1) Computer Science Erwin High School Fall 2014.
1 Object-Oriented Software Engineering CS Interfaces Interfaces are contracts Contracts between software groups Defines how software interacts with.
Object Oriented Programming: Java Edition By: Samuel Robinson.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Java Quiz Bowl A fun review of the Java you should know from CMPT 201 If you don’t know the answers - this week is for you to study up!
ArrayList, Multidimensional Arrays
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Copyright © 2002, Systems and Computer Engineering, Carleton University a-JavaReview.ppt * Object-Oriented Software Development Unit.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
1 Object Oriented Programming Lecture II Classes, Objects, Abstract Data Types, Representation Invariants, Mutables and Immutables.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 4 – August 30, 2001.
P Chapter 2 introduces Object Oriented Programming. p OOP is a relatively new approach to programming which supports the creation of new data types and.
10-Nov-15 Java Object Oriented Programming What is it?
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Overview The Basics – Python classes and objects Procedural vs OO Programming Entity modelling Operations / methods Program flow OOP Concepts and user-defined.
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Method Overloading  Methods of the same name can be declared in the same class for different sets of parameters  As the number, types and order of the.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Lab 4 - Variables. Information Hiding General Principle: – Restrict the access to variables and methods as much as possible Can label instance variables.
Classes and Objects CS177 Rec 10. Announcements Project 4 is posted ◦ Milestone due on Nov. 12. ◦ Final submission due on Nov. 19. Exam 2 on Nov. 4 ◦
ADT data abstraction. Abstraction  representation of concepts by their relevant features only  programming has two major categories of abstraction process.
3.2 Creating Data Types Introduction to Programming in Java: An Interdisciplinary Approach · Robert Sedgewick and Kevin Wayne · Copyright © 2008 · January.
Non-static classes 1.  a utility class has features (fields and methods) that are all static  all features belong to the class  therefore, you do not.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
CSE 1201 Object Oriented Programming ArrayList 1.
Arrays-. An array is a way to hold more than one value at a time. It's like a list of items.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Java & C++ Comparisons How important are classes and objects?? What mechanisms exist for input and output?? Are references and pointers the same thing??
CS 116 Lecture 1 John Korah Contains content provided by George Koutsogiannakis & Matt Bauer.
Defining Classes I Part B. Information hiding & encapsulation separate how to use the class from the implementation details separate how to use the class.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
College Board Topics – A.P. Computer Science A Program Design - Read and understand a problem's description, purpose, and goals; Apply data abstraction.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
OOP Basics Classes & Methods (c) IDMS/SQL News
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 10 Thinking.
CS 112 Introduction to Programming Design Good Classes: Visualization and OOP Analysis Yang (Richard) Yang Computer Science Department Yale University.
1 Classes and Data Abstraction Chapter What a Class ! ! Specification and implementation Private and public elements Declaring classes data and.
Guest Speaker - Cryptography Wednesday (Apr. 22) – Prof. abhi shelat.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
1 More About Derived Classes and Inheritance Chapter 9.
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.
Chapter 10 Thinking in Objects
Classes and Objects: Encapsulation
Chapter 10 Thinking in Objects
Chapter 10 Thinking in Objects
Chapter 3: Using Methods, Classes, and Objects
CS 302 Week 11 Jim Williams, PhD.
Computer Science II Exam 1 Review.
Chapter 9 Thinking in Objects
Chapter 9 Thinking in Objects
Introduction to Data Structure
Presentation transcript:

Checking Equality of Reference Variables

Arrays and objects are both “reference” types n They are allocated a chunk of memory in the address space n The memory needs to be initialized n Assigning one object/array to another object/array results in an alias How do we check the equality of objects? n We use the == operator for built-in types n Cannot use == for objects Use instance method equals(). Can define this method for any class. Its your responsibility to include this method if you are designing a new class. 2 Reference Variables and Equality

3 Testing for Reference Variable Equality String s1 = new String("hello”); String s2 = new String("hello”); System.out.println( s1.equals(s2) ); true System.out.println( s1 == s2 ); false System.out.println( s1 == s1 ); true String s3 = s1; System.out.println( s1 == s3 ); true

4 Some things can’t be changed: Color objects, for example Immutability. Can't change a Color object's value once created. Consequence. Don't need to worry about aliases. Color a = new Color(160, 82, 45); Color b = a; a = new Color(0, 0, 0); makes a point to a different Color, but does not change b no relevant methods in Color API

5 Java objects: Some are Immutable, Some are Not Immutability. Can't change a Color object's value once created. We can create a new color from an old one. We can change the Color object the reference points to. The String class is immutable too. Mutability. Can change a Picture object's value. D0 D4 D8 DC Color red = new Color(255, 0, 0); pic.set(0, 3, red); no relevant methods in Color API

6 Summary Object. Holds a data type value; variable name refers to object. In Java, programs manipulate references to objects. Exception: primitive types, e.g., boolean, int, double. Reference types: String, Picture, Color, arrays, everything else.

3.2 Creating Data Types

Recap of the “Height” Class Height n Data: – Feet (as an integer value) – Inches (as a double value) n Methods: – Set height – Display height API: n Height(int feet, double inches) //constructor n void displayHeight() //instance method 8

Recap of Height.java (Dr. Java) public class Height { // instance variables int feet; double inches; // constructor Height(int setFeet, double setInches) { feet = setFeet; inches = setInches; } // instance method void displayHeight() { System.out.println("Height: "+feet+" feet and "+inches+ " inches"); } } 9

Principles of Object Oriented Programming Encapsulation: Combine data and the functions that operate on that data into a single unit (object) n Why is encapsulation useful? Data Hiding: Clients should not be able to manipulate data in objects directly n Why is data hiding useful? n Declare instance variables to be “private” – Use getter methods to read data within objects n Any object should be able to invoke the instance methods – Declare instance methods to be “public”

11 Abstract Data Types Abstract data type. Data type whose internal representation is hidden. Separate implementation from design specification. n Class provides data representation and code for operations. n Client uses data type as black box. n API specifies contract between client and class.

12 Summary of Classes (“Charge” Class Discussed in the Textbook)

Complex Numbers

14 Complex Number Data Type Goal. Create a data type to manipulate complex numbers. Set of values. Two real numbers: real and imaginary parts. API. a = 3 + 4i, b = i a + b = 1 + 7i a  b = i |a| = 5 Code in Dr. Java

15 Complex Number Data Type: A Simple Client Client program. Uses data type operations to calculate something. Remark. Can't write a = b*c since no operator overloading in Java. public static void main(String[] args) { Complex a = new Complex( 3.0, 4.0); Complex b = new Complex(-2.0, 3.0); Complex c = a.times(b); StdOut.println("a = " + a); StdOut.println("b = " + b); StdOut.println("c = " + c); } % java TestClient a = i b = i c = i result of c.toString()

16 Complex Number Data Type: Implementation public class Complex { private double re; private double im; public Complex(double real, double imag) { re = real; im = imag; } public String toString() { return re + " + " + im + "i"; } public double abs() { return Math.sqrt(re*re + im*im); } public Complex plus(Complex b) { double real = re + b.re; double imag = im + b.im; return new Complex(real, imag); } public Complex times(Complex b) { double real = re * b.re – im * b.im; double imag = re * b.im + im * b.re; return new Complex(real, imag); } constructor instance variables methods creates a Complex object, and returns a reference to it refers to b's instance variable

17 Applications of Data Types Data type. Set of values and collection of operations on those values. Simulating the physical world. n Java objects model real-world objects. n Not always easy to make model reflect reality. n Ex: charged particle, molecule, CS101 student, …. Extending the Java language. n Java doesn't have a data type for every possible application. n Data types enable us to add our own abstractions. n Ex: complex, vector, polynomial, matrix,....