Interview Question
- -1of 1 vote
Answerpackage jp.co.worksap.global;
- samarth.se August 25, 2014 in India
import java.util.NoSuchElementException;
/**
* The Queue class represents an immutable first-in-first-out (FIFO) queue of objects.
* @param <E>
*/
public class ImmutableQueue<E> {
/**
* requires default constructor.
*/
public ImmutableQueue() {
// modify this constructor if necessary, but do not remove default constructor
}
// add other constructors if necessary
/**
* Returns the queue that adds an item into the tail of this queue without modifying this queue.
* <pre>
* e.g.
* When this queue represents the queue (2, 1, 2, 2, 6) and we enqueue the value 4 into this queue,
* this method returns a new queue (2, 1, 2, 2, 6, 4)
* and this object still represents the queue (2, 1, 2, 2, 6) .
* </pre>
* If the element e is null, throws IllegalArgumentException.
* @param e
* @return
* @throws IllegalArgumentException
*/
public ImmutableQueue<E> enqueue(E e) {
return null;
}
/**
* Returns the queue that removes the object at the head of this queue without modifying this queue.
* <pre>
* e.g.
* When this queue represents the queue (7, 1, 3, 3, 5, 1) ,
* this method returns a new queue (1, 3, 3, 5, 1)
* and this object still represents the queue (7, 1, 3, 3, 5, 1) .
* </pre>
* If this queue is empty, throws java.util.NoSuchElementException.
* @return
* @throws java.util.NoSuchElementException
*/
public ImmutableQueue<E> dequeue() {
return null;
}
/**
* Looks at the object which is the head of this queue without removing it from the queue.
* <pre>
* e.g.
* When this queue represents the queue (7, 1, 3, 3, 5, 1),
* this method returns 7 and this object still represents the queue (7, 1, 3, 3, 5, 1)
* </pre>
* If the queue is empty, throws java.util.NoSuchElementException.
* @return
* @throws java.util.NoSuchElementException
*/
public E peek() {
return null;
}
/**
* Returns the number of objects in this queue.
* @return
*/
public int size() {
return -1;
}
}| Report Duplicate | Flag | PURGE
Data Structures
Interview Type: Written Test
This file saved in C:\jp\co\worksap\global\ImmutableQueue.java
- Nitin Vitthal Sonawane August 27, 2014package jp.co.worksap.global; //package created
import java.util.ArrayList; //import arraylist
import java.util.NoSuchElementException;
import java.io.*; //import input output
public class ImmutableQueue<E> //create class ImmutableQueue, also save this file by ImmutableQueue name
{
private ArrayList<E>arr; //create private ArrayList
public ImmutableQueue() //class name costructor
{
arr=new ArrayList<E>(); //store ArrayList in arr
}
public ImmutableQueue(ArrayList<E>cl) //create ImmutableQueue function and pass arryList
{
arr=cl; //store cl data in arr
}
public ImmutableQueue<E>enqueue(E e)throws IllegalArgumentException //enqueue function, it throws exception when queue is empty
{
if(e==null) //if queue is null then throws exception
{
throw new IllegalArgumentException(); //exception name, IllegalArgumentException
}
ArrayList<E>cloneArr=new ArrayList<E>(); //create a new object of ArrayList, name as cloneArr
for(int i=0;i<arr.size();i++) //for loop used to get data from user upto arr.size(), and arr.size() used for getting size of arr
cloneArr.add(arr.get(i)); //add i th element into cloneArr
cloneArr.add(e); //add into queue
return new ImmutableQueue<E>(cloneArr); //return if operation successful/unsusseful
}
public ImmutableQueue<E>dequeue()throws NoSuchElementException //deenqueue function, it throws exception when queue is empty
{
if(arr.size()==0) //arr.size is 0 then throws exception
{
throw new NoSuchElementException(); //exception name NoSuchElementException
}
ArrayList<E>cloneArr=new ArrayList<E>(); //create a new object of ArrayList, name as cloneArr
for(int i=1;i<arr.size();i++) //for loop used to delete data from queue upto arr.size()
cloneArr.add(arr.get(i));
return new ImmutableQueue<E>(cloneArr);
}
/*
public E peek()throws NoSuchElementException
{
if(arr.size()==0)
{
throw new NoSuchElementException(); //queue empty then throws exception
}
else
{
return arr.get(0); //this is used for displyaing only next element of queue
}
}
*/
public void peekAll()throws NoSuchElementException //peekAll function used for displaying all next element
{
if(arr.size()==0) //if queue empty then throws exception
{
throw new NoSuchElementException(); //name of exception, NoSuchElementException
}
else //otherwise
{
int queueSize = arr.size(); //arr.size() means arr size stored in queueSize variable, declare as integer
for(int i=0;i<queueSize;i++) //loop work from 0 to queueSize
{
System.out.println(arr.get(i)); //print arr
}
}
}
public int size() //size function used for calcutaing size, and it can be used in all prog
{
return arr.size(); //returns the size of arr
}
}
This file saved in C:\Queue.java
import jp.co.worksap.global.*; //imported that created package in jp\co\worksap\global\
import java.io.*; //input output package
public class Queue //class name is queue, so file name is queue
{
public static void main(String args[]) throws IOException //its a main method and throws input and output exception if any error occeured
{
ImmutableQueue <Integer> q=new ImmutableQueue <Integer>(); //create a object name as q, of a class Immutable queue, in a another file name as ImmutableQueue
BufferedReader bin=new BufferedReader(new InputStreamReader(System.in)); //it can be used for reading input string from user
System.out.println("How Many Elements: "); //normal msg for printing data on screen
int ele=Integer.parseInt(bin.readLine()); //conversion of string into number and store in ele variable (ele get)
System.out.println("\nEnter Elements: "); //normal msg for printing data on screen
for(int i=0;i<ele;i++) //for loop starts from 0 to ele, and ele already get from user
{
q=q.enqueue(Integer.parseInt(bin.readLine())); //enqueu operation, means insert element into queue
}
q=q.dequeue(); //deleted first element of queue
/* System.out.println("\nnext Queue Element is: ");
System.out.println(q.peek()); //peek used for displyaing next element of queue. coz fist element is deleted by deuqueue
*/
System.out.println("\nNew Queue Elements: ");
q.peekAll(); //peekAll used for displaying all next elemeny]t of queue, coz first element is deleted bt dequeue
}
}