



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




Homework 3: Programming Project (Java) 11:59 PM, Feb 7 2006.
The purpose of this assignment is for you to gain experience with object-oriented programming in Java, and in par- ticular the idea of inheritance. It will also give you some exposure to how some programming language features are implemented, i.e. what happens “behind the scenes” when you execute a program. Although no language provides the exact features in this assignment, several languages (e.g., Awk, Icon, Lisp, CLUS, SISAL) do provide similar features. Your program needs to implement several abstractions. The first abstraction is an Element, of which there are many kinds: MyInteger, MyChar and Sequence. MyInteger and MyChar respectively are class-based abstractions of int and char. Class Sequence is a dynamic array of Element objects. A Sequence object, thus, stores objects of type MyInteger, MyChar and Sequence. The following is an example of a Sequence object:
[ 1 ‘a’ [1 3 ‘b’]]
In this example, the Sequence object contains three Element objects: 1 , ‘a’ and another Sequence object [ 3 ‘b’]. Thus, a Sequence object may contain nested Sequence objects.
The first part of this homework requires you to define and implement the classes and their member methods. Use the names Element, MyInteger, MyChar and Sequence for your class definitions. Note that there is an is-a relationship between Element and MyInteger, MyChar and Sequence classes.
class MyChar extends Element { public MyChar() { ... } public char Get() { ... } public void Set(char val) { ... } ... } class MyInteger extends Element { public MyInteger() { ... } public int Get() { ... } public void Set(int val) { ... } ... }
Methods Get and Set respectively are used to access and modify the value associated with an object. For instance, invocation of Get over an MyInteger object returns the integer value associated with the object.
Extend the definitions of the classes by defining the following methods:
Class Sequence defines the following additional methods:
public Element index(int pos);
index is used to access the ith element of a Sequence object. For instance, S.index(0) returns the first element of S. If pos is not between 0 and the length of the Sequence object, flag an error and exit.
public Sequence flatten();
An example usage of flatten is shown below: flatten([1 2 [1 3 4] [‘s’ a b] ]) = [1 2 1 3 4 ‘s’ a b]
Note that flatten returns a new Sequence object.
public Sequence copy();
// Return the sum of two matrices (mat and this) Matrix Product(Matrix mat); // Return the product of two matrices: mat and this. void Print(); // print the elements of matrix }
This part of the homework involves modifying the Sequence class so that the elements of a Sequence object can be stored and retrieved using a key. We will call this class Map. A Map object therefore stores a set of Pair objects. Each Pair object contains a key object and a value object. A key object refers to a MyChar object whereas a value object can refer to any type of Element. An example of a Map object is shown below:
[ (‘1’ ‘a’) (‘3’ ‘h’) (‘8’ 4) ]
This map object contains three Pair objects: (‘1’ ‘a’), (‘3’ ‘h’) and (‘8’ 4). The first component in each Pair object is the key object whereas the second is the value object. Note that the elements of the Map object are ordered according to the key object. Define the following for the Map class:
[ (‘1’ 1) (‘2’ 2) (‘3’ 8) ]
The first is the key object, whereas the second is the value object.
public void add(Pair inval);
add adds a Pair object, say addpair, in a Map object, say mp, by comparing the key value of addpair with the key values of the Pair objects of mp, and inserts addpair such that the key values of the Pair objects are in ascending order. Note that a Map object may contain Pair objects with identical keys. In the case when there are multiple Pair objects with the same key, addpair is added after these Pair objects.
public MapIterator find(MyChar key);
The argument to the find method is the key component of a Pair object. It is used to search a Map object for a Pair object. If the object exists, return a MapIterator object that points to the element. If the object does not exist, return a MapIterator object that points to the end element. Also, if multiple Pair objects with the same key exist, return a MapIterator object that points to the first such Pair object.