Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

java program Exercise 4: A queue is a first in first out linear data structure i

ID: 3756553 • Letter: J

Question

java program

Exercise 4: A queue is a first in first out linear data structure in which items are added at one end and removed from the other end. A generic Queue class can be built with the following specification Queue0 void enqueue T dequeue0 int size0: boolean isEmpty): void clear) T peekO creates an empty queue add new item to th remove and return the item from the front of the queue return the number of elements in the queue return true if the queue is empty, false otherwise clear the queue return the entry from the front of the queue, null if the queue is empty In addition to the above basic operations, it is useful to have the following for queue applications int positionOf(T item): Return the position of the specified item and -1 if not found. void remove(T item) Remove the first occurrence (from front) of specified item T first0: T next0: Return the first item in the queue(front), null if queue is empty Return the next item in the queue relative to the previous call to first or next. Returrn null if end of queue is reached. Implement the generic Queue class using an ArrayList to store the Queue elements. Ensure appropriate error checks. public class GenericQueueT private ArrayList

Explanation / Answer

import java.io.BufferedReader;
import java.io.File;
import java.util.Scanner;
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.OutputStream;

public class EncapsDecaps{

public static void main(String[] args) {

byte[] bytes = new byte[4098];
File file = new File("demo.txt");
Encapsulation(bytes);
try {
OutputStream os = new FileOutputStream(file);
os.write(bytes);
os.close();
Decapsulation(new File("demo.txt"));
} catch (Exception e) {
e.printStackTrace();
}
}
public static void Encapsulation(byte[] bytes){
Scanner sc = new Scanner(System.in);
System.out.println("Enter The Message : ");
String in = sc.nextLine();
byte[] msg = in.getBytes();
System.out.println("Enter The Person details : ");
in = sc.nextLine();
byte[] pd = in.getBytes();
System.out.println("Enter The Address : ");
in = sc.nextLine();
byte[] ad = in.getBytes();
System.out.println("Enter The State : ");
in = sc.nextLine();
byte[] sd = in.getBytes();
int c = 0;
for(int i = 3072;i<bytes.length;i++){
if(c < msg.length){
bytes[i] = msg[c++];
}else{
bytes[i] = ' ';
}
}
c = 0;
for(int i = 2048;i<3072;i++){
if(c < pd.length){
bytes[i] = pd[c++];
}else{
bytes[i] = ' ';
}
}
c = 0;
for(int i = 1024;i<2048;i++){
if(c < ad.length){
bytes[i] = ad[c++];
}else{
bytes[i] = ' ';
}
}
c = 0;
for(int i = 0;i<1024;i++){
if(c < sd.length){
bytes[i] = sd[c++];
}else{
bytes[i] = ' ';
}
}
}
public static void Decapsulation(File file) throws Exception {
byte[] bytesArray = new byte[(int) file.length()];

FileInputStream fis = new FileInputStream(file);
fis.read(bytesArray); //read file into bytes[]
fis.close();
byte[] msg = new byte[1024];
byte[] pd = new byte[1024];
byte[] ad = new byte[1024];
byte[] sd = new byte[1024];
int c = 0;
for(int i = 0;i<1024;i++){
sd[i] = bytesArray[c++];
}
System.out.println("Printing Decapsulation Datas : ");
System.out.println("State : "+ (new String(sd)).trim());
for(int i = 0;i<1024;i++){
ad[i] = bytesArray[c++];
}
System.out.println("Address : "+(new String(ad)).trim());
for(int i = 0;i<1024;i++){
pd[i] = bytesArray[c++];
}
System.out.println("Person : "+(new String(pd)).trim());
for(int i = 0;i<1024;i++){
msg[i] = bytesArray[c++];
}
System.out.println("Message : "+(new String(msg)).trim());
}