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
//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:
Design a Java class named StringQueue for storing strings of first names into a queue. The...
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...
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...
****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...
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 =...
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 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...
Design and implement a class Q that uses Q.java as a code base. The queue ADT must use class LinkedList from Oracle's Java class library and its underlying data structure (i.e. every Q object has-a (contains) class LinkedList object. class Q is not allowed to extend class LinkedList. The methods that are to be implemented are documented in Q.java. Method comment blocks are used to document the functionality of the class Q instance methods. The output of your program must...
please write in Java and include the two classes
Design a class named Fan (Fan.java) to represent a fan. The class contains: An int field named speed that specifies the speed of the fan. A boolean field named fanStatus that specifies whether the fan is on (default false). A double field named radius that specifies the radius of the fan (default 5). A string field named color that specifies the color of the fan (default blue). A no-arg (default) constructor...
Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...
in java Write a class named Palindrome.java and Write a method isPalindrome that takes an IntQueue as a parameter and that returns whether or not the numbers in the queue represent a palindrome (true if they do, false otherwise). A sequence of numbers is considered a palindrome if it is the same in reverse order. For example, suppose a Queue called q stores this sequence of values: front [3, 8, 17, 9, 17, 8, 3] back Then the following call:...