Diferență între revizuiri ale paginii „OOP Lab Task 2”

De la WikiLabs
Jump to navigationJump to search
Linia 9: Linia 9:
 
== Requirements ==
 
== Requirements ==
  
* Create a Java application project whose name is <span style="font-family:'Lucida Console', monospace">oop_lab2</span>.
+
* Create a Java application project, or open your previous one.
 
* Add to your project the class '''Message''' exactly as defined in OOP Lab Task 1.
 
* Add to your project the class '''Message''' exactly as defined in OOP Lab Task 1.
* Add another class, named '''Inbox''', that encapsulates an array of Message objects. The Inbox class fields are:
+
* Add another class, named '''Inbox''', that encapsulates an array of Message objects and treats them as a queue of messages. The Inbox class fields are:
** a reference to an array of Message objects;
+
** a reference (immutable) to an array of Message objects;
** a constant that defines the size of the array;
+
** two integers that denotes the head of the queue (that points to the oldest unread message) and its tail (the place where the next message will be stored);
** two integers that denotes the index of the next free cell in the array and of the oldest unread message;
+
* All fields are private. The queue is handled only through the public methods of this class.
* Add to the Inbox class a constructor without arguments that instantiates an array of Messages whose size is given by the constant field.
+
* Add to the Inbox class a constructor that instantiates the encapsulated array of Messages, whose length is given as the constructor's sole argument. This constructor sets also the head and the tail of the message queue.
* Add a method, named <span style="font-family:'Lucida Console', monospace">add</span>, which does not return anything and has only one argument, a Message. The method put the new message in inbox at the location indicated by the index of the next free cell.
+
* Add a method, named <span style="font-family:'Lucida Console', monospace">add</span>, which does not return anything and has only one argument, a Message. This method adds the new message to the queue by setting the element at the tail to point to it. Then advances the tail to the next element of the array. If the queue is already full the message is not added and the method outputs "The inbox is full" on the screen.
* Add a method, named <span style="font-family:'Lucida Console', monospace">read</span>, which takes no argument and returns the oldest message, as a formatted string containing the sender and the message content. This method calls one of the Message methods, and also advances the oldest unread message index to the next unread message. If there is no message to retrieve from the inbox, the method returns an empty string and does not advance the index.
+
* Add a method, named <span style="font-family:'Lucida Console', monospace">read</span>, which takes no argument and returns a reference to the oldest unread message. It also advances the head of the queue to the next unread message. If the inbox is empty, the method returns no reference and outputs "The inbox is empty" on the screen.
 
+
* The head and tail indexes wrap around to the first element of the array after they reach it's last element (the queue is implemented as a circular buffer).
* Write another class called '''Main''' (the default class created if you created a project within NetBeans), which should contain only the '''main''' method, which instantiates an inbox, adds three messages to the inbox (either created in advance, or instantiated as parameters of the add method calls), and displays the messages read from the inbox (repeatedly calling the inbox read method - try to call it one more time, to see that no more message is displayed).
+
* The third method, named '''list''', does not change the queue at all and returns a string that is formatted in the following way. If the queue is empty the method returns the string "There are no messages in inbox". Otherwise the first line is "Inbox messages from:" followed by one line for each message in the inbox, starting from the oldest message down to the newest. The lines are numbered and on each line appears only the name of the sender of that message. For example, if three messages are received from George, Mary and Andrew, the returned string, when printed on the screen must be:
 +
<syntaxhighlight lang="xorg_conf">
 +
Inbox messages from:
 +
1 George
 +
2 Mary
 +
3 Andrew
 +
</syntaxhighlight>
 +
* The listed numbers show the order of the messages from the oldest one to the newest. They do not necessarily correspond to the actual indexes in the array!
 +
* Write another class called '''Main''' (the default class created if you created a project within NetBeans), which should contain only the '''main''' method, which instantiates a small inbox, let's say of 3 or 4 locations, and play than at will with adding to, reading from, and listing the inbox. You may create separately some messages or you may instantiate them right in the call of the '''ad''' method:
 +
<code style="color:green;">inbox.add(new Message("Mary","Hello everybody!"));</code>
 +
where the constructor returns exactly the type of argument that the '''ad''' method expects.
  
 
== Submitting ==
 
== Submitting ==

Versiunea de la data 9 noiembrie 2016 16:55

Required Tutorials

Requirements

  • Create a Java application project, or open your previous one.
  • Add to your project the class Message exactly as defined in OOP Lab Task 1.
  • Add another class, named Inbox, that encapsulates an array of Message objects and treats them as a queue of messages. The Inbox class fields are:
    • a reference (immutable) to an array of Message objects;
    • two integers that denotes the head of the queue (that points to the oldest unread message) and its tail (the place where the next message will be stored);
  • All fields are private. The queue is handled only through the public methods of this class.
  • Add to the Inbox class a constructor that instantiates the encapsulated array of Messages, whose length is given as the constructor's sole argument. This constructor sets also the head and the tail of the message queue.
  • Add a method, named add, which does not return anything and has only one argument, a Message. This method adds the new message to the queue by setting the element at the tail to point to it. Then advances the tail to the next element of the array. If the queue is already full the message is not added and the method outputs "The inbox is full" on the screen.
  • Add a method, named read, which takes no argument and returns a reference to the oldest unread message. It also advances the head of the queue to the next unread message. If the inbox is empty, the method returns no reference and outputs "The inbox is empty" on the screen.
  • The head and tail indexes wrap around to the first element of the array after they reach it's last element (the queue is implemented as a circular buffer).
  • The third method, named list, does not change the queue at all and returns a string that is formatted in the following way. If the queue is empty the method returns the string "There are no messages in inbox". Otherwise the first line is "Inbox messages from:" followed by one line for each message in the inbox, starting from the oldest message down to the newest. The lines are numbered and on each line appears only the name of the sender of that message. For example, if three messages are received from George, Mary and Andrew, the returned string, when printed on the screen must be:
Inbox messages from:
1 George
2 Mary
3 Andrew
  • The listed numbers show the order of the messages from the oldest one to the newest. They do not necessarily correspond to the actual indexes in the array!
  • Write another class called Main (the default class created if you created a project within NetBeans), which should contain only the main method, which instantiates a small inbox, let's say of 3 or 4 locations, and play than at will with adding to, reading from, and listing the inbox. You may create separately some messages or you may instantiate them right in the call of the ad method:

inbox.add(new Message("Mary","Hello everybody!")); where the constructor returns exactly the type of argument that the ad method expects.

Submitting

  • The assignment will be evaluated automatically by the Web-CAT platform.
  • You could access the Web-CAT platform using the username and the password with which you acces the electronica.curs.pub.ro intranet.
  • Select the OOP Lab Task 2 assignment.
  • Submit your work as a single .zip archive (give it whatever name you choose) containing only the Java source code files.
  • Attention Any deviation from these instructions may lead to the loss of the entire amount of points.