Chapter 23 Slides - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Chapter 23 Slides

Description:

Pre-defined Java Identifiers. User-defined Identifiers. Java Integer Data Types ... The Odometer and Integer Memory 1 = 1 = 32767 1 = 32768. 0 9 9 9 9 9 ... – PowerPoint PPT presentation

Number of Views:37
Avg rating:3.0/5.0
Slides: 57
Provided by: johns381
Category:

less

Transcript and Presenter's Notes

Title: Chapter 23 Slides


1
Exposure Java
Chapter 23 Slides
Review of Java Syntax
2
Java Keywords
? Reserved Words ? Pre-defined Java
Identifiers ? User-defined Identifiers
3
Java Integer Data Types
byte 1 -128 127 short 2 -32,768 32,76
7 int 4 -2,147,483,648 2,147,483,647 long
8 -9,223,372,036,854,775,808 9,223,372,036,854
,775,807
4
Java Integer Operations
25 10 35 // Addition 25 - 10 15 //
Subtraction 25 10 250 // Multiplication 25
/ 10 2 // Integer Division 25 10 5 //
Modulus (Remainder) Division
5
Integer Division Examples
12 / 3 4 12 / 4 3 12 / 5
2 12 / 8 1 12 / 12 1 12 / 15
0
6
Modulus (Remainder) Division Examples
12 3 0 12 4 0 12 5
2 12 8 4 12 12 0 12 15
12
7
Flashback To Elementary School Long Division
8
(No Transcript)
9
(No Transcript)
10
Memory Overflow Problems
Memory overflow is a situation where the assigned
value of a variable exceeds the allocated storage
space. The resulting value that is stored will
be inaccurate and can change from positive to
negative or negative to positive. Avoid memory
overflow problems by using a data type that can
handle the size of the assigned values. It is
important to save computer memory. However, do
not be so stingy with memory that overflow
problems occur.
11
Java Real Number Data Types
float 4 double 8
12
Java Real Number Operations
25.0 10.0 35.0 // Addition 25.0 - 10.0
15.0 // Division 25.0 10.0 250.0 //
Multiplication 25.0 / 10.0 2.5 // Real Number
Division
13
Java Unary Operators
k is the same as k k 1 k is the
same as k k 1 k-- is the same as k
k - 1 --k is the same as k k - 1
14
Proper Usage of Shortcuts
Proper Usage k System.out.println(k) --k S
ystem.out.println(k) Problematic
Usage System.out.println(k) System.out.print
ln(--k)
15
Binary Operator Shortcuts
No Shortcut Notation Shortcut Notation k k
5 k 5 k k - 5 k - 5 k k
5 k 5 k k / 5 k / 5 k k
5 k 5
16
Shortcut Warning
Do not combine shortcuts in one program
statement!!! num num num
17
String Concatenation
Concatenation is the appending of a second
string to a first string. "Hello" "World"
"HelloWorld" "Hello" " " "World" "Hello
World" "100" "200" "100200"
18
Mathematics and Java
Mathematics Java Source Code 5XY 5XY 4X
3Y 4X 3Y 6(A - B) 6(A - B) 5 5/7 7 A
B (A B)/(A - B) A - B AB (A B)/(X
Y) XY
19
APCS Examination Alert
The int, double, boolean and String data types
will be tested. The byte, short, long, float and
char data types will not be tested.
20
Program Flow
Program Flow follows the exact sequence of
listed program statements, unless directed
otherwise by a Java control structure.
21
Types of Control Structures
  • Program execution flow is controlled by three
    general types of control structures.
  • They are simple sequence, selection, and
    repetition. Java provides syntax, and special
    keywords for each one of the three control
    structures.
  • Before we look at actual Java source code
    required to implement control, let us first take
    a look at diagrams that explain each control
    structure.

22
Simple Sequence

Program Statement 1
23
Selection
Frequently, programs cannot follow a single,
simple sequence, path. Decisions need to be
made. Should the applicant be hired or not?
Does the employee get overtime pay? Which tax
bracket is the deduction to be computed from?
Selection is also called conditional branching
or decision making or sometimes even decision .
The program flow encounters a special condition.
The value of the condition determines if the
program flow will branch off from the main
program sequence. Three diagrams will be
shown. The first diagram shows one-way
selection, the second diagram shows two-way
selection, and the third one shows multiple-way
selection.
24
One-Way Selection
25
Two-Way Selection
26
Multi-Way Selection
27
Repetition

Program Statement
Program Statement
Program Statement
Condition
True
False
Program Statement
Program Statement
28
Conditional Statement Definition
A conditional statement is a program expression
that evaluates to true or false. Most
conditional statements require a relational
operator. All conditions must be placed inside
parentheses.
29
Relational Operators
30
Program Input
For the examples in this Chapter to be meaningful
we need the ability for the user to enter
information. Before we look at programs
demonstrating Control Structures, we will see 5
programs that introduce Command Line Input.
Java has MANY, MANY ways to do input, some of
which you will learn later this year. For
right now, Command Line Input is the simplest.
31
Command Line Argument Notes
All command line arguments are entered as
strings, separated by a space between multiple
arguments. If a space is meant to be part of the
entered string, such as "Hello there", the string
must be placed between quotes. Strings arguments
are entered into the args variable with an
index value starting with args0 for the first
argument, args1 for the second argument, and so
on. Integer.parseInt and Double.parseDouble
convert strings to integers and real numbers.
32
Main Method Arguments with JCreator
It IS possible to enter main method arguments
while using JCreator as well! Do the
following Step 1 Look for the toolbar at the
top of the JCreator IDE. Click Configure
followed by clicking on Options. Step 2 Click
JDK Tools on the left side of the Options
window. Step 3 Look for the Select Tool Type
window. If Run Application appears, skip to the
next step, if not pull down the window and
click on Run Application Step 4 Click on
ltdefaultgt. Make sure the ltdefaultgt bar becomes
shaded. Click on the Edit button. The Tool
Configuration Run Application window
appears Step 5 Click on the Parameters tab. The
Tool Configuration Run Application window
will change. Click a check-mark in the box
next to Prompt for main function arguments,
Step 6 Click OK twice. You are now ready to
execute your program.
33
JCreator Main Method Argument Notes
If you have done all of the steps on the previous
slide, the following window will show up whenever
you execute a Java application. NOTE Applets
dont have a main method, and hence no main
method arguments. You enter the exact same data
here that you would type after the file name at
the DOS prompt. An example of Java0605 data is
shown below. If this window shows up, and your
program needs no data, just click ltOKgt. Clicking
ltCancelgt will terminate program execution.
34
Two-Way Selection Syntax
Two-Way selection general syntax if
(condition true) execute first program
statement else // when condition is
false execute second program statement if
(GPA gt 90.0) System.out.println ( "Youre
an honor graduate") else
System.out.println ("Youre not an honor
graduate")
35
Multi-Way Selection Syntax
switch(selectionVariable) case
selectionConstant program statement break
case selectionConstant program
statement break default
program statement
switch(courseGrade) case A
points 4 break case B points
3 break case C points 2
break case D points 1 break
case F points 0 break
default points 0 The default
statement is used to handle the situation when a
proper match is not found. Frequently an error
message is used to indicate that no match was
found.
36
Fixed Repetition with ltforgt
for (Part1 Part2 Part3) loop body The
for loop has three distinct parts Part1
initializes the Loop Control Variable
(LCV). Part2 sets the exit condition for the
loop. Part3 determines how the LCV changes. for
(k 1 k lt 10 k) System.out.println("J
ava is 10 times more fun")
37
while Loop Syntax
initialize condition variable while(condition is
true) loop body alter condition
variable in loop body x 0 //
initialize condition variable while(x lt 10)
x // alter condition variable
System.out.println("x " x)
38
(No Transcript)
39
(No Transcript)
40
do...while Loop Syntax
initialize condition variable while(condition is
true) loop body alter condition
variable in loop body x 0 //
initialize condition variable do x
// alter condition variable System.out.println(
"x " x) while (x lt 10)
41
Worked-Out Exercises
The next section in this chapter will present a
variety of small programs. Each program has a
control structure that was introduced in this
chapter. Our concern will be with the output of
each program, and more importantly, develop some
methods to determine program output correctly,
which involves control structures.
42
public class Ex01 public static void main
(String args) for (int x 1 x lt 8
x2) System.out.println("x " x)

Ex01 Answer x Output 1 x 1 3 x 3 5 x
5 7 x 7
43
public class Ex02 public static void main
(String args) for (int x 1 x lt 8
x2) System.out.println("x " x)

Ex02 Answer x Output 1 x 1 3 x 3 5 x
5 7 x 7
44
public class Ex03 public static void main
(String args) for (int x 0 x lt
8 x2) System.out.println("x "
x)
Ex03 Answer x Output 0 x 0 2 x 2 4 x
4 6 x 6 8 x 8
45
public class Ex04 public static void main
(String args) int x 0 int y
0 for (x 1 x lt 5 x)
y System.out.println("y " y)

Ex04 Answer x y Output 0 0 1 1 2 2 3 3 4
4 5 5 y 5
46
public class Ex05 public static void main
(String args) int x 0 int y
0 for (x 1 x gt 1 x--) y
System.out.println("y " y)

Ex05 Answer x y Output 0 0 y 0
47
public class Ex06 public static void main
(String args) int x 0 int y
0 while (x lt 5) y
x y System.out.println("y
" y)
Ex06 Answer y x Output 0 0 1 1 2 2 3 3 4
4 5 5 y 5
48
public class Ex07 public static void main
(String args) int x 0 int y
0 while (x lt 10) y x
2 x y 3
System.out.println("y " y)
Ex07 Answer y x Output 0 0 2 5 7 10 y 7
49
public class Ex08 public static void main
(String args) int x 0 int y
0 while (x lt 10) y x
2 x
System.out.println("x " x)
System.out.println("y " y)
Ex08 Answer y x Output 0 0 0 1 2 2 4 3 6
4 8 5 10 6 12 7 14 8 16 9 18 10 x
10 y 18
50
public class Ex09 public static void main
(String args) int x 2 while
(x lt 10) if (x 2 0)
x2 else x
System.out.println("x " x)
Ex09 Answer x x 2 0 Output 2 true 4 true
6 true 8 true 10 x 10
51
public class Ex10 public static void main
(String args) int x 2 do
if (x 2 0) x2
else x while (x lt 10)
System.out.println("x " x)
Ex10 Answer x x 2 0 Output 2 true 4 true
6 true 8 true 10 true x 10
52
public class Ex11 public static void main
(String args) int x 10 int
y 20 do x y 2
y x - 2 while (x lt y)
System.out.println("x " x)
Ex11 Answer x y Output 10 20 22 20 x 22
53
public class Ex12 public static void main
(String args) int x 10 int
y 1 do if (x 2
0) x 5 else y 2
while (y lt x)
System.out.println("x " x)
Ex12 Answer x 2 0 x y Output 10 1 true 1
5 false 3 false 5 false 7 false 9 fa
lse 11 false 13 false 15 x 15
54
public class Ex13 public static void main
(String args) int x 1 int y
3 int z 5 while (z gt x y)
x y z y x z
z x - y System.out.println("x
" x) System.out.println("y " y)
System.out.println("z " z)
Ex13 Answer x y z Output 1 3 5 8 13 -5 x
8 y 13 z -5
55
public class Ex14 public static void main
(String args) int x 1 int y
2 int z 3 for (int k 1 k lt
10 k) x y z y
x z z x - y
System.out.println("x " x)
System.out.println("y " y)
System.out.println("z " z)
Ex14 Answer k x y z Output 1 2 3 1 5 8 -3 2
5 2 3 3 5 8 -3 4 5 2 3 5 5 8 -3 6 5 2 3
7 5 8 -3 8 5 2 3 9 5 8 -3 10 5 2 3 x
5 y 2 z 3
56
public class Ex15 public static void main
(String args) int x 168 int
y 90 int z 0 do
z x y if (z 0)
System.out.println("y " y) else
x y y z
while (z ! 0)
Ex15 Answer x y z Output 168 90 0 78 90 78
12 78 12 6 12 6 0 y 6
Write a Comment
User Comments (0)
About PowerShow.com