Question

In: Computer Science

Write the Java program: In this assignment, you will create a program implementing the functionalities of...

Write the Java program:

In this assignment, you will 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).

The Queue3503 class will contain:
a. An int[] data filed named elements to store the int values in the queue.
b. An int data field named size that stores the number of elements in the queue.
c. A no-arg constructor that creates a Queue object with default capacity 0.
d. A constructor that takes an int argument representing the capacity of the queue.
e. A method with signature enqueue(int v) that adds the int element v into the queue.
f. A method with signature dequeue() that removes and returns the first element of the
queue.
g. A method with signature empty() that returns true if the queue is empty.
h. A method with signature getSize() that returns the size of the queue (return type is hence
int)).

The queue class you develop should be tested using the following steps: (In other words, your program named Main will consist of the following)


a. Start your queue with an initial capacity of 8.


b. When the dequeue() method is called, an element from the queue at the beginning of the queue must be removed.

c. The main() method in your Main class should consist of statements to:
i. Create a queue object;
ii. Call enqueue() to insert twenty integers (taken from the user) into the queue.
iii. After the above task is completed, include a for-loop that will print out the contents of the queue.


d. After printing the queue filled with twenty integers, call dequeue() repeatedly to remove the beginning element of the queue.


e. Print the contents of the queue after removing every fifth number.


f. For your reference, the execution of the Main program is shown below. User inputs for populating the Queue is shown in the first line. Next, your program outputs are shown.

*Make sure the code can run in Repl.it*

Points to think about

*Make sure the code can run in Repl.it*

  • Which data structure will be the best fit to implement this Queue class - Array or ArrayList?

Sample Run

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

Initial content: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

After removing 5 elements: 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

After removing 5 elements: 11 12 13 14 15 16 17 18 19 20

After removing 5 elements: 16 17 18 19 20

Solutions

Expert Solution

import java.util.*;

/* Class queue3503 */
class queue3503
{
protected int Queue[] ;
protected int front, rear, size, getsize;

/* Constructor */
public queue3503(int n)
{
size = 8;
getsize = 0;
Queue = new int[size];
front = -1;
rear = -1;
}
/* Function to check if queue is empty */
public boolean empty()
{
return front == -1;
}
/* Function to check if queue is full */
public boolean isFull()
{
return front==0 && rear == size -1 ;
}
/* Function to get the size of the queue */
public int getSize()
{
return getsize ;
}
/* Function to check the front element of the queue */
public int peek()
{
if (empty())
throw new NoSuchElementException("Underflow Exception");
return Queue[front];
}
/* Function to enqueue an element to the queue */
public void enqueue(int i)
{
if (rear == -1)
{
front = 0;
rear = 0;
Queue[rear] = i;
}
else if (rear + 1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else if ( rear + 1 < size)
Queue[++rear] = i;
getsize++ ;
}
/* Function to dequeue front element from the queue */
public int dequeue()
{
if (empty())
throw new NoSuchElementException("Underflow Exception");
else
{
getsize-- ;
int ele = Queue[front];
if ( front == rear)
{
front = -1;
rear = -1;
}
else
front++;
return ele;
}
}
/* Function to display the status of the queue */
public void display()
{
System.out.print("\nQueue = ");
if (getsize == 0)
{
System.out.print("Empty\n");
return ;
}
for (int i = front; i <= rear; i++)
System.out.print(Queue[i]+" ");
System.out.println();
}
}

/* Class QueueImplement */
public class QueueImplement
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);

System.out.println("Array Queue Test\n");
System.out.println("Enter Size of Integer Queue ");
int n = scan.nextInt();
/* creating object of class queue3503 */
queue3503 q = new queue3503(n);
/* Perform Queue Operations */
char ch;
do{
System.out.println("\nQueue Operations");
System.out.println("1. enqueue");
System.out.println("2. dequeue");
System.out.println("3. peek");
System.out.println("4. check empty");
System.out.println("5. check full");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter integer element to enqueue");
try
{
q.enqueue( scan.nextInt() );
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}   
break;   
case 2 :
try
{
System.out.println("dequeued Element = "+q.dequeue());
}
catch(Exception e)
{
System.out.println("Error : " +e.getMessage());
}
break;   
case 3 :
try
{
System.out.println("Peek Element = "+q.peek());
}
catch(Exception e)
{
System.out.println("Error : "+e.getMessage());
}
break;
case 4 :
System.out.println("Empty status = "+q.empty());
break;
case 5 :
System.out.println("Full status = "+q.isFull());
break;
case 6 :
System.out.println("Size = "+ q.getSize());
break;   
default : System.out.println("Wrong Entry \n ");
break;
}
/* display Queue */
q.display();
System.out.println("\nDo you want to continue (Type y or n) \n");
ch = scan.next().charAt(0);

} while (ch == 'Y'|| ch == 'y');
}
}

run this code you will get your desired output

as you need to change the count of no removed 5 you have to just add a count*5 to remove as follows

Queue<string> queueCopy = new Queue<string>(numbers.ToArray());
string[] array = new string[numbers.Count * 5];
numbers.CopyTo(array, numbers.Count);


Related Solutions

In this assignment, you will create a Java program to search recursively for a file in...
In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively in the given...
Java program In this assignment you are required to create a text parser in Java/C++. Given...
Java program In this assignment you are required to create a text parser in Java/C++. Given a input text file you need to parse it and answer a set of frequency related questions. Technical Requirement of Solution: You are required to do this ab initio (bare-bones from scratch). This means, your solution cannot use any library methods in Java except the ones listed below (or equivalent library functions in C++). String.split() and other String operations can be used wherever required....
For this assignment you will write a Java program using a loop that will play a...
For this assignment you will write a Java program using a loop that will play a simple Guess The Number game. Create a new project named GuessANumber and create a new Java class in that project named GuessANumber.java for this assignment. The program will randomly generate an integer between 1 and 200 (including both 1 and 200 as possible choices) and will enter a loop where it will prompt the user for a guess. If the user has guessed the...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale...
Program Requirements: This assignment requires you to create one program in Java to simulate a Point-of-Sale (POS) system. The solution must be in JAVA language. You create the main program called POSmain.java and two classes: ShoppingCart and CashRegister. Your POSmain program should take three file names from command line arguments. The first file contains a list of products and their prices; the second and third files are lists of items in two shopping carts of two customers. The POSmain program...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, and write them in reverse order to an output file. 1. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. For example, assume your package name is FuAssign5 and your main class name is FuAssignment5, and your executable files are in “C:\Users\2734848\eclipse-workspace\CIS 265 Assignments\bin”. The...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
I. General Description In this assignment, you will create a Java program to search recursively for...
I. General Description In this assignment, you will create a Java program to search recursively for a file in a directory. • The program must take two command line parameters. First parameter is the folder to search for. The second parameter is the filename to look for, which may only be a partial name. • If incorrect number of parameters are given, your program should print an error message and show the correct format. • Your program must search recursively...
I. General Description In this assignment, you will create a Java program to read undergraduate and...
I. General Description In this assignment, you will create a Java program to read undergraduate and graduate students from an input file, sort them, and write them to an output file. This assignment is a follow up of assignment 5. Like assignment 5, your program will read from an input file and write to an output file. The input file name and the output file name are passed in as the first and second arguments at command line, respectively. Unlike...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer...
Using Repl.it In this assignment you will create a Java program that allows a Bank customer to do the following: 1) Create a bank account by supplying a user id and password .2) Login using their id and password 3) Quit the program. Now if login was successful the user will be able to do the following: 1) Withdraw money. 2) Deposit money. 3) Request balance. 4) Quit the program. If login was not successful (for example the id or...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT