Question

In: Computer Science

Give an example of a program that uses the nongeneric version of a class from the...

Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?

Solutions

Expert Solution

Source Code in Java:

import java.util.ArrayList;
class NonGeneric
{
public static void main(String[] args)
{
ArrayList a=new ArrayList(); //creating object of ArrayList
//adding two elements to the ArrayList
a.add("Debjit");
a.add("Ganguli");
//calling get() to get the elements
String s1=(String)a.get(0);
String s2=(String)a.get(1);
//printing the elements
System.out.println(s1+" "+s2);
}
}

import java.util.ArrayList;
class Generic
{
public static void main(String[] args)
{
ArrayList<String> a=new ArrayList<String>(); //creating object of ArrayList for String elements
//adding two elements to the ArrayList
a.add("Debjit");
a.add("Ganguli");
//calling get() to get the elements
String s1=a.get(0);
String s2=a.get(1);
//printing the elements
System.out.println(s1+" "+s2);
}
}

Output:

The two implementations differ in that Generic class requires a type to be called, whereas Non-generic class does not.

The advantages of Generic classes are:

  • Generic classes need to be written once for all data types, but Non-generic classes have to be written separately for every data type.
  • Generic classes produce errors at compile time for type mismatch, but Non-generic classes generate errors at runtime for type mismatch.

Related Solutions

Give an example of a program that uses the nongeneric version of a class from the...
Give an example of a program that uses the nongeneric version of a class from the STL and the equivalent program that uses the generic version. How do the two implementations differ? Why is having the syntax of generics better?
3. write a program that uses a class called "garment" that is derived from the class...
3. write a program that uses a class called "garment" that is derived from the class "fabric" and display some values of measurement. 20 pts. Based on write a program that uses the class "fabric" to display the square footage of a piece of large fabric.           class fabric            {                private:                    int length;                    int width;                    int...
Temperature Converter Modify the previous version of this program so that it uses a loop to...
Temperature Converter Modify the previous version of this program so that it uses a loop to display a range of temperature conversions for either Fahrenheit to Celsius or Celsius to Fahrenheit. Note: You can start with the code from the previous version, then modify it slightly after it prompts the user for the direction to convert. It will then ask the user for a starting temperature and ending temperature. Assuming they entered the lower number first (if not, tell them...
Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
give an example of an application that should not be implemented using n-version Programming. Explain why?
give an example of an application that should not be implemented using n-version Programming. Explain why?
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class...
Complete java program below. Complete non-recursive version nthFibonacciWithLoop() method. Complete recursive version nthFibonacciWithRecursion() method. public class Fibonacci { // Fib(N): N N = 0 or N = 1 // Fib(N-1) + Fib(N-2) N > 1 // For example, // Fib(0) = 0 // Fib(1) = 1 // Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1 // Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements...
Task 3: Class Polynomial (Version 2) In a separate namespace, the class Polynomial (Version 2) re-implements a polynomial as a linear array of terms ordered by exponent. Each polynomial is also reduced to simplest terms, that is, only one term for a given exponent. public class Polynomial { // P is a linear array of Terms private Term[ ] P; … // Re-implement the six methods of Polynomial (including the constructor) … }
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Create HiArrayPerson class to store objects of type class Person (in java ) and Give example
Give an example of how to build an array of objects of a super class with...
Give an example of how to build an array of objects of a super class with its subclass objects. As well as, use an enhanced for-loop to traverse through the array while calling a static method(superclass x). Finally, create a static method for the class that has the parent class reference variable.
Write a modified version of the program below. In this version, you will dynamically allocate memory...
Write a modified version of the program below. In this version, you will dynamically allocate memory for the new C-string and old C-string, using the new keyword. Your program should ask the user for the size of the C-string to be entered, and ask the user for the C-string, then use new to create the two pointers (C-strings).   Then, call Reverse, as before. Don’t forget to use delete at the end of your program to free the memory! #include <iostream>...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT