Java Quick and Short Exercise about Generics, programming homework help
Java Quick and Short Exercise about Generics, programming homework help
Generics
What is Generics?
Generics is the capability to parameterize types. With this capability, you can define a class or a method with generic types that can be substituted using concrete types by the compiler. For example, you may define a generic stack class that stores the elements of a generic type. From this generic class, you may create a stack object for holding strings and a stack object for holding numbers. Here, strings and numbers are concrete types that replace the generic type.
Why Use Generics?
The key benefit of generics is to enable errors to be detected at compile time rather than at runtime. A generic class or method permits you to specify allowable types of objects that the class or method may work with. If you attempt to use the class or method with an incompatible object, a compile error occurs.
Generics Method Example:
public static <E> void reverseList(E[] list) {
for (int i1 = 0, i2 = list.length-1; i1 < list.length/2; i1++, i2–){
E temp = list[i1];
list[i1] = list[i2];
list[i2] = temp;
}
}
Generic Class Example:
public class GenericStack<E> {_x000D_ private java.util.ArrayList<E> list = new java.util.ArrayList<>();_x000D_ _x000D_ public int getSize() {_x000D_ return list.size();_x000D_ }_x000D_ _x000D_ public E peek() {_x000D_ return list.get(getSize() - 1);_x000D_ }_x000D_ _x000D_ public void push(E o) { // puts parameter at the end of the ArrayList_x000D_ list.add(o);_x000D_ }_x000D_ _x000D_ public E pop() { // removes the last item in the ArrayList_x000D_ E o = list.get(getSize() - 1);_x000D_ list.remove(getSize() - 1);_x000D_ return o;_x000D_ }_x000D_ _x000D_ public boolean isEmpty() {_x000D_ return list.isEmpty();_x000D_ }_x000D_ _x000D_ @Override_x000D_ public String toString() {_x000D_ return "stack: " + list.toString();_x000D_ }_x000D_ }_x000D_
Read Oracle’s Java tutorial pages about Generics by clicking on the link below, and the Liang textbook (Chapter 19 “Generics”) for more details about how to use Java Generics, then do Exercise 10.1 below.
http://docs.oracle.com/javase/tutorial/java/generics/index.html
Exercise 10.1 (no late submissions allowed, upload the .java document)
Write a generic static method in Java that prints each element in a generic array in REVERSE ARRAY ORDER (in which each element is of the generic type), one element per line, but DON’T CHANGE THE ORDER OF THE ARRAY ELEMENTS. This method must have a generic array (NOT ArrayList nor other class) as its ONLY parameter.
Write main so it tests this method (put in the class with the generic method). Call the method passing an array of Doubles with at least 5 elements, then an array of Strings with at least 7 elements. RUN THE PROGRAM. Upload the .java file with the output copied and pasted to the end of the file, commented out.
"You need a similar assignment done from scratch? Our qualified writers will help you with a guaranteed AI-free & plagiarism-free A+ quality paper, Confidentiality, Timely delivery & Livechat/phone Support.
Discount Code: CIPD30
Click ORDER NOW..


