In this module we continue looking at ADTs and focus on behavioral extensions. We introduce the notion of pre-conditions, post-conditions, and the Liskov substitutability principle as a means to reason about behavioral subtypes.
We then begin our exploration of abstraction and reuse in Java introducing Generics, how to design and use Java programs with Generics, and how the JVM implements Generics. We conclude this module with the introduction of Java Exceptions.
- Given the implementation for an Abstract Data Type, write an extension using inheritance.
- Given the implementation for an Abstract Data Type, write an extension using forwarding.
- Given a set of Java classes, extract their common behavior.
- Given a set of Java classes, abstract over their types using Generics.
- Given a Java class that uses Generics, draw its UML class diagram.
- Given a recursive method, refactor to use a looping structure.
- Write a Java application for generic lists that implements the iterator interface.
- Write a Java application for generic lists that implements the iterable interface.
- Given a Java program that uses a looping structure, write down the loop invariant.
- Write JUnit tests that verify exceptions are thrown for specific inputs.
- Given a set of Java classes that are part of a Java Package, draw the corresponding UML class diagram.
- Given a UML class diagram for simple geometric shapes, implement shape intersection.
- Explain the difference between overriding and overloading a method.
- Explain the difference between checked and runtime exceptions.
- Given a specification for an Abstract Data Type (ADT), translate into appropriate Java classes.
- Given a Java method write a pre-condition for its expected behavior.
- Given a Java method write a post-condition for its expected behavior.
- Write down the condition needed for the Liskov substitutability principle.
- Given a Java class that implements an interface, draw the corresponding UML class diagram.
- DUE DATE: February 4th @12:00pm (NOON)
Restrictions
For this assignment you must follow the following instructions/rules.
-
You can use classes that you create and/or classes in the package
java.lang
. - All classes and methods that you write must contain Javadoc comments.
- All methods that you create must have JUnit tests.
Problem 1
All Java source code that is part of your solution to this problem must reside inside a java package with the name
edu.neu.ccs.cs5004.seattle.assignment3.problem1
You are asked to provide the design and implementation in Java for a Set, as in the mathematical notion of a set. Here is the specification given to you describing all the operations on Set. The specification uses:
-
{}
to denote the empty set, -
{1,2,3}
to denote the set with the elements1
,2
,3
. -
∪ to denote union of two sets, i.e.,
a ∪ b
.
Operation | Specification | Comments |
---|---|---|
EmptySet(): Set
|
|
|
isEmpty() : boolean
|
|
|
add(Integer n) : Set
|
|
|
contains(Integer n) : boolean
|
|
|
remove(Integer ele) : Set
|
|
|
size() : Integer
|
|
|
You are also required to provide the following methods for Sets
-
equals(Object o)
should returntrue
if and only if the two sets have the same number of elements and for each element inthis
the same element exists ino
and for each element ino
also exists inthis
. -
hashCode()
ensure that your implementation ofhashCode()
andequals()
satisfies the contracts for both methods.
Problem 2
All Java source code that is part of your solution to this problem must reside inside a java package with the name
edu.neu.ccs.cs5004.seattle.assignment3.problem2
You are asked to provide the design and implementation
in Java of a Bag. A Bag is a container for a
group of data, in
our case a Bag can hold String
s. A Bag
can contain duplicates. The elements of a Bag have no
order. Here is the specification given to you describing
all operations on Bags. The specification uses
-
[]
to denote an empty Bag -
[a,b,s]
to denote a bag with three stringsa
b
,s
. The order of the elements does not matter, e.g.,[a,b,c]
is the same Bag as[b,c,a]
and[c,a,b]
etc., -
++
to denote the addition of an element in a Bag, e.g.,b ++ [a,b] = [a,b,b]
-
|aBag|
to denote the number of elements inaBag
.
Operation | Specification | Comments |
---|---|---|
EmptyBag() : Bag
|
|
|
isEmpty() : boolean
|
|
|
size() : Integer
|
|
|
add(String s) : Bag
|
|
|
contains(String s) : boolean
|
|
|
You are also required to provide the following methods for Bags
-
equals(Object o)
that returnstrue
if the two bags have the same exact elements; exactly the sameString
and exactly the same number of times in the bag (if there are duplicates). Remember that the order of elements in the bag does not matter. -
hashCode()
ensure that your implementation ofhashCode()
andequals()
satisfies the contracts for both methods.
Problem 3
All Java source code that is part of your solution to this problem must reside inside a java package with the name
edu.neu.ccs.cs5004.seattle.assignment3.problem3
One of your teammates would like your help with one of his projects. He was given the following specification for a Queue and is asking you to provide a design and Java implementation. This Queue holds data in a first in first out (FIFO) order, thus the order by which the elements are added into the Queue matters. The specification uses
-
[| |]
to denote an empty Queue. -
[|a,b,s|]
to denote a queue with three integersa
b
,s
. -
| aQueue |
to denote the number of elements inaQueue
.
Operation | Specification | Comments |
---|---|---|
EmptyQueue() : Queue
|
|
|
isEmpty() : boolean
|
|
|
size() : Integer
|
|
|
add(Integer s) : Queue
|
|
|
contains(Integer x) : boolean
|
|
|
pop() : Queue
|
|
|
peek(): Integer
|
|
|
You are also required to provide implementations for the
methods equals(Object o)
and hashCode()
.
For equals()
two queues are equal if and only
if they have the same exact elements and in the same order
(identical), i.e., [| 1,2,3,1,2,3 |]
is not
equal with [| 1,2,3,1,2, |]
or
[| 2,1,3,1,2,3 |]
.