Download Slide 1

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
5/24/2017
1
• Declaring/initializing an array, page 408
<type>[] <name> = new <type>[<length>];
• Example:
int[] numbers = new int[10];
subscript 0
value 0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
• The length can be any integer expression:
int x = 2 * 3 + 1;
int[] data = new int[x % 5 + 2];
2
• Arrays are very commonly used with for loops
to access each element
• This is a space problem: 1st element, 2nd element,
… etc, compared with the time sequence
problem we mentioned earlier (1st iteration, 2nd
iteration, 3rd iteration, …)
• Solution: 1st iteration handles 1st element, 2nd
iteration handles 2nd element, …., etc
5/24/2017
3
• Methods can accept as many parameters as you like, p286.
– When the method is called, it must be passed values for each of its
parameters.
• Multiple parameters declaration syntax:
public static void <name> (<type> <name>,
<type> <name>, ..., <type> <name>) {
<statement(s)>;
}
• Multiple parameters call syntax:
<name>(<expression>, <expression>, ..., <expression>);
5/24/2017
4
•
Declaring a method that returns a value (p298):
public static <type> <name>(<parameters>) {
<statement(s)>;
}
•
Returning a value from a method:
return <expression>;
•
Example:
// Returns the given number cubed (to the third power).
public static int cube(int number) {
return number * number * number;
}
5/24/2017
5
• This is a space & timing problem: to find the
redundancy of a certain part of program running
at different time.
• Compared with the timing problem in loop: 1st,
2nd, 3rd … iterations
• Compared with the space problem in arrays: 1st,
2nd, 3rd … elements
• 1st, 2nd, 3rd … appearances of the same/similar
procedure in your program!
5/24/2017
6
`
• summing the values in an array
public static int sum (______________){
int result=0;
for(int x= 0; x<____________; x++)
result+=n[x];
return result;
}
7
public static int sum (______________){
int result=0;
n?
for(int x= 0; x<____________; x++) result+=n[x];
return result;
}
8
Declaring/initializing an array, page 408
<type>[] <name> = new <type>[<length>];
public static int sum (__int [ ] n__){
int result=0;
for(int x= 0; x<____________; x++) result+=n[x];
return result;
}
9
public static int sum (__int [ ] n__){
0+n[0]
…+n[1]
…+n[2]
…
…+n[?}
int result=0;
for(int x= 0; x<____________; x++) result+=n[x];
return result;
}
10
public static int sum (__int [ ] n__){
int result=0;
for(int x= 0; x<__n.length_; x++) result+=n[x];
return result;
}
11
• finding the highest value in an array
public static int highest (int [] n ){
int result=_____________;
for(int x= ________; x<____________; x++)
if (n[x] > result) result = n[x];
return result;
}
12
Each possible position: 0, 1, 2, …, ?
public static int highest (int [] n ){
int result=_____________;
for(int x= ________; x<____________; x++)
if (n[x] > result) result = n[x];
return result;
}
13
Initialization?
public static int highest (int [] n ){
int result=_____________;
for(int x= __0___; x<__n.length___; x++)
if (n[x] > result) result = n[x];
return result;
}
14
n[0] = -1, n[1]=-2,
n[2] = -3, ….
public static int highest (int [] n ){
int result=_____________;
for(int x= __0___; x<__n.length___; x++)
if (n[x] > result) result = n[x];
return result;
}
15
public static int highest (int [] n ){
int result=___n[0]__________;
for(int x= __0___; x<__n.length___; x++)
if (n[x] > result) result = n[x];
return result;
}
16
• Write a static method called isPrime that
returns whether an integer (passing by the
argument of the caller) is a prime number or
not. A number is "prime" if its factors are 1
and itself only. A "factor" is a number that
divides another number evenly.
17
public static
boolean
isPrime
(
int
x
) {
Finding the factor?
2, 3, 4, 5, …, n-1
}
18
Finding the factor?
2, 3, 4, 5, …, n-1
public static
boolean
isPrime
(
int
x
for ( factor =2 ;x%factor!=0; factor ++
{
) {
)
}
x % factor == 0
if (factor == x)
else
}
19
public static boolean isPrime (int x ){
int factor;
for(factor = 2; x%factor != 0; factor ++);
if (factor == x) return true;
else return false;
}
20
Related documents