Question

In: Statistics and Probability

In STS, create Java Project, called java_generics_yourNameLastname that; (Under source directory called, src, create a package...

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)

  1. Create a generic interface called “MyGenInterface” that takes 2 generic types K,V with below methods;

public K getKey();

public V getValue();

  1. Create a generic class called “MyGenClass” that implements “MyGenInterface” interface.
    1. has attributes “K key” and “V value” that can be inherited
    2. has a constructor that takes “K _key”, “V _value” inputs and initializes “key”, “value” attributes
    3. has implementation of “getKey” and “getValue” methods returning “key” and “value” respectively.
  2. Create a regular class “MyRegularClass” with a public static generic method called “getSum” that
    1. takes T generic class that can be children of java’s Number class
    2. takes 2 inputs of “T inp1”, “T inp2”
    3. returns type T inp1
  1. Create class called MainEntry that
    1. has a static main method
    2. in main method, create local variables;
      1. mgi1 of type MyGenInterface that takes Integer and String as K,V
      2. mgi2 of type MyGenInterface that takes Integer and Integer as K,V
      3. Initialize mgi1 to instance of MyGenClass with inputs

(1, “yourName”)

  1. Initialize mgi2 to instance of MyGenClass with inputs

(1, 2017)

  1. Integer i1 set to returned value from MyRegularClass’ getSum method passing (10,20)
  2. Float f1 set to returned value from MyRegularClass’ getSum method passing (100f,200f)

  1. In main method, have a System.out.println statement that prints key and value of mgi1 and mgi2 instances
  2. In main method, have a System.out.println statement that prints i1 and f1

Solutions

Expert Solution

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


Related Solutions

Using PyCharm, create a new Project lab8. Under lab8 create a new Directory exercise, then create...
Using PyCharm, create a new Project lab8. Under lab8 create a new Directory exercise, then create a Python file called nested.py, type the following: Test your code by putting the following at the bottom of the file: make_table_of_stars(2,3) (as shown in the code above). Run the file by right clicking in the code area of Pycharm, then choosing “Run nested”. Your output should be: *** *** (2 rows with 3 columns of stars). Reverse the 2 and 3. What happens?...
You first needs to create a file called “myJS.js” in your project directory. Then, include the...
You first needs to create a file called “myJS.js” in your project directory. Then, include the AngularJS library and myJS.js in the header of the index.html page. Now, you need to define your angular application and a controller in this file such that the controller has the following variables with these initial values: name = "Alex Cool"; faculty= "Faculty of Busienss and IT"; university = {name:"University of Ontario Institute of Technology", url:"http://www.uoit.ca"}; contacts = {email:"[email protected]", phone:"xxx-xxx-xxxx"}; skills = [ {name:...
Create a Java project called (clsCashRegister.java) The context of this project is a cash register at...
Create a Java project called (clsCashRegister.java) The context of this project is a cash register at a shop. The cash register should display a list of items for sale with the price for each item. The cash register should calculate the total cost of the user’s purchase, receive a payment from the user, add tax to the cost, deduct the cost from the payment, and return the exchange back to the user. Display a welcome message: “Welcome to Cash Mart”...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name...
In a Package called characterArray, ********JAVA CODE********** 1) Create a char array containing your full name and print it out. 2) Create an uninitialized char array and copy into it      the char array containing your full name and print it out. 3) Create a Character array and copy into it the char array containing      your full name and print it out. 4) Into the Character array, use toUpperCase() to copy the char array containing     your full name...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class...
Create a Java project called Lab3B and a class named Lab3B. Create a second new class named Book. In the Book class: Add the following private instance variables: title (String) author (String) rating (int) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. Add a second constructor that receives only 2 String parameters, inTitle and inAuthor. This constructor should only assign input parameter values to title and...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class...
Create a Java project called Lab3A and a class named Lab3A. Create a second new class named Employee. In the Employee class: Add the following private instance variables: name (String) job (String) salary (double) Add a constructor that receives 3 parameters (one for each instance variable) and sets each instance variable equal to the corresponding variable. (Refer to the Tutorial3 program constructor if needed to remember how to do this.) Add a public String method named getName (no parameter) that...
1. Create a new Java project called L2 and a class named L2 2. Create a...
1. Create a new Java project called L2 and a class named L2 2. Create a second class called ArrayExaminer. 3. In the ArrayExaminer class declare the following instance variables: a. String named textFileName b. Array of 20 integers named numArray (Only do the 1st half of the declaration here: int [] numArray; ) c. Integer variable named largest d. Integer value named largestIndex 4. Add the following methods to this class: a. A constructor with one String parameter that...
Create a Java project called 5 and a class named 5 Create a second new class...
Create a Java project called 5 and a class named 5 Create a second new class named CoinFlipper Add 2 int instance variables named headsCount and tailsCount Add a constructor with no parameters that sets both instance variables to 0; Add a public int method named flipCoin (no parameters). It should generate a random number between 0 & 1 and return that number. (Important note: put the Random randomNumbers = new Random(); statement before all the methods, just under the...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In...
Part 1 – Classes and objects Create a new Java project called usernamePart1 in NetBeans. In my case the project would be called rghanbarPart1. Select the option to create a main method. Create a new class called Vehicle. In the Vehicle class write the code for: • Instance variables that store the vehicle’s make, model, colour, and fuel type • A default constructor, and a second constructor that initialises all the instance variables • Accessor (getters) and mutator (setters) methods...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class...
Step 1: Create a new Java project called Lab5.5. Step 2: Now create a new class called aDLLNode. class aDLLNode { aDLLNode prev;    char data;    aDLLNode next; aDLLNode(char mydata) { // Constructor data = mydata; next = null;    prev = null;    } }; Step 3: In the main() function of the driver class (Lab5.5), instantiate an object of type aDLLNode and print the content of its class public static void main(String[] args) { System.out.println("-----------------------------------------");    System.out.println("--------Create...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT