public class Complex
Download
Report
Transcript public class Complex
Checking Equality of Reference Variables
Reference Variables and Equality
Arrays and objects are both “reference”
types
They are allocated a chunk of memory in the
address space
The memory needs to be initialized
Assigning one object/array to another object/array
results in an alias
How do we check the equality of objects?
We use the == operator for built-in types
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
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
3
Some things can’t be changed: Color objects, for example
Immutability. Can't change a Color object's value once created.
no relevant methods in Color API
Consequence. Don't need to worry about aliases.
Color a = new Color(160, 82,
Color b = a;
a = new Color(0, 0, 0);
45);
makes a point to a different Color, but does not change b
4
Java objects: Some are Immutable, Some are Not
Immutability. Can't change a Color object's value once created.
no relevant methods in Color API
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.
Color red = new Color(255, 0,
pic.set(0, 3, red);
0);
D0
D4
D8
DC
5
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.
6
3.2 Creating Data Types
Recap of the “Height” Class
Height
Data:
– Feet (as an integer value)
– Inches (as a double value)
Methods:
– Set height
– Display height
API:
Height(int feet, double inches) //constructor
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)
Why is encapsulation useful?
Data Hiding: Clients should not be
able to manipulate data in objects
directly
Why is data hiding useful?
Declare instance variables to be “private”
– Use
getter methods to read data within objects
Any object should be able to invoke the
instance methods
– Declare
instance methods to be “public”
Abstract Data Types
Abstract data type. Data type whose
internal representation is hidden.
Separate implementation from design
specification.
Class provides data representation
and code for operations.
Client uses data type as black box.
API specifies contract between client
and class.
11
Summary of Classes
(“Charge” Class Discussed in the Textbook)
12
Complex Numbers
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
14
Complex Number Data Type: A Simple Client
Client program. Uses data type operations to calculate something.
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);
}
%
a
b
result of c.toString()
c
java TestClient
= 3.0 + 4.0i
= -2.0 + 3.0i
= -18.0 + 1.0i
Remark. Can't write a = b*c since no operator overloading in Java.
15
Complex Number Data Type: Implementation
public class Complex {
private double re;
private double im;
instance variables
public Complex(double real, double imag) {
re = real;
im = imag;
}
public String toString() { return reconstructor
+ " + " + 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);
}
creates a Complex object,
and returns a reference to it
public Complex times(Complex b) {
double real = re * b.re – im * b.im; refers to b's instance variable
double imag = re * b.im + im * b.re;
return new Complex(real, imag);
}
}
methods
16
Applications of Data Types
Data type. Set of values and collection of operations on those values.
Simulating the physical world.
Java objects model real-world objects.
Not always easy to make model reflect reality.
Ex: charged particle, molecule, CS101 student, ….
Extending the Java language.
Java doesn't have a data type for every possible application.
Data types enable us to add our own abstractions.
Ex: complex, vector, polynomial, matrix, ....
17