Objects Getting classy Your current job Gain experience
Objects
Getting classy • Your current job – Gain experience creating and manipulating objects from the standard Java types • Why – Prepares you for defining your own classes and creating and manipulating the objects of those classes
Values versus objects • Numbers – Have values but they do not have behaviors • Objects – Have attributes and behaviors • System. in • References an Input. Stream – Attribute: keyboard – Behaviors: reading • System. out – References an Output. Stream • Attribute: monitor • Behaviors: printing
Some other Java object types • Scanner • String • Rectangle • Color • JFrame
Consider • Statements int peas. Per. Pod = 8; String message = "Don't look behind the door!“ • How show we represent these definitions according to the notions of Java?
Representation
Shorthand representation
Examples • Consider String a = "excellence“; String b = a; • What is the representation?
Examples • Consider String a = "excellence“; String b = a; • What is the representation?
Uninitialized versus null • Consider String day. Of. Week; Scanner in. Stream; • What is the representation?
Uninitialized versus null • Consider String day. Of. Week; Scanner in. Stream; • What is the representation?
Uninitialized versus null • Consider String font. Name = null; Scanner file. Stream = null; • What is the representation?
Uninitialized versus null • Consider String font. Name = null; Scanner file. Stream = null; • What is the representation?
Assignment • Consider String word 1 = "luminous"; String word 2 = "graceful"; Word 1 = word 2; • Initial representation
Assignment • Consider String word 1 = "luminous"; String word 2 = "graceful"; Word 1 = word 2; • After assignment
Using objects • Consider Scanner stdin = Scanner. create(System. in); System. out. print("Enter your account name: "); String response = stdin. next. Line();
Using objects • Consider Scanner stdin = Scanner. create(System. in); System. out. print("Enter your account name: "); String response = stdin. next. Line();
Using objects • Consider Scanner stdin = Scanner. create(System. in); System. out. print("Enter your account name: "); String response = stdin. next. Line(); • Suppose the user interaction is Enter your account name: artiste
String representation • Consider – String alphabet = "abcdefghijklmnopqrstuvwxyz"; • Standard shorthand representation • Truer representation
String representation • Consider – String alphabet = "abcdefghijklmnopqrstuvwxyz"; – char c 1 = alphabet. char. At(9); – char c 2 = alphabet. char. At(15); – char c 3 = alphabet. char. At(2); • What are the values of c 1, c 2, and c 3? Why?
Program Word. Length. java public class Word. Length { public static void main(String[] args) { Scanner stdin = Scanner. create(System. in); System. out. print("Enter a word: "); String word = stdin. next. Line(); int word. Length = word. length(); System. out. println("Word " + word + " has length " + word. Length + ". "); } }
More String methods • Consider String wedding. Date = "August 21, 1976"; String month = wedding. Date. substring(0, 6); System. out. println("Month is " + month + ". "); • What is the output?
More String methods • Consider String wedding. Date = "August 21, 1976"; String month = wedding. Date. substring(0, 6); System. out. println("Month is " + month + ". "); • What is the output? Month is August.
More String methods • Consider String int n 1 int n 2 int n 3 fruit = "banana"; search. String = "an"; = fruit. index. Of(search. String, 0); = fruit. index. Of(search. String, n 1 + 1); = fruit. index. Of(search. String, n 2 + 1); System. out. println("First search: " + n 1); System. out. println("Second search: " + n 2); System. out. println("Third search: " + n 3); • What is the output?
More String methods • Consider String int n 1 int n 2 int n 3 fruit = "banana"; search. String = "an"; = fruit. index. Of(search. String, 0); = fruit. index. Of(search. String, n 1 + 1); = fruit. index. Of(search. String, n 2 + 1); System. out. println("First search: " + n 1); System. out. println("Second search: " + n 2); System. out. println("Third search: " + n 3); • What is the output? First search: 1 Second search: 3 Third search: -1
More String methods • Consider int v 1 = -12; double v 2 = 3. 14; char v 3 = 'a'; String s 1 = String. value. Of(v 1); String s 2 = String. value. Of(v 2); String s 3 = String. value. Of(v 3);
Final variables • Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; • What is the representation?
Final variables
Rectangle
Rectangle
Rectangle • Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK. set. Location(1, 4); BLOCK. resize(8, 3);
Rectangle • Consider final Rectangle BLOCK = new Rectangle(6, 9, 4, 2); BLOCK. set. Location(1, 4); BLOCK. resize(8, 3);
Final variables • Consider final String LANGUAGE = "Java";
- Slides: 33