Создайте файл GenQDemo.java и введите в него приведенный ниже код. В этом коде демонстрируется работа обобщенной очереди. /* Проект 13.1. Демонстрация обобщенного класса очереди. */ class GenQDemo { public static void main(String args[]) { // создать очередь для хранения целых чисел Integer iStoref] = new Integer[10]; GenQueue q = new GenQueue(iStore); Integer iVal; System.out.println("Demonstrate a queue of Integers."); try { for(int i=0; i < 5; i++) { System.out.println("Adding " + i + " to the q."); q.put(i); // ввести целочисленное значение в очередь q } } catch (QueueFullException exc) { System.out.println(exc) ; } System.out.println; try { for(int i=0; i < 5; i++) { System.out.print("Getting next Integer from q: "); iVal = q.get; System.out.println(iVal); } } catch (QueueEmptyException exc) { System.out.println(exc); } System.out.println ; // создать очередь для хранения чисел с плавающей точкой Double dStore[] = new Double[10]; GenQueue q2 = new GenQueue(dStore); Double dVal; System.out.println("Demonstrate a queue of Doubles."); try { for(int i=0; i < 5; i++) { System.out.println("Adding " + (double)i/2 + " to the q2."); q2.put((double)i/2); // ввести значение типа double в очередь q2 } } catch (QueueFullException exc) { System.out.println(exc); } System.out.println; try { for(int i=0; i < 5; i++) { System.out.print("Getting next Double from q2: "); dVal = q2.get ; System.out.println(dVal); } } catch (QueueEmptyException exc) { System.out.println(exc); } } }

Скомпилируйте программу и запустите ее на выполнение. В итоге на экране отобразится следующий результат: ``` Demonstrate a queue of Integers. Adding 0 to the q. Adding 1 to the q. Adding 2 to the q. Adding 3 to the q. Adding 4 to the q.

Getting next Integer from q: 0 Getting next Integer from q: 1 Getting next Integer from q: 2 Getting next Integer from q: 3 Getting next Integer from q: 4

Demonstrate a queue of Doubles. Adding 0.0 to the q2. Adding 0.5 to the q2. Adding 1.0 to the q2. Adding 1.5 to the q2. Adding 2.0 to the q2.

Перейти на страницу:

Похожие книги