Explain how java allows the constraints on a generic type, JAVA Programming

Assignment Help:

Consider the following C++ template class.
#include
using namespace std;

template
class SortedList
{
public:
SortedList()
{size = 0;}
void insert(T item);
friend ostream& operator<<(ostream& out, const SortedList& list)
{return list.put(out);}
private:
ostream& put(ostream& out) const;
T list[length];
int size;
};

template
void SortedList::insert(T item)
{
if (size == length)
throw exception("List Full");
int i = size - 1;
while (i >= 0 && item < list[i])
{
list[i+1] = list[i];
i--;
}
list[i+1] = item;
size++;
}

template
ostream& SortedList::put(ostream& out) const
{
for (int i = 0; i < size; i++)
cout << list[i] << " " ;
cout << endl;
return out;
}

int main()
{
int values[] = {5, 1, 7, 8, 11, 2};
SortedList list;
for (int i = 0; i < 6; i++)
list.insert(values[i]);
cout << list;
return 0;
}
The class SortedList cannot be instantiated for any arbitrary type. For example, consider the following instantiation for a wrapper integer class.

class Int
{
public:
Int(int i) {this->i = i;}
private:
int i;
};

int main()
{
Int values[] = {Int(5), Int(1), Int(7), Int(8), Int(11), Int(2)};
SortedList list;
for (int i = 0; i < 6; i++)
list.insert(values[i]);
cout << list;
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.


Related Discussions:- Explain how java allows the constraints on a generic type

Convert string to super ascii, A string S is said to be "Super ASCII", if i...

A string S is said to be "Super ASCII", if it contains the character frequency equal to their ascii values. String will contain only lower case alphabets (''''a''''-''''z'''') and

Explain data updates issues in data management, Explain Data Updates issues...

Explain Data Updates issues in data management? DATA UPDATES: Old titles must be removed regularly Data changes every time Prices change Transportation / shipping cost change

Need java programmer for an assignment, Need java programmer for an assignm...

Need java programmer for an assignment "Prepare a program that will accept a sequence of file names as command-line arguments. These files will consist of ASCII text representin

What is the basic principle of rmi architecture, The RMI architecture is ba...

The RMI architecture is based on one significant principle: the definition of behavior and the execution of that behavior are separate concepts. RMI allows the code that describes

Describe tostring() methods, Describe toString() Methods ? Print method...

Describe toString() Methods ? Print methods are general in some languages, but most Java programs operate differently. You can use System.out.println() to print any object. The

Coarse grained soa, Coarse Grained SOA : Services are also could be coa...

Coarse Grained SOA : Services are also could be coarse grained. E.g:   You may have a purchase element with individual functions to make a purchase order, add line items, and a

Forward action and include action, What is the difference among ForwardActi...

What is the difference among ForwardAction and IncludeAction? Ans) The difference is that you require to use the IncludeAction only if the action is going to be contained by ano

Method to define the packages in java programme, Q. Write the method to def...

Q. Write the method to define the packages in java programme. Explain. Ans. Package: When we work on a project we have to break our programme in several classes. To organize

Explain animate gifs, Explain Animate gifs? We can save 16 gif images ...

Explain Animate gifs? We can save 16 gif images of the previous instance in a single file in the form of an animated gif, and then used it in a regular tag to display a movi

I need android kiosk mode, I need Android Kiosk Mode Project Description...

I need Android Kiosk Mode Project Description: I need an Android platform developer to develop an app when device start it start into kiosk mode. Only browser window open a c

Write Your Message!

Captcha
Free Assignment Quote

Assured A++ Grade

Get guaranteed satisfaction & time on delivery in every assignment order you paid with us! We ensure premium quality solution document along with free turntin report!

All rights reserved! Copyrights ©2019-2020 ExpertsMind IT Educational Pvt Ltd