Access over 20 million homework & study documents

Generic Classes

Content type
User Generated
Subject
Programming
Type
Homework
Rating
Showing Page:
1/5

Sign up to view the full document!

lock_open Sign Up
Showing Page:
2/5

Sign up to view the full document!

lock_open Sign Up
Showing Page:
3/5

Sign up to view the full document!

lock_open Sign Up
End of Preview - Want to read all 5 pages?
Access Now
Unformatted Attachment Preview
Syntax of generic classes and methods Course number and name Student name Date a) How were those classes implemented without the generic syntax? Consider taking ArrayList class Without generics ArrayList obj=new ArrayList(); //creates an empty ArrayList instance which is used to hold Object since it is the super class for all the classes // Adding Objects of different types to ArrayList class obj.add(123); obj.add(2.3f); obj.add("Hello"); // Retrieving the data // Since ArrayList stores Object appropriate typecasting is required to convert to designated type to retrieve data Integer a=(Integer)obj.get(0); Float b=(Float)obj.get(1); String c=(String)obj.get(2); b) Give an example of a program that uses the non-generic version of a class and the equivalent program that uses the generic version Non generic class class NonGenericClass { int a; float b; //getters and setters for int and float data void setInt(int a) { this.a = a; } int getInt() { return this.a; } void setFloat(float b) { this.b = b; } float getFloat() { return this.b; } public static void main(String[] args) { NonGenericClass obj = new NonGenericClass(); obj.setInt(2);//calling setInt() to set int value System.out.println(obj.getInt()); obj.setFloat(2.3f);//calling setFloat() to set real value System.out.println(obj.getFloat()); } } Generic class /*In this program for reading int and float data, separate methods for reading and displaying int(setInt() and getInt()) and reading and displaying float(setFloa ...
Purchase document to see full attachment
User generated content is uploaded by users for the purposes of learning and should be used following Studypool's honor code & terms of service.

Anonymous
Just what I needed. Studypool is a lifesaver!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4