Dear Experts, pls modify this program. Must include few arraylist operation usin
ID: 3583998 • Letter: D
Question
Dear Experts,
pls modify this program. Must include few arraylist operation using deque. This program must content Queue, Dequeue, Linked list and the usage of arraylist. Thanks.
package queuetask;
/**
*
* @author thana
*/
import java.util.LinkedList;
import java.util.Queue;
import java.util.Deque;
import java.util.ArrayList;
public class Queuetask {
public static void main(String[] args) {
System.out.println("Learn Queue Operations");
System.out.println("-----------------------");
System.out.println("Adding items to the Queue");
//Creating queue would require you to create instannce of LinkedList and assign
//it to Queue
//Object. You cannot create an instance of Queue as it is abstract
Queue queue = new LinkedList();
ArrayList deque = new ArrayList();
//you add elements to queue using add method
queue.add("Jan");
queue.add("Feb");
queue.add("March");
queue.add("May");
queue.add("June");
System.out.println("Items in the queue..." + queue );
//You remove element from the queue using .remove method
//This would remove the first element added to the queue,
System.out.println("remove element: " + queue.remove() );
//.element() returns the current element in the queue, here when "java" is removed
//the next most top element is .NET, so .NET would be printed.
System.out.println("peek element: " + queue.peek());
System.out.println("poll element: " + queue.poll());
System.out.println("retrieve element: " + queue.element());
System.out.println("Any arralist operation from deque" + deque.);
System.out.println(" " + deque.());
System.out.println(": " + deque);
System.out.println("dequeue Items " + queue );
}
}
Explanation / Answer
PROGRAM:
import java.util.ArrayList;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
public class ListDemo {
Scanner s = new Scanner(System.in);
//Implementation of Queue
void queueDemo(String c[]){
Queue<String> q = new LinkedList<String>();
for(int i=0;i<c.length;i++) {
q.add(c[i]); //Adding elements into Queue
}
System.out.println("Enter the month into Queue:");
String m = s.next();
if(q.contains(m)){ // Checks whether Queue Contains element
q.remove(m); // Removes element from Queue
System.out.println(m+" "+"is removed from Queue.");
}
else{
q.offer(m); // Adds element to Queue
System.out.println(m+" "+"is Added to Queue.");
}
System.out.println("Final Queue is:"+q);
System.out.println("************************************");
}
//Implementation of De-Queue
void dequeuDemo(String c[]){
Deque<String> dq = new LinkedList<String>();
for(int i=0;i<c.length;i++){
dq.offer(c[i]); //Adding elements into De-Queue
}
System.out.println("Enter month to enter to Deque:");
String m = s.next();
if(dq.peekFirst().equals(m)){ // Checks first element by peeking
dq.poll();// Removes element from De-Queue
System.out.println(m+" "+"is removed from deque.");
}
System.out.println("Final Dequeue is:"+dq);
System.out.println("**************************************");
}
//Implementation of ArrayList
void arrayListDemo(String c[]){
ArrayList<String> al = new ArrayList<String>();
for(int i=0;i<c.length;i++) {
al.add(c[i]); //Adding elements into ArrayList
}
System.out.println("Enter the String to retrieve from AarrayList:");
int m = s.nextInt();
System.out.println("The Month retrieved is:"+al.get(m));
System.out.println("****************************************");
}
//Implementation of LinkedList
void linkedListDemo(String c[]){
LinkedList<String> ll = new LinkedList<String>();
for(int i=0;i<c.length;i++){
ll.offer(c[i]); //Adding elements into LinkedList
}
System.out.println("Enter the month to add to List:");
String m = s.next();
if(ll.contains(m)) // Checks whether List Contains element
ll.addLast(m); // Adds Element to Last
else
ll.addFirst(m); // Adds Element to First
System.out.println("The final List is:"+ll);
System.out.println("*****************************************");
}
public static void main(String[] args) {
String months[] = {"JANUARY","OCTOBER","MARCH","AUGUST","MAY","NOVEMBER"};
ListDemo listDemo = new ListDemo();
listDemo.queueDemo(months);
listDemo.dequeuDemo(months);
listDemo.arrayListDemo(months);
listDemo.linkedListDemo(months);
}
}
OUTPUT:
Enter the month into Queue:
AUGUST
AUGUST is removed from Queue.
Final Queue is:[JANUARY, OCTOBER, MARCH, MAY, NOVEMBER]
************************************
Enter month to enter to Deque:
JANUARY
JANUARY is removed from deque.
Final Dequeue is:[OCTOBER, MARCH, AUGUST, MAY, NOVEMBER]
**************************************
Enter the String to retrieve from AarrayList:
3
The Month retrieved is:AUGUST
****************************************
Enter the month to add to List:
OCTOBER
The final List is:[JANUARY, OCTOBER, MARCH, AUGUST, MAY, NOVEMBER, OCTOBER]
*****************************************
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.