Sie sind auf Seite 1von 2

Introduction to Computer Science 2

Lab 2: Interfaces and Polymorphism

Learning Goals:

• To learn how to use interfaces.


• To learn how the principle of polymorphism allows us to write generic classes.
• To learn how to use the strategy pattern.

Exercise 1 (3 points)

A person has a name and height in centimeters. Use the implementation of the DataSet class for
the strategy pattern (see slides of lecture 2) to process a collection of Person objects. Display the
average height and the name of the tallest person.

Exercise 2 (3 points)

Define an interface Filter as follows:

public interface Filter {


boolean accept(Object x);
}

Modify the implementation of the DataSet class for the strategy pattern (see slides of lecture 2)
to use both a Measurer object and a Filter object. Only objects that the filter accepts should
be processed. Demonstrate your modification by having a data set process a collection of bank
accounts, filtering out all accounts with balances less than 1000 euros.

Exercise 3 (4 points)

Look up the definition of the standard Comparable interface in the Java API documentation. Modify
the DataSet class based on the Measurable interface (see slides of Lecture 2) to accept Comparable
objects. With this interface, it is not longer meaningful to compute the average. The DataSet class
should record the minimum and maximum data values. Test your modified DataSet class by adding
a number of String objects. The String class implements the Comparable interface.

1
Honor code, coding style, and deliverable:

Try to solve the exercises with what you already know. You are welcome to expand your program to
do extra things but they are not mandatory.
If you use some code found online (or anywhere else, e.g., stackoverflow), clearly state it (with
comment about where you found it) in your .java files. That means you should mention the author
and the www source you found it. Be sure that you read the terms of the licence for the code that you
are reusing. Most code that is found openly in the Internet has specific licences that you should not
violate.
Please follow the instructions about the input and output of the assignments. Do not use extra inputs,
or extra output, or different variations.
Plagiarism is not allowed! We will run sophisticated software that automatically detects similarities
on source code among students. All plagiarism incidents will be immediately reported to the Board
of Examiners!

Submission!

Claim your grade for this lab by submitting your java files to EleUM.
Ask your instructor in case there is a problem with your submission.
DO NOT SEND SUBMISSIONS VIA EMAIL
YOUR LAB WILL NOT GET GRADED!

Das könnte Ihnen auch gefallen