0% found this document useful (0 votes)
515 views

Java Programming Lab Test

The document describes a laboratory exercise on queues. It provides two activities to debug and modify code to produce specific outputs. The first activity deals with a priority queue class and the second deals with a queue class. Students are expected to be able to identify how queues are used as a data structure and debug given programs to obtain required outputs.

Uploaded by

jesreelamorganda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
515 views

Java Programming Lab Test

The document describes a laboratory exercise on queues. It provides two activities to debug and modify code to produce specific outputs. The first activity deals with a priority queue class and the second deals with a queue class. Students are expected to be able to identify how queues are used as a data structure and debug given programs to obtain required outputs.

Uploaded by

jesreelamorganda
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

G0016

Laboratory Exercise 6

Queues
Objectives:
At the end of the exercise, the students should be able to:
Identify how queues are used as data structure
Debug the given program to obtain the required outputs

Materials:

PC with JCreator or any Java Editor


1 Flash disk

Procedures:
Activity1
Given the program below, debug and modify the application to acquire the following outputs:
o 36.0 98.0 546.0 687.0 957.0
o 546.0 687.0 36.0 98.0 957.0
class PriorityQ {
private int maxSize;
private double[] queArray;
private int nItems;
//-----------------------------------------------------------public Priority(int s) {
maxSize = s;
queArray = new double[maxsize];
nItems = 0;
}
//----------------------------------------------------------public void insert(double item) {
int j;
if(nItems==0)
queArray[nItems++] = item;
else {
for(j=nItems-1; j>=0; j--) {
if(item > queArray[j])
queArray[i+1] = item;
else
break;
}
queArray[j+1] = item;
nItems--;
}
}
//----------------------------------------------------------public double remove() {
return queArray[--nItems];
}
//----------------------------------------------------------public double peekMin() {
return queArray[nItems-1];
}
//----------------------------------------------------------public boolean isEmpty() {
return(nItems==0);
}
Laboratory Exercise 6

*Property of STI
Page 1 of 3

G0016

//----------------------------------------------------------public boolean isFull() {


return(nItems == maxSize);
}
}
//----------------------------------------------------------class PriorityQApp {
public static void main(String[] args) throws IOException {
PriorityQ thePQ = new PriorityQ(5);
thePQ.insert(546);
thePQ.insert(687);
thePQ.insert(36);
thePQ.insert(98);
thePQ.insert(957);
while( !thePQ.isempty() ) {
double item = thePQ.remove();
System.out.print(item + " ");
}
System.out.println("");
}
}
Activity2
Given the program below, debug and modify the application to acquire the following outputs:
o 40 50 60 70 80
o 70 80 10 20 60
o 80 40 50 60 70 80
o 40 50 60 70 80 40
o 50 60 70 80 40
import java.io.*;
class Queue {
private int[] queArray;
private int front;
private int rear;
private int nItems;
//--------------------------------------------------------------public Queue(int s) {
maxSize = s;
queAray = new int[maxSize];
front = 0;
rear = -1;
nItems = 0;
}
//--------------------------------------------------------------public void insert(int j) {
if(rear == nItems-1)
rear = -1;
queArray[++rear] = j;
nItems-;
}
//--------------------------------------------------------------public int remove() {
int temp = queArray[rear++];
if(front == maxSize)
rear = 0;
nItems--;
return temp--;
}
Laboratory Exercise 6

*Property of STI
Page 2 of 3

G0016

//--------------------------------------------------------------public int peekFront() {


return queArray[front];
}
//--------------------------------------------------------------public boolean isEmpty() {
return (maxsize==0);
}
//--------------------------------------------------------------public boolean isFull() {
return(nItems==maxSize);
}
//--------------------------------------------------------------public int size() {
return nItems;
}
//--------------------------------------------------------------}
/////////////////////////////////////////////////////////////////
class QueueApp {
public static void main(String[] args) {
Queue theQueue = new Queue(5);
theQueue.insert(10);
theQueue.insert(20);
theQueue.insert(30);
theQueue.insert(40);
theQueue.remove();
theQueue.remove();
theQueue.remove();
theQueue.insert(50);
theQueue.insert(60);
theQueue.insert(70);
theQueue.insert(80);
while ( !theQueue.isEmpty() ) {
int n = theQueue.remove();
System.out.print(n);
}
System.out.println(" ");
}
}
Activity3
Explain on the space provided below what the two activities do.

Laboratory Exercise 6

*Property of STI
Page 3 of 3

You might also like