Uploaded by 邱育翔

- Java Slides - summary 1

advertisement
Introduction to Computers,
Programs, and Java
Chapter Summary One
Ch 01 – Ch 04
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
1
Why Java?
The answer is that Java enables users to develop and
deploy applications on the Internet for servers, desktop
computers, and small hand-held devices. The future of
computing is being profoundly influenced by the Internet,
and Java promises to remain a big part of that future. Java
is the Internet programming language.
Java
is a general purpose programming language.
Java
is the Internet programming language.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
2
Java, Web, and Beyond
 Java
can be used to develop standalone
applications.
 Java can be used to develop applications
running from a browser.
 Java can also be used to develop applications
for hand-held devices.
 Java can be used to develop applications for
Web servers.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
3
Java’s History

James Gosling and Sun Microsystems

Oak

Java, May 20, 1995, Sun World

HotJava
– The first Java-enabled Web browser

Early History Website:
http://www.java.com/en/javahistory/index.jsp
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
4
JDK Editions

Java Standard Edition (J2SE)
– J2SE can be used to develop client-side standalone
applications or applets.

Java Enterprise Edition (J2EE)
– J2EE can be used to develop server-side applications
such as Java servlets, Java ServerPages, and Java
ServerFaces.

Java Micro Edition (J2ME)
– J2ME can be used to develop applications for mobile
devices such as cell phones.
This book uses J2SE to introduce Java programming.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
5
Creating, Compiling, and
Running Programs
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
6
Compiling Java Source Code
You can port a source program to any machine with appropriate
compilers. The source program must be recompiled, however, because
the object program can only run on a specific machine. Nowadays
computers are networked to work together. Java was designed to run
object programs on any platform. With Java, you write the program
once, and compile the source program into a special type of object
code, known as bytecode. The bytecode can then run on any computer
with a Java Virtual Machine, as shown below. Java Virtual Machine
is a software that interprets Java bytecode.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
7
animation
Trace a Program Execution
Enter main method
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
8
animation
Trace a Program Execution
Execute statement
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
9
animation
Trace a Program Execution
// This program prints Welcome to Java!
public class Welcome {
public static void main(String[] args) {
System.out.println("Welcome to Java!");
}
}
print a message to the
console
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
10
Reading Input from the Console
1. Create a Scanner object
Scanner input = new Scanner(System.in);
2. Use the method nextDouble() to obtain to a double
value. For example,
System.out.print("Enter a double value: ");
Scanner input = new Scanner(System.in);
double d = input.nextDouble();
Animation
ComputeAreaWithConsoleInput
Run
ComputeAverage
Run
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
11
Named Constants
final datatype CONSTANTNAME = VALUE;
final double PI = 3.14159;
final int SIZE = 3;
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
12
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
63
63
–2 to 2 – 1
(i.e., -9223372036854775808 to 9223372036854775807)
64-bit signed
float
Negative range:
-3.4028235E+38 to -1.4E-45
Positive range:
1.4E-45 to 3.4028235E+38
32-bit IEEE 754
double
Negative range:
-1.7976931348623157E+308 to -4.9E-324
64-bit IEEE 754
Positive range:
4.9E-324 to 1.7976931348623157E+308
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
13
Reading Numbers from the Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method
Description
nextByte()
reads an integer of the byte type.
nextShort()
reads an integer of the short type.
nextInt()
reads an integer of the int type.
nextLong()
reads an integer of the long type.
nextFloat()
reads a number of the float type.
nextDouble() reads a number of the double type.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
14
Floating-Point Literals
Floating-point literals are written with a decimal
point. By default, a floating-point literal is
treated as a double type value. For example, 5.0
is considered a double value, not a float value. You
can make a number a float by appending the letter f
or F, and make a number a double by appending the
letter d or D. For example, you can use 100.2f or
100.2F for a float number, and 100.2d or 100.2D
for a double number.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
15
Conversion Rules
When performing a binary operation involving two
operands of different types, Java automatically
converts the operand based on the following rules:
a+b
1. If one of the operands is double, the other is
converted into double.
2. Otherwise, if one of the operands is float, the other is
converted into float.
3. Otherwise, if one of the operands is long, the other is
converted into long.
4. Otherwise, both operands are converted into int.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
16
Type Casting
Implicit casting
double d = 3; (type widening)
Explicit casting
int i = (int)3.0; (type narrowing)
int i = (int)3.9; (Fraction part is truncated)
What is wrong?
int x = 5 / 2.0;
range increases
byte, short, int, long, float, double
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
17
Software Development Process
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
18
Requirement Specification
A formal process that seeks to understand
the problem and document in detail what
the software system needs to do. This
phase involves close interaction between
users and designers.
Requirement
Specification
System
Analysis
System
Design
Implementation
Testing
Most of the examples in this book are simple,
and their requirements are clearly stated. In
the real world, however, problems are not
well defined. You need to study a problem
carefully to identify its requirements.
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
19
System Analysis
Requirement
Specification
System
Analysis
System
Design
Seeks to analyze the business
process in terms of data flow, and
to identify the system’s input and
output.
Implementation
Part of the analysis entails modeling
the system’s behavior. The model is
intended to capture the essential
elements of the system and to define
services to the system.
Testing
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
20
System Design
The process of designing the
system’s components.
Requirement
Specification
System
Analysis
System
Design
Implementation
Testing
This phase involves the use of many levels
of abstraction to decompose the problem into
manageable components, identify classes and
interfaces, and establish relationships among
the classes and interfaces.
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
21
IPO
Requirement
Specification
System
Analysis
Input, Process , Outp ut
System
Design
Implementation
Testing
The essence of system analysis and design is input,
process, and output. This is called IPO.
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
22
Implementation
The process of translating the
system design into programs.
Separate programs are written for
each component and put to work
together.
Requirement
Specification
System
Analysis
System
Design
Implementation
This phase requires the use of a
programming language like Java.
The implementation involves
coding, testing, and debugging.
Testing
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
23
Testing
Requirement
Specification
Ensures that the code meets the
requirements specification and
weeds out bugs.
System
Analysis
System
Design
Implementation
An independent team of software
engineers not involved in the design
and implementation of the project
usually conducts such testing.
Testing
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
24
Deployment
Requirement
Specification
Deployment makes the project
available for use.
System
Analysis
System
Design
Implementation
Testing
For a Java program, this means
installing it on a desktop or on the
Web.
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
25
Maintenance
Requirement
Specification
Maintenance is concerned with
changing and improving the
product.
System
Analysis
System
Design
Implementation
Testing
A software product must continue to
perform and improve in a changing
environment. This requires periodic
upgrades of the product to fix newly
discovered bugs and incorporate changes.
Deployment
Maintenance
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
26
One-way if Statements
if (boolean-expression) {
statement(s);
}
if (radius >= 0) {
area = radius * radius * PI;
System.out.println("The area"
+ " for the circle of radius "
+ radius + " is " + area);
}
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
27
The Two-way if Statement
if (boolean-expression) {
statement(s)-for-the-true-case;
}
else {
statement(s)-for-the-false-case;
}
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
28
switch Statements
switch (status) {
case 0: compute taxes for single filers;
break;
case 1: compute taxes for married file jointly;
break;
case 2: compute taxes for married file separately;
break;
case 3: compute taxes for head of household;
break;
default: System.out.println("Errors: invalid status");
System.exit(1);
}
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
29
switch Statement Flow Chart
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
30
Mathematical Functions
Java provides many useful methods in the Math
class for performing common mathematical
functions.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
31
The Math Class

Class constants:
– PI
–E

Class methods:
– Trigonometric Methods
– Exponent Methods
– Rounding Methods
– min, max, abs, and random Methods
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
32
Trigonometric Methods

sin(double a)

cos(double a)

tan(double a)

acos(double a)

asin(double a)

atan(double a)
Examples:
Math.sin(0) returns 0.0
Math.sin(Math.PI / 6)
returns 0.5
Math.sin(Math.PI / 2)
returns 1.0
Math.cos(0) returns 1.0
Math.cos(Math.PI / 6)
returns 0.866
Math.cos(Math.PI / 2)
returns 0
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
33
Exponent Methods

exp(double a)
Returns e raised to the power of a.

log(double a)
Returns the natural logarithm of a.

log10(double a)
Returns the 10-based logarithm of
a.

pow(double a, double b)
Returns a raised to the power of b.

sqrt(double a)
Returns the square root of a.
Examples:
Math.exp(1) returns 2.71
Math.log(2.71) returns 1.0
Math.pow(2, 3) returns 8.0
Math.pow(3, 2) returns 9.0
Math.pow(3.5, 2.5) returns
22.91765
Math.sqrt(4) returns 2.0
Math.sqrt(10.5) returns 3.24
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
34
Rounding Methods

double ceil(double x)
x rounded up to its nearest integer. This integer is returned as a double
value.

double floor(double x)
x is rounded down to its nearest integer. This integer is returned as a
double value.

double rint(double x)
x is rounded to its nearest integer. If x is equally close to two integers,
the even one is returned as a double.

int round(float x)
Return (int)Math.floor(x+0.5).

long round(double x)
Return (long)Math.floor(x+0.5).
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
35
Rounding Methods Examples
Math.ceil(2.1) returns 3.0
Math.ceil(2.0) returns 2.0
Math.ceil(-2.0) returns –2.0
Math.ceil(-2.1) returns -2.0
Math.floor(2.1) returns 2.0
Math.floor(2.0) returns 2.0
Math.floor(-2.0) returns –2.0
Math.floor(-2.1) returns -3.0
Math.rint(2.1) returns 2.0
Math.rint(2.0) returns 2.0
Math.rint(-2.0) returns –2.0
Math.rint(-2.1) returns -2.0
Math.rint(2.5) returns 2.0
Math.rint(-2.5) returns -2.0
Math.round(2.6f) returns 3
Math.round(2.0) returns 2
Math.round(-2.0f) returns -2
Math.round(-2.6) returns -3
static methods
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
36
min, max, and abs

max(a, b)and min(a, b)
Returns the maximum or
minimum of two parameters.

abs(a)
Returns the absolute value of the
parameter.

random()
Returns a random double value
in the range [0.0, 1.0).
Examples:
Math.max(2, 3) returns 3
Math.max(2.5, 3) returns
3.0
Math.min(2.5, 3.6)
returns 2.5
Math.abs(-2) returns 2
Math.abs(-2.1) returns
2.1
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
37
The random Method
Generates a random double value greater than or equal to 0.0 and less
than 1.0 (0 <= Math.random() < 1.0).
Examples:
(int)(Math.random() * 10)
Returns a random integer
between 0 and 9.
50 + (int)(Math.random() * 50)
Returns a random integer
between 50 and 99.
In general,
a + Math.random() * b
Returns a random number between
a and a + b, excluding a + b.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
38
Case Study: Computing Angles
of a Triangle
x2, y2
c
A = acos((a * a - b * b - c * c) / (-2 * b * c))
B = acos((b * b - a * a - c * c) / (-2 * a * c))
C = acos((c * c - b * b - a * a) / (-2 * a * b))
a
B
C
x3, y3
A
x1, y1
b
Write a program that prompts the user to enter the
x- and y-coordinates of the three corner points in a
triangle and then displays the triangle’s angles.
ComputeAngles
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
Run
39
Character Data Type
char letter = 'A'; (ASCII)
char numChar = '4'; (ASCII)
Four hexadecimal digits = 2 bytes
char letter = '\u0041'; (Unicode)
char numChar = '\u0034'; (Unicode)
NOTE: The increment and decrement operators can also be used
on char variables to get the next or preceding Unicode character.
For example, the following statements display character b.
char ch = 'a';
System.out.println(++ch);
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
40
Unicode Format
Java characters use Unicode, a 16-bit encoding scheme
established by the Unicode Consortium to support the
interchange, processing, and display of written texts in the
world’s diverse languages. Unicode takes two bytes,
preceded by \u, expressed in four hexadecimal numbers
that run from '\u0000' to '\uFFFF'. So, Unicode can
represent 65535 + 1 characters.
Unicode \u03b1 \u03b2 \u03b3 for three Greek
letters
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
41
ASCII Code for Commonly Used
Characters
Characters
Code Value in Decimal
Unicode Value
'0' to '9'
'A' to 'Z'
'a' to 'z'
48 to 57
65 to 90
97 to 122
\u0030 to \u0039
\u0041 to \u005A
\u0061 to \u007A
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
42
Escape Sequences for Special Characters
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
43
Appendix B: ASCII Character Set
ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
44
ASCII Character Set, cont.
ASCII Character Set is a subset of the Unicode from \u0000 to \u007f
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
45
Casting between char and
Numeric Types
int i = 'a'; // Same as int i = (int)'a';
char c = 97; // Same as char c = (char)97;
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
46
Comparing and Testing
Characters
if (ch >= 'A' && ch <= 'Z')
System.out.println(ch + " is an uppercase letter");
else if (ch >= 'a' && ch <= 'z')
System.out.println(ch + " is a lowercase letter");
else if (ch >= '0' && ch <= '9')
System.out.println(ch + " is a numeric character");
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
47
Methods in the Character Class
Method
Description
isDigit(ch)
Returns true if the specified character is a digit.
isLetter(ch)
Returns true if the specified character is a letter.
isLetterOfDigit(ch)
Returns true if the specified character is a letter or digit.
isLowerCase(ch)
Returns true if the specified character is a lowercase letter.
isUpperCase(ch)
Returns true if the specified character is an uppercase letter.
toLowerCase(ch)
Returns the lowercase of the specified character.
toUpperCase(ch)
Returns the uppercase of the specified character.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
48
The String Type
The char type only represents one character. To represent a string
of characters, use the data type called String. For example,
String message = "Welcome to Java";
String is a “class”
String is actually a predefined class in the Java library just like the
System class and Scanner class. The String type is not a primitive
type. It is known as a reference type. Any Java class can be used
as a reference type for a variable. Reference data types will be
thoroughly discussed in Chapter 9, “Objects and Classes.” For the
time being, you just need to know how to declare a String
variable, how to assign a string to the variable, how to concatenate
strings, and to perform simple operations for strings.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
49
Simple Methods for String Objects
Method
Description
length()
charAt(index)
Returns the number of characters in this string.
concat(s1)
toUpperCase()
Returns a new string that concatenates this string with string s1.
toLowerCase()
trim()
Returns a new string with all letters in lowercase.
Returns the character at the specified index from this string.
Returns a new string with all letters in uppercase.
Returns a new string with whitespace characters trimmed on both sides.
All are instance methods, depending on objects created.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
50
Simple Methods for String Objects
Strings are objects in Java. The methods in the preceding table can
only be invoked from a specific string instance. For this reason, these
methods are called instance methods. A non-instance method is
called a static method. A static method can be invoked without
using an object. All the methods defined in the Math class are static
methods. They are not tied to a specific object instance. The syntax to
invoke an instance method is
referenceVariable.methodName(arguments).
Non-instance method (static method)
Math.rint(2.5)
Not for a particular object
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
51
Getting String Length
String message = "Welcome to Java";
System.out.println("The length of " + message + " is "
+ message.length());
Instance Variable
Variable.methodName(arguments)
message.length()
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
52
Getting Characters from a String
String message = "Welcome to Java";
System.out.println("The first character in message is "
+ message.charAt(0));
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
53
Converting Strings
"Welcome".toLowerCase() returns a new string, welcome.
"Welcome".toUpperCase() returns a new string, WELCOME.
" Welcome ".trim() returns a new string, Welcome.
"Welcome".toLowerCase()
message.charAt(0)
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
54
String Concatenation
String s3 = s1.concat(s2); or String s3 = s1 + s2;
// Three strings are concatenated
String message = "Welcome " + "to " + "Java";
// String Chapter is concatenated with number 2
String s = "Chapter" + 2; // s becomes “Chapter2”
// String Supplement is concatenated with character B
String s1 = "Supplement" + 'B'; // s1 becomes
“SupplementB”
Type conversion before concatenation
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
55
Reading a String from the Console
Scanner input = new Scanner(System.in);
System.out.print("Enter three words separated by spaces: ");
String s1 = input.next();
String s2 = input.next();
String s3 = input.next();
System.out.println("s1 is " + s1);
System.out.println("s2 is " + s2);
System.out.println("s3 is " + s3);
input.next() knows the next input should be read as a string.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
56
Reading a Character from the
Console
Scanner input = new Scanner(System.in);
System.out.print("Enter a character: ");
String s = input.nextLine(); // read till a carriage return
char ch = s.charAt(0);
System.out.println("The character entered is " + ch);
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
57
Comparing Strings
Method
Description
equals(s1)
Returns true if this string is equal to string s1.
equalsIgnoreCase(s1)
compareTo(s1)
Returns true if this string is equal to string s1; it is case insensitive.
Returns an integer greater than 0, equal to 0, or less than 0 to indicate whether
this string is greater than, equal to, or less than s1.
compareToIgnoreCase(s1) Same as compareTo except that the comparison is case insensitive.
Returns true if this string starts with the specified prefix.
startsWith(prefix)
Returns true if this string ends with the specified suffix.
endsWith(suffix)
OrderTwoCities
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
Run
58
Obtaining Substrings
Method
Description
substring(beginIndex)
Returns this string’s substring that begins with the character at the specified
beginIndex and extends to the end of the string, as shown in Figure 4.2.
substring(beginIndex,
endIndex)
Returns this string’s substring that begins at the specified beginIndex and
extends to the character at index endIndex – 1, as shown in Figure 9.6.
Note that the character at endIndex is not part of the substring.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
59
Finding a Character or a Substring
in a String
Method
Description
indexOf(ch)
Returns the index of the first occurrence of ch in the string. Returns -1 if not
matched.
indexOf(ch, fromIndex)
Returns the index of the first occurrence of ch after fromIndex in the string.
Returns -1 if not matched.
indexOf(s)
Returns the index of the first occurrence of string s in this string. Returns -1 if
not matched.
indexOf(s, fromIndex)
Returns the index of the first occurrence of string s in this string after
fromIndex. Returns -1 if not matched.
lastIndexOf(ch)
Returns the index of the last occurrence of ch in the string. Returns -1 if not
matched.
lastIndexOf(ch,
fromIndex)
lastIndexOf(s)
Returns the index of the last occurrence of ch before fromIndex in this
string. Returns -1 if not matched.
lastIndexOf(s,
fromIndex)
Returns the index of the last occurrence of string s before fromIndex.
Returns -1 if not matched.
Returns the index of the last occurrence of string s. Returns -1 if not matched.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
60
Finding a Character or a Substring
3
in a String
int k = s.indexOf(' ');
String firstName = s.substring(0, k);
String lastName = s.substring(k + 1);
0 ~ 3-1
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
61
Conversion between Strings and
Numbers
“123”
“12.34”
int intValue = Integer.parseInt(intString);
double doubleValue = Double.parseDouble(doubleString);
String s = number + "";
number is converted to String before “+“
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
62
Formatting Output
Use the printf statement.
System.out.printf(format, items);
Where format is a string that may consist of substrings and
format specifiers. A format specifier specifies how an item
should be displayed. An item may be a numeric value,
character, boolean value, or a string. Each specifier begins
with a percent sign.
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
63
Frequently-Used Specifiers
Specifier Output
Example
%b
a boolean value
true or false
%c
a character
'a'
%d
a decimal integer
200
%f
a floating-point number
45.460000
%e
a number in standard scientific notation
4.556000e+01
%s
a string
"Java is cool"
int count = 5;
items
double amount = 45.56;
System.out.printf("count is %d and amount is %f", count, amount);
display
count is 5 and amount is 45.560000
Liang, Introduction to Java Programming, Tenth Edition, Global Edition. © Pearson Education Limited 2015
64
Download