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

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...
Give an example of an inner and outer class. (Java)
Give an example of an inner and outer class. (Java)
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...
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?
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) … }
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>...
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.
a) Give an example of process clash, from your own experience. b) Give an example of...
a) Give an example of process clash, from your own experience. b) Give an example of metrics abuse, from your own experience.
Give an example of a program in java that creates a GUI with at least one...
Give an example of a program in java that creates a GUI with at least one button and several textfields. Some of the textfields should be for input and others for output. Make the output textfields uneditable. When the button is clicked, the input fields should be read, some calculation performed and the result displayed in the output textfield(s).
explain the principle of the inheritance give an example( CS related) of class inheritance
explain the principle of the inheritance give an example( CS related) of class inheritance
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT