1 / 29

Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

Chapter 3: Using Objects Objectives. Object-Oriented Program Intro to Objects and Classes Roles of Constructor & Methods String Class String Operations and Methods Prepares you for defining your own classes and creating and manipulating the objects of those classes.

Download Presentation

Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3: Using Objects Objectives • Object-Oriented Program • Intro to Objects and Classes • Roles of Constructor & Methods • String Class • String Operations and Methods • Prepares you for defining your own classes and creating and manipulating the objects of those classes Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  2. Values versus Objects, Objects and Variables • Numbers • Have values but they do have behaviors • Objects • Have attributes and behaviors • System.in • References an InputStream • Attribute: keyboard • Behaviors: reading • System.out • References an OutputStream • Attribute: monitor • Behaviors: printing

  3. Other Java object types • String • Rectangle • Color • JFrame Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  4. Representation

  5. Shorthand representation Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  6. Examples • Consider String a = "excellence“; String b = a; • What is the representation? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  7. Examples • Consider String a = "excellence“; String b = a; • What is the representation? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  8. Examples The first two parameters of the int x = 3; int y = 4; position of the upper-left-hand int width = 5; corner of the new Rectangle int height = 2; new Rectangle r = Rectangle(x, y, width, height); The third and fourth parameters of the Rectangle constructor the new Rectangle Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  9. Examples

  10. Uninitialized versus null • Consider String dayOfWeek; BufferedReader inStream; Rectangle frame; • What is the representation? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  11. Uninitialized versus null • Consider String dayOfWeek; BufferedReader inStream; Rectangle frame; • What is the representation? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  12. Uninitialized versus null • Consider String fontName = null; BufferedReader fileStream = null; Rectangle boundingBox = null; • What is the representation? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  13. Assignment • Consider String word1 = "luminous"; String word2 = "graceful"; Word1 = word2; • Initial representation Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  14. Assignment • Consider String word1 = "luminous"; String word2 = "graceful"; Word1 = word2; • After assignment Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  15. Final variables • Consider final String POEM_TITLE = “Appearance of Brown"; final String WARNING = “Weather ball is black"; • What is the representation? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  16. Final variables Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  17. Final variables • Consider final Rectangle BLOCK = new Rectangle(6,9,4,2); BLOCK.setLocation(1,4); BLOCK.resize(8,3); • Initial representation Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  18. Final variables • Consider final Rectangle BLOCK = new Rectangle(6,9,4,2); BLOCK.setLocation(1,4); BLOCK.resize(8,3); • Initial representation Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  19. Final variables • Consider String LANGUAGE = "Java"; Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  20. Using objects • Consider BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine(); Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  21. Using objects • Consider BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine(); Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  22. Using objects • Consider BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter your account name: "); String response = stdin.readLine(); • Suppose the user interaction is Enter your account name: artiste Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  23. String representation • Consider • String alphabet = "abcdefghijklmnopqrstuvwxyz"; • Standard shorthand representation • Truer representation

  24. String representation • Consider • String alphabet = "abcdefghijklmnopqrstuvwxyz"; • char c1 = alphabet.charAt(9); • char c2 = alphabet.charAt(15); • char c3 = alphabet.charAt(2); • What are the values of c1, c2, and c3? Why? Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  25. Program WordLength.java public class WordLength { public static void main(String[] args) throws IOException { BufferedReader stdin = new BufferedReader( new InputStreamReader(System.in)); System.out.print("Enter a word: "); String word = stdin.readLine(); int wordLength = word.length(); System.out.println("Word" + word + "haslength“ + wordLength + "."); } } Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  26. More String methods • Consider String weddingDate = "August 21, 1976"; String month = weddingDate.substring(0, 6); System.out.println("Month is " + month + "."); • What is the output? Month is August. Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  27. More String methods • Consider String fruit = "banana"; String searchString = "an"; int n1 = fruit.indexOf(searchString, 0); int n2 = fruit.indexOf(searchString, n1 + 1); int n3 = fruit.indexOf(searchString, n2 + 1); System.out.println("First search: " + n1); System.out.println("Second search: " + n2); System.out.println("Third search: " + n3); • What is the output? First search: 1 Second search: 3 Third search: -1 Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  28. More String methods • Consider int v1 = -12; double v2 = 3.14; char v3 = 'a'; String s1 = String.valueof(v1); String s2 = String.valueof(v2); String s3 = String.valueof(v3); Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

  29. Assignment 3 – Reading: Chapter 3 3-12. 3-13. 3-15. 3-17. 3-25. Week 2-3 05/23/2005 Course ISM4234-004 Dr. Simon Qiu

More Related