Building Java Programs Chapter 2 Primitive Data and Definite Loops.
Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
-
Upload
allyson-tenneson -
Category
Documents
-
view
234 -
download
0
Transcript of Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.
![Page 1: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/1.jpg)
1
Chapter 4
Ch 1 – Introduction toComputers and Java
Flow of ControlLoops
![Page 2: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/2.jpg)
2
Chapter 4
4.1 Java Loop Statements
4.2 Programming with Loops
4.3 Graphics Supplement
![Page 3: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/3.jpg)
3
4.1Java Loop
Statementsrepeat...repeat...repeat
![Page 4: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/4.jpg)
4
Repetition simplifies coding
![Page 5: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/5.jpg)
5
How would you sum up 1 through 100?
sum = 1 + 2 + 3 + ..... 100
sum = 1sum += 2
...sum += 100
![Page 6: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/6.jpg)
6
How about 1 through 100,00?
We need a more efficient wayto accomplish this task.
One that can scale up easily!
![Page 7: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/7.jpg)
7
The for Statement
sum = 0
num = 1
sum += num
num <= 100 num > 100
num++
end
![Page 8: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/8.jpg)
8
for Deconstructed<Sum of 1-100>
int sum = 0;
for (int num = 1; num <= 100; num++) { sum += num;}
Is this solution scalable?
int sum = 0;
for (int num = 1; num <= 10000; num++) { sum += num;}
initial step
condition step
update step
![Page 9: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/9.jpg)
9
Application Deconstructed<SumNumbers.java>
package sumnumbers;import java.util.Scanner;
public class SumNumbers { public static void main(String[] args) { int upperBound; int sumOfNumbers = 0; Scanner keyboard = new Scanner(System.in); System.out.println("I will add numbers from 1 to n."); System.out.print("Please enter n: "); upperBound = keyboard.nextInt();
![Page 10: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/10.jpg)
10
Application Deconstructed<SumNumbers.java>
for (int num = 1; num <= upperBound; num++) { sumOfNumbers += num; } //end for System.out.println(""); System.out.println("The sum of 1 to " + upperBound + " is " + sumOfNumbers); } //end main()} //end SumNumbers
![Page 11: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/11.jpg)
11
Recap: for
Use to repeat a fixed number of times
Excellent choice for counting problems
Very concise – all steps on one line
![Page 12: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/12.jpg)
12
Sometimes you may not wantto repeat an action at all
![Page 13: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/13.jpg)
13
Summing a list of numbers
First decide what ends the list
Let's say that -1 will signal the end of the input
Now, could the list be empty?
It could, if user enters -1 as the first number
![Page 14: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/14.jpg)
14
The while Statement
get num
sum += num
num != -1 num == -1
get num
end
![Page 15: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/15.jpg)
15
while Deconstructed<Sum of numeric list>
int num;int sumOfNumericList = 0;
System.out.print("Enter number of -1 to quit: ");num = keyboard.nextInt();
while(num != -1) { sumOfNumericList += num;
System.out.print("Enter number of -1 to quit: "); num = keyboard.nextInt();}
initial step
condition step
update step
![Page 16: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/16.jpg)
16
Application Deconstructed<SumNumericList.java>
package sumnumericlist;import java.util.Scanner;
public class SumNumericList { public static void main(String[] args) { int num; int sumOfNumericList = 0; Scanner keyboard = new Scanner(System.in);
System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); while (num != -1) { sumOfNumericList += num; System.out.print("Enter a number or -1 to quit: "); num = keyboard.nextInt(); } //end while
![Page 17: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/17.jpg)
17
Application Deconstructed<SumNumericList.java>
System.out.println("The sum of the numbers is " + sumOfNumericList); } //end main()} //end SumNumericList
![Page 18: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/18.jpg)
18
Recap: while
Use to repeat 0 or more times
Number of iterations is unknown before hand
Test before execution
![Page 19: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/19.jpg)
19
And sometimes you may wantto repeat an action at least once
![Page 20: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/20.jpg)
20
Sum numbers until you reach 100
Add next number if sum is less than 100
Stop when sum becomes greater than or equal to 100
![Page 21: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/21.jpg)
21
The do-while Statement
sum = 0
get num
sum < 100 sum >= 100
sum += num
end
![Page 22: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/22.jpg)
22
do-while Deconstructed<Sum to reach 100>
int num;int sumToReach = 0;
do { System.out.print("Enter number: "); num = keyboard.nextInt();
sumToReach += num;
} while(sumToReach < 100);
initial step
condition step
update step
![Page 23: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/23.jpg)
23
Application Deconstructed<SumToReach.java>
package sumtoreach;import java.util.Scanner;
public class SumToReach { public static void main(String[] args) { int num; int sumToReach = 0; Scanner keyboard = new Scanner(System.in); do { System.out.print("Enter a number: "); num = keyboard.nextInt(); sumToReach += num; } while(sumToReach < 100);
![Page 24: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/24.jpg)
24
Application Deconstructed<SumToReach.java>
System.out.println("The sum is " + sumToReach); } //end main()} //end SumToReach
![Page 25: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/25.jpg)
25
Recap: do-while
Use to repeat 1 or more times
Number of iterations is unknown before hand
Test after execution
![Page 26: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/26.jpg)
26
The for-each Statement
Allows you to cycle through an enumeration or collection, in its entirety
![Page 27: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/27.jpg)
27
for-each Deconstructed<Cycle through an enumeration>
enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES}
for ( Suit nextSuit : Suit.values() ) { System.out.print(nextSuit + " ");}
![Page 28: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/28.jpg)
28
Application Deconstructed<ForEachEnum.java>
package foreachenum;
public class ForEachEnum { enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES } public static void main(String[] args) { for ( Suit nextSuit : Suit.values() ) { System.out.print(nextSuit + " "); } //end for System.out.println(""); } //end main()} //end ForEachEnum
![Page 29: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/29.jpg)
29
4.2Programming
withLoops
![Page 30: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/30.jpg)
30
In Class Examples
for: Count downUser enters upper bound, lower boundand step down. Program outputs list.
while: Number reversalUser enters integer to reverse.Program displays reversed number.
![Page 31: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/31.jpg)
31
In Class Examples
while: Structure violation (implement in class)
int num = 0;int count = 0;
while (num != -1) { read number count number}
![Page 32: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/32.jpg)
32
Recap: Repetition
Strive to always choose the most appropriate loop for the task
Each loop has a purpose and a structure
Do not violate either one
![Page 33: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/33.jpg)
33
4.3Graphic
Supplement
![Page 34: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/34.jpg)
34
Applet Deconstructed<FallingLetters.java>
package fallingletters;import java.awt.Color;import javax.swing.JApplet;import java.awt.Graphics;
public class FallingLetters extends JApplet { public static final int LETTER_COUNT = 100; int windowWidth; int windowHeight; @Override public void paint(Graphics canvas) { windowWidth = this.getWidth(); windowHeight = this.getHeight();
![Page 35: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/35.jpg)
35
Applet Deconstructed<FallingLetters.java>
@Override public void paint(Graphics canvas) { windowWidth = this.getWidth(); windowHeight = this.getHeight(); int x; int y; char c; int red; int green; int blue;
![Page 36: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/36.jpg)
36
Applet Deconstructed<FallingLetters.java>
for (int letter = 1; letter <= LETTER_COUNT; letter++) { x = (int)(Math.random() * windowWidth); y = (int) (Math.random() * windowHeight); c = (char)( (int) (Math.random() * 26) + 65); red = (int)(Math.random() * 256); green = (int)(Math.random() * 256); blue = (int)(Math.random() * 256); canvas.setColor(new Color(red, green, blue)); canvas.drawString(String.valueOf(c), x, y); }// end for }// end paint()}// end FallingLetters
![Page 37: Chapter 4 Ch 1 – Introduction to Computers and Java Flow of Control Loops 1.](https://reader036.fdocuments.in/reader036/viewer/2022062417/5519c14a550346047c8b4678/html5/thumbnails/37.jpg)
37
Applet Deconstructed<FallingLetters.java>