In this module we continue looking at more complex data definitions including self referencial data definitions. We then proceed to introduce the notion of Abstract Data Types (ADTs) and show a pattern for designing ADTs in Java.
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.
- HtDC (How to Design Classes) chapters 12, 13, 14, 15, 16, Intermezzo 2
- Inheritance
- Abstract Data Types
- Applying Design by Contract
- Behavioral Contracts and Behavioral Subtyping
- Exceptions
- Exception Testing
- DUE DATE: January 31st @11:59pm
Submission Criteria
Repository Contents
Your repositories must contain only the following
- pom.xml file and
- src folder with appropriate sub-folders with your code and tests
Code Criteria
- Java Language Feature Restrictions for Assignments
-
Your solution for each problem should
be in its own package. The package names
should follow this naming convention
edu.neu.ccs.cs5004.assignmentN.problemM
where you replaceN
with the assignment number andM
with the problem number, e.g., all your code for problem 1 for this assignment must be in a package namededu.neu.ccs.cs5004.assignment3.problem1
. - Your project should successfully build using maven generating all the default reports.
- Your Javadoc generation should complete with no errors or warnings.
- Your Checkstyle report must have no violations.
- Your JaCoCo report must indicate 75% code coverage per package for "Branches" and "Instructions" or more.
- Your FindBugs report must have no violations (except for violations categorized as PERFORMANCE or SECURITY, you can ignore those).
- Your PMD report should have no violations.
Problem 1
You are asked to design a Java program for list of strings. Your implementation must provide for the following operations on lists of strings.
isEmpty
: to check if the list is empty or not.size
: to get the total number of elements in the list.contains
: consumes a string and checks if the string is in the list or not.containsAll
: consumes another list of string checks that all elements of this list are in the list passed as argument.-
filterLargerThan
: takes the maximum string length and returns a list with all list elements whose length is greater than the maximum length removed. -
hasDuplicates
: check if the list has at least one duplicate element. -
removeDuplicates
: returns the list with all duplicates removed.
Along with your implementation you are also required to provide
- Templates for all your classes.
- The UML Class Diagram for your final solution.
- A sequence diagram for your implementation of removeDuplicates. The diagram should be based on an example of a list with at least 3 elements and at least 1 being a duplicate.
Problem 2
We are asked to extend our Shapes solution to accommodate for composite shapes. A composite shape is a collection of two or more Shapes. Our Shapes (including our new composite shape) must support the following operations
moveX
: takes the amount of units to move the pin in the X direction. For a composite, all shapes that are part of the composite need to move by the amount provided.moveY
: takes the amount of units to move the pin in the Y direction. For a composite, all shapes that are part of the composite need to move by the amount provided.-
area
: returns the area of a shape. For a composite shape this is the sum of all areas of its shapes. -
perimeter
: returns the perimeter of a shape. For a composite shape this is the sum of all perimeters of its shapes.
Along with your implementation you are also required to provide
- The UML Class diagram for your final solution.
- A Sequence Diagram for the method moveX on a composite object. The composite object needs to be composed of at least 2 shapes.
Problem 3
You are asked to design a Java program for a list of list of integers that supports the following operations.
-
size
: returns the number of list of integers inside this list, e.g., ((), (1), (2 3)) returns 3 -
length
: returns the number of total integers inside this list, e.g., ((), (), (2, 3)) returns 2 -
sum
: returns the sum of all integers inside this list, e.g., ((), (1), (2 3)) returns 6 -
isEmpty
: check if this list is empty, e.g., () returns true, (()) returns false -
add
: takes a list of integers and prepends (adds) it to this list of list of integers -
removeInteger
: takes an integer and removes the first occurrence of this integer in the list -
removeAllInteger
: takes an integer and removes the all occurrence of this integer in the list
- DUE DATE: Feb 7th @11:59pm
Submission Criteria
Repository Contents
Your repositories must contain only the following
- pom.xml file and
- src folder with appropriate sub-folders with your code and test code only.
Code Criteria
-
Java
Language Feature Restrictions for Assignments
-
Your solution for each problem should
be in its own package. The package names
should follow this naming convention
edu.neu.ccs.cs5004.assignmentN.problemM
where you replaceN
with the assignment number andM
with the problem number, e.g., all your code for problem 1 for this assignment must be in a package namededu.neu.ccs.cs5004.assignment4.problem1
. -
For each problem you should have
- Blackbox tests. These should be placed in a Java package that is different than the package that contains your solution's source code.
- Whitebox tests. These should be placed in the same Java package as your solution's source code.
- Your project should successfully build using maven generating all the default reports.
- Your Javadoc generation should complete with no errors or warnings.
- Your Checkstyle report must have no violations.
- Your JaCoCo report must indicate 75% code coverage per package for "Branches" and "Instructions" or more.
- Your FindBugs report must have no violations (except for violations categorized as PERFORMANCE or SECURITY, you can ignore those).
- Your PMD report should have no violations.
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.assignment4.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.assignment4.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.assignment4.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 |]
.
- DUE DATE: Feb 14th @11:59pm
Submission Criteria
Repository Contents
Your repositories must contain only the following
- pom.xml file and
- src folder with appropriate sub-folders with your code and test code only.
Code Criteria
-
Your solution for each problem should
be in its own package. The package names
should follow this naming convention
edu.neu.ccs.cs5004.assignmentN.problemM
where you replaceN
with the assignment number andM
with the problem number, e.g., all your code for problem 1 for this assignment must be in a package namededu.neu.ccs.cs5004.assignment5.problem1
. -
For each problem you should have
- Blackbox tests. These should be placed in a Java package that is different than the package that contains your solution's source code.
- Whitebox tests. These should be placed in the same Java package as your solution's source code.
- Your project should successfully build using maven generating all the default reports.
- Your Javadoc generation should complete with no errors or warnings.
- Your Checkstyle report must have no violations.
- Your JaCoCo report must indicate 75% code coverage per package for "Branches" and "Instructions" or more.
- Your FindBugs report must have no violations (except for violations categorized as PERFORMANCE or SECURITY, you can ignore those).
- Your PMD report should have no violations.
- You cannot use setter methods for this assignment.
Problem 1
Extend your implementation of Set
from your previous assignment to include the following operations
-
union(Set s) : Set
; callings1.union(s2)
wheres1
s2
are bothSet
s returns a newSet
that is the set union ofs1
ands2
. -
intersection(Set s) : Set
; callings1.intersection(s2)
wheres1
s2
are bothSet
s returns a newSet
that is the set intersection ofs1
ands2
. -
difference(Set s) : Set
; callings1.difference(s2)
wheres1
s2
are bothSet
s returns a newSet
that is the set symmetric difference ofs1
ands2
. -
subset(Set s) : Boolean
; callings1.subset(s2)
wheres1
s2
are bothSet
s returns true ifs1
is a subset ofs2
and false otherwise.
Problem 2
You are asked to implement a Priority Queue (PQ). Each element of a PQ is made up of
- a priority; typically an positive integer
- a value associated with the priority; in our case the value will be a string
The PQ should have the following operations
-
createEmtpy() : PQ
; creates an empty priority queue. -
add(Integer priority, String value) : PQ
; adds the given value with its associated priority in PQ. -
peek() : String
; returns the value in PQ that has the highest priority. For two positive integersi,j
such thati < j
theni
has a lower priority thanj
. The PQ remains unchanged. Callingpeek()
on an empty PQ should signal an error. -
pop() : PQ
; returns the PQ without the element with the highest priority. Callingpop()
on an empty PQ should signal an error. -
isEmpty() : Boolean
; returns true if the PQ is empty, returns false otherwise.
Problem 3
Design and implement in Java an Integer Binary Tree (IBT) and an Integer Binary Search Tree (IBST) whose nodes can
hold Integer
objects.
An IBT is one of
- empty
-
a node that contains
- an integer value inside the node
- a left subtree that is itself an IBT,
- a right subtree that is iteself an IBT
An IBT must not contain any duplicate integer values.
An IBST is similar to an IBT with the following extra restriction
-
every node in an IBST contains
- an integer value inside the node
- a left subtree that is itself an IBST, and, all the integer values in the left-subtree are smaller than the value of this node
- a right subtree that is iteself an IBST, and, all the integer values in the right-subtree are larger than the value of this node.
Your design and implementation should support at a minimum the following operations on a IBT:
- create an empty IBT
- add a given integer to the IBT. The operation returns the IBT with the node added. You are free to chose where to add the new node in the IBT.
- the ability to check if the tree is the empty tree
- the ability to know if a given Integer is already stored in the IBT inside a node
Your design and implementation should support at a minimum the following operations on a IBST:
- create an empty IBST
- add a given integer value to an IBST. The operation should return a new IBST that should contain a new node with the new integer value passed as an argument. Your operation must return a valid IBST.
- the ability to check if the tree is the empty tree
- the ability to know if a given Integer is already stored in the IBST inside a node
-
add the operation
deleteNode
to IBST; the operation takes in anInteger
,element
, and if there is a node with the valueelement
in the IBST then remove that node from the IBST and return the new IBST, else ifelement
is not in the IBST return the IBST unchanged. The IBST returned must be a valid IBST with all nodes connected as a tree (no dead nodes/leafs). -
add the operation
mirror
to IBST; the operation returns an IBT which is the mirror image of the current IBST. Here is an example (empty nodes are not shown in the example diagrams)IBST Example IBST Example's Mirror Image
Finally you should design the following operation for both IBTs and IBSTs
-
an operation called
isBst()
that returns true when called on an instance that satisfies the conditions for an IBST, and false otherwise. CallingisBst()
on the return values of the operationmirror
should return false. CallingisBst()
on the return values of the other operations that return an IBST should return true.
NOTE: all IBSTs are IBTs but not all IBTs are IBSTs. Your solution must exploid this property.