Question

In: Computer Science

Given the following program(Java); we are asked to do the following 1. Add a loop in...

Given the following program(Java); we are asked to do the following

1. Add a loop in the main to enqueue 12 items of your choice.

2. Be sure to implement some form of error checking that lets you know if the loop tries to add too many items to the queue. Error message: "Unexpected overflow"

3. Add a loop to dequeue items and print them on their own line with their location.

Location = ? item = ?

package Khatrijavaarrayqueue;

public class KhatriJavaArrayQueue
{
   private Object[] theArray;
   private int currentSize;
   private int front;
   private int back;
   static final int DEFAULT_CAPACITY = 10;
  
   public static void main( String[] args)
   {
       KhatriJAvaArrayQueue q = new KhatriArrayJavaQueue();
   }
  
   // constructing the queue
  
   public KhatriJavaArrayQueue()
   {
       this(DEFAULT_CAPACITY);
   }
  
   // constructing the queue
  
   public KhatriJavaArrayQueue()
   {
       theArray = new Object[capacity];
       makeEmpty();
   }
  
   /*
   * Now, let's add our functions
   * these functions go below our constructor,
   * but still inside the class
   */
  
   /*
   * Adding the push() function.
   * this function inserts a new item into the queue.
   */
  
   public void enqueue(Object x) throws IndexOutOfBoundsException
   {
       if(isFull())
       {
           throw new IndexOutOfBoundException();
       }
       back = increment(back);
       theArray[back] = x;
       currentSize++;
   }
   /*
   * Dequeue function; this function returns the least recently
   * inserted item from the queue.
   */
   public Object dequeue()
   {
       if(isEmpty())
       {
           return null;
       }
       currentSize--;
      
       Object frontItem = theArray[front];
       theArray[front] = null;
       front = increment(front);
       return frontItem;
   }
   // adding peek() function
   // this function returns the copy of the element at the top
   // of the stack to be viewed but not removed.
  
   public long peek()
   {
       return stackArray[top];
   }
  
   // adding isEmpty() function

   public boolean isEmpty()
   {
       return currentSize == 0;
   }
   // adding makeEmpty() function
   public void makeEmpty()
   {
       currentSize = 0;
       front = 0;
       back = -1;
   }
   // adding getFront()
   // returns the least recently inserted item in the queue.
   public Object getFront()
   {
       if(isEmpty())
       {
           return null;
       }
       return theArry[front];
   }
   //adding isFull() function; returns true if full
   public boolean isFull()
   {
       return currentSize == theArray.length;
   }
   //increment; internal method to increment the wraparound.
   private int increment(int x)
   {
       if(++x == theArray.length)
       {
           x = 0;
       }
       return x;
   }
}

Solutions

Expert Solution

I have implemented above program. In main() method add "12" items to the queue but when we try to enter "11"th item in queue it wil show the error message "Unexpected OverFlow!" and then call the dequeue method which remove items from the queue and then print item with its location in main() method.

In the questioned program, you done some mistakes in some methods that I solved and attached below full implemented queue program.

Program:-

package Khatrijavaarrayqueue;

import java.util.Scanner;

public class KhatriJavaArrayQueue
{
private Object[] theArray;
private int currentSize;
private int front;
private int back;
static final int DEFAULT_CAPACITY = 10;
  
public static void main( String[] args)
{
// create an object of KhatriJavaArrayOueue Class
KhatriJavaArrayQueue q = new KhatriJavaArrayQueue();

// create an object of Scanner class for user input.
Scanner sc = new Scanner(System.in);

System.out.println("---------- Add 12 Items in the QUEUE ----------\n");
// add 12 items using for loop
for(int i=0; i<12; i++){

// get the user input from the user
System.out.print("Enter item "+(i+1)+" : ");
int num = sc.nextInt();

if(i < KhatriJavaArrayQueue.DEFAULT_CAPACITY){
// add into queue by calling enqueue() method
q.enqueue(num);
}else{
// here queue is full so print the error message
System.err.println("Unexpected OverFlow!");
break;
}

}

System.out.println("\n---------- Dequeue Items from the QUEUE ----------\n");
// now add a loop to dequeue items from queue and print location and item
while(!q.isEmpty()){

// print location of item and item value
System.out.println("Location = "+q.front+" Item = "+q.dequeue() );
}

}
  
// constructing the queue
  
public KhatriJavaArrayQueue()
{
// intialize default capacity by calling parameterized constructor
this(DEFAULT_CAPACITY);
}
  
// constructing the queue
  
public KhatriJavaArrayQueue(int capacity)
{
theArray = new Object[capacity];
makeEmpty();
}
  
/*
* Now, let's add our functions
* these functions go below our constructor,
* but still inside the class
*/
  
/*
* Adding the enqueue() function.
* this function inserts a new item into the queue.
*/
  
public void enqueue(Object x) throws IndexOutOfBoundsException
{
if(isFull())
{
throw new IndexOutOfBoundsException();
}
back = increment(back);
theArray[back] = x;
currentSize++;
}
/*
* Dequeue function; this function returns the least recently
* inserted item from the queue.
*/
public Object dequeue()
{
if(isEmpty())
{
return null;
}
currentSize--;
  
Object frontItem = theArray[front];
theArray[front] = null;
front = increment(front);
return frontItem;
}

// adding peek() function
// this function returns the copy of the element at the front
// of the queue to be viewed but not removed.
public Object peek()
{
// check whether queue is empty or not?
if(isEmpty())
{
// if empty then returns -1
return null;
}
else
return theArray[front];
}
  
// adding isEmpty() function

public boolean isEmpty()
{
return currentSize == 0;
}

// adding makeEmpty() function
public void makeEmpty()
{
currentSize = 0;
front = 0;
back = -1;
}

// adding getFront()
// returns the least recently inserted item in the queue.
public Object getFront()
{
if(isEmpty())
{
return null;
}
return theArray[front];
}

//adding isFull() function; returns true if full
public boolean isFull()
{
return currentSize == theArray.length;
}

//increment; internal method to increment the wraparound.
private int increment(int x)
{
if(++x == theArray.length)
{
x = 0;
}
return x;
}
}

Output:-

In above ouput, we can see that when user try to enter "11"th item then program will show the error message "Unexpected Overflow!".

I hope you will understand the above program.

Do you feel needful and useful then please upvote me.

Thank you.


Related Solutions

Write a Java program that uses a while loop to do the following: Repeatedly asks the...
Write a Java program that uses a while loop to do the following: Repeatedly asks the user to enter a number or -1 to exit the program. Keeps track of the smallest and largest numbers entered so far: You will need a variable for the smallest number and another variable for the largest number. Read the first number the user enters, and if it is not -1 set the smallest and largest variables to that number. Use a loop to...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1...
*JAVA PROGRAM* Write a do loop that prompts a user to enter an integer between 1 and 100 inclusive. The user will be repeatedly prompted until they input a valid integer.
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade...
Java Program. Sentinel While Loop Lab Do the following: Prompts the user to enter a grade or a -1 to quit. IF the user entered a -1 THEN Display a message that the User is done entering grades ELSE Count each grade as it is entered. Compute a running total of the grades entered. END IF After the user enters the sentinel of -1, calculate the average of the grades entered. When computing the average, make sure that there is...
What we want the program to do: We need to write a program, in Java, that...
What we want the program to do: We need to write a program, in Java, that will let the pilot issue commands to the aircraft to get it down safely on the flight deck. The program starts by prompting (asking) the user to enter the (start) approach speed in knots. A knot is a nautical mile and is the unit used in the navy and by the navy pilots. After the user enters the approach speed, the user is then...
Java Program Use for loop 1.) Write a program to display the multiplication table of a...
Java Program Use for loop 1.) Write a program to display the multiplication table of a given integer. Multiplier and number of terms (multiplicand) must be user's input. Sample output: Enter the Multiplier: 5 Enter the number of terms: 3 5x0=0 5x1=5 5x2=10 5x3=15 2 Create a program that will allow the user to input an integer and display the sum of squares from 1 to n. Example, the sum of squares for 10 is as follows: (do not use...
TO DO in JAVA: The program that runs is TrackInsurance. Open this up and add five...
TO DO in JAVA: The program that runs is TrackInsurance. Open this up and add five instances of your ArtInsurance class at the noted location in the main method. Use whatever data you like. TrackInsurance(below) package itp120mod6; import java.util.*; public class TrackInsurance extends Object { public static Scanner scan = new Scanner(System.in); // method that runs first public static void main(String[] args) throws Exception { // make an ArrayList of customers and insurance policies ArrayList cust = new ArrayList(); //...
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1....
DO THIS PROGRAM IN JAVA Write a complete Java console based program following these steps: 1. Write an abstract Java class called Shape which has only one abstract method named getArea(); 2. Write a Java class called Rectangle which extends Shape and has two data membersnamed width and height.The Rectangle should have all get/set methods, the toString method, and implement the abstract method getArea()it gets from class Shape. 3. Write the driver code tat tests the classes and methods you...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints...
*Java program* Use while loop 1.) Write a program that reads an integer, and then prints the sum of the even and odd integers. 2.) Write program to calculate the sum of the following series where in is input by user. (1/1 + 1/2 + 1/3 +..... 1/n)
Write a program that produces the following output using nested for loop in Java. ****** ////////////...
Write a program that produces the following output using nested for loop in Java. ****** //////////// ****** ***** //////////\\ ***** **** ////////\\\\ **** *** //////\\\\\\ *** ** ////\\\\\\\\ ** * //\\\\\\\\\\ * \\\\\\\\\\\\
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify...
Complete the java program. /* Note: Do not add any additional methods, attributes. Do not modify the given part of the program. Run your program against the provided Homework2Driver.java for requirements. */ /* Hint: This Queue implementation will always dequeue from the first element of the array i.e, elements[0]. Therefore, remember to shift all elements toward front of the queue after each dequeue. */ public class QueueArray<T> { public static int CAPACITY = 100; private final T[] elements; private int...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT