You need to write a class called SingleItemBox (in a file called SingleItemBox.java that you create). This class has a constructor that takes a single item (of any type) and puts it in the box. You also need a method called getItem() which provides the item back to the user but does not remove it from the box (this is an "accessor", if you remember, sometimes called a "getter"). Make sure to comment your code as you go in proper JavaDoc style. Please implement this code using generics concept in java.

Respuesta :

Answer:

class SingleItemBox<ItemType> {

 

  ItemType box;

  SingleItemBox(ItemType item) {

      this.box = item;

  }

  public ItemType getitem() {

      return this.box;

  }

}

public class Main {

  public static void main(String[] args) {

      SingleItemBox <Integer> intObj = new SingleItemBox(10);

      System.out.println(intObj.getitem());

     

      SingleItemBox <String> sObj = new SingleItemBox("banana");

     

      System.out.println(sObj.getitem());

  }

}

Explanation:

  • Inside the singleItemBox, create a variable box of type itemType.  
  • Create a constructor of this class and pass this item as a parameter.
  • Create the getItem method that returns this Box.  

  • Inside the main function create an object of singleItemBox class.  
  • Display the box by calling the getter method.
  • Create an object of singleItemBox class of type string and call the getter method.
RELAXING NOICE
Relax