Download presentation
Presentation is loading. Please wait.
Published byMarion Lester Modified over 9 years ago
1
Checking Equality of Reference Variables
2
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
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
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
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
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.
7
3.2 Creating Data Types
8
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
9
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
10
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
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
12 Summary of Classes (“Charge” Class Discussed in the Textbook)
13
Complex Numbers
14
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 = -2 + 3i a + b = 1 + 7i a b = -18 + i |a| = 5 Code in Dr. Java
15
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 = 3.0 + 4.0i b = -2.0 + 3.0i c = -18.0 + 1.0i result of c.toString()
16
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
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,....
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.