Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 02 Data and Expressions.

Similar presentations


Presentation on theme: "Chapter 02 Data and Expressions."— Presentation transcript:

1 Chapter 02 Data and Expressions

2 Contents 2. 1 Character Strings 2. 2 Variables and Assignment
2. 3 Primitive Data Types 2. 4 Expressions 2. 5 Data Conversion 2. 6 Interactive Programs 2. 7 Graphics 2. 8 Applets 2. 9 Drawing Shapes

3 System.out.println ("Whatever you are, be a good one.");
2. 1 Character Strings String literal example "This is a string literal." "123 Main Street" "X" defined by the String class every string literal represents a String object The print and println methods System.out.println ("Whatever you are, be a good one."); method name information provided to the method (parameters) object

4 2. 1 Character Strings The print method String concatenation
the System.out object provides the print method is similar to the println method, except that it does not advance to the next line see Countdown.java (page. 89) String concatenation + ; string concatenation operator "Peanut butter " + "and jelly" see Facts.java (page. 91) see Addition.java (page. 92)

5 2. 1 Character Strings Escape sequences some java escape sequences
see Roses.java (page. 94) Escape Sequence \b \t \n \r \" \' \\ Meaning backspace tab newline carriage return double quote single quote backslash

6 2. 2 Variables and Assignment
declaration initialization see PianoKeys.java (page. 96) data type variable name int total; int count, temp, result; int sum = 0; int base = 32, max = 149;

7 2. 2 Variables and Assignment
The assignment statement the assignment operator is the = sign see Geometry.java (page. 97) Constants the final modifier to declare a constant total = 55; final int MIN_HEIGHT = 69;

8 2. 3 Primitive Data Types Integers and floating points Characters
byte(8), short(16), int(32), long(64) floating point numbers float(32), double(64) Characters a single character 'a' 'X' '7' '$' ',' '\n‘ char topGrade = 'A'; char terminator = ';', separator = ' ';

9 2. 3 Primitive Data Types Booleans a true or false condition
the reserved word true and false are the only valid values for a boolean type boolean done = false;

10 2. 4 Expressions Arithmetic operators Operator precedence operators
see TempConverter.java (page. 108) Fahrenheit = 9/5 Celsius + 32 Addition + Subtraction - Multiplication * Division / Remainder %

11 2. 4 Expressions Increment and decrement operators
the increment operator (++) adds one to its operand the decrement operator (--) subtracts one from its operand the statement count++; is functionally equivalent to count = count + 1; form ++count;

12 2. 4 Expressions Assignment operators for example, the statement
num += count; is equivalent to num = num + count; operators Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y

13 2. 5 Data Conversion Conversion techniques
data conversions can occur in three ways: assignment conversion / promotion / casting assignment conversoin money is a float variable dollars is an int variable money = dollars; promotion ; happens automatically sum is a float variable count is an int variable result = sum / count; casting total and count are int variables result = (float) total / count;

14 2. 6 Interactive Programs The Scanner class
input values of various types some methods of the Scanner class see Figure 2.7 (page. 115) see Echo.java (page. 116) see GasMileage.java (page. 117)

15 2. 7 Graphics Introduction
focus on graphics and graphical user interfaces a picture or drawing must be digitized a picture is made up pixels (picture elements) picture resolution the number of pixels used to represent a picture monitor resolution the number of pixels that can be displayed by a monitor digitization resolution color depth

16 2. 7 Graphics Coordinate systems
in a Java program, we can a coordinate system with the origin in the top-left corner 112 Y X (0, 0) 40 (112, 40)

17 2. 7 Graphics Representing color The Color class 1-bit black/white
8-bits grayscale 0 ~ 255 24-bits true color (RGB color system) Red ; 0 ~ 255 Green ; 0 ~ 255 Blue ; 0 ~ 255 The Color class see Figure 2.10 (page. 121) Object Color.black Color.blue Color.cyan Color.orange Color.white Color.yellow RGB Value 0, 0, 0 0, 0, 255 0, 255, 255 255, 200, 0 255, 255, 255 255, 255, 0

18 2. 8 Applets Introduction Java application Java applet
stand-alone program with a main method Java applet a program that is intended to transported over the Web execute using a web browser doesn’t have a main method defines an applet extends the Applet class use of inheritance instead, there are several special methods init / start / stop / destroy methods paint method see Einstein.java (page. 123)

19 2. 8 Applets Executing applets using the Web the HTML applet tag
<head> <title>The Einstein Applet</title> </head> <body> <applet code="Einstein.class" width=350 height=175> </applet> </body> </html>

20 2. 9 Drawing Shapes The Graphics class Drawing a line
defined in the java.awt package a shape can be filled or unfilled Drawing a line X Y 10 150 20 45 page.drawLine (10, 20, 150, 45); page.drawLine (150, 45, 10, 20); or

21 2. 9 Drawing Shapes Drawing a rectangle 50 X 20 40 100 Y
page.drawRect (50, 20, 100, 40);

22 2. 9 Drawing Shapes Drawing an oval 175 X 20 80 bounding rectangle 50
Y 175 20 80 bounding rectangle 50 page.drawOval (175, 20, 50, 80);

23 2. 9 Drawing Shapes Drawing shapes background color / foreground color
see Figure 2.12 (page. 126) see Snowman.java (page. 128)

24 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

25 [ Exercises ] Programming Projects (page. 135)
2. 3 reads two floating point numbers and prints their sum, difference, and product 2. 4 the TempConverter application to convert form Fahrenheit to Celsius 2.12 the Snowman applet modifications 2.18 displays a business card of your own design


Download ppt "Chapter 02 Data and Expressions."

Similar presentations


Ads by Google