1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of...

42
1 Chapter 2 Elementary Programming

Transcript of 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of...

Page 1: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

1

Chapter 2

Elementary Programming

Page 2: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

2

Introducing Programming with an Example

Computing the Area of a Circle

This program computes the area of the circle.

Page 3: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

3

Variables

• A variable represents a value stored in the computer’s

memory.

• Its called a variable because its value can be changed.

Page 4: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

4

Numerical Data Types

Name Range Storage Size

byte –27 to 27 – 1 (-128 to 127) 8-bit signed

short –215 to 215 – 1 (-32768 to 32767) 16-bit signed

int –231 to 231 – 1 (-2147483648 to 2147483647) 32-bit signed

long –263 to 263 – 1 64-bit signed (i.e., -9223372036854775808 to 9223372036854775807)

float Negative range: 32-bit IEEE 754 -3.4028235E+38 to -1.4E-45 Positive range: 1.4E-45 to 3.4028235E+38

double Negative range: 64-bit IEEE 754 -1.7976931348623157E+308 to -4.9E-324 Positive range: 4.9E-324 to 1.7976931348623157E+308

Page 5: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

5

Identifiers• An identifier is a sequence of characters that consist of letters,

digits, underscores (_), and dollar signs ($).

• An identifier must start with a letter, an underscore (_), or a dollar

sign ($). It cannot start with a digit.

• An identifier cannot be a reserved word.

• An identifier cannot be true, false, or null.

• An identifier can be of any length.

Page 6: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

6

Declaring and Initializingin One Step

• int x = 1;

• double d = 1.4;

Page 7: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

7

Declaring Variables

int x; // Declare x to be an // integer variable;

double radius; // Declare radius to // be a double variable;

char a; // Declare a to be a // character variable;

Page 8: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

8

Assignment Statements

x = 1; // Assign 1 to x;

radius = 1.0; // Assign 1.0 to radius;

a = 'A'; // Assign 'A' to a;

Page 9: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

9

Variables

// Compute the first arearadius = 1.0;area = radius * radius * 3.14159;System.out.println("The area is “ + area + " for radius ” + radius);

// Compute the second arearadius = 2.0;area = radius * radius * 3.14159;System.out.println("The area is “ + area + " for radius ” + radius);

Page 10: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

10

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }}

no valueradius

allocate memory for radius

Page 11: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

11

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }}

no valueradius

memory

no valuearea

allocate memory for area

Page 12: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

12

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }}

20radius

no valuearea

assign 20 to radius

Page 13: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

13

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }}

20radius

memory

1256.636area

compute area and assign it to variable area

Page 14: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

14

Trace a Program Execution

public class ComputeArea { /** Main method */ public static void main(String[] args) { double radius; double area; // Assign a radius radius = 20; // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius " + radius + " is " + area); }}

20radius

memory

1256.636area

print a message to the console

Page 15: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

15

Named Constants

final datatype CONSTANTNAME = VALUE;

Ex:

final double PI = 3.14159; final int SIZE = 3;

Page 16: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

16

Naming Conventions

• Choose meaningful and descriptive names.

• Variables and method names:

• Use lowercase. If the name consists of several words• concatenate all in one, use lowercase for the first word, and

capitalize the first letter of each subsequent word in the name.

Ex:

the variables radius and areaand the method computeArea.

Page 17: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

17

Naming Conventions, cont.

• Class names:

• Capitalize the first letter of each word in the name. • Ex: the class name ComputeArea.

• Constants:

• Capitalize all letters in constants, and use underscores to connect words.

• Ex: the constant PI and MAX_VALUE

Page 18: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

18

Reading Input from the Console

1. Create a Scanner object

Scanner input = new Scanner(System.in);

NOTE: FIRST, you must import the scanner class as follows:

import java.util.Scanner; // Scanner is in the java.util package

Page 19: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

19

Reading Input from the Console

2. Use the methods • next()

• nextByte()

• nextShort()

• nextInt()

• nextLong()

• nextFloat()

• nextDouble()

• or nextBoolean()

to obtain to a string, byte, short, int, long, float, double, or boolean value.

Page 20: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

20

For example:

System.out.print("Enter a double value: ");

Scanner input = new Scanner(System.in);

double d = input.nextDouble();

Reading Input from the Console

Page 21: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

21

Chapter 2

Elementary Programming

Page 22: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

22

Numeric Operators “Arithmetic”

Page 23: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

23

Integer Division

• 5 / 2 yields an integer 2.

• 5.0 / 2 yields a double value 2.5

• Remainder Operator

5 % 2 yields 1 (the remainder of the division)

Remainder is very useful in programming.

Ex:

an even number % 2 is always 0 and

an odd number % 2 is always 1.

So you can use this property to determine whether a number is even or odd

Page 24: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

24

Arithmetic Expressions

)94

(9))(5(10

5

43

y

x

xx

cbayx

is translated to

(3+4*x)/5 – 10*(y-5)*(a+b+c)/x + 9*(4/x + (9+x)/y)

Page 25: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

25

Precedence of arithmetic operators

Page 26: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

26

How to Evaluate an Expression

3 + 4 * 4 + 5 * (4 + 3) - 1 3 + 4 * 4 + 5 * 7 – 1 3 + 16 + 5 * 7 – 1 3 + 16 + 35 – 1 19 + 35 – 1 54 - 1 53

(1) inside parentheses first

(2) multiplication

(3) multiplication

(4) addition

(6) subtraction

(5) addition

Page 27: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

27

Problem: Converting Temperatures

Write a program that converts a Fahrenheit degree to Celsius using the formula:

)32)(( 95 fahrenheitcelsius

Note: you have to write:

celsius = (5.0 / 9) * (fahrenheit – 32)

Page 28: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

28

Page 29: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

29

Shortcut Assignment Operators

Operator Example Equivalent

+= i += 8 i = i + 8

-= f -= 8.0 f = f - 8.0

*= i *= 8 i = i * 8

/= i /= 8 i = i / 8

%= i %= 8 i = i % 8

Page 30: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

30

Increment and Decrement Operators

Operator Name Description

++var preincrement The expression (++var) increments var by 1 and evaluates to the new value in var after the

increment.

var++ postincrement The expression (var++) evaluates to the original value in var and increments var by 1.

--var predecrement The expression (--var) decrements var by 1 and evaluate to the new value in var after the

decrement.

var-- postdecrement The expression (var--) evaluates to the original value in var and decrements var by 1.

Page 31: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

31

int i = 10; int newNum = 10 * i++;

int newNum = 10 * i; i = i + 1;

Same effect as

int i = 10; int newNum = 10 * (++i);

i = i + 1; int newNum = 10 * i;

Same effect as

Increment and Decrement Operators

Page 32: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

32

• int i = 10;• int newNum = 10 * i++;• System.out.print("i is " + i + ", newNum is " + newNum);

• Output: i is 11, newNum is 100

• int i = 10;• int newNum = 10 * (++i); • System.out.print("i is " + i + ", newNum is " + newNum);

• Output: i is 11, newNum is 110

Page 33: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

33

Problem: Displaying TimeWrite a program that obtains minutes and remaining second from seconds.

Page 34: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

34

Exponent Operations

System.out.println(Math.pow(2, 3)); // Displays 8.0 System.out.println(Math.pow(4, 0.5)); // Displays 2.0System.out.println(Math.pow(2.5, 2));// Displays 6.25System.out.println(Math.pow(2.5, -2)); // Displays 0.16

Page 35: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

35

The String Type

The char type only represents one character. To represent a string of characters, use the data type called String.

Ex:  

String message = "Welcome to Java"; 

Page 36: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

36

Page 37: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

37

Escape Sequences for Special Characters

Description Escape Sequence Unicode

Backspace \b \u0008

Tab \t \u0009

Linefeed \n \u000A

Carriage return \r \u000D

Backslash \\ \u005C

Single Quote \' \u0027

Double Quote \" \u0022

Page 38: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

38

Page 39: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

39

String Concatenation

// Three strings are concatenatedString message = "Welcome " + "to " + "Java"; 

// String Chapter is concatenated with number 2String s = "Chapter" + 2; // s becomes Chapter2 

// String Supplement is concatenated with character BString s1 = "Supplement" + 'B'; // s1 becomes SupplementB

Page 40: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

40

Casting between char and Numeric Types

int i = 'a'; // Same as int i = (int)'a';

char c = 97; // Same as char c = (char)97;

Page 41: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

41

Converting Strings to Integers

The input returned from the input dialog box is a string.

If you enter a numeric value such as 123, it returns “123”.

To obtain the input as a number, you have to convert a string into a number.  To convert a string into an int value, you can use the static parseInt method in the Integer class as follows: 

int intValue = Integer.parseInt(intString); where intString is a numeric string such as “123”.

Page 42: 1 Chapter 2 Elementary Programming. 2 Introducing Programming with an Example Computing the Area of a Circle This program computes the area of the circle.

42

Converting Strings to Doubles

To convert a string into a double value, you can use the static parseDouble method in the Double class as follows:

 

double doubleValue =Double.parseDouble(doubleString);

 

where doubleString is a numeric string such as “123.45”.