Question

Create UML class diagram extension for an array data structure support of the iterator pattern. •...

Create UML class diagram extension for an array data structure support of the iterator pattern. • Write pseudo code to support iterator pattern for an array data structure

Homework Answers

Answer #1

UML CLASS DIAGRAM

// A Java program to demonstrate implementation

// of iterator pattern with the example of

// notifications

// A simple Notification class

class Notification

{

               // To store notification message

               String notification;

               public Notification(String notification)

               {

                              this.notification = notification;

               }

               public String getNotification()

               {

                              return notification;

               }

}

// Collection interface

interface Collection

{

               public Iterator createIterator();

}

// Collection of notifications

class NotificationCollection implements Collection

{

               static final int MAX_ITEMS = 6;

               int numberOfItems = 0;

               Notification[] notificationList;

               public NotificationCollection()

               {

                              notificationList = new Notification[MAX_ITEMS];

                              // Let us add some dummy notifications

                              addItem("Notification 1");

                              addItem("Notification 2");

                              addItem("Notification 3");

               }

               public void addItem(String str)

               {

                              Notification notification = new Notification(str);

                              if (numberOfItems >= MAX_ITEMS)

                                             System.err.println("Full");

                              else

                              {

                                             notificationList[numberOfItems] = notification;

                                             numberOfItems = numberOfItems + 1;

                              }

               }

               public Iterator createIterator()

               {

                              return new NotificationIterator(notificationList);

               }

}

// We could also use Java.Util.Iterator

interface Iterator

{

               // indicates whether there are more elements to

               // iterate over

               boolean hasNext();

               // returns the next element

               Object next();

}

// Notification iterator

class NotificationIterator implements Iterator

{

               Notification[] notificationList;

               // maintains curr pos of iterator over the array

               int pos = 0;

               // Constructor takes the array of notifiactionList are

               // going to iterate over.

               public NotificationIterator (Notification[] notificationList)

               {

                              this.notificationList = notificationList;

               }

               public Object next()

               {

                              // return next element in the array and increment pos

                              Notification notification = notificationList[pos];

                              pos += 1;

                              return notification;

               }

               public boolean hasNext()

               {

                              if (pos >= notificationList.length ||

                                             notificationList[pos] == null)

                                             return false;

                              else

                                             return true;

               }

}

// Contains collection of notifications as an object of

// NotificationCollection

class NotificationBar

{

               NotificationCollection notifications;

               public NotificationBar(NotificationCollection notifications)

               {

                              this.notifications = notifications;

               }

               public void printNotifications()

               {

                              Iterator iterator = notifications.createIterator();

                              System.out.println("-------NOTIFICATION BAR------------");

                              while (iterator.hasNext())

                              {

                                             Notification n = (Notification)iterator.next();

                                             System.out.println(n.getNotification());

                              }

               }

}

// Driver class

class Main

{

               public static void main(String args[])

               {

                              NotificationCollection nc = new NotificationCollection();

                              NotificationBar nb = new NotificationBar(nc);

                              nb.printNotifications();

               }

}

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a UML Class Diagram of a Food Supermarket. Show inheritance of common methods
Create a UML Class Diagram of a Food Supermarket. Show inheritance of common methods
Create a UML class hierarchy diagram (be sure to consult the notes so that you construct...
Create a UML class hierarchy diagram (be sure to consult the notes so that you construct the correct diagram) with a minimum of 6 classes for each of the categories below. Each diagram should include at least three examples of one class inheriting from another. Provide a couple of sentences in each case explaining the inheritance decisions behind your design. a. Technology relevant to remote learning b. A personal passion or hobby of choice
Create a UML class hierarchy diagram (be sure to consult the notes so that you construct...
Create a UML class hierarchy diagram (be sure to consult the notes so that you construct the correct diagram) with a minimum of 6 classes for each of the categories below. Each diagram should include at least three examples of one class inheriting from another. Provide a couple of sentences in each case explaining the inheritance decisions behind your design. a. Technology relevant to remote learning b. A personal passion or hobby of your choice
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to...
C++ //StudentDataStructure.txt //Student records are stored in a parallel-array Data Structure. Here is the code to generate and populate Parallel-Array Data Structure: const int NG = 4; //Number of Grades string names[] = {"Amy Adams", "Bob Barr", "Carla Carr", "Dan Dobbs", "Elena Evans" }; int exams[][NG]= { {98,87,93,88}, {78,86,82,91}, {66,71,85,94}, {72,63,77,69}, {91,83,76,60} };    --------------------------------------------------------------------------------- 1 A) Create and Populate a Parallel-Array Data Structure using the code described in "StudentDataStructure.txt". B) Define a Record Data Structure for student records. It...
The following is for a Java Program Create UML Class Diagram for these 4 java classes....
The following is for a Java Program Create UML Class Diagram for these 4 java classes. The diagram should include: 1) All instance variables, including type and access specifier (+, -); 2) All methods, including parameter list, return type and access specifier (+, -); 3) Include Generalization and Aggregation where appropriate. Java Classes description: 1. User Class 1.1 Subclass of Account class. 1.2 Instance variables __ 1.2.1 username – String __ 1.2.2 fullName – String __ 1.2.3 deptCode – int...
(Data Structure) Suppose we want to store the data of a binary heap in an array....
(Data Structure) Suppose we want to store the data of a binary heap in an array. Show the data of that array after inserting the given keys one by one. Assume that initially binary heap is empty. Show all steps of insertions. Note: No need to show the changes for each step of “percolate up”. The contents of the array must be shown after completing one insertion. Create the “Min Heap” for the following data set 7,      3,      18,    -9,    ...
. Create a Matlab script that will load the file finc.mat and create a structure from...
. Create a Matlab script that will load the file finc.mat and create a structure from the appropriate with the following fields: • Company (string) • First_Name (string) • Last_Name (string) • Address (string) • Credit_Card_Number (Integer) • Account_Balance (Real) • Credit_Score (Real) • Transaction (Array) To the right of the field names above are the data type in parentheses. Your code should calculate and display the following: • The descriptive statistics discussed in class of the Credit Scores for...
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
write C++ programs Array of Payroll Objects Design a PayRoll class that has data members for...
write C++ programs Array of Payroll Objects Design a PayRoll class that has data members for an employee’s hourly pay rate and number of hours worked. Write a program with an array of seven PayRoll objects. The program should read the number of hours each employee worked and their hourly pay rate from a file and call class functions to store this information in the appropriate objects. It should then call a class function, once for each object, to return...
The UML diagram bellow shows an example for simple chain of inheritance relationship where ClassC inherits...
The UML diagram bellow shows an example for simple chain of inheritance relationship where ClassC inherits ClassB’s members, including the ones that ClassB inherited from ClassA. ClassA constructor has a println statement that prints "Hello This is ClassA", ClassB constructor has a println statement that prints "Hello this is ClassB" and ClassC constructor has a println statement that prints "Hello this is ClassC". ClassC is the driver class. If you create an object of ClassC,you will get the following output:...