Activity: Create a collection of objects using Java generics. use java to write code. Create a Month class that can accept integer or String data type as an argument. The parameter type will be decided at run time. For this exercise, we will use Month class to store integer and string values that contains 12 months. For example, the integer value of 12 corresponds to string value of “December.” Use the following minimal class definition for Month class. public class Month { private T month; public void setMonth(T month); public T getMonth(); }; Create a new class, MonthApp. This new class will have main method, and will instantiate Month objects to store month indices and names. Here is an example using arrays. // define Month[] monthsByNumber = new Month<>[12]; Month[] monthsByName = new Month<>[12]; // adding values monthsByNumber[0] = new Month(); monthsByNumber[0].setMonth(1); monthsByName[0] = new Month(); monthsByName[0].setMonth("December"); Add Collections. Instead of arrays, use different collections to manage the month objects. ArrayList: Add two collection sets to main method of MonthApp class. One collection set will store monthIndex objects and other will store monthName objects. HashMap: Use a Month as key, and Month as value in a collection. This will make a key/value pair. Populate the collection sets with appropriate month objects for month indexes and names. In total you will end up with 24 instances of Month class that will have integer values (1 thru 12) and string values (January thru December) for all 12 months.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Activity: Create a collection of objects using Java generics.  use java to write code.

  • Create a Month class that can accept integer or String data type as an argument. The parameter type will be decided at run time. For this exercise, we will use Month class to store integer and string values that contains 12 months. For example, the integer value of 12 corresponds to string value of “December.” Use the following minimal class definition for Month class.

public class Month<T>

{

private T month;

public void setMonth(T month);

public T getMonth(); };

  • Create a new class, MonthApp. This new class will have main method, and will instantiate Month objects to store month indices and names. Here is an example using arrays.

// define
Month<Integer>[] monthsByNumber = new Month<>[12];
Month<String>[] monthsByName = new Month<>[12];
// adding values
monthsByNumber[0] = new Month<Integer>();
monthsByNumber[0].setMonth(1);
monthsByName[0] = new Month<String>();
monthsByName[0].setMonth("December");

  • Add Collections. Instead of arrays, use different collections to manage the month objects. 
  •  
    • ArrayList: Add two collection sets to main method of MonthApp class. One collection set will store monthIndex objects and other will store monthName objects.
  •  
    • HashMap: Use a Month<Integer> as key, and Month<String> as value in a collection. This will make a key/value pair.

 

  • Populate the collection sets with appropriate month objects for month indexes and names. In total you will end up with 24 instances of Month class that will have integer values (1 thru 12) and string values (January thru December) for all 12 months.
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 4 steps with 1 images

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY