Download presentation
Presentation is loading. Please wait.
1
2/24/2019
2
value 9 8 7 6 5 4 3 2 1 subscript Example:
Declaring/initializing an array, page 408 <type>[] <name> = new <type>[<length>]; Example: int[] numbers = new int[10]; The length can be any integer expression: int x = 2 * 3 + 1; int[] data = new int[x % 5 + 2]; value 9 8 7 6 5 4 3 2 1 subscript
3
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 2/24/2019
4
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>); 2/24/2019
5
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; 2/24/2019
6
Compared with the timing problem in loop: 1st, 2nd, 3rd … iterations
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! 2/24/2019
7
` summing the values in an array
public static int sum (______________){ int result=0; for(int x= 0; x<____________; x++) result+=n[x]; return result; }
8
public static int sum (______________){ int result=0; for(int x= 0; x<____________; x++) result+=n[x]; return result; } n?
9
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; }
10
0+n[0] …+n[1] …+n[2] … …+n[?} public static int sum (__int [ ] n__){ int result=0; for(int x= 0; x<____________; x++) result+=n[x]; return result; }
11
public static int sum (__int [ ] n__){ int result=0; for(int x= 0; x<__n.length_; x++) result+=n[x]; return result; }
12
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; }
13
public static int highest (int [] n ){ int result=_____________;
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; }
14
public static int highest (int [] n ){ int result=_____________;
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; }
15
public static int highest (int [] n ){ int result=_____________;
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; }
16
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; }
17
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.
18
x public static boolean isPrime ( ) int { } Finding the factor?
( ) int { Finding the factor? 2, 3, 4, 5, …, n-1 }
19
Counter-controlled loop? How many iterations? Event-control loop!
Finding the factor? 2, 3, 4, 5, …, n-1 public static boolean isPrime ( ) int x { factor =2 factor ++ for ( ;x%factor!=0; ) { } How f changes? 1st? 2nd? 3rd? Counter-controlled loop? How many iterations? Event-control loop! When to stop? What is the 1st factor? x % factor == 0 if (factor == x) else }
20
public static boolean isPrime (int x ){
int factor; for(factor = 2; x%factor != 0; factor ++); if (factor == x) return true; else return false; }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.