04 - Chapter 3 - Using Classes and Objects
04 - Chapter 3 - Using Classes and Objects
04 - Chapter 3 - Using Classes and Objects
John Lewis
William Loftus
numChars = title.length();
num1 -
name1 -
References
Note that a primitive variable contains the value
itself, but an object variable contains the address
of the object (object reference)
An object reference can be thought of as a pointer
to the location of the object
Rather than dealing with arbitrary addresses, we
often depict a reference graphically
num1 = 38;
name1 = new String(Steve Jobs);
num1 38
num1 38
Before:
num2 96
num2 = num1;
num1 38
After:
num2 38
name2 = name1;
Evaluating a variable that stores an object returns the address of the object.
Copyright 2012 Pearson Education, Inc.
Aliases
Two or more references that refer to the same
object are called aliases of each other
That creates an interesting situation: one object
can be accessed using multiple reference variables
Aliases can be useful, but should be managed
carefully
Changing an object (state) through one reference
changes it for all of its aliases, because there is
really only one object
> bill
String Indexes
It is occasionally helpful to refer to a particular
character within a string
This can be done by specifying the character's
numeric index
The indexes begin at zero in each string
In the string "Hello", the character 'H' is at
index 0 and the 'o' is at index 4
See StringMutation.java
continued
26
the final frontier.
SPACE, THE FINAL FRONTIER.
26
TRUE or FALSE?
TRUE
Its classes are not part of the Java language per se,
but we rely on them heavily
Or you can import the class, and then use just the
class name
import java.util.Scanner;
import java.util.Random;
num1 = generator.nextInt();
System.out.println ("A random integer: " + num1);
num1 = generator.nextInt(10);
System.out.println ("From 0 to 9: " + num1);
continued
num1 = generator.nextInt(10) + 1;
System.out.println ("From 1 to 10: " + num1);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
num2 = generator.nextFloat();
System.out.println ("A random float (between 0-1): " + num2);
gen.nextInt(25)
gen.nextInt(6) + 1
gen.nextInt(100) + 10
gen.nextInt(50) + 100
gen.nextInt(10) 5
gen.nextInt(22) + 12
Range
0 to 12
1 to 20
15 to 20
-10 to 0
Range
0 to 12 gen.nextInt(13)
1 to 20 gen.nextInt(20) + 1
15 to 20 gen.nextInt(6) + 15
-10 to 0 gen.nextInt(11) 10
import java.util.Scanner;
continued
A) 9
B) 8
C) 6
D) 4
E) 27
Quiz
What will be displayed by this command:
System.out.println(Math.pow(3, 3-1));
125.0
1.0
2.0
6
The fourth statement raises an exception
100 = 100/64 * 26 = 1.562510 * 26 = 1.10012 * 26
Outline
Creating Objects
The String Class
The Random and Math Classes
Formatting Output
Enumerated Types
Wrapper Classes
Components and Containers
Images
getPercentInstance()
import java.util.Scanner;
import java.text.NumberFormat;
int quantity;
double subtotal, tax, totalCost, unitPrice;
continued
See CircleStats.java
import java.util.Scanner;
import java.text.DecimalFormat;
continued
//-----------------------------------------------------------------
// Creates and uses variables of the Flavor type.
//-----------------------------------------------------------------
public static void main (String[] args)
{
Flavor cone1, cone2, cone3;
cone1 = Flavor.rockyRoad;
cone2 = Flavor.chocolate;
continued
System.out.println ();
System.out.println ("cone2 value: " + cone2);
System.out.println ("cone2 ordinal: " + cone2.ordinal());
System.out.println ("cone2 name: " + cone2.name());
cone3 = cone1;
System.out.println ();
System.out.println ("cone3 value: " + cone3);
System.out.println ("cone3 ordinal: " + cone3.ordinal());
System.out.println ("cone3 name: " + cone3.name());
}
}
Prints: true
Instanceof
import java.util.Random;
public class InstanceOfExample {
import java.awt.*;
import javax.swing.*;
frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
continued
primary.add (label1);
primary.add (label2);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
primary.add (label1);
primary.add (label2);
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
continued
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
frame.getContentPane().add(primary);
frame.pack();
frame.setVisible(true);
}
}
import java.awt.*;
import javax.swing.*;
continued
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
String m = maria;
String n = nicola;
m = n;
System.out.println(m);
System.out.println(n+n == m+m);
String m = maria;
String n = nicola;
m = n;
System.out.println(m);
System.out.println(n+n == m+m);
nicola
false
Copyright 2012 Pearson Education, Inc.
Quiz
Is there any semantic difference in these two code
snippets?
int num1 = 3;
String name1 = "Tim Cook";
int num1;
String name1;
num1 = 3;
name1 = new String("Tim Cook");
Quiz
Write a statement that prints the number of
characters in a String object called overview.
What output is produced by the following code
fragment?
String m1, m2, m3;
m1 = "Quest for the Holy Grail";
m2 = m1.toLowerCase();
m3 = m1 + " " + m2;
System.out.println(m3.replace('h', 'z'));
Quest for tze Holy Grail quest for tze zoly grail
Copyright 2012 Pearson Education, Inc.
Quiz
What is the effect of the following import
statement?
import java.awt.*;
Write an assignment statement that computes the
square root of the sum of num1 and num2 and
assigns the result to num3. Hint: use the sqrt static
method of the class Math.