Tutorial 6

advertisement
Leopold-Franzens
University of Innsbruck
Institute of
University Innsbruck, Institute of Computer Science
José Garcı́a, Benedikt Hupfauf, Nelia Lasierra, Doris Silbernagl
Computer Science
Research Group
Quality Engineering
Master Thesis
Programming
Methodology
Adoption of Tutorial 6
Decision Deferring Techniques
in Plan-driven
Software
Projects
Exercise
1 (Dynamic
Binding
– 3 Points)
Take a look at
following
classes. What is the output of Test.java? Explain in detail
A the
Controlled
Experiment
why the program produces this output. If you want, you can describe the different
scenarios once, and only state which scenario applies to each output.
Author
1
2
3
4
5
Listing 1: Moe.java
Supervisor
class
Moe Michael
{
Bakk.techn.
Schier
Dr.Barbara Weber
public void print ( Moe p ) {
System . out
. println ( " Moe 1\ n " );
April 21, 2008
}
}
Listing 2: Larry.java
1
2
3
4
5
6
7
8
class Larry extends Moe {
public void print ( Moe p ) {
System . out . println ( " Larry 1\ n " );
}
public void print ( Larry l ) {
System . out . println ( " Larry 2\ n " );
}
}
Listing 3: Curly.java
1
2
3
4
5
6
7
class Curly extends Larry {
public void print ( Moe p ) {
System . out . println ( " Curly 1\ n " );
}
public void print ( Larry l ) {
System . out . println ( " Curly 2\ n " );
}
Programming Methodology – Tutorial 6
public void print ( Curly b ) {
System . out . println ( " Curly 3\ n " );
}
8
9
10
11
}
Listing 4: Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public class Test {
public static void main ( String [] args ) {
Larry stooge1 = new Curly ();
Moe stooge2 = new Larry ();
Moe stooge3 = new Curly ();
Curly stooge4 = new Curly ();
Larry stooge5 = new Larry ();
Object stooge6 = new Curly ();
Object stooge7 = new Moe ();
Object stooge8 = new Larry ();
stooge1 . print ( new Moe ());
(( Curly ) stooge1 ). print ( new Larry ());
(( Larry ) stooge2 ). print ( new Moe ());
stooge2 . print ( new Curly ());
stooge3 . print ( new Curly ());
stooge3 . print ( new Moe ());
stooge3 . print ( new Larry ());
(( Curly ) stooge3 ). print ( new Larry ());
(( Curly ) stooge3 ). print ( new Curly ());
stooge4 . print ( new Curly ());
stooge4 . print ( new Moe ());
stooge4 . print ( new Larry ());
stooge5 . print ( new Curly ());
stooge5 . print ( new Larry ());
stooge5 . print ( new Moe ());
(( Curly ) stooge6 ). print ( new Larry ());
(( Moe ) stooge7 ). print ( new Curly ());
(( Larry ) stooge8 ). print ( new Moe ());
(( Moe ) stooge6 ). print ( new Moe ());
(( Larry ) stooge6 ). print ( new Larry ());
(( Moe ) stooge8 ). print ( new Moe ());
}
}
Exercise 2 (FIFO and LIFO – 4 Points)
Implement an abstract base-class for lists. Your implementation should be based on a
simple array that you resize, and not on any classes from the Collection package. The
class does not need to be generic; use an array of type Object. Your class should support
at least the following methods: a constructor, push(), get(), isEmpty(), and length().
Create two sub-classes FIFO (first in, first out) and LIFO (first in, last out) that are based
on your abstract super-class and implement the functionality according to the name.
Which of the methods/attributes are part of the abstract base-class, which are part of
the sub-classes?
2
Programming Methodology – Tutorial 6
Exercise 3 (Generics: Basics – 5 Points)
a) Write a static, generic method that returns the minimum of an array (argue for
the signature that you chose!). How is this implementation different from a nongeneric implementation? What are the advantages/disadvantages? Hint: Have a
look at the Comparable interface. (1 point)
b) Implement a simple static, generic method to print the elements of an array; use
System.out.print(), the return value is void. Provide a second version of this method
that allows you to print only a subset of the array’s elements. The generic interface
Filter<T> that is provided as part of the exercise should be used for this. The
interface only specifies a single method, public boolean filter(T t), which returns
true if an element should be printed.
Next, implement a class RangeFilter, which implements the interface Filter<Integer>
and allows to select integers within the range [min, max]. Use a constructor to set
min and max. Create an instance of this filter and print all positive integers from
an array. (3 points)
Example: A range filter of [0, 100] applied to the Array {1, 2, -4, 123, 1} should
print the values 1, 2 and 1.
c) Consider the following example: you have implemented a filter according to the
interface Filter<Integer>. You want to use this filter in a more general implementation, which requires you to implement the interface Filter<Number>. Integer
is a subclass of Number, so the statement Number n = new Integer(); is correct. Does
this hold for generics too, i.e., is Filter<Number> n = new Filter<Integer>(); a correct
statement? (1 point)
Important Note: Enter your estimated points in u2l and upload solutions to u2l (.txt,
.java or .pdf). The deadline for the upload is 04.05.2014, 23:59.
3
Download