Question

Design a Java class named StringQueue for storing strings of first names into a queue. The...

Design a Java class named StringQueue for storing strings of first names into a queue.

The StringQueue.java class contains:

* A String[] data field named names that stores the String values in the queue.

* A private data field named size that stores the number of names in the queue.

* A public static final data field named DEFAULT_CAPACITY = 10 for the array.

* A private static data field named firstInitCtr that counts the number of names with the same first initial as your first initial.

* A StringQueue no-arg constructor that uses this(DEFAULT_CAPACITY) to construct an object with the default capacity for the string array.

* A StringQueue constructor that receives a new capacity to construct an object with the new capacity for the queue. The new capacity cannot be larger than the default capacity, and cannot be less than 1.

* The method enqueue(String value) that adds value into the queue.

* The method dequeue() that returns the first name from the queue and then shifts the remaining names in the queue to the left.

* The method empty() that returns true if the queue is empty.

* The method getSize() that returns the size of the queue.

* The method checkFirstInit(char firstInit) that adds 1 to the firstInitCtr if the name begins with your first initial.

* The method getFirstInitCtr() that returns the number of names with the same first initial as your name in the queue.

Write a test program called TestStringQueue.java that gets Scanner input for your first name, the number of first names in your queue (indicate the maximum is 10), and then ask for the names to populate the queue using the enqueue method. Invoke the checkFirstInit method to see if the first initial is the same as your first initial, and if so, increment the firstInitCtr. The first initial can either be in upper or lower case for it to be equal. Then, print out the names using the dequeue method. Finally, print out the number of names that begin with your first initial.

Sample runs:

Please enter your first name: joe

Please enter the number of names in your array. Maximum is 10: 11

Please enter the number of names in your array. Maximum is 10: 0

Please enter the number of names in your array. Maximum is 10: 5

Please enter 5 names for the queue: jim John ben carol Joe

Names dequeued:

jim John ben carol Joe

The number of names with your first initial is 3

Please enter your first name: Dan

Please enter the number of names in your array. Maximum is 10: 3

Please enter 3 names for the queue: sam dave ed

Names dequeued:

sam dave ed

The number of names with your first initial is 1

0 0
Add a comment Improve this question Transcribed image text
Answer #1


//StringQueue.java

public class StringQueue {
  
   private String[] names;
   private int size;
   public static int DEFAULT_CAPACITY = 10;
   private static int firstInitCtr =0;

   // default constructor
   public StringQueue()
   {
       this(DEFAULT_CAPACITY);
   }
  
   // parameterized constructor
   public StringQueue(int capacity)
   {
       // check if capacity is atleast 1 and atmost DEFAULT_CAPACITY, else set it to DEFAULT_CAPACITY
       if(capacity >=1 && capacity <=DEFAULT_CAPACITY)
       {
           names = new String[capacity];
       }else
           names = new String[DEFAULT_CAPACITY];
       size = 0;
   }
  
   // method to insert value at the end of the queue
   public void enqueue(String value)
   {
       // if queue is not empty,insert at the end and increment the size
       if(size < names.length)
       {
           names[size] = value;
           size++;
       }
   }
  
   // method to delete and return the front element of the queue
   public String dequeue()
   {
       // if queue is not empty
       if(!empty())
       {
           String data = names[0]; // get the first name of the queue
           // shift the elements to the left
           for(int i=0;i<size-1;i++)
           {
               names[i] = names[i+1];
           }
           size--; // decrement the size
          
           return data;
       }
      
       return null; // empty queue
   }
  
   // method that checks and returns if the queue is empty or not
   public boolean empty()
   {
       return(size == 0);
   }
  
   // method to return number of elements in the queue
   public int getSize()
   {
       return size;
   }
  
   // method to count the number of names with the same first initial as passed parameter, firstInit
   public void checkFirstInit(char firstInit)
   {
       // loop over the array
       for(int i=0;i<size;i++)
           // check if name at index i starts with firstInit, then increment the firstInitCtr
           if(names[i].toLowerCase().startsWith((firstInit+"").toLowerCase()))
               firstInitCtr++;
   }
  
   // method to return the firstInitCtr
   public int getFirstInitCtr()
   {
       return firstInitCtr;
   }
  
}
//end of StringQueue.java

//TestStringQueue.java

import java.util.Scanner;

public class TestStringQueue {
  
   public static void main(String[] args) {

      
       Scanner scan = new Scanner(System.in);
       String name;
       int capacity;
       // input of first name of the user
       System.out.print("Please enter your first name: ");
       name = scan.next();
       // input of capacity of the queue
       System.out.print("Please enter the number of names in your array. Maximum is "+StringQueue.DEFAULT_CAPACITY +": ");
       capacity = scan.nextInt();
       // validate and re-prompt until valid
       while(capacity < 1 || capacity > StringQueue.DEFAULT_CAPACITY)
       {
           System.out.print("Please enter the number of names in your array. Maximum is "+StringQueue.DEFAULT_CAPACITY +": ");
           capacity = scan.nextInt();
       }
       // create a queue of capacity
       StringQueue queue = new StringQueue(capacity);
      
       // input capacity number of names from user and insert into the queue
       System.out.print("Please enter "+capacity+" names for the queue : ");
       for(int i=0;i<capacity;i++)
           queue.enqueue(scan.next());
       // count the number of elements in queue with the same firstInitial as user's
       queue.checkFirstInit(name.charAt(0));
       // dequeue the elements of the queue until empty and display the names
       System.out.println("Names dequeued:");
       while(!queue.empty())
           System.out.print(queue.dequeue()+" ");
       System.out.println();
       // display the number of names with the same first initial as user
       System.out.println("The number of names with your first initial is "+queue.getFirstInitCtr());
       scan.close();
   }

}
//end of TestStringQueue.java

Output:

Add a comment
Know the answer?
Add Answer to:
Design a Java class named StringQueue for storing strings of first names into a queue. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503....

    In Java, create a program implementing the functionalities of a standard queue in a class called Queue3503. You will test the functionalities of the Queue3503 class from the main() method of the Main class. In a queue, first inserted items are removed first and the last items are removed at the end (imagine a line to buy tickets at a ticket counter). Do NOT change your class name from "Main". The Main class should come first in your code. Your filename should be "Main.java". The Queue3503 class will contain:...

  • Suppose we want to implement a circular queue using an array that has an initial capacity...

    Suppose we want to implement a circular queue using an array that has an initial capacity (maximum number of elements) MAX. A circular queue is like a regular queue except that elements can be enqueued or dequeued by wrapping around it. Assume we enqueue on the tail and dequeue from the head. An example circular queue with sample operations is shown below: head head tail head tail tail head Enqueue(9) a) Write a program in C that implements this circular...

  • Study the "Queue as linked list simple example" posted in this module. Rewrite the code to...

    Study the "Queue as linked list simple example" posted in this module. Rewrite the code to use array instead of linked lists. Please do not change anything besides the data structure. Instead of linked list use an array. The functionality should remain exactly the same. LListQueue.cpp ///--------------------------------------------------------------- /// File: LListQueue.cpp /// Purpose: Implementation file for a demonstration of a queue /// implemented as an array. Data type: Character /// Programming Language: C++ ///--------------------------------------------------------------- #include "LListQueue.h" ///-------------------------------------------- /// Function: LListQueue() ///...

  • JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings...

    JAVA --Design a class named StackOfStrings that contains: a. A private array elements to store strings in the stack b. A private data field size to store the number of strings in the stack c. A constructor to construct an empty stack with a default capacity of 4 d. A constructor to construct an empty stack with a specified capacity e. A method empty() that returns true if the stack is empty f. A method push(String value) that stores value...

  • ****WRITTEN IN JAVA. MUST INCORPORATE A STACK OR QUEUE**** A file has genealogy data for a...

    ****WRITTEN IN JAVA. MUST INCORPORATE A STACK OR QUEUE**** A file has genealogy data for a collection of N people. The first line of the file contains the integer N followed by N additional lines of data. Each of these additional lines specifies a list of children for a single person. The line starts with the name of the person, followed by the number of that person's children, followed by the names of the children. Here is an example of...

  • Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for...

    Collect/finish the Java code (interface and the complete working classes) from lecture slides for the for the following ADT: 3) Queue ADT that uses an array internally (call it AQueue) Make sure you keep the same method names as in the slides (automatic testing will be performed)! Make sure your classes implement the corresponding interfaces. Put your classes in a package called cse11. Try to make the code robust and try to use generics. The Queue ADT The Queue ADT...

  • Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class...

    Create a class named Module2. You should submit your source code file (Module2.java). The Module2 class should contain the following data fields and methods (note that all data and methods are for objects unless specified as being for the entire class) Data fields: A String object named firstName A String object named middleName A String object name lastName Methods: A Module2 constructor method that accepts no parameters and initializes the data fields from 1) to empty Strings (e.g., firstName =...

  • java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(),...

    java Create a Queue class based on java.util.LinkedList class. Your Queue class should have an enqueue(), dequeue(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods: A method to randomly generate a number of elements between two given values and save them in a queue A method to print a queue (10 elements per line). The original queue should remain as is after the print A method to return the number of elements on the...

  • QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members:...

    QUESTION 5 (15 Marks) Inheritance and Polymorphism Design a Ship class that has the following members: • A private String data field named name for the name of the ship. • A private String data field named yearBuilt for the year that the ship was built. • A constructor that creates a ship with the specified name and the specified year that the ship was built. • Appropriate getter and setter methods. A toString method that overrides the toString method...

  • In JAVA #3 Write a program named nameChanger.java. This program will only have a driver class...

    In JAVA #3 Write a program named nameChanger.java. This program will only have a driver class with a main. This program will have the user enter a series of names(first and last), which will then be stored in a String array. You will then change the values of each of the names in the array, so that they are stored lastname, firstname, and then output the revised array. Sample: Enter a name (first and last): Tonya Pierce Enter a name...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT