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

Nested For-Loop, I''m having trouble with creating a nested for loop to cre...

I''m having trouble with creating a nested for loop to create a table that displays every number divisible by two within the given value. For example, if the number is 8 then there

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

Banking database system - java database connectivity, JDBC Assignment B...

JDBC Assignment Banking Database System:  BankCustomer CUST_ID  CUST_NAME ADDRESS ACC_TYPE CUST_ID

Use javascript create some basic client-side validation, The last exercise ...

The last exercise of each assignment will be continuous from assignment to assignment and will have the goal of building an e-store website by the end of the course. Date & Tim

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

Give an example of class or static members, Give an example of Class or sta...

Give an example of Class or static Members? Below is a Car class along with such a speedLimit field and getSpeedLimit() method. public class Car { private String licensePla

Illustrate dynamic vs. static class loading?, 1. "Classes are statically lo...

1. "Classes are statically loaded with Java's "new" operator." This would imply that static methods could be executed without the class being loaded. 2. "Unlike the static loa

Game of life, Conway's Game of Life is a "cellular automaton" that is playe...

Conway's Game of Life is a "cellular automaton" that is played on a 2D grid (array) of cells. At the start of the game, an initial configuration is set up in which a number of cell

How to implementing the cloneable interface, How to Implementing the Clonea...

How to Implementing the Cloneable Interface The java.lang.Object class contains a clone() method which returns a bitwise copy of the current object. protected native Object cl

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