Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Java programming: (7) 53. (a) Write a generic class called MapString that stores

ID: 3909104 • Letter: J

Question

Java programming:

(7) 53. (a) Write a generic class called MapString that stores two instance variables, one is of a general type, and the other is a String. It should have a constructor that takes in a general type, set it to the first instance variable, and set the second instance variable to be thetoString ) representation of the first instance variable. It should have two accessor methods, and a mutator method called setFirst that changes the first instance variable, and the second instance variable to be the toString representation of the first instance variable

Explanation / Answer

a)

public class MapString<T> {

T first;

String second;

public MapString(T first) {

this.first = first;

this.second="MapString [a=" + first + "]";

}

public T getFirst() {

return first;

}

public void setFirst(T first) {

this.first = first;

}

public String getSecond() {

return second;

}

}

b)

public static void main(String args[])

{

MapString<Double> ms=new MapString<Double>(12.0);

}

c) If we make first instance variable of MapString class to be of object type then it can handle any type of data that we pass. That is no need explicitly create MapString object of type double.integer etc.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote