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

Describe about applet security, Describe about Applet Security? The pos...

Describe about Applet Security? The possibility of surfing the Net, wandering across a random page, playing an applet and catching a virus is a fear in which has scared several

What are the difference between throw and throws, What are the difference b...

What are the difference between throw and throws? Differences are between throws and throw are: Throw is employed to trigger an exception while throws is used in declara

Describe features of spring, Here are some features of spring:- ? Lightw...

Here are some features of spring:- ? Lightweight: spring is lightweight when it comes to size and transparency. The essential version of spring framework is around 1MB. And t

Java application - gui and backend projects, JNLP/ Java Application - GUI a...

JNLP/ Java Application - GUI and Backend Projects Project Description: I am looking for assist in making changes to a complex java application. System launches from web pa

Explain the relational operator precedence, Explain the Relational Operator...

Explain the Relational Operator Precedence ? Whenever a new operator is introduced you have to ask yourself whereas it fits in the precedence tree. If you seem back at the ins

Illustrate an example to define a student class, Task - Defining a Student ...

Task - Defining a Student class The below illustration will explain how to write a class. We want to write a "Student" class which - should be able to store the below charac

Packages and interfaces, what are the advantages of packages and interface?...

what are the advantages of packages and interface? why we use them? and what is the way of using packages and interfaces complete description with examples?

Loops, Use a while loop to count down from 100 and display all the numbers ...

Use a while loop to count down from 100 and display all the numbers in the * following pattern (you have to figure out what the pattern is - don''t go below 0): * 100 99 97 94

Describe exception handling in java, Question A What are the difference be...

Question A What are the difference between an interface and an abstract class? Question B Describe Exception Handling in JAVA Question C Describe the following with resp

Vehicle loan application - weblogic administrator, Vehicle Loan Application...

Vehicle Loan Application: Type                                         Web-based Application Role                                          Weblogic administrator + develop

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