chapter 9: primitive types java programming from the beginning copyright © 2000 w. w. norton &...

114
Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9: Primitive Types Java Java Programming Programming FROM THE BEGINNING FROM THE BEGINNING Chapter 9 Primitive Types

Upload: daniella-shelton

Post on 13-Dec-2015

229 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

1

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Chapter 9

Primitive Types

Page 2: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

2

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

9.1 Types

• Numbers, like all data, must be stored in computer memory.

• Computer memory consists of bits (0s and 1s).• Bits are grouped into larger units, known as bytes,

each containing eight bits.• The type of a data item determines how much

space is used to store the item and how the bits in the item are interpreted.

Page 3: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

3

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Primitive Types Versus Reference Types

• Java has two kinds of types: primitive types and reference types.

• A variable of a primitive type stores a data item directly.

• A variable of a reference type stores the location of the data, not the data itself.

• The primitive types fall into two categories:– Numeric (the byte, short, int, long, float, double, and char types)

– Boolean (the boolean type)

Page 4: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

4

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Literals

• The representation of a particular value within a program is said to be a literal.– 12 is an int literal.– true is a boolean literal.

• There’s usually no single way to represent a particular number.

• Humans write the same number in different ways. The Romans wrote “twelve” as XII instead of 12.

• Similarly, programmers sometimes use different literals that represent the same number.

Page 5: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

5

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

9.2 Integer Types

• A variable of type int can store any number between –2,147,483,648 and +2,147,483,647.

• This unusual range of values stems from the fact that computers store numbers in binary (base 2) rather than decimal (base 10).

Page 6: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

6

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Binary Numbers

• Decimal numbers are written in a positional notation, with each digit corresponding to a power of 10:153 = 1 102 + 5 101 + 3 100

• A binary number consists of a series of binary digits (bits), each of which is either 0 or 1.

• Each bit is implicitly multiplied by a power of 2.

• The binary number 10011001 is equivalent to the decimal number 153:1 27 + 0 26 + 0 25 + 1 24 +

1 23 + 0 22 + 0 21 + 1 20 =

128 + 0 + 0 + 16 + 8 + 0 + 0 + 1 = 153

Page 7: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

7

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Binary Numbers

• Inside the computer, numbers are stored using a fixed number of bits.

• A number that has int type is stored using 32 bits.

• In int form, the number 153 would be stored as 00000000000000000000000010011001 rather than just 10011001.

Page 8: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

8

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Sign Bit

• The leftmost bit of an integer is used to store the number’s sign.– If the leftmost bit is 0, the number is positive (or zero).

– If the leftmost bit is 1, the number is negative.

• Because of the presence of this sign bit, the largest int is 01111111111111111111111111111111, which has the value1 230 + 1 229 + … + 1 20 = 231 – 1 = 2,147,483,647

Page 9: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

9

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Negative Integers

• Java stores negative integers in a form known as two’s complement.

• An algorithm for determining the bits in a negative number:1. Write the positive version of the number in binary.

2. Complement the number (change each 0 bit to 1 and change each 1 bit to 0).

3. Add 1 to the result.

Page 10: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

10

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Negative Integers

• To determine how the number –12 would be stored as an int value, the first step is to write 12 in binary: 00000000000000000000000000001100

• Next, find the complement of the number: 11111111111111111111111111110011

• Finally, add 1 to the complement: 11111111111111111111111111110011

+ 1

11111111111111111111111111110100

• The sign bit is 1, indicating a negative number.

Page 11: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

11

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The long Type

• For integers that exceed 2,147,483,647, Java has another type named long, which stores integers using 64 bits.

• The largest value that can be stored as a long is 263 – 1.

• If that’s still not big enough, there’s a class named BigInteger whose instances can store integers of any size whatsoever.

Page 12: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

12

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The byte and short Types

• When working with numbers that are known to be small, it’s sometimes advantageous to use a “smaller” type than int.

• Java has two such types, byte and short.• A byte value is stored in 8 bits, so its values

range from –128 (–27) to +127 (27 – 1).• A short value is stored in 16 bits; its values

range from –32,768 (–215) to +32,767 (215 – 1).

Page 13: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

13

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Summary of the Integer Types

• A summary of Java’s integer types: Type Size Smallest Value Largest Valuebyte 8 bits –128 127short 16 bits –32,768 32,767int 32 bits –2,147,483,648 2,147,483,647long 64 bits –263 263–1

• Most of the time, int is the best choice.• Operations on long values may require more

time than operations on int values.

Page 14: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

14

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Integer Literals

• Java allows integer literals to be written in decimal (base 10), octal (base 8), or hexadecimal (base 16).

• Decimal literals contain digits between 0 and 9, but must not begin with a zero:15 255 32767

• Octal literals contain only digits between 0 and 7, and must begin with a zero:017 0377 077777

• Hexadecimal literals contain digits between 0 and 9 and letters between a and f, and always begin with 0x:0xf 0xff 0x7fff

Page 15: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

15

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Integer Literals

• The letters in a hexadecimal literal may be either upper or lowercase:0xff 0xfF 0xFf 0xFF 0Xff 0XfF 0XFf 0XFF

• Writing numbers in octal or hexadecimal has no effect on how the numbers are actually stored.

• The literals 15, 017, or 0xf all represent the same number (fifteen).

• A program can switch from one notation to another at any time, and even mix them: 10 + 015 + 0x20 has the value 55 (decimal).

Page 16: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

16

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Octal and Hexadecimal

• An octal number is written using only the digits 0 through 7.

• Each position in an octal number represents a power of 8.• The octal number 237 represents the decimal number

2 82 + 3 81 + 7 80 = 128 + 24 + 7 = 159.

• A hexadecimal (or hex) number is written using the digits 0 through 9 plus the letters A through F, which stand for 10 through 15, respectively.

• Each position in a hex number represents a power of 16.• The hex number 1AF has the decimal value

1 162 + 10 161 + 15 160 = 256 + 160 + 15 = 431.

Page 17: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

17

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Octal and Hexadecimal

• Although integers are stored in binary form, humans usually prefer to work with decimal numbers.

• In some types of programs, however, it’s necessary to work directly with binary numbers.

• Unfortunately, writing numbers in binary is painful and error-prone.

• Because of the close connection between the binary number system and the octal and hexadecimal systems, it’s easy to translate binary numbers to either octal or hexadecimal.

Page 18: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

18

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Octal and Hexadecimal

• To translate a binary number into octal, first collect the bits into groups of three, starting from the right end:

011 111 101 111 001 111 000 111 101 110 100

• Next, translate each group into a single octal digit:0 11 111 101 111 001 111 000 111 101 110 100

3 7 5 7 1 7 0 7 5 6 4

• Finally, join the octal digits together. In this example, 11111101111001111000111101110100 (binary) is equivalent to 37571707564 (octal).

Page 19: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

19

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Octal and Hexadecimal

• To translate a binary number into hexadecimal, first collect the bits into groups of four, starting from the right end:

1111 1101 1110 0111 1000 1111 0111 0100

• Next, translate each group into a single hex digit:1111 1101 1110 0111 1000 1111 0111 0100 F D E 7 8 F 7 4

• Joining the digits together gives FDE78F74 (hexadecimal), which is equivalent to 11111101111001111000111101110100 (binary).

Page 20: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

20

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Type of an Integer Literal

• By default, an integer literal has type int.

• Examples of int literals:512 0777 0x10000000

• A literal that ends with the letter L has type long:512L 0777L 0x10000000L

• The L suffix can be lowercase, but that’s not a good idea—it’s too easy to confuse the letter l with the digit 1.

• An integer literal whose value is greater than 2147483647 or less than –2147483648 must have the L suffix.

Page 21: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

21

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Converting Strings to Integers

• Methods that convert a string containing an integer into the actual value of the integer:Byte.parseByteInteger.parseIntLong.parseLongShort.parseShort

• NumberFormatException is thrown if the string doesn’t contain a valid integer or it represents a number outside the range of allowable values.

• For example, Byte.parseByte would reject the string "128", because the largest byte value is 127.

Page 22: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

22

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

9.3 Floating-Point Types

• The integer types aren’t suitable for variables that need to store numbers with digits after the decimal point, or numbers that are exceedingly large or small.

• Java provides two types to use in this situation: float and double, the floating-point types.

• Characteristics of the floating-point types: Smallest Type Size Positive Value Largest Value Precisionfloat 32 bits 1.40 10–45 3.40 1038 6 digitsdouble 64 bits 4.94 10–324 1.79 10308 15 digits

Page 23: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

23

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Floating-Point Types

• float is suitable when accuracy isn’t critical.• double provides greater precision.

• For numbers that must be stored even more precisely, Java provides a class named BigDecimal.

• In most languages, the format of floating-point numbers isn’t specified, because not all computers store floating-point numbers in the same way.

• For portability, Java requires that float and double values be stored in the format specified by IEEE Standard 754.

Page 24: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

24

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The IEEE Floating-Point Standard

• IEEE Standard 754, developed by the Institute of Electrical and Electronics Engineers, provides two primary formats for floating-point numbers:– Single precision (32 bits)

– Double precision (64 bits)

• Numbers are stored in a form of scientific notation, with each number having three parts:– Sign

– Exponent

– Fraction

Page 25: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

25

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The IEEE Floating-Point Standard

• The number of bits reserved for the exponent determines how large (or small) numbers can be

• The number of bits in the fraction determines the precision.

• Single-precision format:– 8-bit exponent and 23-bit fraction– Maximum value of approximately 3.40 1038, with a

precision of about 6 decimal digits

• Double-precision format:– 11-bit exponent and 52-bit fraction– Maximum value of approximately 1.79 10308, with a

precision of about 15 decimal digits

Page 26: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

26

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Floating-Point Literals

• When a literal represents a float or double value, it is said to be a floating-point literal.

• A floating-point literal may contain a decimal point:2.9748 414.270984 .0008915

• It may have an exponent instead:29748e-4 414270984e-6 8915e-7

• Or it may have both a decimal point and an exponent:297.48e-2 4.14270984e+2 89.15e-5

• The + in the exponent is optional, and the e can be uppercase.

Page 27: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

27

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Floating-Point Literals

• A floating-point literal may end with the letter f (or F), to indicate that it has type float, or it may end with d (or D), to indicate that it has type double.

• By default, a floating-point literal is treated as a double value.

• A floating-point literal must contain a decimal point and/or an exponent, unless f or d is appended to the literal:597d 597f

Page 28: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

28

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Floating-Point Literals

• A double value can’t be assigned to a float variable. As a result, a floating-point literal without a suffix can’t be assigned to a float variable:float x;x = 1.0; // WRONG; 1.0 has type double

• One solution is to declare x as a double variable:double x;x = 1.0; // Legal

• The other is to put the letter f at the end of the literal:float x;x = 1.0f; // Legal

Page 29: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

29

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Special Values

• Java’s float and double types include three special values: +, –, and NaN (Not a Number).

• Dividing a positive number by zero yields +.• Dividing a negative number by zero yields –.• NaN is the result of a mathematically undefined

operation, such as dividing zero by zero.• Examples:1.0 / 0.0 +-1.0 / 0.0 –0.0 / 0.0 NaN

Page 30: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

30

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Special Values

• Special values can be used in computations.• + and – have much the same properties as they

do in ordinary mathematics.• Dividing an ordinary floating-point number by

infinity gives zero as the result:1.0 / (1.0 / 0.0) 0.01.0 / (-1.0 / 0.0) –0.0

Page 31: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

31

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Special Values

• Using NaN in a calculation always results in a value of NaN:1.0 / (0.0 / 0.0) NaN0.0 * (0.0 / 0.0) NaN

• NaN can also result from multiplying infinity by zero:(1.0 / 0.0) * 0.0 NaN(-1.0 / 0.0) * 0.0 NaN

Page 32: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

32

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Special Values

• Expressions whose values are +, –, and NaN can be printed with System.out.print or System.out.println:System.out.println(1.0 / 0.0); // Prints "Infinity"

System.out.println(-1.0 / 0.0); // Prints "-Infinity"

System.out.println(0.0 / 0.0); // Prints "NaN"

Page 33: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

33

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The Math class, which is part of the java.lang package, provides a number of methods that are useful when working with numbers, especially floating-point numbers.

• The Math class also defines two constants, named E and PI, which represent the familiar mathematical constants e and .

• These constants can be accessed by writing Math.E and Math.PI.

Page 34: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

34

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The cos, sin, and tan methods compute the cosine, sine, and tangent functions:Math.cos(Math.PI) –1.0Math.sin(Math.PI) 1.2246467991473532E-16Math.tan(Math.PI) –1.2246467991473532E-16

• Angles are measured in radians, not degrees.

Page 35: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

35

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The Math class also provides methods that compute the exponential and logarithmic functions.

• The exp method computes the exponential function:Math.exp(1.0) 2.7182818284590455

• The log method computes the natural logarithm of its argument:Math.log(Math.E) 1.0

Page 36: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

36

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The pow and sqrt methods compute powers and square roots.

• pow(a, b) throws ArithmeticException if a is equal to 0.0 and b is less than or equal to 0.0, or if a is less than 0.0 and b is not a whole number.

• The sqrt method returns NaN if its argument is less than zero.

Page 37: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

37

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The abs, max, and min methods compute the absolute value and the maximum and minimum of two numbers.

• Each method is overloaded. There are actually four different methods named abs, four named max, and four named min.

• One version of the abs method takes an int argument and returns an int value; the other versions take long, float, and double arguments.

Page 38: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

38

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The ceil, floor, rint, and round methods are used to round float and double values to integers.

• The ceil method returns the smallest integer that’s not less than the argument:Math.ceil(4.1) 5.0Math.ceil(4.9) 5.0Math.ceil(-4.1) –4.0Math.ceil(-4.9) –4.0

Page 39: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

39

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The floor method returns the largest integer that’s not greater than the argument:Math.floor(4.1) 4.0Math.floor(4.9) 4.0Math.floor(-4.1) –5.0Math.floor(-4.9) –5.0

Page 40: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

40

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The rint method returns the integer closest to the argument:Math.rint(4.1) 4.0Math.rint(4.5) 4.0Math.rint(4.9) 5.0Math.rint(5.5) 6.0Math.rint(-4.1) –4.0Math.rint(-4.5) –4.0Math.rint(-4.9) –5.0Math.rint(-5.5) –6.0

• If the argument to rint is equally close to two integers, rint returns the even one.

Page 41: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

41

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Math Class

• The two round methods behave exactly like rint; the only difference is that they return int or long values instead of double values.

• One version of round rounds a float value to the nearest int value; the other rounds a double value to the nearest long value.

• The Math class also provides the random method, which returns a pseudorandom double value that’s greater than or equal to 0.0 and less than 1.0.

Page 42: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

42

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Pseudorandom Numbers

• Programs that play games or simulate real-world phenomena often need a source of numbers that appear to be random.

• Truly random numbers are hard to come by, so programmers settle for pseudorandom numbers.

• Pseudorandom numbers appear to be random but are actually generated by a mathematical formula.

• A pseudorandom sequence begins with a seed value; each value after that in the sequence is generated from the previous value.

Page 43: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

43

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Pseudorandom Numbers

• Pseudorandom numbers have one advantage over truly random numbers: a pseudorandom sequence can be reproduced by starting with the same seed.

• The most popular method for generating pseudorandom numbers, the linear congruential method, uses the following formula:

Xn+1 = (aXn + c) mod m

Xn is the nth element in the sequence and a, c, and m are constants.

• Proper choice of a, c, and m is important, to avoid having the sequence begin repeating too soon.

Page 44: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

44

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Pseudorandom Numbers

• Math.random uses the linear congruential method to generate a 53-bit integer, which is divided by 253 to produce a double value between 0.0 and 1.0.

• The seed is based on the time of day at which the program is run, so the pseudorandom sequence will probably be different each time.

• Math.random uses the services of the Random class, which belongs to the java.util package.

• An instance of the Random class keeps track of a single pseudorandom sequence.

Page 45: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

45

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

9.4 The char Type

• A variable of type char can be assigned any single character:char ch;

ch = 'a'; // Lowercase ach = 'A'; // Uppercase Ach = '0'; // Zeroch = ' '; // Space

• Character literals are enclosed in single quotes.

Page 46: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

46

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The ASCII Character Set

• A character set is a list of characters together with a unique code for each character.

• The most widely used character set is ASCII (American Standard Code for Information Interchange).

• There are 128 characters in ASCII.• The space character (character 32) precedes the

digits (48–57).• The uppercase letters (65–90) come later, before

the lowercase letters (97–122).

Page 47: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

47

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The ASCII Character Set

• The first 32 characters in ASCII (and the last character, del) are control characters.

• Control characters can’t be displayed on the screen or printed.

• They represent commands entered by the user or commands to be sent to an output device, such as a printer.

• The characters other than the control characters and the space character are said to be printing characters.

Page 48: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

48

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The ASCII Character Set

• Control characters can be entered at the keyboard by pressing special keys such as Backspace and Tab.

• Control characters can also be entered by holding down the Control key and pressing a letter key.

• Because the largest ASCII value is 127, the code for any ASCII character fits in a single 8-bit byte.

• For example, the character A is stored as 01000001, which is 65 in decimal or 101 in octal.

Page 49: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

49

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Unicode Character Set

• Instead of relying on ASCII to represent characters, Java uses a slightly different encoding known as Unicode.

• A Unicode character requires 16 bits.• Unicode is compatible with ASCII. Adding eight

zero bits to the beginning of the ASCII code for a character gives the character’s value in Unicode.

• For example, the Unicode version of the character A would be 0000000001000001.

Page 50: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

50

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Unicode Character Set

• The first 256 characters of Unicode match the Latin1 character set.

• Latin1 is an extension of ASCII that includes the characters needed by European languages (such as é, ñ, and ô) plus a number of common symbols.

• Programs that need only ASCII (the first 128 characters of Unicode) or Latin1 can simply ignore the other Unicode characters.

Page 51: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

51

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Unicode Character Set

• A graphical depiction of the relationship between ASCII, Latin1, and Unicode:

Page 52: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

52

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Unicode Character Set

• Unicode can support up to 65,536 characters.• Java relies on Unicode both for writing programs

and for storing character data within a program.• ASCII is still the most popular character set.• Some computers rely on even older character sets.• IBM mainframes use an 8-bit character set named

EBCDIC.

Page 53: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

53

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Escape Sequences

• A character literal is usually one character enclosed in single quotes.

• Special characters, including control characters, can’t be represented in this way.

• To include a special character in a literal, an escape sequence must be used.

• Escape sequences can be enclosed in single quotes to form character literals or embedded in string literals.

Page 54: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

54

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Escape Sequences

• Escape sequences for use in character and string literals: Name Escape SequenceBackspace \bForm feed \fLine feed \nCarriage return \rHorizontal tab \tBackslash \\Single quote \'Double quote \"Octal escape \0–\377

Page 55: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

55

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Octal Escapes

• Octal escapes can be used to represent any character whose Unicode value lies between 0 and 377 (octal).

• An octal escape consists of the \ character followed by an octal number with at most three digits.

• The octal code for the escape character (the character produced when the user presses the Esc key) is 33; the octal escape for this character would be \33 or \033.

Page 56: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

56

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Unicode Escapes

• Java also provides Unicode escapes, which have the form \udddd, where each d is a hex digit.

• Unlike the other kinds of escapes, Unicode escapes are allowed anywhere in a program, not just in character and string literals.

• This property allows the use of Unicode characters in identifiers and comments.

• In particular, identifiers may contain letters and digits from other languages.

Page 57: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

57

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Operations on Characters

• Java treats characters as integers.• The codes for Unicode characters range from

0000000000000000 to 1111111111111111, which can be thought of as the integers from 0 to 65,535.

• The character 'a' has the value 97, 'A' has the value 65, '0' has the value 48, and ' ' has the value 32.

• Java considers char to be one of the integral types, along with byte, short, int, and long.

Page 58: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

58

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Operations on Characters

• When a character appears in a computation, Java simply uses its integer value:int i = 'a'; // i is now 97char ch = 65; // ch is now 'A'ch++; // ch is now 'B'

• Characters can be compared, just as numbers can:if ('a' <= ch && ch <= 'z') …

• Comparisons such as 'a' <= ch are done using the integer values of the characters involved.

Page 59: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

59

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Operations on Characters

• A character variable can be used as the controlling expression in a switch statement:switch (letter) { case 'a': case 'e': case 'i': case 'o': case 'u': System.out.println("Vowel"); break; case 'y': System.out.println("Possible vowel"); break; default: System.out.println("Consonant"); break;}

Page 60: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

60

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Operations on Characters

• A character variable can be used as a loop counter:for (char ch = 'A'; ch <= 'Z'; ch++) …

• Although treating characters as numbers is convenient, there are disadvantages as well.– It is possible to write meaningless expressions such as 'a' * 'b' / 'c'.

– A bug caused by accidentally using a character as a number won’t be caught by the Java compiler.

Page 61: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

61

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Characters Versus Strings

• A character stored in a String object can be retrieved by calling the charAt method.

• charAt requires a single argument, representing the position of the desired character.

• A loop that visits all the characters in a string:for (int i = 0; i < str.length(); i++) …

Inside the loop, the call str.charAt(i) would be used to access the character at position i.

Page 62: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

62

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Characters Versus Strings

• A loop that displays an ISBN number with the dashes removed. for (int i = 0; i < isbn.length(); i++) { char ch = isbn.charAt(i); if (ch != '-') System.out.print(ch);}

Page 63: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

63

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Character Class

• The Character class, which belongs to the java.lang package, provides methods for testing and converting characters.

• isDigit checks whether its argument is a digit:Character.isDigit('a') falseCharacter.isDigit('0') trueCharacter.isDigit(' ') false

• isISOControl checks whether its argument is a control character:Character.isISOControl('a') falseCharacter.isISOControl('0') falseCharacter.isISOControl('\n') true

Page 64: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

64

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Character Class

• isLetter checks whether its argument is a letter:Character.isLetter('a') trueCharacter.isLetter('0') falseCharacter.isLetter(' ') false

• isLetterOrDigit checks whether its argument is a letter or a digit:Character.isLetterOrDigit('a') trueCharacter.isLetterOrDigit('0') trueCharacter.isLetterOrDigit(' ') false

Page 65: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

65

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Character Class

• isLowerCase checks whether its argument is a lowercase letter:Character.isLowerCase('a') trueCharacter.isLowerCase('A') false

• isUpperCase checks whether its argument is an uppercase letter:Character.isUpperCase('a') falseCharacter.isUpperCase('A') true

Page 66: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

66

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Character Class

• isWhitespace checks whether its argument is a white-space character—a character that’s invisible to the user.

• Some control characters—including the tab character and the line feed—are white-space characters, as is the space character itself.Character.isWhitespace('a') falseCharacter.isWhitespace(' ') trueCharacter.isWhitespace('\n') true

Page 67: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

67

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

The Character Class

• toLowerCase accepts any character as its argument. If the argument is an uppercase letter, the lowercase version of the letter is returned; otherwise, the argument itself is returned:Character.toLowerCase('a') 'a'Character.toLowerCase('A') 'a'Character.toLowerCase('5') '5'

• toUpperCase is similar:Character.toUpperCase('a') 'A'Character.toUpperCase('A') 'A'Character.toUpperCase('5') '5'

Page 68: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

68

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

9.5 Type Conversion

• For a computer to perform an arithmetic operation, the operands must usually be of the same size and be stored in the same way.

• Java, however, allows primitive types to be mixed in expressions.

• The Java compiler may have to generate instructions that convert operands to different types so that the hardware will be able to evaluate the expression.

Page 69: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

69

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Implicit Versus Explicit Conversions

• Examples:– If an int is added to a long, the compiler will arrange

for the int value to be converted to 64 bits.

– If an int and a float are added, the compiler will arrange for the int to be converted to float format.

• The compiler handles these conversions automatically, so they’re known as implicit conversions.

• Java also allows the programmer to perform explicit conversions.

Page 70: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

70

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Implicit Conversions

• Situations in which implicit conversions are performed:– When the operands in a numeric expression don’t have

the same type.

– When the type of the expression on the right side of an assignment doesn’t match the type of the variable on the left side.

– When the type of an argument in a method call doesn’t match the type of the corresponding parameter.

– When a value needs to be converted to string form (as in the expression "THX" + 1138).

Page 71: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

71

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Numeric Promotion

• Many Java operators allow operands of different types. If the operands have different types, they must be converted to some common type before the operation can be performed.

• If f is a float variable and i is an int variable, it’s safer to convert i to type float rather than convert f to type int.

• Java’s strategy is to convert operands to a type that will safely accommodate both values.

• This process is called numeric promotion.

Page 72: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

72

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Numeric Promotion

• Rules for numeric promotion:1. If either operand is of type double, the other is converted to double.

2. If either operand is of type float, the other is converted to float.

3. If either operand is of type long, the other is converted to long.

4. Otherwise, both operands are converted to int.

Page 73: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

73

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Numeric Promotion

• Examples:double + int doubleint + double doublefloat + int floatlong + float floatshort + long longbyte + int intshort + byte int

Page 74: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

74

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Numeric Promotion

• Numeric promotion tries to change the way a number is stored, without changing the number’s value.

• However, when converting from int or long to float, or from long to double, some of the original number’s precision may be lost.

Page 75: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

75

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Numeric Promotion

• The numeric promotions are applied to the operands of most binary operators, including:– Multiplicative operators (*, /, and %)

– Additive operators (+ and -)

– Relational operators (<, <=, >, and >=)

– Equality operators (== and !=)

Page 76: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

76

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Conversion During Assignment

• In an assignment, Java converts the expression on the right side to the type of the variable on the left side, provided that the variable’s type is at least as “wide” as the expression’s type.

• The numeric types, in order of increasing width:

Page 77: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

77

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Conversion During Assignment

• Examples of legal assignments:short s;int i;float f;double d;…i = s; // s is converted to intf = i; // i is converted to floatd = f; // f is converted to double

• In addition, a char value can be assigned to a variable of type int, long, float, or double.

Page 78: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

78

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Conversion During Assignment

• An assignment in which the expression on the right side has a wider type than the variable on the left side is an error:int i;float f;…i = f; // WRONG

• The reason for this rule is that storing a value into a narrower variable can cause bugs in the program.

Page 79: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

79

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Argument Conversion

• When a method is called, Java will try to convert each argument of a primitive type to the type of the corresponding parameter.

• Example:acct.deposit(100);

The argument will be converted to double, the type of the parameter for the deposit method.

Page 80: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

80

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Argument Conversion

• The rules for converting arguments are nearly the same as the rules for conversion during assignment.

• Java converts each argument to the type of the matching parameter, provided that the parameter’s type is at least as “wide” as the argument's type.

• If a method has a byte, short, or char parameter, however, the argument can’t be an integer literal (or any integer constant expression).

Page 81: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

81

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Casting

• A conversion that’s done at the programmer’s request is called a cast.

• Casting is done by enclosing the desired type in parentheses and inserting it before the expression:int i;float f;…i = (int) f; // Cast f to int type

• Casting a floating-point number to an integer type causes the number to be truncated.

• General form of a cast:( type-name ) expression

Page 82: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

82

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Truncating a Floating-Point Number

• Casting can be used to find the fractional part of a floating-point number:float f, fractionalPart;…fractionalPart = f - (long) f;

Using long rather than int reduces the chance that f is too large to represent as an integer.

• Casting can also be used to test whether a floating-point variable x contains an integer value:if (x == (long) x) …

Page 83: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

83

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Truncating a Floating-Point Number

• Truncating a floating-point number is useful for generating pseudorandom integers.

• The Math.random method generates a random floating-point number between 0.0 and 1.0, which can be scaled to any range of integer values.

• A statement that generates a random integer between 0 and 9:int randomInteger = (int) (Math.random() * 10);

Page 84: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

84

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Dividing Integers Without Truncation

• When integers are divided, the result is truncated, even when the result is assigned to a float or double variable:double quotient;int dividend, divisor;…quotient = dividend / divisor;

• To avoid truncation, a cast expression can be used:quotient = (double) dividend / divisor;

Page 85: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

85

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Dividing Integers Without Truncation

• Java regards ( type-name ) as a unary operator.• Unary operators have higher precedence than

binary operators, so the compiler interprets the expression(double) dividend / divisor

as((double) dividend) / divisor

• divisor doesn’t need a cast because casting dividend to double forces the compiler to convert divisor to double also.

Page 86: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

86

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Dividing Integers Without Truncation

• Division of integers can also be done using the statementquotient = dividend / (double) divisor;

or the statementquotient = (double) dividend / (double) divisor;

Page 87: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

87

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Avoiding Overflow

• An arithmetic operation may cause overflow to occur, even when the result is assigned to a variable that’s wide enough to store the correct result:long i;int j = 1000000;i = j * j; // WRONG

• Using a cast avoids the problem:i = (long) j * j;

• The following statement wouldn’t work, because the overflow would occur before the cast:i = (long) (j * j); // WRONG

Page 88: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

88

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Performing Arithmetic onbyte, short, and char Values

• Arithmetic on byte values doesn’t always work as expected:byte a, b, c;…c = a * b; // WRONG

a and b are promoted to int before the multiplication, so the result has type int.

• A cast can be used to fix the problem:c = (byte) (a * b); // Legal

• The same problem occurs when arithmetic is performed on short values or char variables.

Page 89: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

89

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Program: Encrypting a MessageUsing the Rot13 Technique

• The “rot13” method is one of the simplest (but least effective!) ways to encrypt a message.

• This algorithm replaces each letter in the original message by the letter that comes 13 places after it in the alphabet:A B C D E F G H I J K L M N O P Q R S T U V W X Y Z N O P Q R S T U V W X Y Z A B C D E F G H I J K L M

• There’s no letter 13 places after N, so it’s necessary to “wrap around” to the beginning of the alphabet. Rot13 is an abbreviation for “rotate alphabet 13 places.”

Page 90: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

90

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

User Interface

• The Rot13 program will ask the user for a message and then display an encrypted version of the message:Enter message to be encrypted: The truth is out there

Encrypted message: Gur gehgu vf bhg gurer

• One of the useful properties of the rot13 method is that a message can be decrypted by applying the same algorithm to the encrypted message.

• If Rot13 is given the encrypted message as its input, it will display the original message:Enter message to be encrypted: Gur gehgu vf bhg gurerEncrypted message: The truth is out there

Page 91: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

91

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the Rot13 Program

• The program will need a loop that visits each character in the user’s message, using the charAt method to extract characters one by one.

• An algorithm for encrypting each character:1. If the character is not a letter, leave it unchanged.

2. If the character is an uppercase letter, replace it by the uppercase letter that’s 13 positions after it in the alphabet, wrapping to the beginning of the alphabet if necessary.

3. If the character is a lowercase letter, replace it by the lowercase letter that’s 13 positions after it in the alphabet, wrapping to the beginning of the alphabet if necessary.

Page 92: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

92

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the Rot13 Program

• Character.isLetter and Character.isUpperCase can be used to test whether a character is a letter or an uppercase letter.

• Replacing a letter by the one that’s 13 positions after in the alphabet can be done by adding 13 to the letter.

• However, the problem of “wrap-around” must be dealt with.

Page 93: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

93

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the Rot13 Program

• One way to solve this problem would be to use an if statement.

• With the help of the % operator, however, it’s possible to write a single formula that handles all letters between A and Z.

• Suppose that ch is an uppercase letter. (The technique will be similar for lowercase letters.)

• Subtracting 'A' from ch gives a number between 0 and 25: ch - 'A'

Page 94: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

94

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the Rot13 Program

• Adding 13 gives a number between 13 and 38:(ch - 'A') + 13

• Applying the remainder operator produces a number between 0 and 25 :((ch - 'A') + 13) % 26

• Adding the letter 'A' converts back to a Unicode value:((ch - 'A') + 13) % 26 + 'A'

• The cast operator converts the expression to type char:(char) (((ch - 'A') + 13) % 26 + 'A')

Page 95: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

95

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Rot13.java// Uses the "rot13" algorithm to encrypt a message entered by// the user

import jpb.*;

public class Rot13 { public static void main(String[] args) { // Prompt the user to enter the message to be encrypted SimpleIO.prompt("Enter message to be encrypted: "); String userInput = SimpleIO.readLine();

// Display the encrypted message System.out.print("Encrypted message: "); for (int i = 0; i < userInput.length(); i++) System.out.print(encrypt(userInput.charAt(i))); System.out.println(); }

Page 96: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

96

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

// Returns the encrypted version of ch using the "rot13" // algorithm private static char encrypt(char ch) { if (!Character.isLetter(ch)) return ch; if (Character.isUpperCase(ch)) return (char) (((ch - 'A') + 13) % 26 + 'A'); return (char) (((ch - 'a') + 13) % 26 + 'a'); }}

Page 97: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

97

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

9.6 Case Study:Drawing Random Circles

• The RandomCircles program will draw randomly generated circles within a frame.

• Each circle will consist of 360 lines that share a common endpoint (the center of the circle).

• The program will use a “delay” to slow down the drawing process so that the user can see each circle being drawn.

• This is an example of animation—changing the on-screen image at regular intervals to create the illusion of motion.

Page 98: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

98

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

User Interface

• The user will enter the size of the frame, the number of circles to be drawn, and the “delay time,” which controls the drawing speed:Enter frame size: 300Enter number of circles: 10Enter delay (in milliseconds): 5

Page 99: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

99

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

User Interface

• The program will ask the user to re-enter any input value that is not a valid integer:Enter frame size: whateverNot an integer; try again.Enter frame size: 300Enter number of circles: a bunchNot an integer; try again.Enter number of circles: 10Enter delay (in milliseconds): 5

Page 100: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

100

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Program Behavior

• After the user has successfully entered all three input values, the program will display a frame.

• The width and height of the drawable portion of the frame will match the size entered by the user.

• The program will draw a series of circles, each with a random location, radius, and color.

• The lines in each circle will be drawn in clockwise order, starting at the 12 o’clock position.

• The delay time entered by the user will determine the amount of delay between successive lines.

Page 101: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

101

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Program Behavior

• A snapshot of the frame after four circles have been drawn and the fifth is currently being drawn:

Page 102: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

102

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Program Behavior

• The frame after 10 circles have been drawn:

Page 103: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

103

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the RandomCircles Program

• An initial design for the program:1. Prompt the user to enter the frame size, the number of circles, and the delay.

2. Create a frame titled “Random Circles” and set its size to the value entered by the user.

3. Get the frame’s graphics context.

4. Draw a series of circles.

• A helper method named readInt will read each input value as a string and convert it to an int value.

• If NumberFormatException occurs, readInt will ask the user to re-enter the input value.

Page 104: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

104

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the RandomCircles Program

• Steps 2 and 3 are easy. Step 4 can be written as a method named DrawAllCircles. This method will contain a loop that executes once for each circle.

• A design for the loop body:1. Select a color in which each component is a random number between 0 and 255.

2. Select a random radius between 0 and frameSize/4, where frameSize is the frame size chosen by the user; call this radius.

3. Select a random center whose coordinates fall between radius and frameSize - radius.

4. Display a circle with the chosen center, radius, and color.

Page 105: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

105

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the RandomCircles Program

• Steps 1 through 3 are easy to do with the help of the Math.random method. Step 4 will be done by a helper method named DrawOneCircle.

• After setting the drawing color, DrawOneCircle will need to display 360 lines inside a loop.

• A design for the loop body:1. Compute the coordinates of the current line’s endpoint (the one at the edge of the circle).

2. Draw a line from the center of the circle to the edge of the circle.

3. Call repaint to update the screen.

4. Pause between successive lines.

Page 106: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

106

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

Design of the RandomCircles Program

• Computing the coordinates of the current line’s endpoint will require converting the line’s angle from degrees to radians, then using the Math.cos and Math.sin methods.

• Calling Thread.sleep will produce a delay between successive lines.

Page 107: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

107

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

RandomCircles.java// Program name: RandomCircles// Author: K. N. King// Written: 1998-05-19// Revised: 1999-04-25// // Draws randomly generated circles within a frame. The user// will enter the size of the frame, the number of circles to// be drawn, and the "delay time," which controls the drawing// speed. If the user enters a non-integer value, the program// will ask the user to re-enter the input. The location,// radius, and color of each circle is chosen randomly. The// radius is at most the frame size divided by 4. The random// location is chosen so that the circle is entirely contained// within the frame. Each circle is drawn as 360 lines. The// lines are drawn in clockwise order, starting from the 12// o'clock position. The delay time entered by the user// determines the amount of delay between successive lines.

Page 108: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

108

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

import java.awt.*;import jpb.*;

public class RandomCircles { private static DrawableFrame df; // Frame in which // circles are displayed private static Graphics g; // Graphics context for // frame private static int delay; // Delay between lines // (milliseconds) private static int numCircles; // Number of circles private static int frameSize; // Size of frame (pixels)

public static void main(String[] args) { // Prompt the user to enter the frame size, the number of // circles, and the delay frameSize = readInt("Enter frame size: "); numCircles = readInt("Enter number of circles: "); delay = readInt("Enter delay (in milliseconds): ");

Page 109: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

109

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

// Create a frame titled "Random Circles" and set its // size to the value entered by the user df = new DrawableFrame("Random Circles"); df.show(); df.setSize(frameSize, frameSize);

// Get the frame's graphics context g = df.getGraphicsContext();

// Draw a series of circles drawAllCircles(); }

Page 110: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

110

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

/////////////////////////////////////////////////////////// // NAME: drawAllCircles // BEHAVIOR: Draws a series of circles with random // colors, sizes, and locations. The number of // circles is determined by numCircles. The // radius of each circle is at most // frameSize/4. The center of each circle is // chosen so that the circle stays within the // frame. // PARAMETERS: None // RETURNS: Nothing /////////////////////////////////////////////////////////// private static void drawAllCircles() { for (int i = 0; i < numCircles; i++) { // Select a color in which each component is a random // number between 0 and 255 int red = (int) (256 * Math.random()); int green = (int) (256 * Math.random()); int blue = (int) (256 * Math.random()); Color color = new Color(red, green, blue);

Page 111: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

111

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

// Select a random radius between 0 and frameSize/4 int radius = (int) ((frameSize / 4 + 1) * Math.random());

// Select a random center with each coordinate between // radius and frameSize - radius. int xCenter = radius + (int) ((frameSize - 2 * radius + 1) * Math.random()); int yCenter = radius + (int) ((frameSize - 2 * radius + 1) * Math.random());

// Display a circle with the chosen center, radius, and // color drawOneCircle(xCenter, yCenter, radius, color); } }

Page 112: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

112

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

/////////////////////////////////////////////////////////// // NAME: drawOneCircle // BEHAVIOR: Draws one circle with the specified center, // radius, and color. The circle is drawn as a // series of 360 lines meeting at the center of // the circle. Lines are drawn clockwise, // starting from the 12 o'clock position. The // amount of delay between successive lines // (measured in milliseconds) is determined by // the value of the delay variable. // PARAMETERS: xCenter - x coordinate of center of circle // yCenter - y coordinate of center of circle // radius - radius of circle in pixels // color - color of circle // RETURNS: Nothing /////////////////////////////////////////////////////////// private static void drawOneCircle(int xCenter, int yCenter, int radius, Color color) { // Change drawing color g.setColor(color);

Page 113: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

113

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

for (int angle = 360; angle >= 1; angle--) { // Compute coordinates of current line's endpoint (the // one at the edge of the circle) double radians = (angle + 90) * Math.PI / 180; int xEdge = xCenter + (int) (Math.cos(radians) * radius); int yEdge = yCenter - (int) (Math.sin(radians) * radius);

// Draw line from center of circle to edge of circle g.drawLine(xCenter, yCenter, xEdge, yEdge);

// Call repaint to update the screen df.repaint();

// Pause between successive lines try { Thread.sleep(delay); } catch (InterruptedException e) {} } }

Page 114: Chapter 9: Primitive Types Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 9 Primitive Types

Copyright © 2000 W. W. Norton & Company.All rights reserved.

114

Chapter 9: Primitive Types

JavaJava ProgrammingProgrammingFROM THE BEGINNINGFROM THE BEGINNING

/////////////////////////////////////////////////////////// // NAME: readInt // BEHAVIOR: Prompts the user to enter a number, reads // the user's input, and converts it to int // form. If the conversion fails, the user is // prompted to re-enter the input. The process // repeats indefinitely until the user's input // represents a valid integer. // PARAMETERS: prompt - the prompt to be displayed // RETURNS: User's input after conversion to int /////////////////////////////////////////////////////////// private static int readInt(String prompt) { while (true) { SimpleIO.prompt(prompt); String userInput = SimpleIO.readLine(); try { return Integer.parseInt(userInput); } catch (NumberFormatException e) { System.out.println("Not an integer; try again."); } } }}