chapter day 5. © 2007 pearson addison-wesley. all rights reserved2-2 agenda day 5 questions from...

31
Chapter Day 5

Post on 19-Dec-2015

213 views

Category:

Documents


0 download

TRANSCRIPT

ChapterDay 5

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-3

Outline

Character Strings

Variables and Assignment

Primitive Data Types

Expressions

Data Conversion

Interactive Programs

Graphics

Applets

Drawing Shapes

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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)

• 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

© 2007 Pearson Addison-Wesley. All rights reserved 2-7

The HTML applet Tag

<html>

<head> <title>The Einstein Applet</title> </head>

<body> <applet code="Einstein.class" width=350 height=175> </applet> </body>

</html>

© 2007 Pearson Addison-Wesley. All rights reserved 2-8

Outline

Character Strings

Variables and Assignment

Primitive Data Types

Expressions

Data Conversion

Interactive Programs

Graphics

Applets

Drawing Shapes

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-10

Drawing a Line

X

Y

10

20

150

45

page.drawLine (10, 20, 150, 45);

page.drawLine (150, 45, 10, 20);

or

© 2007 Pearson Addison-Wesley. All rights reserved 2-11

Drawing a Rectangle

X

Y

page.drawRect (50, 20, 100, 40);

50

20

100

40

© 2007 Pearson Addison-Wesley. All rights reserved 2-12

Drawing an Oval

X

Y

page.drawOval (175, 20, 50, 80);

175

20

50

80

boundingrectangle

© 2007 Pearson Addison-Wesley. All rights reserved 2-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)

© 2007 Pearson Addison-Wesley. All rights reserved 2-14

In class Lab

• Drawing a face from page 30 of lab manual

• Applettemplate.java

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

Chapter3Using Classes and Objects5TH EDITION

Lewis & Loftus

java Software Solutions Foundations of Program Design

© 2007 Pearson Addison-Wesley. All rights reserved

© 2007 Pearson Addison-Wesley. All rights reserved 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

© 2007 Pearson Addison-Wesley. All rights reserved 2-18

Outline

Creating Objects

The String Class

Packages

Formatting Output

Enumerated Types

Wrapper Classes

Components and Containers

Images

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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 isa special method that sets up the object

• Creating an object is called instantiation

• An object is an instance of a particular class

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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 96Before:

num2 = num1;

num1 38

num2 38After:

© 2007 Pearson Addison-Wesley. All rights reserved 2-24

Reference Assignment

• For object references, assignment copies the address:

name2 = name1;

name1

name2Before:

"Steve Jobs"

"Steve Wozniak"

name1

name2After:

"Steve Jobs"

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-27

Outline

Creating Objects

The String Class

Packages

Formatting Output

Enumerated Types

Wrapper Classes

Components and Containers

Images

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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

© 2007 Pearson Addison-Wesley. All rights reserved 2-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)

© 2007 Pearson Addison-Wesley. All rights reserved 2-31

Outline

Creating Objects

The String Class

Packages

Formatting Output

Enumerated Types

Wrapper Classes

Components and Containers

Images