All answers that we give appears at the end of this handout
Loops
Given below are four tasks, which can be solved with a loop an initialization. With each, we have given the postcondition and the loop invariant. Write the loop (and initialization). The loop must be understood using the invariant and the four parts of the checklist for understanding loops. For example, if your loop doesn't truthify the invariant, you lose 25% of the grade on this question. Remember the checklist:
1. The task is to store in x the largest value in b[j..k].
Postcondition: the largest of b[j..k] is in
x
Invariant: the largest of b[j..h] is in x
2. The task is to store in x the largest value in b[j..k].
Postcondition: the largest of b[j..k] is in
x
Invariant: the largest of b[j..h-1] is in x
3. The task is to store in x the largest value in b[j..k].
Postcondition: the largest of b[j..k] is in
x
Invariant: the largest of b[h..k] is in x
3. The task is to store in x the largest value in b[j..k].
Postcondition: the largest of b[j..k] is in x
Invariant: the largest of b[h+1..k] is in x
Also, turn to Lesson page 7-1 in PLive, click on the Labs icon, and do all the questions in all the labs. Especially those having to do with developing a loop from an invariant.
Arrays
1. Turn to Lesson 8 of ProgramLive, click the Labs icon, and do the two labs (Using arrays and Timing execution).
2. Turn to Lesson 8 of ProgramLive, and click the Homework icon. Do all the exercises in assignment PGL-1, Simple array exercises. There are 18 exercises dealing with arrays there. That should be enough for anyone. Don't just do them on paper; do them in DrJava. Make sure you check out each exercise using suitable test cases, so that you are positive it gives the right results. Then, you have really learned the material and you don't need us to tell you whether it is right or wrong.
Note that, with arrays, you are expected to know all the technical details of arrays that are given on Lesson pages 8-1 and 8-2.
Executing method calls
Find your own method calls and execute them by hand. Look at assignments you have written during the semester. They have lots of method calls.
Classes and subclasses
You are expected to know the terminology and to be able to write classes and subclasses. Here is one possible question. Consider a class Animal:
public class Animal {
private String kind; // kind
of animal --e.g. "cat"
private String name; // the asnimal's name
// Constructor: an instance with kind k and name s
public Animal(String k, String s)
{kind= k; name= s;}
// = description of this Animal
public String toString() {
return "Name: " +
s + ", kind " + k);
}
}
Here's an expression that creates an instance: new Animal("cat", "softy");
Write a subclass Lion of Animal that represents lions and also gives their age. The only instance variable of the subclass should be an int variable that contains the age. The constructor should have two parameters --the age and the name. There should be a getter method for obtaining the age and a getter method for obtaining the name. Override method toString so that a call on it returns a description with all three properties --kind, name, age. This method should contain a call on the constructor of the superclass.
ANSWERS
1. h= j; x= b[j];
while (h != k) {
h= h+1;
if (b[h]
> x)
x= b[h];
}
2. h= j+1; x= b[j];
while (h <= k) {
if (b[h] > x)
x= b[h];
h= h+1;
}
3. h= k; x= b[k];
while (j != h) {
h= h-1;
if (b[h] > x)
x= b[h];
}
3. h= k-1; x= b[k];
while (j <= h) {
if (b[h] > x)
x= b[h];
h= h-1;
}
public class Lion extends Animal {
private int age; //the animal's
age
// Constructor: a lion with name n and age a
public Lion(String n, int a) {
super("lion", n);
age= a;
}
// = age of this lion
public int getAge() {
return age;
}
// = String representation of this lion
public String toString() {
return super.toString()
+ ", age " + age;
}
}