CS计算机代考程序代写 data structure Java SWEN20003

SWEN20003

Object Oriented Software Development

Workshop 8

May 3, 2021

Workshop

This week is all about generics.

1. Generics have a parameterised type, usually written as a capital letter (e.g. ). This is one kind of
polymorphism.

2. Classes and interfaces can be generic, such as Comparable.

3. Methods can also be generic without their containing class be generic.

4. The class ArrayList is a generic automatically-resizing array. We usually use this class instead of
arrays in practice.

5. The class HashMap is a dictionary mapping keys of type K to values of type V. Internally, it works
like a Hash table (the details of this will not be assessed).

Questions

1. Write an immutable class Pair that accepts two type parameters T and U, and simply stores two objects
(one of type T and one of type U). The class should take in the two objects in its constructor.

2. The Number abstract class is part of the Java standard library and is the superclass of all numeric primitive
wrapper classes such as Integer, Double, Long, e.t.c. Using this knowledge, create a new class Rectangle
that contains two instances of Pair, topLeft and bottomRight (with both values in the pairs the same
type), but allows only subclasses of Number to be stored.
Hint: use a bounded type parameter ().

3. Implement a method that processes an ArrayList, and returns a single String, which is a
comma-separated list of all the items in the input that contain only a single word.

4. Implement a method that takes a string as an argument and returns a HashMap con-
taining the number of times each character in the string appears.

5. A tree is a data structure that is made up of nodes. At the most basic level, each node holds a reference
to its children, and a value. A binary tree is a tree that has at most two children per node, conventionally
referred to as ‘left’ and ‘right’.

1

1

28

82 43

36

87

52

83

Figure 1: A simple binary tree

Implement a generic class that can represent a binary tree. (Note that you don’t have to keep the nodes
in sorted order for this problem—but it would be a good extension task!)

6. Write a custom generic class CycleList that is like a regular list, except it can be cycled : it should
define a method T next() that returns the next item in the list, starting at the 0th. When the end of
the list is reached, it should return to the beginning of the list. You should also define:

(a) void add(T value)

(b) boolean contains(T value)

(c) void remove(T value)

(d) void addAll(Collection collection)

7. Write a class SortedCycleList> that acts like a CycleList, except the
next() method cycles through items in sorted order.

2