Lecture 14, CS 302-7 and 8, February 22
i. Due Friday night
ii. Input validation - .hasNextInt, .next
iii. Reminder – documentation
iv. Reminder – don’t try to do ‘too much’
i. Declare/initialize
1. int[] numbers=new int[3];
2. int[] numbers; numbers=new int[3];
3. int[] numbers={1,2,3};
ii. Use
1. numbers[1] (‘numbers sub 1’) -> 2
2. numbers[0] -> 1
iii. Characteristics
1. Type – int, double, String, etc
2. Length - numbers.length
i. int[] data = {…};
For( int value : data)
{
// body
}
i. Note –these require traversing the entire array
i. Start at beginning, search until we find data
ii. Note – search can abort early, once we find answer
i. Faster than linear
ii. Condition – array is sorted
iii. Real-world example – searching through a dictionary.
1. Start in middle, then half each way, etc.
i. Remove – start at one above, copy down
ii. Insert – start from end, copy up, then put item in
1. Harder than remove
i. Int[] numbers2=Arrays.copyOf(numbers, numbers.length);
i. Public static void main(String[] args)
i. Modularity
ii. Maintainability/readability
iii. Reduce redundancy
i. Math.pow, Arrays.copyOf
i. Inputs – parameters/arguments or parameter variables/parameter values
1. can be multiple
2. declare in method header
ii. Outputs – return values
1. only one
2. specify with return statement
3. declare in method header
iii. example - Math.pow(2,3) //2^3
i. Declaring :
public static <return type> <name>(<paramVars>) {
//body
//return <returnVal>;
}
ii. Calling:
<ClassName>.<methodName>(<paramVals>); //Returns <returnVal>