Create an object of this deque class and call these methods one by one. Now,Just
ID: 3600632 • Letter: C
Question
Create an object of this deque class and call these methods one by one.
Now,Just do one thing, modify your return statement according to this and put these four methods before or after Main() method.
Code to modify:
public class theDeque()
{
int maxSize=10; // as mentioned that the size of the deque is 10
int nItems; //no of items in your deque
int left; //key left
int right; //key right
boolean isEmpty() //to check wheather the que is empty or not
{
return nItems == 0;
}
boolean isFull() // to check wheather the que is full or not
{
return nItems == maxSize;
}
/* method to insert left*/
public void insertLeft(long j) {
if (isFull())
throw new RuntimeException("It is full"); // you can omit it also,as it is already in switch statement also.But its a better programming practice
if (left == 0)
left = maxSize; // you can also write is as 10 as declared.But hardcode value is not appreciated in java
theDeque[--left] = j;
nItems++;
/*Method to insert Right*/
public void insertRight(long i) {
if (isFull())
throw new RuntimeException("It is full");
if (right == maxSize - 1)
right = -1;
Deque[++right] = i;
nItems++;
}
/* Method to remove Left*/
public long removeLeft() {
if (isEmpty())
throw new RuntimeException("It is empty");
long temp = Deque[left];
left++;
if (left == maxSize - 1)
left = -1;
nItems--;
return temp;
}
/* Method to remove Right*/
public long removeRight() {
if (isEmpty())
throw new RuntimeException("It is empty");
long temp = Deque[right];
right--;
if (right < 0)
right = maxSize - 1;
nItems--;
return temp;
}
}
You MUST use this main:
public static void main(String[] args) throws IOException
{
Deque theDeque = new Deque(10);
while(true)
{
long value;
System.out.println("");
if( theDeque.isFull() )
System.out.println(
"*** Deque is full. No insertions. ***");
if( theDeque.isEmpty() )
System.out.println(
"*** Deque is empty. No deletions. ***");
System.out.print("Enter first letter of ");
System.out.println("insertLeft, InsertRight, ");
System.out.print("removeLeft, RemoveRight, or display: ");
int choice = getChar();
switch(choice)
{
case 'd':
theDeque.display();
break;
case 'i':
System.out.print("Enter value to insert left: ");
value = getLong();
theDeque.insertLeft(value);
break;
case 'I':
System.out.print("Enter value to insert right: ");
value = getLong();
theDeque.insertRight(value);
break;
case 'r':
value = theDeque.removeLeft();
System.out.println("Removed left: " + value);
break;
case 'R':
value = theDeque.removeRight();
System.out.println("Removed right: " + value);
break;
default:
System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
The output MUST MUST!!! look like this one:
note: Here are many iterations so make sure it gives many iterations in order to print like this:
*** Deque is empty. No deletions. ***
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: i
Enter value to insert left: 15
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: I
Enter value to insert right: 31
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: d
Array: 0 0 0 0 15 31 0 0 0 0
Deque: 15 31
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: r
Removed left: 15
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: d
Array: 0 0 0 0 15 31 0 0 0 0
Deque: 31
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: I
Enter value to insert right: 40
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: d
Array: 0 0 0 0 15 31 40 0 0 0
Deque: 31 40
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: r
Removed left: 31
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: d
Array: 0 0 0 0 15 31 40 0 0 0
Deque: 40
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: i
Enter value to insert left: 55
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: d
Array: 0 0 0 0 15 55 40 0 0 0
Deque: 55 40
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: i
Enter value to insert left: 60
Enter first letter of insertLeft, InsertRight,
removeLeft, RemoveRight, or display: d
Array: 0 0 0 0 60 55 40 0 0 0
Deque: 60 55 40
Explanation / Answer
public class theDeque()
{
int maxSize=10; // as mentioned that the size of the deque is 10
int nItems; //no of items in your deque
int left; //key left
int right; //key right
boolean isEmpty() //to check wheather the que is empty or not
{
return nItems == 0;
}
boolean isFull() // to check wheather the que is full or not
{
return nItems == maxSize;
}
/* method to insert left*/
public void insertLeft(long j) {
if (isFull())
throw new RuntimeException("It is full"); // you can omit it also,as it is already in switch statement also.But its a better programming practice
if (left == 0)
left = maxSize; // you can also write is as 10 as declared.But hardcode value is not appreciated in java
theDeque[--left] = j;
nItems++;
/*Method to insert Right*/
public void insertRight(long i) {
if (isFull())
throw new RuntimeException("It is full");
if (right == maxSize - 1)
right = -1;
Deque[++right] = i;
nItems++;
}
/* Method to remove Left*/
public long removeLeft() {
if (isEmpty())
throw new RuntimeException("It is empty");
long temp = Deque[left];
left++;
if (left == maxSize - 1)
left = -1;
nItems--;
return temp;
}
/* Method to remove Right*/
public long removeRight() {
if (isEmpty())
throw new RuntimeException("It is empty");
long temp = Deque[right];
right--;
if (right < 0)
right = maxSize - 1;
nItems--;
return temp;
}
}
You MUST use this main:
public static void main(String[] args) throws IOException
{
Deque theDeque = new Deque(10);
while(true)
{
long value;
System.out.println("");
if( theDeque.isFull() )
System.out.println(
"*** Deque is full. No insertions. ***");
if( theDeque.isEmpty() )
System.out.println(
"*** Deque is empty. No deletions. ***");
System.out.print("Enter first letter of ");
System.out.println("insertLeft, InsertRight, ");
System.out.print("removeLeft, RemoveRight, or display: ");
int choice = getChar();
switch(choice)
{
case 'd':
theDeque.display();
break;
case 'i':
System.out.print("Enter value to insert left: ");
value = getLong();
theDeque.insertLeft(value);
break;
case 'I':
System.out.print("Enter value to insert right: ");
value = getLong();
theDeque.insertRight(value);
break;
case 'r':
value = theDeque.removeLeft();
System.out.println("Removed left: " + value);
break;
case 'R':
value = theDeque.removeRight();
System.out.println("Removed right: " + value);
break;
default:
System.out.print("Invalid entry ");
} // end switch
} // end while
} // end main()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.