Assignment 3 Simulating Java String Class with C++ Solution

$45.00

Category:

Description

In this assignment, you will practice Classes and Objects using C++. You may develop your solution in the form of four files, MyString.cpp, MyString.h, StringTable.h, and StringData.h.

Java String is a reference type, and each String object contains a reference to a string that belongs to a string pool. The string pool, called “String Literal Pool”, contains a set of strings of unique value, such that all String objects with the same string literal will refer to the one single copy of the string. This pooling method can save memory space and also enables more efficient string manipulation, such as string comparison.

Now, let’s simulate the Java String mechanism with C++. You need to implement at least two classes: MyString and StringTable. You may create an additional class StringData to maintain the string data and its number of instances (occurrences). A MyString object does not store the actual string. Instead, it only maintains an index to an internal string pool which is an object of StringPool.

For example:

Three MyString objects can be created by writing:

MyString s0(“hello”);

MyString s1(“my”);

MyString s2(“my”);

But, in the StringTable object, only two strings, hello and my, are created. The StringTable will maintain a table of strings, each represented as a C-style string, and dynamically allocated/deallocated as required. For simplicity, you can assume to have a table size of 20. However, there is no restriction on the length of the strings.

Since all the MyString objects will share the same string pool, you should have a static member of StringTable object in MyString. The following methods need to be implemented for the MyString class.

class MyString {

public:

MyString(const char* ch); ~MyString();

char* intern();

static void displayPool();

};

  • The constructor will create a MyString object which stores an index to an existing string in the pool table. If the MyString object is with a new string literal, then the actual string should be

allocated in the pool table at the first empty slot. The number of references to each string in the pool table will also increase by 1.

  • The destructor will remove a reference to the corresponding string in the pool, and decrease the count by 1. If the number of references becomes 0, the memory for storing the string literal will be deallocated.

  • The intern method returns the string literal.

  • The displayPool method display the current content in the string pool (for testing purpose). It will list all the strings current in the table, each with its index number and number of references.

Here is a sample driver for the classes

#include <MyString.h>

int main(){

MyString s0(“hello”);

MyString s1(“my”);

{ // a local scope here MyString s2(“world”); MyString::displayPool(); cout << endl;

}

MyString::displayPool();

cout << endl;

MyString s3(“hello”);

MyString s4(“you”);

MyString * s5;

MyString::displayPool();

cout << endl;

{ // another local scope s5 = new MyString(“world”); MyString::displayPool(); cout << endl;

}

MyString s6(“everyone”);

MyString::displayPool();

cout << endl;

delete s5;

MyString::displayPool();

cout << endl;

MyString s7(“Hello”);

MyString::displayPool();

cout << endl;

cout << “now print out all the strings:” << endl; cout << s0.intern() << endl; cout << s1.intern() << endl;

cout << s3.intern() << endl;

cout << s4.intern() << endl;

cout << s6.intern() << endl;

cout << s7.intern() << endl;

}

The output will be:

0 1 hello

1 1 my

2 1 world

0 1 hello

1 1 my

0 2 hello

1 1 my

2 1 you

0 2 hello

1 1 my

2 1 you

3 1 world

0 2 hello

1 1 my

2 1 you

3 1 world

4 1 everyone

0 2 hello

1 1 my

2 1 you

4 1 everyone

0 2 hello

1 1 my

2 1 you

3 1 Hello

4 1 everyone

now print out the strings:

hello

my

hello

you

everyone

Hello

Submission

  • Submit the code files via the online submission system.


error: Content is protected !!