Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing.

Similar presentations


Presentation on theme: "Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing."— Presentation transcript:

1 Chapter Day 5

2 © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing java programs  10 programs from Chapter 1 & 2  Due in 4 days (September 19) Problem set 2 will be posted by next class Quiz 1 will be Oct 3  Chapter 1-4  25 M/C open book, open notes, 40 Min Today we will  Finish up on Chapter 2 Applets  Start on Chap 3 Classes and Objects

3 © 2007 Pearson Addison-Wesley. All rights reserved2-3 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

4 © 2007 Pearson Addison-Wesley. All rights reserved2-4 Applets A Java application is a stand-alone program with a main method (like the ones we've seen so far) A Java applet is a program that is intended to transported over the Web and executed using a web browser An applet also can be executed using the appletviewer tool of the Java Software Development Kit An applet doesn't have a main method Instead, there are several special methods that serve specific purposes

5 © 2007 Pearson Addison-Wesley. All rights reserved2-5 Applets The paint method, for instance, is executed automatically and is used to draw the applet’s contents The paint method accepts a parameter that is an object of the Graphics class A Graphics object defines a graphics context on which we can draw shapes and text The Graphics class has several methods for drawing shapes

6 © 2007 Pearson Addison-Wesley. All rights reserved2-6 Applets The class that defines an applet extends the Applet class This makes use of inheritance, which is explored in more detail in Chapter 8 See Einstein.java (page 95)Einstein.java An applet is embedded into an HTML file using a tag that references the bytecode file of the applet The bytecode version of the program is transported across the web and executed by a Java interpreter that is part of the browser

7 © 2007 Pearson Addison-Wesley. All rights reserved2-7 The HTML applet Tag The Einstein Applet

8 © 2007 Pearson Addison-Wesley. All rights reserved2-8 Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

9 © 2007 Pearson Addison-Wesley. All rights reserved2-9 Drawing Shapes Let's explore some of the methods of the Graphics class that draw shapes in more detail A shape can be filled or unfilled, depending on which method is invoked The method parameters specify coordinates and sizes Shapes with curves, like an oval, are usually drawn by specifying the shape’s bounding rectangle An arc can be thought of as a section of an oval

10 © 2007 Pearson Addison-Wesley. All rights reserved2-10 Drawing a Line X Y 10 20 150 45 page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or

11 © 2007 Pearson Addison-Wesley. All rights reserved2-11 Drawing a Rectangle X Y page.drawRect (50, 20, 100, 40); 50 20 100 40

12 © 2007 Pearson Addison-Wesley. All rights reserved2-12 Drawing an Oval X Y page.drawOval (175, 20, 50, 80); 175 20 50 80 bounding rectangle

13 © 2007 Pearson Addison-Wesley. All rights reserved2-13 Drawing Shapes Every drawing surface has a background color Every graphics context has a current foreground color Both can be set explicitly See Snowman.java (page100)Snowman.java

14 © 2007 Pearson Addison-Wesley. All rights reserved2-14 In class Lab Drawing a face from page 30 of lab manual Applettemplate.java

15 © 2007 Pearson Addison-Wesley. All rights reserved2-15 Summary Chapter 2 focused on:  character strings  primitive data  the declaration and use of variables  expressions and operator precedence  data conversions  accepting input from the user  Java applets  introduction to graphics

16 Chapter 3 Using Classes and Objects 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights reserved

17 2-17 Using Classes and Objects We can create more interesting programs using predefined classes and related objects Chapter 3 focuses on:  object creation and object references  the String class and its methods  the Java standard class library  the Random and Math classes  formatting output  enumerated types  wrapper classes  graphical components and containers  labels and images

18 © 2007 Pearson Addison-Wesley. All rights reserved2-18 Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

19 © 2007 Pearson Addison-Wesley. All rights reserved2-19 Creating Objects A variable holds either a primitive type or a reference to an object A class name can be used as a type to declare an object reference variable String title; No object is created with this declaration An object reference variable holds the address of an object The object itself must be created separately

20 © 2007 Pearson Addison-Wesley. All rights reserved2-20 Creating Objects Generally, we use the new operator to create an object title = new String ("Java Software Solutions"); This calls the String constructor, which is a special method that sets up the object Creating an object is called instantiation An object is an instance of a particular class

21 © 2007 Pearson Addison-Wesley. All rights reserved2-21 Invoking Methods We've seen that once an object has been instantiated, we can use the dot operator to invoke its methods count = title.length() A method may return a value, which can be used in an assignment or expression A method invocation can be thought of as asking an object to perform a service

22 © 2007 Pearson Addison-Wesley. All rights reserved2-22 References Note that a primitive variable contains the value itself, but an object variable contains the address of the object 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 "Steve Jobs" name1 num1 38

23 © 2007 Pearson Addison-Wesley. All rights reserved2-23 Assignment Revisited The act of assignment takes a copy of a value and stores it in a variable For primitive types: num1 38 num2 96 Before: num2 = num1; num1 38 num2 38 After:

24 © 2007 Pearson Addison-Wesley. All rights reserved2-24 Reference Assignment For object references, assignment copies the address: name2 = name1; name1 name2 Before: "Steve Jobs" "Steve Wozniak" name1 name2 After: "Steve Jobs"

25 © 2007 Pearson Addison-Wesley. All rights reserved2-25 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 through one reference changes it for all of its aliases, because there is really only one object

26 © 2007 Pearson Addison-Wesley. All rights reserved2-26 Garbage Collection When an object no longer has any valid references to it, it can no longer be accessed by the program The object is useless, and therefore is called garbage Java performs automatic garbage collection periodically, returning an object's memory to the system for future use In other languages, the programmer is responsible for performing garbage collection

27 © 2007 Pearson Addison-Wesley. All rights reserved2-27 Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images

28 © 2007 Pearson Addison-Wesley. All rights reserved2-28 The String Class Because strings are so common, we don't have to use the new operator to create a String object title = "Java Software Solutions"; This is special syntax that works only for strings Each string literal (enclosed in double quotes) represents a String object

29 © 2007 Pearson Addison-Wesley. All rights reserved2-29 String Methods Once a String object has been created, neither its value nor its length can be changed Thus we say that an object of the String class is immutable However, several methods of the String class return new String objects that are modified versions of the original See the list of String methods on page 119 and in Appendix M Better Yet use Java.sun.com  http://java.sun.com/javase/reference/api.jsp http://java.sun.com/javase/reference/api.jsp

30 © 2007 Pearson Addison-Wesley. All rights reserved2-30 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 (page 120)StringMutation.java

31 © 2007 Pearson Addison-Wesley. All rights reserved2-31 Outline Creating Objects The String Class Packages Formatting Output Enumerated Types Wrapper Classes Components and Containers Images


Download ppt "Chapter Day 5. © 2007 Pearson Addison-Wesley. All rights reserved2-2 Agenda Day 5 Questions from last Class?? Problem set 1 Posted  Introduction on developing."

Similar presentations


Ads by Google