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

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.

Posted Date: 4/6/2013 5:48:54 AM | Location : United States







Related Discussions:- Explain how java allows the constraints on a generic type, Assignment Help, Ask Question on Explain how java allows the constraints on a generic type, Get Answer, Expert's Help, Explain how java allows the constraints on a generic type Discussions

Write discussion on Explain how java allows the constraints on a generic type
Your posts are moderated
Related Questions
What 'System.out.println()' signifies? 'System' is a predefined class. System class gives access to the system. 'out' is the output stream. 'println' is printin

Q. Write short on the method over ridding method in java? Ans. Method over ridding and dynamic binding: We have seen that a method in a super class is inherited by its subclass

a. Write an application for a video store. Place the names of 10 of your favorite movies in a combo box. Let the user select a movie to rent. Let the user select a movie to rent. D

Java Message Service: An interface executed by most J2EE containers to give point-to-point queueing and topic (publish/subscribe) behavior. JMS is frequently used by EJB's that req

What will be the output of this programme? Write comments where you find "//" symbol in the following code? function doSomething() { // 1 setTimeout('doSomething()',10000

In this assignment, you will create 5 objects and get them to interact together. You will create theatres for which patrons will buy tickets from a box office to watch movies.

cocos2d-x puzzle game for kids Project Description: I am seeking a skilled, reliable, and not very expensive cocos2d-x developer (with experience with iOS, Android, and prefe

A Mexican restaurant in London is struggling with preparing the meal ordered by it;s customer in time and has started loosing it;s business. The owner is looking at the possibility

Normal 0 false false false EN-US X-NONE X-NONE

Java Web Developer A web developer is required to finish a webcam site with basic account management and voting functionality. The website can make use of a third party flash