



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An assignment for a java programming course focusing on object-oriented programming concepts such as inheritance, dynamic binding, overriding, and overloading. Students are required to define and implement classes myinteger, mychar, and sequence, and extend their functionality with methods like print, first, rest, length, add, delete, index, flatten, and copy. Additionally, students must create a sequenceiterator class to serve as an iterator over sequence objects.
Typology: Assignments
1 / 6
This page cannot be seen from the preview
Don't miss anything!




The purpose of this assignment is for you to gain experience with object-oriented programming in Java, and in particular the ideas of inheritance, dynamic binding (virtual methods), overriding and overloading methods. 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 [1 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 a 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:
Sequence seq; SequenceIterator it;
for (it = seq.begin(); it != seq.end(); it.advance()) { (it.get()).f(); }
The above applies method f over all elements of seq.
Define a class Matrix by extending Sequence. Matrix represents a two dimensional array of integers, and defines the following methods:
class Matrix extends Sequence { // constructor for creating a matrix of specific number of rows and columns Matrix(int rowsize, int colsize);
void Set(int rowsize, int colsize, int value); // set the value of an element int Get(int rowsize, int colsize); // get the value of an element
Matrix Sum(Matrix mat); // return the sum of two matrices: mat & this Matrix Product(Matrix mat); // return the product of two matrices: mat & 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 com- ponent 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:
The first is the key object, and the second is the value object.