University of Toronto
Department of Computer Science
CSC324 - Principles of Programming Languages
Spring 1998 John Mylopoulos
Programming in Java: Tutorial Notes 6
prepared by Diane Horton
(1) Designing an "interface" for a student class
(not an "interface" as a keyword in the technical Java sense)
Eg:
class Student {
public Student (int n, String s) {
// Record the fact that the student passed a course
// with the given mark.
public void passCourse (int mark) {
// Return the average mark among the courses
that the
// student passed.
public float average() {
}
(2) A little main class to test out Student
- Don't even need to know Student's implementation yet!
(3) Student class's implementation
(4) Designing the interface for a ListOfInts class
to meet needs of Student class
- This will intermingle with (3)
- Can finish Student class's implementation without knowing how
ListOfInts will be implemented
(5) ListOfInt's implementation
- a chance to show how arrays work.
The code
--------
class ListOfInts {
private int [] theList;
private int numElements;
private static final int maxListSize = 40;
public ListOfInts() {
theList = new int[maxListSize];
numElements = 0;
}
// Precondition: numElements < maxListSize
public void addInt (int mark) {
numElements++;
theList[numElements-1] = mark; // Why -1 ?
}
public float average () {
int sum = 0;
for (int i=0; i