Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 5-1 Introduction Types and Variables Statements and Control Flow Reading Input Classes and Objects Arrays Methods Scope and Lifetime Utility classes Introduction to Object-Oriented Analysis and Design Copyright (c) 1999-2004 N. Afshartous Chapter 5: Classes and Objects After this chapter you will be able to: - 5-2 Declare object variables Create objects Incorporate String objects Use different String operations Copyright (c) 1999-2004 N. Afshartous Object-Oriented Terms • Classes specify what operations an object supports • A Car class might define start and stop • An object is an instance of some class corvette class Car taurus Objects Objects jeep 5-3 Copyright (c) 1999-2004 N. Afshartous Another Example • Class Person specifies the behavior of a person fred class Person sally Objects Objects rachel • Fred, Sally and Rachel are objects of type Person 5-4 Copyright (c) 1999-2004 N. Afshartous Memory • Memory is sequence of cells which can be identified using addresses or names (program variables) Addresses 0 1 2 Names 4 x int x = 4; 3 5-5 Copyright (c) 1999-2004 N. Afshartous Declaring Object Variables • Once a class is defined, the class name can be used as a type in a variable declaration • A predefined String class exists in Java String x; • x is a variable that can store the memory address of a String object • We will only be instantiating existing classes in this course 5-6 Copyright (c) 1999-2004 N. Afshartous Creating Objects • The new operator creates objects • Syntax: new ClassConstructor(args) new String(“hello"); • The class constructor is a special operation provided by a class • A class constructor always has the same name as its class 5-7 Copyright (c) 1999-2004 N. Afshartous Initializing String Variables • An object can be assigned to an object variable String x = new String(“hello"); • x stores the address of a String object x 5-8 “hello" Copyright (c) 1999-2004 N. Afshartous String Init Shorthand • Either the long or short form may be used 5-9 String x = new String(“hello"); x “hello" String y = “hello"; y “hello" Copyright (c) 1999-2004 N. Afshartous String Assignment • A new string may be subsequently assigned to a string variable String x = “hello"; x “hello" x = “there"; The object storing “hello" is no longer accessible and will be garbage collected x 5-10 “hello" “there" Copyright (c) 1999-2004 N. Afshartous String Assignment • Assignment between object variables copies addresses x “hello" String x = “hello", y; y y = x; • x and y store the same address 5-11 x “hello" y Copyright (c) 1999-2004 N. Afshartous Assignment with Primitive Types • Assignment involving variables having primitive types copies values int x = 44, y=33; x 44 y 33 x 33 y 33 x = y; Object variables store addresses, variables of primitive type store values 5-12 Copyright (c) 1999-2004 N. Afshartous String Concatenation • The plus operator (+) concatenates strings String x = “hello"; String y = “ there", z; int a = 4; x “hello" y “ there" z “hello there" z = x + y; System.out.println(“z = “ + z); System.out.println(“a = “ + a); Output Output 5-13 z = hello there a=4 Copyright (c) 1999-2004 N. Afshartous Methods • A method is a procedure defined in a class • The String class defines the charAt method • Signature: char charAt (int) return type method name argument type • Lookup charAt under class String in API doc 5-14 Copyright (c) 1999-2004 N. Afshartous Using charAt • charAt is used to extract individual characters from a String • Indexing starts from 0 String x = “hello"; System.out.println(x.charAt(0)); System.out.println(x.charAt(1)); Output Output 5-15 h e Copyright (c) 1999-2004 N. Afshartous String Lengths • Can find out the length of any String using Var.length() String x = “hello"; for (int i = 0; i < x.length(); i++) System.out.println(x.charAt(i)); Output Output 5-16 h e l l o Copyright (c) 1999-2004 N. Afshartous Printing A String In Reverse String x = “Java Mania!"; for (int i = x.length() - 1; i >= 0; i--) System.out.print(x.charAt(i)); System.out.println(); Output Output 5-17 !ainaM avaJ Copyright (c) 1999-2004 N. Afshartous Printing A String In Reverse (Another Way) String x = “Java Mania!"; String y = ““; for (int i = x.length() - 1; i >= 0; i--) y += x.charAt(i); System.out.println(y); Output Output 5-18 !ainaM avaJ Copyright (c) 1999-2004 N. Afshartous Comparing Strings • The equal operator (==) when comparing objects just compares addresses String x = “Java"; String y = x; String z = “Java"; x if (x == y) System.out.println(“x == y"); y if (x != z) System.out.println(“x != z"); Output Output 5-19 z “Java" “Java" x == y x != z Copyright (c) 1999-2004 N. Afshartous Equals Method • The String class has an equals method that compares the String values (not addresses) String x = “Java"; String y = x; String z = “Java"; if (x.equals(z)) System.out.println(“x equals z); if (x != z) System.out.println(“x != z"); Output Output 5-20 x equals z x != z Copyright (c) 1999-2004 N. Afshartous Upper and Lower Case Conversion String x = “Java"; String y = “ Rules"; String a = x.toUpperCase(); String b = y.toLowerCase(); System.out.println(x + y); System.out.println(a + b); • Output ? 5-21 Copyright (c) 1999-2004 N. Afshartous String Methods • public int compareTo(String anotherString) • public String concat(String str) • public String substring(int beginIndex) • public String toLowerCase() • public String toUpperCase() • public boolean startsWith(String 5-22 prefix) Copyright (c) 1999-2004 N. Afshartous It’s Exercise Time 5-23 Copyright (c) 1999-2004 N. Afshartous