Skip to content

Commit

Permalink
hack #1 generics lesson
Browse files Browse the repository at this point in the history
  • Loading branch information
shreya-ahujaa committed Mar 27, 2023
1 parent 896cb3e commit c0360ac
Showing 1 changed file with 135 additions and 56 deletions.
191 changes: 135 additions & 56 deletions _notebooks/2023-03-15-genericslesson.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {
"vscode": {
"languageId": "java"
Expand Down Expand Up @@ -101,7 +101,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {
"vscode": {
"languageId": "java"
Expand Down Expand Up @@ -177,7 +177,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"vscode": {
"languageId": "java"
Expand All @@ -188,7 +188,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"class [LREPL.$JShell$22$Alphabet; 26\n",
"class [LREPL.$JShell$23$Alphabet; 26\n",
"Generic: Alphabet listed by title\n",
"Alphabet: A\n",
"Alphabet: B\n",
Expand Down Expand Up @@ -217,7 +217,7 @@
"Alphabet: Y\n",
"Alphabet: Z\n",
"\n",
"class [LREPL.$JShell$22$Alphabet; 26\n",
"class [LREPL.$JShell$23$Alphabet; 26\n",
"Generic: Alphabet listed by letter\n",
"A\n",
"B\n",
Expand Down Expand Up @@ -333,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {
"vscode": {
"languageId": "java"
Expand All @@ -344,7 +344,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"class [LREPL.$JShell$24$Animal; 6\n",
"class [LREPL.$JShell$25$Animal; 6\n",
"Generic: Animal listed by title\n",
"Animal: Lion, Gold, 8\n",
"Animal: Pig, Pink, 3\n",
Expand All @@ -353,7 +353,7 @@
"Animal: Kitty, Calico, 1\n",
"Animal: Dog, Brown, 14\n",
"\n",
"class [LREPL.$JShell$24$Animal; 6\n",
"class [LREPL.$JShell$25$Animal; 6\n",
"Generic: Animal listed by name\n",
"Lion\n",
"Pig\n",
Expand Down Expand Up @@ -462,7 +462,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {
"vscode": {
"languageId": "java"
Expand All @@ -473,7 +473,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"class [LREPL.$JShell$26$Cupcake; 9\n",
"class [LREPL.$JShell$27$Cupcake; 9\n",
"Generic: Cupcake listed by title\n",
"Cupcake: Red Velvet, Red, 4\n",
"Cupcake: Orange, Orange, 5\n",
Expand All @@ -485,7 +485,7 @@
"Cupcake: Vanilla, Tan, 11\n",
"Cupcake: Chocolate, Brown, 12\n",
"\n",
"class [LREPL.$JShell$26$Cupcake; 9\n",
"class [LREPL.$JShell$27$Cupcake; 9\n",
"Generic: Cupcake listed by flavor\n",
"Red Velvet\n",
"Orange\n",
Expand Down Expand Up @@ -591,7 +591,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {
"vscode": {
"languageId": "java"
Expand Down Expand Up @@ -700,7 +700,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {
"vscode": {
"languageId": "java"
Expand Down Expand Up @@ -822,7 +822,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {
"vscode": {
"languageId": "java"
Expand Down Expand Up @@ -884,7 +884,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {
"vscode": {
"languageId": "java"
Expand Down Expand Up @@ -1077,7 +1077,80 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 15,
"metadata": {
"vscode": {
"languageId": "java"
}
},
"outputs": [],
"source": [
"import java.util.*;\n",
"\n",
"class QueueManager<T> {\n",
" // queue data\n",
" private final String name; // name of queue\n",
" private int count = 0; // number of objects in queue\n",
" public final Queue<T> queue = new Queue<>(); // queue object\n",
"\n",
" // Queue Constructor\n",
" public QueueManager(String name) {\n",
" this.name = name;\n",
" }\n",
"\n",
" // Queue Constructor\n",
" public QueueManager(String name, T[]... seriesOfObjects) {\n",
" this.name = name;\n",
" this.addList(seriesOfObjects);\n",
" this.deleteList(seriesOfObjects);\n",
" }\n",
"\n",
" // adding list of objects to Queue\n",
" public void addList(T[]... seriesOfObjects) { \n",
" for (T[] objects: seriesOfObjects)\n",
" for (T data : objects) {\n",
" this.count++;\n",
" System.out.println(\" New Queued Data: \" + data);\n",
" this.queue.add(data);\n",
" System.out.print(this.name + \"Count: \" + count + \", Data: \");\n",
" for (T x:queue)\n",
" System.out.print(x + \" \");\n",
" System.out.println();\n",
" System.out.println();\n",
" }\n",
" }\n",
"\n",
" // deleting lis tof objects from Queue\n",
" public void deleteList(T[]... seriesOfObjects) {\n",
" for (T[] objects: seriesOfObjects)\n",
" for (T data : objects) {\n",
" this.count--;\n",
" System.out.println(\"Removed Queue Data: \" + data);\n",
" this.queue.delete();\n",
" System.out.print(this.name + \" Count: \" + count + \", Data: \");\n",
" for (T x:queue)\n",
" System.out.print(x + \" \");\n",
" System.out.println();\n",
" System.out.println();\n",
" }\n",
" }\n",
"\n",
" /**\n",
" * Print any array objects from queue\n",
" */\n",
" public void printQueue() {\n",
" System.out.println(this.name + \" count: \" + count);\n",
" System.out.print(this.name + \" data: \");\n",
" for (T data : queue)\n",
" System.out.print(data + \" \");\n",
" System.out.println();\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"vscode": {
"languageId": "java"
Expand All @@ -1088,58 +1161,64 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Words count: 7\n",
"Words data: seven slimy snakes sallying slowly slithered southward \n"
" New Queued Data: seven\n",
"WordsCount: 1, Data: seven \n",
"\n",
" New Queued Data: slimy\n",
"WordsCount: 2, Data: seven slimy \n",
"\n",
" New Queued Data: snakes\n",
"WordsCount: 3, Data: seven slimy snakes \n",
"\n",
" New Queued Data: sallying\n",
"WordsCount: 4, Data: seven slimy snakes sallying \n",
"\n",
" New Queued Data: slowly\n",
"WordsCount: 5, Data: seven slimy snakes sallying slowly \n",
"\n",
" New Queued Data: slithered\n",
"WordsCount: 6, Data: seven slimy snakes sallying slowly slithered \n",
"\n",
" New Queued Data: southward\n",
"WordsCount: 7, Data: seven slimy snakes sallying slowly slithered southward \n",
"\n",
"Removed Queue Data: seven\n",
"Words Count: 6, Data: slimy snakes sallying slowly slithered southward \n",
"\n",
"Removed Queue Data: slimy\n",
"Words Count: 5, Data: snakes sallying slowly slithered southward \n",
"\n",
"Removed Queue Data: snakes\n",
"Words Count: 4, Data: sallying slowly slithered southward \n",
"\n",
"Removed Queue Data: sallying\n",
"Words Count: 3, Data: slowly slithered southward \n",
"\n",
"Removed Queue Data: slowly\n",
"Words Count: 2, Data: slithered southward \n",
"\n",
"Removed Queue Data: slithered\n",
"Words Count: 1, Data: southward \n",
"\n",
"Removed Queue Data: southward\n",
"Words Count: 0, Data: \n",
"\n",
"Words count: 0\n",
"Words data: \n"
]
}
],
"source": [
"/**\n",
" * Driver Class\n",
" * Tests queue with string, integers, and mixes of Classes and types\n",
" * Driver Class used for testing coding\n",
" */\n",
"class QueueTester {\n",
" public static void main(String[] args)\n",
" {\n",
" // Create iterable Queue of Words\n",
" Object[] words = new String[] { \"seven\", \"slimy\", \"snakes\", \"sallying\", \"slowly\", \"slithered\", \"southward\"};\n",
" QueueManager qWords = new QueueManager(\"Words\", words );\n",
" qWords.addList();\n",
" qWords.printQueue();\n",
"\n",
" // Create iterable Queue of Integers\n",
" Object[] numbers = new Integer[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};\n",
" QueueManager qNums = new QueueManager(\"Integers\", numbers );\n",
" // qNums.printQueue();\n",
"\n",
" // Create iterable Queue of NCS Generics\n",
" Animal.setOrder(Animal.KeyType.name);\n",
" Alphabet.setOrder(Alphabet.KeyType.letter);\n",
" Cupcake.setOrder(Cupcake.KeyType.flavor);\n",
" // Illustrates use of a series of repeating arguments\n",
" QueueManager qGenerics = new QueueManager(\"My Generics\",\n",
" Alphabet.alphabetData(),\n",
" Animal.animals(),\n",
" Cupcake.cupcakes()\n",
" );\n",
" // qGenerics.printQueue();\n",
"\n",
" // Create iterable Queue of Mixed types of data\n",
" QueueManager qMix = new QueueManager(\"Mixed\");\n",
" qMix.queue.add(\"Start\");\n",
" qMix.addList(\n",
" words,\n",
" numbers,\n",
" Alphabet.alphabetData(),\n",
" Animal.animals(),\n",
" Cupcake.cupcakes()\n",
" );\n",
" qMix.queue.add(\"End\");\n",
" // qMix.printQueue();\n",
" // for (Object o : words) {\n",
" // System.out.println(\"Enqueued Data: \" + o);\n",
"\n",
" // }\n",
" }\n",
"}\n",
"QueueTester.main(null);"
Expand All @@ -1158,7 +1237,7 @@
"mimetype": "text/x-java-source",
"name": "Java",
"pygments_lexer": "java",
"version": "17.0.5+8-Ubuntu-2ubuntu120.04"
"version": "11.0.16.1+0"
},
"orig_nbformat": 4,
"vscode": {
Expand Down

0 comments on commit c0360ac

Please sign in to comment.