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 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...
(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,    ...
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,...
. 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...
Please create an array of Leg objects, one constructor that takes three parameters as constant C...
Please create an array of Leg objects, one constructor that takes three parameters as constant C string, and one number representing the distance in miles between the two cities Write a code block to create a static array (that is, not dynamic and not a vector) of 3 Leg objects using city names of your choosing. That's THREE objects, each created using THREE parameters. For example, the Leg class declaration looked like, class Leg { const char* const startCity; const...
Create an array named data that can store 100 integers. Write a for loop that loops...
Create an array named data that can store 100 integers. Write a for loop that loops 100 times. Inside the loop store the variable you are using to increment the loop into the current element of the array data. So the array data should have a 0 at index 0, a 1 at index 1, a 2 at index 2, ..., a 99 at index 99.
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:...
visual studio VB.Net write a vb.net program to create an array of 15 structures, each structure...
visual studio VB.Net write a vb.net program to create an array of 15 structures, each structure contains the following fields: user name, number of posts, then write a sub to read from the keyboard values into the structures fields and write a function to find and return the average of number of posts for all users and write a sub to print the user name that has zero number of posts.[ number of posts could be from 0 or more]