OOP Lab Task 2: Diferență între versiuni

De la WikiLabs
 
(Requirements)
 
(Nu s-au afișat 22 de versiuni intermediare efectuate de alți 2 utilizatori)
Linia 6: Linia 6:
 
* [[Java Syntax; A Program's Lexical Structure]]
 
* [[Java Syntax; A Program's Lexical Structure]]
 
* [[Coding Conventions]]
 
* [[Coding Conventions]]
* [[Advanced Notions About Object Oriented Programming]]
 
* [[Java Application Programming Interface (API) (EN)]]
 
  
 
== Requirements ==
 
== Requirements ==
  
* Add two constructors to class '''Message''', accessible to all classes, one that should have arguments for all three fields ('''receiver''', '''sender''' și '''content'''), and one that should only have arguments for sender and content, receiver keeping its default '''null''' value;
+
* Create a Java application project, or open your previous one.
* Add access modifiers to the class members so that:
+
* Add to your project (if it's not already there) the class '''Message''' exactly as defined in OOP Lab Task 1. You may copy it from your uploaded archive of the previous assignment on the Web-CAT.
** the three fields should only be accessible in the class; encapsulate them by adding ''getters'', accessible to all classes;
+
* To this class add a new method, <span style="color:brown;font-weight: bold;font-family:'Lucida Console', monospace">getName</span>, without arguments, that returns only the name of the sender.
** method '''print()''' should be accessible in all classes;
+
* Add to the package '''labutil''' another class, named '''Inbox''', that encapsulates an array of Message objects and treats them as a queue of messages. The Inbox class fields are:
* Override '''toString()''' method (defined in '''java.lang.Object''' class) so that it returns a character string formed similarly with the chat messages: ''sender: receiver'';
+
** a reference (immutable) to an array of Message objects;
* Create a class called '''SystemMessage''' which extends class '''Message'''. '''SystemMessage''' must contain an extra field of type ''int'' called '''type''', only accessible inside the class, and a ''getter'' for this field, accessible to all classes;
+
** two integers for the head of the queue (the head element points to the oldest unread message) and its tail (the tail element will store the reference to the next message to be appended to the queue);
* Add two constructors to class '''SystemMessage''', accessible to all classes, one that should only take an argument for initializing field "type", and the second for initializing both "type" and "sender" (see [[Java Syntax; A Program's Lexical Structure#Class Constructors]] and [[Java Syntax; A Program's Lexical Structure#this and super Keywords]]).
+
* All fields are private. The queue is handled only through the public methods of this class.  
* Add three class constants to class '''SystemMessage''', of type '''int''', having values 0, 1 and 2, named ''TYPE_ERROR'', ''TYPE_OK'', and ''TYPE_CHANGE_NAME'';
+
* 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.
* Override method '''toString()''' defined in class '''Message''', so that it only returns the value of field "type", as a String;
+
* Add a method, named <span style="color:brown;font-weight: bold;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.
* Change the instantiations in method '''main''' in class '''TestClass''' so that they use the new constructors. Add three more instantiations of object of type '''SystemMessage''' and display them in the console by using the '''toString()''' method.
+
* Add a method, named <span style="color:brown;font-weight: bold;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).
 +
* The third method, named <span style="color:brown;font-weight: bold;font-family:'Lucida Console', monospace">list</span>, 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 then play 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 '''add''' method, as below:
 +
<code style="color:green;">inbox.add(new Message("Mary","Hello everybody!"));</code>, where the constructor returns exactly the type of argument (reference to a Message object) that the '''add''' method expects.
 +
* Test also the inbox for the limit cases, trying inside the '''main''' method to read from an empty inbox, to write into a full inbox, or to list an empty one.
 +
 
 +
== Submitting ==
 +
* The assignment will be evaluated automatically by the [http://homework.dcae.pub.ro:/WebObjects/Web-CAT.woa Web-CAT] platform.
 +
* You could access the Web-CAT platform using the username and the password with which you acces the <span style="color:red">electronica.curs.pub.ro</span> 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.
 +
* <font color="red">'''Attention'''</font> Any deviation from these instructions may lead to the loss of the entire amount of points.

Versiunea curentă din 10 noiembrie 2016 14:34

Required Tutorials

Requirements

  • Create a Java application project, or open your previous one.
  • Add to your project (if it's not already there) the class Message exactly as defined in OOP Lab Task 1. You may copy it from your uploaded archive of the previous assignment on the Web-CAT.
  • To this class add a new method, getName, without arguments, that returns only the name of the sender.
  • Add to the package labutil 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 for the head of the queue (the head element points to the oldest unread message) and its tail (the tail element will store the reference to the next message to be appended to the queue);
  • 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 then play 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 add method, as below:

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

  • Test also the inbox for the limit cases, trying inside the main method to read from an empty inbox, to write into a full inbox, or to list an empty one.

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.