Docsity

Homework 3: Programming Project | ECS 140A, Assignments of Programming Languages

Toggle sidebar
bg1
Homework 3: Programming Project (Java)
11:59 PM, Feb 7 2006.
Overview
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 [1
3 ‘b’]. Thus, a Sequence object may contain nested Sequence objects.
Part 1: Class definitions
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.
1. Define constructor for each class. MyInteger objects are initialized to 0,MyChar objects to ’0’, and
Sequence to an empty sequence (a sequence with no elements).
2. Define Get and Set methods for Char and Integer:
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.
ECS 140A -1- Winter 2006
pf3
pf4
pf5

Partial preview of the text

Download Homework 3: Programming Project | ECS 140A and more Assignments Programming Languages in PDF only on Docsity!

Homework 3: Programming Project (Java) 11:59 PM, Feb 7 2006.

Overview

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.

Part 1: Class definitions

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.

  1. Define constructor for each class. MyInteger objects are initialized to 0 , MyChar objects to ’0’, and Sequence to an empty sequence (a sequence with no elements).
  2. Define Get and Set methods for Char and Integer:

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.

Part 2: Additional methods

Extend the definitions of the classes by defining the following methods:

  1. Define a Print member method for each class. Different objects are printed in the following manner:
    • Element: Define Print to be an abstract method.
    • MyInteger: Print the corresponding integer value.
    • MyChar: Print the quoted value of a MyChar object, e.g., ‘c’.
    • Sequence: Print a sequence by surrounding the values of the elements in [, ], e.g., [ [ 1 ] [ 2 ] ‘3’ ‘c’ [ 1 3 [‘4’ ‘5’] ] ]
  2. Define the following methods for Sequence class:
    • Define method first to return the first element of the Sequence. Element first();
    • Define a method rest that returns the rest of the elements. Sequence rest(); Note that rest does not create a new sequence. It merely points to the rest of the elements of the original sequence.
    • length to return the number of elements in a Sequence object. int length();
    • add to add an element at a specified position. void add(Element elm, int pos); If an element already exists at pos, elm is inserted at pos. All elements starting at location pos are pushed to the right. If pos is not between 0 and the length of the Sequence object, flag an error and exit.
    • delete to remove an element at a specified position: void delete(int pos); After deleting the element at pos, all elements to the right of pos are pushed to the left. If there are no elements at pos, the Sequence object remains unchanged.

Part 3: Additional methods of Sequence

Class Sequence defines the following additional methods:

  1. Method index to access an element.

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.

  1. flatten to flatten a sequence.

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.

  1. A method, copy, for performing a deep copy of a 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 }

Part 6: Map

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. Define an iterator class MapIterator for the Map class. The behavior of a MapIterator object is similar to that of a SequenceIterator object. MapIterator implements all methods that SequenceIterator supports.
  2. Methods begin(), end(), and Print(). Methods begin() and end() are similar to begin() and end() of Sequence (see Part 4). Method Print prints a map object in the following manner:

[ (‘1’ 1) (‘2’ 2) (‘3’ 8) ]

The first is the key object, whereas the second is the value object.

  1. add method for adding a Pair 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.

  1. find method for finding a pair object given the key component.

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.

Notes

  • Note that Java provides several of these classes. For this assignment, you can only use preexisting Integer, Char and String classes for defining MyInteger, MyChar and MyString respectively. You cannot use any other libraries or pre-existing classes for implementing other classes such as Sequence and Map. In other words, implement them from scratch.
  • Do not be overly concerned with efficiency. On the other hand, do not write a grossly inefficient program.
  • Express your program neatly. Part of your grade will be based on presentation. Indentation and comments are important. Be sure to define each variable used with a concise comment describing its purpose. In general, each procedure should have a comment at its beginning describing its purpose.
  • Create several of your own test programs. Your will probably want different test data for the different parts. Be sure that your program works for boundary conditions.
  • This program has no input data. Instead, you will be given test programs that will employ and exercise your classes. You may modify the tests during debugging, but you must use the unmodified tests for what you turn in.
  • “Correct” output will also be given. Your output can differ in minor ways yet still be correct. It is up to you to verify your code’s correctness. In general, the easiest thing to do is to make your output conform to the given correct output and then use “make run”, which employs diff to test for and display differences.
  • You must develop your program in parts as outlined above. Grading will be divided as follows: Part Percentage 1 10 2 20 3 15 4 20 5 15 6 20 If your program does not fully work, hand in a listing of the last working part along with your attempt at the next part, and indicate clearly which is which. No credit will be given if the last working part is not turned in. Points will be deducted for not following instructions, such as the above.
  • Work in incremental style, focusing on a small piece of a part at a time, and getting that to work, before moving on. Unless you are a truly expert programmer and designer, do not try to solve the whole program, or even an entire part, at a time.
  • A message giving exact details of what to turn in, where the provided test files are, etc, will be posted to the newsgroup and the home page.
  • Get started now to avoid the last minute rush.