ch08 - Alice in Action with Java Chapter 8 Types and...

Info iconThis preview shows pages 1–14. Sign up to view the full content.

View Full Document Right Arrow Icon
Alice in Action with Java Chapter 8 Types and Expressions
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 2 Objectives Use variables and constants Understand the difference between Java’s fundamental and class types Build complex Java expressions Better understand and use Java’s Scanner and PrintStream classes
Background image of page 2
Alice in Action with Java 3 Types and Expressions Alice and Java allow you to send messages to objects Java uses the double type to represent real numbers Some topics to cover Declaring variables and constants Constructing expressions Additional capabilities of printf() Methods libraries provided by Java
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 4 Introductory Example: Months from Month Numbers Elements of the user story Prompt the user for a month number (1-12) Read the number from the keyboard Compute the corresponding month name Display the month abbreviation, with descriptive labels Derive objects from noun phrases in the user story Derive methods from verb phrases in the user story Features of a String object useful to this program Each character in a String has a numerical index A single character or substring is accessed by index
Background image of page 4
Alice in Action with Java 5 Introductory Example: Months from Month Numbers (continued)
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 6 Introductory Example: Months from Month Numbers (continued)
Background image of page 6
Alice in Action with Java 7 Introductory Example: Months from Month Numbers (continued) Strategy for computing monthAbbrev from a number Build a String ( monthTable ) to store abbreviations Access the month abbreviation using String index Method used: aString.substring(start,stop) start = (monthNumber - 1) * 3; stop = start + 3 Example: monthTable.substring(0,3) “Jan” Every variable in a Java program has a type Primitive types: 8 types that store numbers; e.g., int Reference types: refer to objects; e.g., String
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 8 Introductory Example: Months from Month Numbers (continued)
Background image of page 8
Alice in Action with Java 9 Introductory Example: Months from Month Numbers (continued)
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 10 Introductory Example: Months from Month Numbers (continued)
Background image of page 10
Alice in Action with Java 11 Java’s Primitive Types When a type is declared, memory is allocated Numeric types differ in magnitude of stored value Bit: the smallest unit of memory The bit and the base-2 (binary number) system A single bit can represent either binary digit (1 or 0) Combine bits to store numbers greater than 1 Example: binary 11 = decimal 3 With N bits, there are 2 N different bit patterns Example: 32-bit int can represent 2 32 values Literal: raw value of a type, such as char literal ‘ A
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Alice in Action with Java 12 Java’s Primitive Types (continued)
Background image of page 12
Alice in Action with Java 13 The double Type Represents real numbers (those with a decimal point) Example of declaring and initializing a double type double length = 3.5
Background image of page 13

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 14
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 62

ch08 - Alice in Action with Java Chapter 8 Types and...

This preview shows document pages 1 - 14. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online