{"id":1397,"date":"2017-03-09T16:18:49","date_gmt":"2017-03-09T16:18:49","guid":{"rendered":"http:\/\/www.testingdocs.com\/questions\/?p=1397"},"modified":"2024-11-16T14:47:02","modified_gmt":"2024-11-16T14:47:02","slug":"test-driver-sample-java-program-to-test-queue","status":"publish","type":"post","link":"https:\/\/www.testingdocs.com\/questions\/test-driver-sample-java-program-to-test-queue\/","title":{"rendered":"Test Driver Sample Java program to test Queue"},"content":{"rendered":"<h3><strong>Problem Statement<\/strong><\/h3>\n<p>Write a Queue Test Driver to test FIFO Queue that holds String elements in Java.Queue works like First In First Out principle. Queue follows the FIFO principle. It is an ordered list of elements with some restrictions of inserting and deleting elements. We can insert elements at the end of list and delete the elements from the start of list.<span id=\"more-1767\"><\/span><\/p>\n<h3><strong>Queue Interface<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public interface QueueInterface\r\n{\r\n void enqueue(String element) throws QueueOverflowException;\r\n \/\/ Adds element to the rear of this queue.\r\n\r\nString dequeue() throws QueueUnderflowException;\r\n \/\/ Removes front element from this queue and returns it.\r\n\r\nboolean isFull();\r\n \/\/ Returns true if this queue is full; otherwise, returns false.\r\n\r\nboolean isEmpty();\r\n \/\/ Returns true if this queue is empty; otherwise, returns false.\r\n\r\nint size();\r\n \/\/ Returns the number of elements in this queue.\r\n\r\nint spaceLeft();\r\n \/\/ Returns the number of empty slots in the Queue.\r\n\r\nString printQueue();\r\n \/\/ Returns a string that represents current data in the queue.\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<h3><strong>StringQueue<\/strong><\/h3>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class StringQueue implements QueueInterface\r\n{\r\n protected final int CAPACITY = 50; \r\n protected String[] queueData; \r\n protected int dataSize = 0; \r\n protected int front = 0; \r\n protected int rear;\r\n\r\npublic StringQueue()\r\n {\r\n queueData = new String[CAPACITY];\r\n rear = CAPACITY - 1;\r\n }\r\n\r\npublic StringQueue(int maxSize)\r\n {\r\n queueData = new String[maxSize];\r\n rear = maxSize - 1;\r\n }\r\n\r\n\r\n\r\npublic void enqueue(String element)\r\n \/\/ Adds element to the rear of this queue.\r\n {\r\n try{\r\n if (isFull())\r\n throw new QueueOverflowException(\"Enqueue attempted on a full queue.\");\r\n else\r\n {\r\n rear = (rear + 1) % queueData.length;\r\n queueData[rear] = element;\r\n dataSize = dataSize + 1;\r\n }\r\n }catch(QueueOverflowException qe){\r\n qe.getMessage();\r\n }\r\n }\r\n\r\npublic String dequeue()\r\n \/\/ Removes front element from this queue and returns it.\r\n {\r\n String toReturn =null;\r\n try{\r\n if (isEmpty())\r\n throw new QueueUnderflowException(\"Dequeue attempted on empty queue.\");\r\n else\r\n {\r\n toReturn = queueData[front];\r\n queueData[front] = null;\r\n front = (front + 1) % queueData.length;\r\n dataSize = dataSize - 1;\r\n } \r\n }catch(QueueUnderflowException qe){\r\n qe.getMessage();\r\n }\r\n return toReturn;\r\n }\r\n\r\npublic boolean isEmpty()\r\n \/\/ Returns true if this queue is empty; otherwise, returns false.\r\n {\r\n return (dataSize == 0);\r\n }\r\n\r\npublic boolean isFull()\r\n \/\/ Returns true if this queue is full; otherwise, returns false.\r\n {\r\n return (dataSize == queueData.length);\r\n }\r\n\r\npublic int size()\r\n \/\/ Returns the number of elements in this queue.\r\n {\r\n return dataSize;\r\n }\r\n\r\n\/\/Prints the current data in the queue.\r\n public String printQueue()\r\n {\r\n String current_data=\"\";\r\n for(int i=front;i&lt;=rear;i++)\r\n {\r\n current_data= current_data + \"|\" + queueData[i].toString() + \" \";\r\n }\r\n return current_data;\r\n }\r\n\r\n\/\/returns the empty slots in the queue\r\n public int spaceLeft(){\r\n return queueData.length - dataSize;\r\n }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<h3><strong>QueueTestDriver<\/strong><\/h3>\n<p>&nbsp;<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">public class QueueTestDriver\r\n{\r\n public static void main(String[] args)\r\n {\r\n QueueInterface test = new StringQueue();\r\n Scanner scanner = new Scanner(System.in);\r\n\r\nboolean keepTesting; \r\n String skip;\r\n int constructor; \r\n int operation;\r\n\r\nString enqueueString = \"\", dequeueString = \"\"; \/\/ used by operations\r\n \r\n System.out.println(\"Choose One or Two\");\r\n System.out.println(\"1: StringQueue( )\");\r\n System.out.println(\"2: StringQueue(maxSize)\");\r\n if (scanner.hasNextInt())\r\n constructor = scanner.nextInt();\r\n else\r\n {\r\n System.out.println(\"Please enter an integer.Terminating test.\");\r\n scanner.close();\r\n return;\r\n }\r\n skip = scanner.nextLine();\r\n\r\nswitch (constructor)\r\n {\r\n case 1:\r\n test = new StringQueue();\r\n break;\r\n\r\ncase 2:\r\n System.out.println(\"Enter a maximum size:\");\r\n int maxSize;\r\n if (scanner.hasNextInt())\r\n maxSize = scanner.nextInt();\r\n else\r\n {\r\n System.out.println(\"Error: you must enter an integer.\");\r\n System.out.println(\"Terminating test.\");\r\n scanner.close();\r\n return;\r\n }\r\n skip = scanner.nextLine();\r\n test = new StringQueue(maxSize);\r\n break;\r\n\r\ndefault:\r\n System.out.println(\"Error in constructor choice. Terminating test.\");\r\n scanner.close();\r\n return;\r\n }\r\n\r\n\/\/ Handle test cases\r\n keepTesting = true;\r\n while (keepTesting)\r\n {\r\n System.out.println(\"\\nChoose an operation:\");\r\n System.out.println(\"1: enqueue()\");\r\n System.out.println(\"2: dequeue()\");\r\n System.out.println(\"3: isFull()\");\r\n System.out.println(\"4: isEmpty()\");\r\n System.out.println(\"5: size()\");\r\n System.out.println(\"6: printQueue()\");\r\n System.out.println(\"7: spaceLeft()\");\r\n System.out.println(\"8: Stop\");\r\n if (scanner.hasNextInt())\r\n operation = scanner.nextInt();\r\n else\r\n {\r\n System.out.println(\"Error: you must enter an integer.\");\r\n System.out.println(\"Terminating test.\");\r\n scanner.close();\r\n return;\r\n }\r\n skip = scanner.nextLine();\r\n\r\nswitch (operation)\r\n {\r\n case 1: \/\/ enqueue\r\n System.out.println(\"Enter string to enqueue:\");\r\n enqueueString = scanner.nextLine();\r\n System.out.println(\"enqueue(\\\"\" + enqueueString + \"\\\")\");\r\n try\r\n {\r\n test.enqueue(enqueueString);\r\n }\r\n catch (QueueOverflowException e)\r\n {\r\n System.out.println(\"Overflow Exception: \" + e.getMessage());\r\n } \r\n break;\r\n\r\ncase 2: \/\/ dequeue\r\n System.out.println(\"dequeue()\");\r\n try\r\n {\r\n dequeueString = test.dequeue();\r\n }\r\n catch (QueueUnderflowException e)\r\n {\r\n System.out.println(\"Underflow Exception: \" + e.getMessage());\r\n break;\r\n } \r\n System.out.println(\"Result: \" + dequeueString + \" was returned.\");\r\n break;\r\n\r\ncase 3: \/\/ isFull\r\n System.out.println(\"isFull()\");\r\n System.out.println(\"Result: \" + test.isFull());\r\n break;\r\n\r\ncase 4: \/\/ isEmpty\r\n System.out.println(\"isEmpty()\");\r\n System.out.println(\"Result: \" + test.isEmpty());\r\n break;\r\n\r\ncase 5: \/\/ size\r\n System.out.println(\"size()\");\r\n System.out.println(\"Result: \" + test.size());\r\n break;\r\n\r\ncase 6: \/\/printQueue\r\n System.out.println(\"printQueue()\");\r\n System.out.println(\"Result: \" + test.printQueue());\r\n break;\r\n\r\ncase 7: \/\/ space\r\n System.out.println(\"spaceLeft()\");\r\n System.out.println(\"Result: \" + test.spaceLeft());\r\n break;\r\n\r\n\r\n\r\ncase 8: \/\/ stop testing\r\n keepTesting = false;\r\n break;\r\n\r\ndefault:\r\n System.out.println(\"Error. Terminating test.\");\r\n scanner.close();\r\n return;\r\n }\r\n\r\n}\r\n scanner.close();\r\n System.out.println(\"End of Test Driver\");\r\n }\r\n}\r\n<\/pre>\n<p>&nbsp;<\/p>\n<p>Run output of the program:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1773\" src=\"http:\/\/www.testingdocs.com\/questions\/wp-content\/uploads\/Queue-Java-Program.png\" alt=\"Queue Java Program\" width=\"1045\" height=\"621\" title=\"\"><\/p>\n<h3><strong>Sample run of the test driver<\/strong><\/h3>\n<p>Choose One or Two<br \/>\n1: StringQueue( )<br \/>\n2: StringQueue(maxSize)<br \/>\n2<br \/>\nEnter a maximum size:<br \/>\n10<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n1<br \/>\nEnter string to enqueue:<br \/>\nIronMan<br \/>\nenqueue(\u201cIronMan\u201d)<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n1<br \/>\nEnter string to enqueue:<br \/>\nJurassic Park<br \/>\nenqueue(\u201cJurassic Park\u201d)<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n1<br \/>\nEnter string to enqueue:<br \/>\nAvatar<br \/>\nenqueue(\u201cAvatar\u201d)<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n6<br \/>\nprintQueue()<br \/>\nResult: |IronMan |Jurassic Park |Avatar<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n4<br \/>\nisEmpty()<br \/>\nResult: false<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n3<br \/>\nisFull()<br \/>\nResult: false<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n7<br \/>\nspaceLeft()<br \/>\nResult: 7<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n2<br \/>\ndequeue()<br \/>\nResult: IronMan was returned.<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n6<br \/>\nprintQueue()<br \/>\nResult: |Jurassic Park |Avatar<\/p>\n<p>Choose an operation:<br \/>\n1: enqueue()<br \/>\n2: dequeue()<br \/>\n3: isFull()<br \/>\n4: isEmpty()<br \/>\n5: size()<br \/>\n6: printQueue()<br \/>\n7: spaceLeft()<br \/>\n8: Stop<br \/>\n8<br \/>\nEnd of Test Driver<\/p>\n<!--themify_builder_content-->\n<div id=\"themify_builder_content-1397\" data-postid=\"1397\" class=\"themify_builder_content themify_builder_content-1397 themify_builder tf_clear\">\n    <\/div>\n<!--\/themify_builder_content-->\n","protected":false},"excerpt":{"rendered":"<p>Problem Statement Write a Queue Test Driver to test FIFO Queue that holds String elements in Java.Queue works like First In First Out principle. Queue follows the FIFO principle. It is an ordered list of elements with some restrictions of inserting and deleting elements. We can insert elements at the end of list and delete [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1397","post","type-post","status-publish","format-standard","hentry","category-automation","has-post-title","has-post-date","has-post-category","has-post-tag","has-post-comment","has-post-author",""],"_links":{"self":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1397","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/comments?post=1397"}],"version-history":[{"count":1,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1397\/revisions"}],"predecessor-version":[{"id":19615,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/posts\/1397\/revisions\/19615"}],"wp:attachment":[{"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/media?parent=1397"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/categories?post=1397"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.testingdocs.com\/questions\/wp-json\/wp\/v2\/tags?post=1397"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}