Chapter 3
Chapter 3
Lecture 3
Arithmetic Operations, and Console
Input/Output
2
Revision: Basic Java Program Structure
•There are 5 components that we usually include in a Java
program, which are:
[ Documentation Statement(s) ]
[ Package Statement ]
[ Import Statement(s) ]
public class className {
public static void main(String[] args) {
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 3
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Objectives
• Write Arithmetic operations, expressions
containing variables and constants
• Write Java statements that accomplish
keyboard input,
screen output
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 4
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Arithmetic
Operations
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 5
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Arithmetic Operators
• When both operands are of the same type, the result is of that
type.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 6
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Arithmetic Operators
• Example
• If hoursWorked is an int to which the value 40 has been
assigned, and payRate is a double to which 8.25 has been
assigned then
hoursWorked * payRate
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 7
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Primitive Datatype and Common
Operations
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 8
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
The Division Operator
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 9
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
The mod Operator
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 10
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Arithmetic Expression
•Example
balance + (balance * rate)
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 11
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Arithmetic Expression
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 12
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Parentheses and Precedence
• Parentheses can be used to make the code clearer.
balance + (interestRate * balance)
• Examples:
(cost + tax) * discount
(cost + (tax * discount))
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 14
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Precedence Rules
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 15
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Precedence of Arithmetic Operators -
Examples
• What is the order of evaluation in the following
expressions?
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 16
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Sample of Arithmetic Expressions in
Java
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 17
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Shorthand Assignment Operator
• Assignment operators can be combined with arithmetic operators (including
-, *, /, and %, discussed later).
• The combination of an operator with the = used for assignment form is
called a shorthand assignment operator
• Example
amount = amount + 5;
can be written as
amount += 5;
yielding the same results.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 18
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Increment and Decrement Operators
• Used to increase (or decrease) the value of a variable by 1
• They cannot be applied to constants! Why?
count--;
--count;
count = count - 1;
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 20
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Increment and Decrement Operators in
Expressions
•after executing
int m = 4;
int result = 3 * (++m)
result has a value of 15 and m has a value of 5
•after executing
int m = 4;
int result = 3 * (m++)
result has a value of 12 and m has a value of 5
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 21
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Keyboard and
Screen I/O
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 22
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Scanner Class
This statement tells the compiler which Scanner class we are referring
to because there might be another class named Scanner
23
Scanner Class
• To initialize the Scanner class, we have to declare/define a Scanner
variable/object named keyboard and creates a Scanner that reads
input from System.in:
• Syntax:
Scanner keyboard= new Scanner (System.in);
Note:
• The new operator return a reference to the variable keyboard in the
memory
• you may name the Scanner variable any name you wish such as: in,
src, source, etc
24
Some Scanner Class Methods
25
Some Scanner Class Methods
26
Keyboard Input Demonstration
27
Keyboard Input Demonstration
Sample
screen
output
28
nextLine()Method Caution
• The nextLine() method reads
• The remainder of the current line,
• Even if it is empty.
• Example
int n;
String s1, s2;
Scanner keyboard = new Scanner (System.in);
n = keyboard.nextInt();
s1 = keyboard.nextLine();
s2 = keyboard.nextLine();
29
nextLine()Method Caution
• Why when we read a String followed by an int, everything works just fine.
But when we read an int followed by a String, something strange happens?
• nextInt returns the value 42. The program then waits for the user to enter
s1 and runs nextLine, which reads characters until it gets to a newline.
• But since the next character is already a newline, nextLine returns the
empty string "".
30
nextLine()Method Caution
• How can we solve this problem?
• We need an extra nextLine after nextInt :
int n;
String s1, s2;
Scanner keyboard = new Scanner (System.in);
n = keyboard.nextInt();
keyboard.nextLine(); //// read the newline
s1 = keyboard.nextLine();
s2 = keyboard.nextLine();
31
Keyboard Input Demonstration
32
Keyboard Input Demonstration
Sample
Screen
Output
33
Screen Output
•We've seen several examples of screen output already.
•Example:
System.out.println("Enter a whole number from 1
to 99.");
System.out.println(quarters + "quarters");
•Where:
• System is a standard class
• System.out is an object that is part of Java.
• println() is one of the methods available to the
34
Screen Output
• You can also use the println method to display the value of a
String variable
• Example:
String greeting = "Hello Programmers!";
System.out.println(greeting);
36
Screen Output
•Every invocation of println ends a line of output I.e. it
inserts a new line!
37
Escape Characters
38
Escape Characters
39
Printing Escape Characters Examples
System.out.println("abc\\def"); abc\def
new
System.out.println("new\nline");
line
40
Formatted Output with printf ( )
• printf works like the print method except it allows you to add
formatting instructions that specify things such as the number of
digits to include after a decimal point.
• Example,
double price = 19.5;
System.out.println("Price using println:" + price);
System.out.printf("Price using printf formatting:%6.2f", price);
This code outputs the following lines: The format specifier %6.2f
says to output a floating-point
Price using println:19.5 number in a field (number of
Price using printf formatting: 19.50 spaces) of width six (room for
six characters) and to show
exactly two digits after the
decimal point. So, 19.5 is
expressed as "19.50" in a
field of width six.
41
Common Format Specifiers
42
Common Format Specifiers Example
double price = 19.5;
int quantity = 2;
String item = "Widgets";
43
Q/Show the exact output produced by the following
println methods:
int counter = 5;
System.out.println("counter = " + counter++);
//counter = 5
System.out.println("counter = " + counter%2);
// counter = 0
44
Q/Show the exact output produced by the following
println methods:
System.out.println("hello "+"programmers!");
45
Summary
• You have learned about assignment
statements and expressions.
• You have learned about simple keyboard
input and screen output.
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 46
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Reading
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 47
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved
Questions?
JAVA: An Introduction to Problem Solving & Programming, 8th Ed. By Walter Savitch 48
ISBN 0134462033 © 2018 Pearson Education, Inc., Hoboken, NJ. All Rights Reserved