Week 5 is a review week.
- Again, what is a method
- Cover: define a method, arguments, return types, calling methods, getting back values
- Background again on arrays
- Strings, review last week + talk about char arrays
- create arrays
- min value of array
- average from an array
- copy arrays
- insert into array
- delete from array
You can’t use any built-in string functions for the following problems except length() and charAt().
For procedures that take an index and/or length be sure to have boundary checking and test with bad values (e.g. negative or too long). Clip values that are outside the valid range.
- What is a string
- char arrays
- How to count the vowels in a string? (write a method)
- Where is a given char in a string? (write a method)
- Count occurrences of a given char in a String
- write a substring function manually
- substring with method signature: (array, startIndex, length)
- compare two strings char by char (essentially we implement .equals)
- count how many words are in a string (ignore all whitespace)
- Easy: split on whitespace, then trim
- Hard: for each char increment count if word char seen, and record if you are inside a word char or not.
System.out.println( "Value[ " + ( intIndex + 1 ) + " ] = " + aintValues[ intIndex ] );
You can use String.format() to format strings.
User %s to sub strings, %d for numbers
System.out.println(String.format("Value[%d] = %d", intIndex + 1, aintValues[intIndex]));
Why is this not the right way to copy an array? How should you do it differently?
public static int[] AddValueToEndOfArray(int aintOldValues[], int intValueToAdd)
{
int NewSize = 0;
int aintNewValues [];
// Allocate space.
NewSize = aintOldValues.length + 1;
aintNewValues = new int[ NewSize ];
// Copy Values.
aintNewValues [0] = aintOldValues[0];
aintNewValues [1] = aintOldValues[1];
aintNewValues [2] = aintOldValues[2];
aintNewValues [3] = aintOldValues[3];
// Add to end.
aintNewValues [4] = intValueToAdd;
return aintNewValues;
}