Chapter 12. 12.1. Squares on the tic-tac-toe board; cards in a deck or in a player’s hand; videos a customer has rented; 12.2. a. The final value of i is 10. b. The loop computes the sum of the integers from 1 through 9. 12.3. This loop does not terminate. The variable i always contains an odd value, and never equals 10. It appears to be intended to compute the sum of the odd numbers from 1 through 9.
12.4.
/**
* The sum of the integers 1 through n, inclusive.
* precondition: n >= 1
*/
public int nSum (int n) {
int i = 1;
int sum = 0;
while (i <= n) {
sum = sum + i; // accumulator - sometimes written sum += i ;
i = i + 1;
}
return sum;
}
12.5.
/**
* The sum of the odd integers 1 through n, inclusive.
* precondition: n >= 1
*/
public int oddSum (int n) {
int i = 1;
int sum = 0;
while (i <= n) {
sum = sum + i; // same as sum += i ;
i = i + 2; // same as i += 2;
}
return sum;
}
12.6. a. 10 times. b. 11 times. c. 11 times. d. 6 times. e. 10 times. f. 9 times. g. 5 times. h. 10 times. i. Does not terminate. j. Does not terminate.
12.7. Change to make the list an array
private Student [] enrollees
Constructor:
public Course (int numberStudents ) {
Student enrollees = new Student[numberStudents];
size = 0;
// see below concerning size variable
}
/**
* Enroll the specified Student in this Course.
*/
public void enroll (Student s){
int i = 0;
while (enrollees[i] != null) {
i = i + 1;
}
enrollees[i] = s;
}
Other (less computationally expensive)
techiques are to keep track of how many have been enrolled.
Maintain an instance variable size.
/**
* precondition: size has been initialized and is set to the
number of elements in the array at the current time.
/*
public void enroll (Student s){
enrollees[size] = s;
size = size + 1;
}
What if already enrolled?
Two possibilities are: require that the Student not already be
enrolled in the Course; or do nothing
if the Student is already enrolled. E.g.,
/**
* Enroll the specified Student in this Course.
* prerequisite: s is not enrolled in this Course.
*/
public void enroll (Student s){
enrollees[size] = s;
size = size + 1;
}
/**
* Enroll the specified Student in this Course. Do nothing if the
* Student is already enrolled in this Course.
*/
public void enroll (Student s){
booloean there = false;
for (int i = 0, i < size, i++) {
if enrolles[i] = s {
there = true;
i = size;
}
}
if there = false {
enrollees[size] = s;
size = size + 1;
}
}
/**
* Drop the specified Student from this Course.
* prerequisite: s is enrolled in this Course.
*/
public void drop (Student s){
remove(enrollees, indexof(enrolles, s) );
}
/** precondition: element is the same type as array
* postcondition: returns index of element if present; if not present, returns -1
/*
public int indexof(Object [] array, Object element) {
int location = -1;
for (int i = 0; i < array.length -1; i++){
if array[i] = element {
location = i;
i = array.length; //jump out of for loop
}
}
return location;
}
This could very well depend on implementation desires.
Maybe it is OK in the implementation to have null values inbetween.
Maybe not. Discuss.
Suppose do not want null inbetween:
public void remove(Object [] array, int index) {
// move all after index forward by one location
for (i = index; i < size -1; i++)
array[i] = array[i+1];
}
Discuss switching and the use of temp switch two locations first and second what if I do: first = second; second = first; instead we use a temp variable: temp = first; first = second; second = temp;