Download 1 - CIS @ Temple University

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
CSC141 Computer Science I
Zhen Jiang
Dept. of Computer Science
West Chester University
West Chester, PA 19383
[email protected]
5/12/2017
1
Loop

Price is right.

Sample execution (click on this link to try)


http://www.cis.temple.edu/~jiang/price_is_right.exe
Each button in the above sample REPEAT …?
5/12/2017
2

While loop


5/12/2017
Format & Logic, page 197, Figure 4-1.
Sample, code 4-3, page 198.
3
<initialization>;
while (<test>) {
<body>;
}
5/12/2017
4

Do-while loop



5/12/2017
Format, page 208
Logic, page 209, Figure 4-6.
Sample, code 4-6, page 209.
5

5/12/2017
How does this differ from
the while loop?
 The controlled
<statement(s)> will
always execute the first
time, regardless of
whether the <test> is
true or false.
6

For loop



5/12/2017
Format, page 212, Figure 4-7.
Logic, page 212, Figure 4-8.
Sample, code 4-7, page 213.
7
for (<init>; <test>; <update>) {
<body>;
}
5/12/2017
8

Summary
Body first, and then
event change/update
5/12/2017
9

Development process

5/12/2017
http://www.cis.temple.edu/~jiang/LoopDevelopment.htm
10
5/12/2017
11
Controlling Number of
Loop Iterations

If the number of iterations is known before
the loop starts, the loop is called a count-
controlled loop.



5/12/2017
Counter =0, counter++, counter
<number
Counter = 1, counter++, counter
<=number
Use for loop for an easy development.
12
5/12/2017
13
5/12/2017
14

Code:
for (int i = 1; i <= 4; i++) {
System.out.println(i + " squared is " + (i * i));
}
Output:
1
2
3
4
squared
squared
squared
squared
5/12/2017
is
is
is
is
1
4
9
16
15

Code:
n=keyboard.nextInt(); // try 6!
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
System.out.print("*");
}
System.out.println();
}
Output:
******
******
******
******
******
******
5/12/2017
16

Code:
n=keyboard.nextInt(); // try 5!
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 10; j++) {
System.out.print((i * j) + " ");
}
System.out.println();
}
Output:
1
2
3
4
5
2 3 4 5 6 7 8 9 10
4 6 8 10 12 14 16 18 20
6 9 12 15 18 21 24 27 30
8 12 16 20 24 28 32 36 40
10 15 20 25 30 35 40 45 50
5/12/2017
17

Code:
n=keyboard.nextInt(); // try 6!
for (i = 1; i<=n; i++) System.out.print(“*”);
System.out.println(“”);
for (i = 1; i <= n-2; i++) {
System.out.print(“*”);
for (int j = 1; j <= n-2; j++)
System.out.print(“ ”);
System.out.println(“*”);
}
for (i = 1; i<=n; i++) System.out.print(“*”);
System.out.println(“”);
Output:
******
*
*
*
*
*
*
*
*
******
5/12/2017
18

Code:
n=keyboard.nextInt(); // try 6!
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
Output:
*
**
***
****
*****
******
5/12/2017
19

Code:
n=keyboard.nextInt(); // try 6!
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println();
}
Output:
1
22
333
4444
55555
666666
5/12/2017
20

Code:
n=keyboard.nextInt(); // try 5!
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= (n - i); j++) {
System.out.print(" ");
}
for (int k = 1; k <= i; k++) {
System.out.print(i);
}
System.out.println();
}
Output:
1
22
333
4444
55555
5/12/2017
21
Controlling Event of Loop
Iterations

Otherwise (unknown or unclear), the loop is
called a event-controlled loop.


Use a while loop or a do-while loop for an
easy checkpoint development.
Asking the user before each iteration if it is time to
end the loop is called the ask-before-iterating
technique.

5/12/2017
Appropriate status update (or event initializing) for
a sequence of iterations
22
5/12/2017
23

Finds and prints a number's first factor other than 1:
int n = keyboard.nextInt(); // try 91
int f = 2;
while (n % f != 0) {
f++;
}
System.out.println("First factor:" + f);
Sample run:
First factor:7
5/12/2017
24

Write a program that will repeatedly prompt the user to
type a number until the user types a non-negative number,
then square it.
Example log:
Type a non-negative integer: -5
Invalid number, try
Invalid number, try
Invalid number, try
Invalid number, try
11 squared is 121
5/12/2017
again:
again:
again:
again:
-1
-235
-87
11
25
System.out.print("Type a non-negative integer: ");
int n = keyboard.nextInt();
while (n < 0) {
System.out.print("Invalid number, try again: ");
n = keyboard.nextInt();
}
int square = n * n;
System.out.println(n + " squared is " + square);

Notice that the number variable had to be declared outside
the while loop in order to remain in scope.
5/12/2017
26

Write a class named DigitSum that reads an integer from
the user and prints the sum of the digits of that number.
You may assume that the number is non-negative.
Example:
Enter a nonnegative number:
29107
prints out 19 (i.e.,2+9+1+0+7 )

Hint: Use the % operator to extract the last digit of a
number. If we do this repeatedly, when should we stop?
5/12/2017
27
import java.util.Scanner;
public class DigitSum {
public static void main(String [] args) {
Scanner keyboard = new Scanner(System.in);
int n = keyboard.nextInt();
int sum = 0;
while (n > 0) {
sum += n % 10;
// add last digit to sum
n = n / 10;
// remove last digit
}
System.out.println(“sum = “ + sum);
}
}
5/12/2017
28

Write a program named CountFactors that reads in an
integer and displays its number of factors.

For example, if the user enters 60, CountFactors displays 12
because 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, and 60 are all factors
of 60.
Scanner keyboard = new Scanner(System.in);
int n = keyboard.nextInt();
int sum = 0, k = ?;
while (
) {
}
System.out.println(“sum = “ + sum);
5/12/2017
29

Exercise

population


TV purchase


http://www.cis.temple.edu/~jiang/tv563.exe
1+2+4+8+...


http://www.cis.temple.edu/~jiang/6billion.exe
http://www.cis.temple.edu/~jiang/1_2_4.exe
1+2+3+4+...+99

http://www.cis.temple.edu/~jiang/1to99.exe
5/12/2017
30
5/12/2017
31
Solution
5/12/2017
32

File writing, page 237-240





5/12/2017
Filename
PringWriter
Println
Close
Sample, code 4-17, page 237
33

Appending data to a (existing) file

5/12/2017
FileWriter (, true), page 240
34

File Reading, page 241-245





5/12/2017
File
Scanner
nextXXXX( )
close
Sample, code 4-18, page 242.
35

Detecting the end of a file



hasNext
Code 4-19, page 245.
Detecting the existence of a file


5/12/2017
exists
Code 4-21, page 249.
36

Random number generator


5/12/2017
randomNumbers.nextXXX( )
Sample, code 4-23, page 253.
37

Objects of the Random class generate pseudo-random
numbers.


Class Random is found in the java.util package.
import java.util.*;
The methods of a Random object
Method name
Description
nextInt()
returns a random integer
nextInt(max)
returns a random integer in the range [0, max)
in other words, from 0 to one less than max
nextDouble() returns a random real number in the range [0.0, 1.0)
5/12/2017
38
Related documents