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

Project on image processing, Project Description: Computer vision and Im...

Project Description: Computer vision and Image processing project to extract book title Skills required: Java, C Programming

Difference between bean factory and application context, On the surface, an...

On the surface, an application context is similar as a bean factory. But application context offers much more.. ? Application contexts give a means for resolving text messages,

Why do we need wrapper classes, Why do we need wrapper classes? It is s...

Why do we need wrapper classes? It is sometimes simpler to deal with primitives as objects. Moreover most of the collection classes keep objects and not primitive data types. A

What are the flow control statements in java? , The flow control statement...

The flow control statements give you to conditionally execute statements, to repeatedly operate a block of statements, or to just modify the sequential flow of control.

What is the use of textmessage, TextMessage having instance of java.lang.St...

TextMessage having instance of java.lang.String as its payload. Therefore it is very useful for exchanging textual data. It can also be used for exchanging difficult character data

What is javaserver faces technology, A framework for building server-side u...

A framework for building server-side user interfaces for Web applications is written in the Java programming language.

What is javaserver faces expression language, A easy expression language us...

A easy expression language used by a JavaServer Faces UI component tag attributes to bind the associated component to a bean property or to bind the associated component's value to

I want prosport savings plans, I want ProSport Savings Plans Project Des...

I want ProSport Savings Plans Project Description: To prepare an application on iPhone, Mac, iPad and Android, which does the subsequent:- Provides a calculation of the am

Interface, Interface to calculate carbon foot print

Interface to calculate carbon foot print

What is a packet in the network environment, What is a packet within the ne...

What is a packet within the network environment? What kind of information does it contain? A packet is the shortest unit of data transmitted over a computer network. It's a mes

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