Tasks Showing the Difference for Constructor/Destructor in Java/C++ and Generic/Primitive Type

  1. Home
  2. Homework Library
  3. Computer Science
  4. Java Programming
  5. Tasks Showing the Difference for Constructor/Destructor in Java/C++ and Generic/Primitive Type

QuestionQuestion

1. Consider the following Java definition of a mutable string class.
class MutableString
{
    private char[] chars = new char[200];
    private int size = 0;
    public boolean set(char aChar, int index)
    {
       if (index < 0 || index >= chars.length)
            return false;
       chars[index] = aChar;
       return true;
    }
    public String toString()
    {
       String result = "";
       for (int i = 0; i < size; i++)
            result += chars[i];
       return result;
    }
}

Suppose this class was rewritten in C++ in two ways, the first with the array that represents the list as a stack-dynamic variable, and then with the list as a heap dynamic variable. Explain when constructors and destructors would be needed. Explain why no constructors or destructors are needed in the Java implementation.


2. Consider the following C++ template class.
template <typename T, int length>
class Vector
{
public:
    Vector(T values[length])
    {
       for (int i = 0; i < length; i++)
            list[i] = values[i];
    }
    friend bool operator<(const Vector<T, length>& left, const Vector<T, length>& right)
    {
       bool result = true;
       for (int i = 0; i < length; i++)
            result &= left.list[i] < right.list[i];
return result;
    }
private:
    T list[length];
};


int main()
{
    int first[] = {1, 2}, second[] = {2, 3};
    Vector<int, 2> vector1(first), vector2(second);
    cout << (vector1 < vector2) << endl;
    return 0;
}

The class Vector cannot be instantiated for any arbitrary type. For example, consider the following instantiation for a wrapper integer class.
class Int
{
public:
    Int(int i = 0) {this->i = i;}
private:
    int i;
};

int main()
{
    Int first[] = {Int(1), Int(2)}, second[] = {Int(2), Int(3)};
    Vector<Int, 2> vector1(first), vector2(second);
    cout << (vector1 < vector2) << endl;
    return 0;
}

Explain why the second implementation fails. What must be added to that class so this program will compile? Suppose this program were written in Java. Explain how Java allows the constraints on a generic type parameter to be specified and how they would be specified in this case.
Java does have one limitation, however. Although wrapper classes can be used to instantiate generic type parameters, primitive types cannot. Explain why.

Solution PreviewSolution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

Question 1
The first implementation from below is with heap that is dynamically allocated when the MutableString class is instantiated. It is interesting to observe when both constructor and destructor are called because this way we can know either when the object is instantiated (by constructor) or when the object is terminated (by destructor). In case of Java implementation, those are not required because the JRM (Java run-time) has garbage collector. That acts as recovery from memory feature when it is noticed that certain objects are no longer used....
$35.00 for this solution

PayPal, G Pay, ApplePay, Amazon Pay, and all major credit cards accepted.

Find A Tutor

View available Java Programming Tutors

Get College Homework Help.

Are you sure you don't want to upload any files?

Fast tutor response requires as much info as possible.

Decision:
Upload a file
Continue without uploading

SUBMIT YOUR HOMEWORK
We couldn't find that subject.
Please select the best match from the list below.

We'll send you an email right away. If it's not in your inbox, check your spam folder.

  • 1
  • 2
  • 3
Live Chats