given NonZeroNumQueue.txt public class NonZeroNumQueue{ private int[] data; priv
ID: 3711616 • Letter: G
Question
given
NonZeroNumQueue.txt
public class NonZeroNumQueue{
private int[] data;
private int total;
private int front;
private int tail;
public NonZeroNumQueue(){
data=new int[10];
total=0;
front=tail=0;
}
public void enqueue(int num){
//Implement this method here
}
public void dequeue(){
if(!isEmpty()){
total--;
front = (front+1) %data.length;
}
}
public int peek(){
if(isEmpty())
return 0; //0 means the queue is empty
return data[front];
}
public boolean isEmpty(){
return total==0;
}
public boolean isFull(){
return false;
}
public int size(){
return total;
}
}
Explanation / Answer
import java.io.*; class FileDemo { public static void main(String args[]) { FileReader fr = null; FileWriter fw = null; try { fr = new FileReader("1.txt"); fw = new FileWriter("2.txt"); int c = fr.read(); while(c!=-1) { fw.write(c); c = fr.read(); } } catch(IOException e) { e.printStackTrace(); } finally { close(fr); close(fw); } } public static void close(Closeable stream) { try { if (stream != null) { stream.close(); } } catch(IOException e) { //... } } } import java.io.*; class FileDemo { public static void main(String args[]) { //this will close the resources automatically //even if an exception rises try (FileReader fr = new FileReader("1.txt"); FileWriter fw = new FileWriter("2.txt")) { int c = fr.read(); while(c!=-1) { fw.write(c); c = fr.read(); } } catch(IOException e) { e.printStackTrace(); } } }
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.