int[] elevations = new int[5];
String[] professors = {"Horstmann", "Heller", "O'Brien"} ;
elevations[2] = 5270; professors[1] = "Wesley"; things[i] = 10; //changes the ith element to 10
[0,0,5270,0,0] ["Horstmann", "Wesley", "O'Brien]
int e = elevations[2]; String p = professors[0]; int value = things[i]; // value contains whatever is at element i
e => 5270 p => "Horstmann"
elevations.length => 5 professors.length => 3
Record your participation in Piazza clicker question Lesson17 Q1
Assume the array primes has been initialized like thisint[] primes = { 2, 3, 5, 7, 11 };What does it contain after executing the following loop?
for (int i = 0; i < primes.length; i++) { primes[i]++; }
Note: primes[i]
accesess the ith element of the primes array and primes[i]++
increments the ith element of the array
for (int i = 0; i < professors.length; i++) { System.out.println(professors[i]); }
for (String p: professors) { System.out.println(p); }
Horstmann Wesley O'Brien
Record your participation in Piazza clicker question Lesson17 Q2
Assume the array primes has been initialized like thisint[] primes = { 2, 3, 5, 7, 11 };What does it contain after executing the following loop?
for (int p: primes) { p++; }
Record your participation in Piazza clicker question Lesson17 Q3
Complete the method body for the getCombination method in the class Lottery. It returns an array of n random numbers less than 100 that might then be used in buying a lottery ticket. Create a Bluej project and copy in this code. You will need to instantiate a Random object. Test the method in the workbenchimport java.util.Random; public class Lottery { public int[] getCombination(int n) { //create random object //create an array that can hold n ints //for every cell in the array, fill it with a random number } //possibly other methods }
Record your participation in Piazza clicker question Lesson17 Q4
Here is some starter code (arrays.zip). It contains a class IntArrayProcessor
and an IntArrayProcessorTester
. You can download it, unzip it, and open the Bluej project. We are going to practice working with array algorithms
getMax
to return the largest element in IntArrayProcessor
Record your participation in Piazza clicker question Lesson17 Q5
evenCount
to the IntArrayProcessor
class. evenCount
finds and returns the number of even integers in the array
Record your participation in Piazza clicker question Lesson17 Q6
commaSeparatedList
in IntArrayProcessor
which returns a String listing the integers in the array. The integers are listed separated with a ", " (That is Record your participation in Piazza clicker question Lesson17 Q7
IntArrayProcessor
to swap the elements at index1 and index2
public void swap(int index1, int index2)
IntArrayProcessorTester
to test the method.Record your participation in Piazza clicker question Lesson17 Q8
Here is a starter project for working with partially filled arrays