02 Slide
02 Slide
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
1
Lecture 1 Review
• Create, compile, and run a Java program.
• Programming Errors
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
2
Motivations
• How to solve practical problems programmatically
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
3
Writing a program
Writing a program involves designing a
strategy for solving the problem and then
using a programming language to implement
that strategy.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
4
Algorithm
• An algorithm describes how a problem is solved by
listing the actions that need to be taken and the order
of their execution.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
5
Introducing Programming with
an Example
• Computing the Area of a Circle
• The algorithm for calculating the area of a
circle
1. Read in the circle’s radius.
2. Compute the area using the following formula:
area = radius * radius * pi;
3. Display the result.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
6
Writing a program
Every Java program begins with a class
definition in which the keyword class is
followed by the class name.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
7
Writing a program
Every Java program must have a main method
where program execution begins.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
9
Writing a program
Choose descriptive names for variables:
– radius for radius, and area for area
To let the compiler know what radius and area are, specify
their data types.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
10
Writing a program
Primitive data types:
– Integers,
– Real numbers,
– Characters,
– Boolean types.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
11
Writing a program
Use the keyword double to declare a floating-
point variable.
ComputeArea Note: Clicking the green button displays the source code
with interactive animation. You can also run the code in
a browser. Internet connection is needed for this button.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
14
animation
// Assign a radius
radius = 20;
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
15
animation
// Assign a radius
radius = 20;
allocate memory
// Compute area for area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
16
animation
// Compute area
area = radius * radius * 3.14159;
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
17
animation
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
18
animation
// Compute area
area = radius * radius * 3.14159; print a message to the
console
// Display results
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
19
Reading Input from the Console
• Reading input from the console enables the
program to accept input from the user.
• Use the Scanner class for console input
• Java uses System.out to refer to the standard
output device and System.in to the standard input
device.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
20
Reading Input from the Console
1. Create a Scanner object
Scanner input = new Scanner(System.in);
ComputeAreaWithConsoleInput
ComputeAverage
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
21
ComputeAreaWithConsoleInput.java
import java.util.Scanner; // Scanner is in the java.util package
// Compute area
double area = radius * radius * 3.14159;
// Display result
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
22
ComputeAreaWithConsoleInput.java
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
23
ComputeAverage.java
import java.util.Scanner; // Scanner is in the java.util package
// Compute average
double average = (number1 + number2 + number3) / 3;
// Display result
System.out.println("The average of " + number1 + " " + number2 +
" " + number3 + " is " + average);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
24
ComputeAverage.java
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
25
import statement
• specific import specifies a single class in the
import statement.
import java.util.Scanner;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
26
print vs. println
• println moves to the beginning of the next
line after displaying the string.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
27
Identifiers
• Identifiers are the names that identify the elements such as classes, methods,
and variables in a program.
• An identifier must start with a letter, an underscore (_), or a dollar sign ($). It
cannot start with a digit.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
28
Variables
• Variables are used to represent values that may be
changed in the program.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
29
Variables
// Compute the first area
radius = 1.0;
area = radius * radius * 3.14159;
System.out.println("The area is “ +
area + " for radius "+radius);
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
31
Assignment Statements
• An assignment statement designates a value for a
variable.
• The equal sign (=) is used as the assignment operator .
• Syntax:
variable = expression;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
32
Assignment Statements
x = 1; // Assign 1 to x;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
33
Declaring and Initializing
in One Step
Syntax:
datatype variable = expression;
int x;
int x = 1;
x = 1;
34
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
Expression
An expression represents a computation involving values,
variables, and operators that, taking them together, evaluates
to a value.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
35
Expression
• The following statement is true
System.out.println(x = 1);
x = 1;
System.out.println(x);
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
36
Named Constants
• A named constant is an identifier that represents a
permanent value.
• Syntax:
final datatype CONSTANTNAME = VALUE;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
37
ComputeAreaWithConstant.java
public class ComputeAreaWithConstant {
public static void main(String[] args) {
final double PI = 3.14159; // Declare a constant
// Compute area
double area = radius * radius * PI;
// Display result
System.out.println("The area for the circle of radius " +
radius + " is " + area);
}
}
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
38
Naming Conventions
Choose meaningful and descriptive names.
Variables and method names:
– Use lowercase. If the name consists of several
words, concatenate all in one, use lowercase
for the first word, and capitalize the first letter
of each subsequent word in the name.
– For example, the variables radius and area,
and the method computeArea.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
39
Naming Conventions, cont.
Class names:
– Capitalize the first letter of each word in
the name. For example, the class name
ComputeArea.
Constants:
– Capitalize all letters in constants, and use
underscores to connect words. For
example, the constant PI and
MAX_VALUE
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
40
Numerical Data Types
Name Range Storage Size
Positive range:
4.9E-324 to 1.7976931348623157E+308
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
41
Reading Numbers from the Keyboard
Scanner input = new Scanner(System.in);
int value = input.nextInt();
Method Description
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
42
Numeric Operators
+ Addition 34 + 1 35
% Remainder 20 % 3 2
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
43
Division
• When both operands of a division are
integers, the result of the division is the
quotient and the fractional part is truncated.
5 / 2 yields an integer 2,
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
45
Remainder Operator
Suppose today is Saturday and you and your friends are
going to meet in 10 days. What day is in 10 days? You can
find that day is Tuesday using the following expression:
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
46
Problem: Displaying Time
Write a program that obtains minutes and
remaining seconds from seconds.
DisplayTime
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
47
NOTE
Calculations involving floating-point numbers are
approximated because these numbers are not stored
with complete accuracy. For example,
System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
displays 0.5000000000000001, not 0.5, and
System.out.println(1.0 - 0.9);
displays 0.09999999999999998, not 0.1. Integers are
stored precisely. Therefore, calculations with integers
yield a precise integer result.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
48
Exponent Operations
System.out.println(Math.pow(2, 3));
// Displays 8.0
System.out.println(Math.pow(4, 0.5));
// Displays 2.0
System.out.println(Math.pow(2.5, 2));
// Displays 6.25
System.out.println(Math.pow(2.5, -2));
// Displays 0.16
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
49
Number Literals
A literal is a constant value that appears directly
in the program. For example, 34, 1,000,000, and
5.0 are literals in the following statements:
int i = 34;
long x = 1000000;
double d = 5.0;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
50
Integer Literals
• An integer literal can be assigned to an integer variable
as long as it can fit into the variable.
• A compilation error would occur if the literal were too
large for the variable to hold.
• For example, the statement byte b = 1000 would cause
a compilation error, because 1000 cannot be stored in a
variable of the byte type.
– (Note that the range of byte value is from -128 to 127.)
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
51
Integer Literals
• An integer literal is assumed to be of the int
type, whose value is between
-231 (-2147483648) to 231–1 (2147483647).
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
53
Floating-Point Literals
• 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 and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
54
double vs. float
The double type values are more accurate than the
float type values. For example,
System.out.println("1.0 / 3.0 is " + 1.0 / 3.0);
16 digits
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
55
Scientific Notation
• Floating-point literals can also be specified in
scientific notation.
• For example,
– 1.23456e+2, same as 1.23456e2, is equivalent to
123.456, and
– 1.23456e-2 is equivalent to 0.0123456.
– E (or e) represents an exponent and it can be either in
lowercase or uppercase.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
56
JShell
JShell is a command line interactive tool
introduced in Java 9. JShell enables you to type a
single Java statement and get it executed to see the
result right away without having to write a
complete class. This feature is commonly known as
REPL (Read-Evaluate-Print Loop), which
evaluates expressions and executes statements as
they are entered and shows the result immediately.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
57
Arithmetic Expressions
3 + 4 x 10( y - 5)( a + b + c ) 4 9+ x
- + 9( + )
5 x x y
is translated to
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
58
How to Evaluate an Expression
Though Java has its own way to evaluate an
expression behind the scene, the result of a Java
expression and its corresponding arithmetic
expression are the same. Therefore, you can safely
apply the arithmetic rule for evaluating a Java
expression. 3 + 4 * 4 + 5 * (4 + 3) - 1
(1) inside parentheses first
3 + 4 * 4 + 5 * 7 – 1
(2) multiplication
3 + 16 + 5 * 7 – 1
(3) multiplication
3 + 16 + 35 – 1
(4) addition
19 + 35 – 1
(5) addition
54 - 1
(6) subtraction
53
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
59
Problem: Converting Temperatures
Write a program that converts a Fahrenheit degree
to Celsius using the formula:
celsius = ( 95 )( fahrenheit - 32)
FahrenheitToCelsius
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
60
Problem: Displaying Current Time
Write a program that displays current time in GMT in the
format hour:minute:second such as 1:45:19.
The currentTimeMillis method in the System class returns
the current time in milliseconds since the midnight, January
1, 1970 GMT. (1970 was the year when the Unix operating
system was formally introduced.) You can use this method
to obtain the current time, and then compute the current
second, minute, and hour as follows.
Elapsed
time
Time ShowCurrentTime
Unix Epoch Current Time
01-01-1970 System.currentTimeMills()
00:00:00 GMT
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
61
Augmented Assignment Operators
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
62
Augmented Assignment Operators
• The augmented assignment operator is
performed last after all the other operators
in the expression are evaluated.
• For example,
x /= 4 + 5.5 * 1.5;
is same as
x = x / (4 + 5.5 * 1.5);
Cauition!
There are no spaces in the augmented assignment operators.
For example, + = should be +=.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
63
Example: Show the output of the
following code
double a = 6.5;
a += a + 1;
System.out.println(a);
a = 6;
a /= 2;
System.out.println(a);
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
64
Increment and
Decrement Operators
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
65
Increment and
Decrement Operators, cont.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
66
Increment and
Decrement Operators, cont.
Using increment and decrement operators makes
expressions short, but it also makes them complex and
difficult to read.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
67
Example: Show the output of the
following code
int x = 3;
int y = 4;
int k = x * y++;
int l = x * ++y;
System.out.println("x " + x);
System.out.println("y " + y);
System.out.println("k " + k);
System.out.println("l " + l);
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
68
Numeric Type Conversion
Consider the following statements:
byte i = 100;
long k = i * 3 + 4;
double d = i * 3.1 + k / 2;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
69
Conversion Rules
When performing a binary operation involving two
operands of different types, Java automatically
converts the operand based on the following rules:
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
70
Type Casting
• Casting is an operation that converts a value of
one data type into a value of another data type
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)
SalesTax
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
73
Software Development Process
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
74
Requirement Specification
A formal process that seeks to understand
Requirement
Specification
the problem and document in detail what
the software system needs to do. This
System phase involves close interaction between
Analysis
users and designers.
System
Design
Implementation
Testing
Implementation
System
Design
Implementation
Testing
System
Analysis Input, Process , Outp ut
System
Design
Implementation
Testing
Maintenance
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
78
Implementation
Requirement The process of translating the
Specification
system design into programs.
System Separate programs are written for
Analysis
each component and put to work
System together.
Design
Implementation
Testing
This phase requires the use of a
programming language like Java. Deployment
The implementation involves
coding, testing, and debugging. Maintenance
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
79
Testing
Requirement
Specification Ensures that the code meets the
requirements specification and
System
Analysis weeds out bugs.
System
Design
Implementation
Testing
An independent team of software
engineers not involved in the design Deployment
and implementation of the project
usually conducts such testing. Maintenance
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
80
Deployment
Requirement
Specification Deployment makes the project
available for use.
System
Analysis
System
Design
Implementation
Testing
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
81
Maintenance
Requirement
Specification Maintenance is concerned with
changing and improving the
System
Analysis product.
System
Design
Implementation
Testing
A software product must continue to
perform and improve in a changing Deployment
environment. This requires periodic
upgrades of the product to fix newly Maintenance
discovered bugs and incorporate changes.
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
82
Problem:
Computing Loan Payments
This program lets the user enter the interest
rate, number of years, and loan amount, and
computes monthly payment and total
payment.
loanAmount ´ monthlyInterestRate
monthlyPayment =
1- 1
(1 + monthlyInterestRate) numberOfYears´12
ComputeLoan
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
83
Common Errors and Pitfalls
Common Error 1: Undeclared/Uninitialized
Variables and Unused Variables
Common Error 2: Integer Overflow
Common Error 3: Round-off Errors
Common Error 4: Unintended Integer Division
Common Error 5: Redundant Input Objects
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
84
Common Error 1:
Undeclared/Uninitialized Variables
and Unused Variables
double interestRate = 0.05;
double interest = interestrate * 45;
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
85
Common Error 2: Integer Overflow
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
86
Common Error 3: Round-off Errors
System.out.println(1.0 - 0.1 - 0.1 - 0.1 - 0.1 - 0.1);
• displays 0.5000000000000001, not 0.5, and
System.out.println(1.0 - 0.9);
• displays 0.09999999999999998, not 0.1
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
87
Common Error 4: Unintended Integer
Division
int number1 = 1; int number1 = 1;
int number2 = 2; int number2 = 2;
double average = (number1 + number2) / 2; double average = (number1 + number2) / 2.0;
System.out.println(average); System.out.println(average);
(a) (b)
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
88
Common Pitfall 1: Redundant Input
Objects
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
89
Common Pitfall 1: Redundant Input
Objects (cont.)
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int v1 = input.nextInt();
System.out.print("Enter a double value: ");
double v2 = input.nextDouble();
Liang, Introduction to Java Programming and Data Structures, Twelfth Edition, Global Edition,
Copyright © 2022 Pearson Education Ltd. All rights reserved.
90