Question

In: Computer Science

write the code based on this pseudocode //Create Class any name (ex: aQ) //Declaring private integer...

write the code based on this pseudocode

//Create Class any name (ex: aQ)
//Declaring private integer array empty one ex: name arrQ[]
//Declare private integers front, rear, size and len (example, but you call any other names)

//Write constructors
Called public and same name of the class (parameters integer n)
Inside Declare where:
size is equal to n
len is equal to zero
arrQ is equal to new integer[size]
front is equal minus 1
rear is equal minus 1

//Declare function to check if Queue is empty
In this public you should declare boolean (any name you want)
Which return front is equal minus 1

//Declare function to check if queue is full
Called public boolean (any name you want)
which
return front is equal to zero and rear is equal to size minus 1

// Declare function to get the size of the queue
Should be public integer (any name to get size)
which
return len

//Declare function to check the front element of the queue

Should be public integer peek
then
condition
if (call method that you created above to check if is empty)
//then you can write:
throw new NoSuchElementException("Underflow Exception");
then
return name of your array that you declared in first line [front]


//Declare function to insert an element to the queue
Create public void insert or enque with parameters integer i
then condition
if rear is equal minus 1
front is equal to zero
rear is equal to zero
name of array[rear] is equal to i

//else if rear plus 1 is greater equal to size
//you can write:
throw new IndexOutOfBoundsException("Overflow Exception");
else if rear plus 1 is less then size
name of array[increment rear] is equal to i
increment len

//Declare function to remove front element from the queue
Create public integer remove or deque
then condition
if to call method that you created above to check if is empty
//then you can write:
throw new IndexOutOfBoundsException("Overflow Exception");

// condition else
decrement len
declare variable integer (any name, ex: element) which is equal to name of array[front]
condition if front is equal to rear
front equal minus 1
rear equal minus 1
condition else
increment front
and
return element
//Declare function to display the queue
Should be public void display
then
sys.out.print Queue:
condition if len is equal to zero
sys.out.print no values



//then just:
return
then create for loop where i equal to front and i is less equal rear and increment i
then:
sys.out.print name of array[i] + " "


//Scanner scan = new Scanner(System.in);

System.out.println("Enter the Size of Queue ");
int n = scan.nextInt();
/* creating object of class aQ */
aQ q = new aQ(n);

char ch;
do{
System.out.println("\n Queue Menu");
System.out.println("1. Insert Value”);
System.out.println("2. Remove Value”);
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Check the size of Queue”);
int choice = scan.nextInt();
switch (choice)

for java

for java. create code from this pseudo

step 1. reate Class any name (ex: aQ)

step 2. Declaring private integer array empty one ex: name arrQ

step 3. Declare private integers front, rear, size and len (example, but you call any other names)

step 4Write constructors
   Called public and same name of the class (parameters integer n)
   Inside Declare where:
   size is equal to n
   len is equal to zero
   arrQ is equal to new integer[size]
   front is equal minus 1
   rear is equal minus 1
  
step 5. Declare function to check if Queue is empty
   In this public you should declare boolean (any name you want)
   Which return front is equal minus 1
  
step 6. Declare function to check if queue is full
   Called public boolean (any name you want)
   which
   return front is equal to zero and rear is equal to size minus 1
  
step 7. Declare function to get the size of the queue
   Should be public integer (any name to get size)
   which
   return len
  
step 8. Declare function to check the front element of the queue
  
   Should be public integer peek
   then
   condition
   if (call method that you created above to check if is empty)
   //then you can write:
   throw new NoSuchElementException("Underflow Exception");
   then
   return name of your array that you declared in first line [front]
  

step 9. Declare function to insert an element to the queue
Create public void insert or enque with parameters integer i
then condition
if rear is equal minus 1
front is equal to zero
rear is equal to zero
name of array[rear] is equal to i

step 10. else if rear plus 1 is greater equal to size
you can write:
throw new IndexOutOfBoundsException("Overflow Exception");
else if rear plus 1 is less then size
name of array[increment rear] is equal to i
increment len

step 11. Declare function to remove front element from the queue
    Create public integer remove or deque
   then condition
   if to call method that you created above to check if is empty
   then you can write:
   throw new IndexOutOfBoundsException("Overflow Exception");
  
step 12. condition else
    decrement len
    declare variable integer (any name, ex: element) which is equal to name of         array[front]
    condition if front is equal to rear
    front equal minus 1
    rear equal minus 1
    condition else
    increment front
    and
    return element

step 13. Declare function to display the queue
Should be public void display
then
sys.out.print Queue:
condition if len is equal to zero
sys.out.print no values



step 14. then just:
return
then create for loop where i equal to front and i is less equal rear and increment i
then:
sys.out.print name of array[i] + " "


step 15. Scanner scan = new Scanner(System.in);

System.out.println("Enter the Size of Queue ");
            int n = scan.nextInt();
            /* creating object of class aQ */
            aQ q = new aQ(n);       
                   
            char ch;
            do{
                System.out.println("\n Queue Menu");
                System.out.println("1. Insert Value”);
                System.out.println("2. Remove Value”);
                System.out.println("3. Peek");
                System.out.println("4. Check if is empty");
                System.out.println("5. Check if is full");
                System.out.println("6. Check the size of Queue”);
                int choice = scan.nextInt();
                switch (choice)

Solutions

Expert Solution

Q.java:

Driver.java:

raw_code:

Q.java:

// importing NoSuchElementException
import java.util.NoSuchElementException;

// Q class
public class Q{
// private data variable arrQ is an array of integers
private int arrQ[];
// private data variables of type int
private int front, rear, size, len;

// Q class constructor
public Q(int n){
this.size = n;
this.len = 0;
this.arrQ = new int[size];
this.front = -1;
this.rear = -1;
}
// isEmpty method, checks whether a Q object is empty
public boolean isEmpty(){
if (front == -1)
return true;
else
return false;
}
// isFull method, checks whether a Q object is full
public boolean isFull(){
if (front == 0 && rear == -1)
return true;
else
return false;
}
// getSize() function to get the size of Queue
public int getSize(){
return len;
}
// peek() to get teh first element of Queue
public int peek(){
if (isEmpty())
throw new NoSuchElementException("Underflow Exception");
else
return arrQ[front];
}
//insert() function to insert an element to the Queue
public void insert(int i){
if (rear == -1){
front = 0;
rear = 0;
arrQ[rear] = i;
len++;
}
else{
if (rear+1 >= size)
throw new IndexOutOfBoundsException("Overflow Exception");
else
if (rear+1 < size){
arrQ[++rear] = i;
len++;
}
}
}
// remover function to remover front element from the Queue
public int remove(){
if (isEmpty())
throw new IndexOutOfBoundsException("Overflow Exception");
else{
len--;
int element = 0;
if (front == rear){
front = -1;
rear = -1;
}
else{
element = arrQ[front];
front++;
}
return element;
}
}
// display() function to display Queue
public void display(){
System.out.print("Queue: ");
if (len == 0)
System.out.print("no values\n");
else{
for(int i = front; i <= rear; i++)
System.out.print(arrQ[i] + " ");
}
}
}

Driver.java:

// import Scanner class
import java.util.Scanner;

public class Driver{
// main method
public static void main(String[] args){
Scanner scan = new Scanner(System.in);
System.out.println("Enter the Size of Queue");
int n = scan.nextInt();
Q q = new Q(n);

char ch;
do{
System.out.println("\nQueue Menu");
System.out.println("1. Insert Value");
System.out.println("2. Remove Value");
System.out.println("3. Peek");
System.out.println("4. Check if is empty");
System.out.println("5. Check if is full");
System.out.println("6. Check the size of Queue");
int choice = scan.nextInt();

// switch statement to check user option
switch(choice){
// user choice is 1
case (1): {
int num = 0;
// prompting number to add
System.out.print("Enter a number: ");
num = scan.nextInt();
// calling insert() method with num as parameter
q.insert(num);
break;
}
// user choice is 2, calling remove method and printing return value
case (2): {
System.out.println(q.remove());
break;
}
// user choice is 3, calling peek method and printing return value
case (3): {
System.out.println(q.peek());
break;
}
// user choice is 4, calling isEmpty method and printing return value
case (4): {
System.out.println(q.isEmpty());
break;
}
// user choice is 5, calling isFull method and printing return value
case (5): {
System.out.println(q.isFull());
break;
}
// user choice is 6, calling getSize method and printing return value
case (6): {
System.out.println(q.getSize());
break;
}
}
// prompting user to quit or not
System.out.print("Enter q to quit: ");
ch = scan.next().charAt(0); // taking input and storing in ch
}
while(ch != 'q');
// calling display method for debugging purpose
q.display();
}
}


Related Solutions

please follow this pseudocode 1) Create class (any name you want)ex: aS 2) Declaring private integer...
please follow this pseudocode 1) Create class (any name you want)ex: aS 2) Declaring private integer array empty one ex: name arr[] 3) Declare private integer variables top, size and len (my example) 4) Declare constructor for the class with the same name and inside of that public aS declare (parameter ex: int n)    5) Inside of that constructor method write command where: size is equal to n len is equal to zero array (name) is equal to new...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name)...
follow pseudo 1) Create class called Node Declare private integer called data (or any other name) Declare private Node called link (or any other name) 2) Declare constructor Should be public (ex: Node) where: link is equal to null data is equal to zero 3) Declare another constructor public Node with parameters integer d, Node n where: data is equal to d link is equal to n 4) Declare function to set link to next Node link equal to n...
Write a pseudocode for the following code: /*every item has a name and a cost */...
Write a pseudocode for the following code: /*every item has a name and a cost */ public class Item {    private String name;    private double cost;    public Item(String name, double cost) {        this.name = name;        this.cost = cost;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public double getCost() {        return cost;...
In Java, Here is a basic Name class. class Name { private String first; private String...
In Java, Here is a basic Name class. class Name { private String first; private String last; public Name(String first, String last) { this.first = first; this.last = last; } public boolean equals(Name other) { return this.first.equals(other.first) && this.last.equals(other.last); } } Assume we have a program (in another file) that uses this class. As part of the program, we need to write a method with the following header: public static boolean exists(Name[] names, int numNames, Name name) The goal of...
write pseudocode for the following problems not c code Pseudocode only Write a C program to...
write pseudocode for the following problems not c code Pseudocode only Write a C program to print all natural numbers from 1 to n. - using while loop Write a C program to print all natural numbers in reverse (from n to 1). - using while loop Write a C program to print all alphabets from a to z. - using while loop Write a C program to print all even numbers between 1 to 100. - using while loop...
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
Java language (a) Write code segments to perform the following: (i) declare and create an integer...
Java language (a) Write code segments to perform the following: (i) declare and create an integer array freqArray of size 8 (ii) declare and initialize an array weight (with suitable type) which contains 48.5, 80 and 68 (iii) declare a Mouse array of size 2 with name mouse and initialize it with Mouse objects using one statement (b) A incomplete definition of a class Temperature is given below: public class Temperature { private double value[] = {36.5, 40, 37, 38.3};...
in java, write code that defines a class named Cat The class should have breed, name...
in java, write code that defines a class named Cat The class should have breed, name and weight as attributes. include setters and getters for the methods for each attribute. include a toString method that prints out the object created from the class, and a welcome message. And use a constructor that takes in all the attributes to create an object.
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE...
Create an array-based implementation of a binary tree. (WRITE IN JAVA) DON'T FORGET TO INCLUDE PSEUDOCODE AND UML DIAGRAM
Create a class named RemoveDuplicates and write code: a. for a method that returns a new...
Create a class named RemoveDuplicates and write code: a. for a method that returns a new ArrayList, which contains the nonduplicate elements from the original list public static ArrayList removeDuplicates(ArrayList list) b. for a sentinel-controlled loop to input a varying amount of integers into the original array (input ends when user enters 0) c. to output the original array that displays all integers entered d. to output the new array that displays the list with duplicates removed Use this TEST...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT