In: Statistics and Probability
In STS, create Java Project, called java_generics_yourNameLastname that;
(Under source directory called, src, create a package csci3444.generics and put all code in it)
public K getKey();
public V getValue();
(1, “yourName”)
(1, 2017)
Answer:
Please find the below code according to the given requirements.
=======================================================
MyGenInterface.java
======================================================
package csci3444.generics;
/*
* a generic interface called “MyGenInterface” that takes 2 generic
types K,V
*/
public interface MyGenInterface<K, V> {
public K getKey();
public V getValue();
}
=====================================================
MyGenClass.java
=====================================================
package csci3444.generics;
/*
* a generic class called “MyGenClass” that implements
“MyGenInterface” interface.
*/
public class MyGenClass<K, V> implements MyGenInterface<K,
V> {
private K key;
private V value;
/**
* a constructor that takes “K _key”, “V _value” inputs
and initializes “key”,
* “value” attributes
*
* @param key
* @param value
*/
public MyGenClass(K _key, V _value) {
key = _key;
value = _value;
}
/**
* @return K returns key K
*/
@Override
public K getKey() {
return key;
}
/**
* @return V return value V
*/
@Override
public V getValue() {
return value;
}
}
==================================================
MyRegularClass.java
==================================================
package csci3444.generics;
/*
* a regular class “MyRegularClass”
*/
public class MyRegularClass {
/*
* generic method called “getSum” that takes 2 inputs
of “T inp1”, “T inp2”
*/
public static <T> T getSum(T inp1, T inp2)
{
return inp1;
}
}
========================================================
MainEntry.java
========================================================
package csci3444.generics;
public class MainEntry {
public static void main(String[] args) {
// local variable:mgi1 of type MyGenInterface that takes Integer and String as K,V
MyGenInterface<Integer, String> mgi1;
// local variable:mgi2 of type
MyGenInterface that takes Integer and Integer as K,V
MyGenInterface<Integer,
Integer> mgi2;
//Initialize mgi1 to instance of
MyGenClass with inputs (1, “yourName”)
mgi1 = new MyGenClass<>(1,
"John");
////Initialize mgi2 to instance of
MyGenClass with inputs (1, 2017)
mgi2 = new MyGenClass<>(1,
2017);
//Integer i1 set to returned
value from MyRegularClass’ getSum method passing (10,20)
Integer i1 =
MyRegularClass.getSum(10, 20);
//Float f1 set to returned value
from MyRegularClass’ getSum method passing (100f,200f)
Float f1 =
MyRegularClass.getSum(100f, 200f);
System.out.println("mgi1 Key: "
+ mgi1.getKey() + ", Value: " + mgi1.getValue());
System.out.println("mgi2 Key: " +
mgi2.getKey() + ", Value: " + mgi2.getValue());
System.out.println("i1: " +
i1);
System.out.println("f1: " +
f1);
}
}
Sample output:
NOTE:: I HOPE YOUR HAPPY WITH MY ANSWER....***PLEASE SUPPORT ME WITH YOUR RATING...
***PLEASE GIVE ME "LIKE"...ITS VERY IMPORTANT FOR ME NOW....PLEASE SUPPORT ME ....THANK YOU