Chapter 2: Objects and Primitive Numeric Primitive Data ! The difference between the numeric primitive

  • View
    17

  • Download
    11

Embed Size (px)

Text of Chapter 2: Objects and Primitive Numeric Primitive Data ! The difference between the numeric...

  • © 2011 Pearson Education, publishing as Addison-Wesley

    Chapter 2: Objects and Primitive Data

    Presentation slides for

    Java Software Solutions for AP* Computer Science

    3rd Edition

    by John Lewis, William Loftus, and Cara Cocking

    Java Software Solutions is published by Addison-Wesley

    Presentation slides are copyright 2006 by John Lewis, William Loftus, and Cara Cocking. All rights reserved.

    Instructors using the textbook may use and modify these slides for pedagogical purposes. *AP is a registered trademark of The College Entrance Examination Board which was not involved in

    the production of, and does not endorse, this product.

  • © 2011 Pearson Education, publishing as Addison-Wesley 2

    Object-Oriented Programming

    Ø  The following concepts are important to object- oriented programming:

    •  object •  attribute •  method •  class •  encapsulation •  inheritance •  polymorphism

  • © 2011 Pearson Education, publishing as Addison-Wesley 3

    Introduction to Objects

    Ø An object represents something with which we can interact in a program

    Ø An object provides a collection of services that we can tell it to perform for us

    Ø  The services are defined by methods in a class that defines the object

    Ø A class represents a concept, and an object represents the embodiment of a class

    Ø A class can be used to create multiple objects

  • © 2011 Pearson Education, publishing as Addison-Wesley 4

    Objects and Classes

    Bank Account

    A class (the concept)

    John’s Bank Account Balance: $5,257

    An object (the realization)

    Bill’s Bank Account Balance: $1,245,069

    Mary’s Bank Account Balance: $16,833

    Multiple objects from the same class

  • © 2011 Pearson Education, publishing as Addison-Wesley 5

    Inheritance

    Ø One class can be used to derive another via inheritance

    Ø Classes can be organized into inheritance hierarchies

    Bank Account

    Account

    Charge Account

    Savings Account

    Checking Account

  • © 2011 Pearson Education, publishing as Addison-Wesley 6

    Using Objects

    Ø  The System.out object represents a destination to which we can send output

    Ø  In the Lincoln program, we invoked the println method of the System.out object:

    System.out.println ("Whatever you are, be a good one.");

    object method information provided to the method (parameters)

  • © 2011 Pearson Education, publishing as Addison-Wesley 7

    The print Method

    Ø  The System.out object provides another service as well

    Ø  The print method is similar to the println method, except that it does not advance to the next line

    Ø  Therefore anything printed after a print statement will appear on the same line

    Ø See Countdown.java (page 61)

  • © 2011 Pearson Education, publishing as Addison-Wesley 8

    Abstraction

    Ø An abstraction hides (or suppresses) the right details at the right time

    Ø An object is abstract in that we don't have to think about its internal details in order to use it

    Ø  For example, we don't have to know how the println method works in order to invoke it

    Ø A human being can manage only seven (plus or minus 2) pieces of information at one time

    Ø But if we group information into chunks (such as objects) we can manage many complicated pieces at once

    Ø Classes and objects help us write complex software

  • © 2011 Pearson Education, publishing as Addison-Wesley 9

    Character Strings

    Ø Every character string is an object in Java, defined by the String class

    Ø Every string literal, delimited by double quotation marks, represents a String object

    Ø  The string concatenation operator (+) is used to append one string to the end of another

    Ø  It can also be used to append a number to a string Ø A string literal cannot be broken across two lines in a

    program Ø See Facts.java (page 64)

  • © 2011 Pearson Education, publishing as Addison-Wesley 10

    String Concatenation

    Ø  The plus operator (+) is also used for arithmetic addition

    Ø  The function that the + operator performs depends on the type of the information on which it operates

    Ø  If both operands are strings, or if one is a string and one is a number, it performs string concatenation

    Ø  If both operands are numeric, it adds them

    Ø  The + operator is evaluated left to right

    Ø Parentheses can be used to force the operation order

    Ø See Addition.java (page 66)

  • © 2011 Pearson Education, publishing as Addison-Wesley 11

    Escape Sequences

    Ø What if we wanted to print a double quote character? Ø  The following line would confuse the compiler

    because it would interpret the second quote as the end of the string

    System.out.println ("I said "Hello" to you.");

    Ø An escape sequence is a series of characters that represents a special character

    Ø An escape sequence begins with a backslash character (\), which indicates that the character(s) that follow should be treated in a special way

    System.out.println ("I said \"Hello\" to you.");

  • © 2011 Pearson Education, publishing as Addison-Wesley 12

    Escape Sequences

    Ø Some Java escape sequences:

    Ø See Roses.java (page 67)

    Escape Sequence \b \t \n \r \" \' \\

    Meaning

    backspace tab

    newline carriage return double quote single quote backslash

  • © 2011 Pearson Education, publishing as Addison-Wesley 13

    Variables

    Ø A variable is a name for a location in memory

    Ø A variable must be declared by specifying the variable's name and the type of information that it will hold

    int total;

    int count, temp, result;

    Multiple variables can be created in one declaration

    data type variable name

  • © 2011 Pearson Education, publishing as Addison-Wesley 14

    Variables

    Ø A variable can be given an initial value in the declaration

    Ø When a variable is referenced in a program, its current value is used

    Ø See PianoKeys.java (page 69)

    int sum = 0; int base = 32, max = 149;

  • © 2011 Pearson Education, publishing as Addison-Wesley 15

    Assignment

    Ø An assignment statement changes the value of a variable

    Ø  The assignment operator is the = sign total = 55;

    Ø  The value that was in total is overwritten Ø You can assign only a value to a variable that is

    consistent with the variable's declared type Ø See Geometry.java (page 70)

    Ø  The expression on the right is evaluated and the result is stored in the variable on the left

  • © 2011 Pearson Education, publishing as Addison-Wesley 16

    Constants

    Ø A constant is an identifier that is similar to a variable except that it holds one value while the program is active

    Ø  The compiler will issue an error if you try to change the value of a constant during execution

    Ø  In Java, we use the final modifier to declare a constant

    final int MIN_HEIGHT = 69;

    Ø Constants: •  give names to otherwise unclear literal values •  facilitate updates of values used throughout a program •  prevent inadvertent attempts to change a value

  • © 2011 Pearson Education, publishing as Addison-Wesley 17

    Primitive Data

    Ø  There are exactly eight primitive data types in Java Ø  Four of them represent integers:

    •  byte, short, int, long

    Ø  Two of them represent floating point numbers: •  float, double

    Ø One of them represents characters: •  char

    Ø And one of them represents boolean values: •  boolean

    Ø Only three are in the AP subset: int, double, and boolean

  • © 2011 Pearson Education, publishing as Addison-Wesley 18

    Numeric Primitive Data

    Ø  The difference between the numeric primitive types is their size and the values they can store.

    Ø  The int type stores only whole numbers while double includes a decimal place.

    Type int double

    Storage 32 bits 64 bits

    Min Value -2,147,483,648 +/- 1.7 x 10308 with 15 significant digits

    Max Value 2,147,483,647

  • © 2011 Pearson Education, publishing as Addison-Wesley 19

    Boolean

    Ø A boolean value represents a true or false condition

    Ø A boolean also can be used to represent any two states, such as a light bulb being on or off

    Ø  The reserved words true and false are the only valid values for a boolean type

    boolean done = false;

  • © 2011 Pearson Education, publishing as Addiso