OOP Lab Task 4: Diferență între versiuni

De la WikiLabs
(Submitting)
(Requirements)
Linia 13: Linia 13:
 
== Requirements ==
 
== Requirements ==
  
# Write a class called '''ServerConfig''' which has the role of reading configuration values from a text file. This class will be used later on during the development of the server application, and needs to meet the following requirements:
+
# Write a class called '''ServerConfig''' whose purpose is to store configuration values read from a text file. This class will be used later on during the development of the server application, and needs to meet the following requirements:
#* The class needs to have two constructors:
+
#* The class is required to have two constructors:
#*# One with an argument of type String, which represents the file to be read
+
#*# One with an argument of type String, which is the name of the file to be read
#*# One without any arguments, which will implicitly consider the configuration file to be ''server.conf''.
+
#*# One without any arguments, which will implicitly consider the configuration file to be ''server.cfg''.
 
#* Objects of type '''ServerConfig''' have to read the file, line by line, and then expose the parameters that were read through methods.
 
#* Objects of type '''ServerConfig''' have to read the file, line by line, and then expose the parameters that were read through methods.
 
#* The methods in class '''ServerConfig''' must throw exceptions if any issues arise when reading or parsing the file:
 
#* The methods in class '''ServerConfig''' must throw exceptions if any issues arise when reading or parsing the file:
#*# '''IOException''' if the file is missing or it cannot be opened (this exception is already being thrown by stream related classes, so you only need to rethrow it further up the execution stack).
+
#*# '''IOException''' if the file is missing or it cannot be opened (this exception is already being thrown by stream related classes, so you only need to rethrow it further up the execution stack or declare that it is thrown).
#*# '''InvalidFormatException''' if at least one line from the file doesn't match the expected pattern (this type of exception doesn't exist and it needs to be created).
+
#*# '''InvalidFormatException''' if at least one line from the file doesn't match the expected pattern (this type of exception doesn't exist and it needs to be defined).
#*# '''UnknownKeyException''' if an unknown property is present in the file (this type of exception doesn't exist and it needs to be created).
+
#*# '''UnknownKeyException''' if an unknown property is present in the file (this type of exception doesn't exist and it needs to be defined).
#*# '''MissingKeyException''' if one of the expected properties is missing from the file (this type of exception doesn't exist and it needs to be created).
+
#*# '''MissingKeyException''' if one of the expected properties is missing from the file (this type of exception doesn't exist and it needs to be defined).
#* The class must be immutable.
+
#* '''InvalidFormatException''', '''UnknownKeyException''' and '''MissingKeyException''' types are all derived from the '''ConfigureException''' type, which is in its turn derived from '''Exception''' class, and must also be defined.
# The following properties are the only ones valid and must exist in the configuration file:
+
#* The '''ServerConfig''' class must be immutable. The configuration parameters read from file must be stored as instance constants inside the '''ServerConfig''' object.
 +
 
 +
# Only the following properties must exist in the configuration file:
 
#* TCP_PORT - representing the TCP port on which the server will be listening on.
 
#* TCP_PORT - representing the TCP port on which the server will be listening on.
#* MAX_CLIENTS - the maximum number of clients that the server an serve.
+
#* MAX_CLIENTS - the maximum number of clients that the server application may serve.
 +
# The property values are integers.
 +
 
 
# Write a test for this class (which should open a file, read its contents and print the read parameters on the screen) and add it to the main method in class '''Main'''.
 
# Write a test for this class (which should open a file, read its contents and print the read parameters on the screen) and add it to the main method in class '''Main'''.
  
 
+
The configuration file that '''ServerConfig''' class constructor is reading is a plain text file, containing lines of the following form:
The configuration file that class '''ServerConfig''' is reading is a text file, containing lines of the following form:
 
 
<syntaxhighlight lang="xorg_conf">
 
<syntaxhighlight lang="xorg_conf">
 
PROPERTY=value
 
PROPERTY=value
 
</syntaxhighlight>
 
</syntaxhighlight>
* Any line starting with thr character #, preceded or not by spaces or "tab" characters is considered to be a comment and it will be ignored.
+
* Any line starting with the character #, preceded or not by spaces or "tab" characters is considered to be a comment and will be ignored.
* Lines that do not contain any printable character but only spaces and/ or tabs will also be ignored.
+
* Lines that do not contain any printable character but only spaces and/or tabs, or are empty will also be ignored.
* Spaces or tabs from the beginning and end of lines are ignored.
+
* Spaces or tabs at the beginning and end of lines are ignored.
 +
* Spaces or tabs around the equal sign are also ignored.
 
An example of a configuration file:
 
An example of a configuration file:
  
 
<syntaxhighlight lang="xorg_conf">
 
<syntaxhighlight lang="xorg_conf">
#this is a config file for the chat server!
+
#this is a config file for John's server
      #  this is made by ME!
+
 
 
MAX_CLIENT=100
 
MAX_CLIENT=100
      TCP_PORT=9000
+
  # changed by Mary
 +
 
 +
    TCP_PORT = 9000
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
'''Notes:'''
 +
* The types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend their base classes. they only need to contain a constructor with an argument of type String that only calls the constructor of the super-class.
 +
* The submitted files will be only Java implementations of the following classes: '''ServerConfig''', '''Main''', '''ConfigureException''', '''InvalidFormatException''', '''UnknownKeyException''' and '''MissingKeyException'''.
  
'''Notes:'''
+
* The following classes and methods may be of use for your implementation. Look them up in the API documentation:
* The three types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend class '''Exception''' and they only need to contain a constructor with an argument of type String that only calls the constructor of the super-class of the same type.
 
* The submitted files will be only Java implementations of the following classes: '''ServerConfig''', '''Main''', '''Message''', '''PrivateMessage''', '''InvalidFormatException''', '''UnknownKeyException''' and '''MissingKeyException'''.
 
* The following classes and methods may be of use in implementing this assignment. Look them up in the API documentation:
 
 
** String.trim
 
** String.trim
 
** String.charAt
 
** String.charAt
Linia 59: Linia 65:
 
** Integer.parseInt
 
** Integer.parseInt
 
** BufferedReader.readLine
 
** BufferedReader.readLine
* Because you can have two different objects of type '''String''' (with different references) which contain the same character sequence, and because operator == is used to compare references, the correct way to compare two Strings is by calling method equals:
+
 
 +
* Because you can have two different objects of type '''String''' (with different references) which contain the same character sequence, and because the operator == is used to compare references, the correct way to compare two Strings is by calling the method ''equals'':
 
<syntaxhighlight lang="java">
 
<syntaxhighlight lang="java">
 
String a = "abc";
 
String a = "abc";
Linia 69: Linia 76:
 
   //...
 
   //...
 
}
 
}
 
 
</syntaxhighlight >
 
</syntaxhighlight >
  

Versiunea de la data 1 decembrie 2015 22:52

Required Tutorials

Requirements

  1. Write a class called ServerConfig whose purpose is to store configuration values read from a text file. This class will be used later on during the development of the server application, and needs to meet the following requirements:
    • The class is required to have two constructors:
      1. One with an argument of type String, which is the name of the file to be read
      2. One without any arguments, which will implicitly consider the configuration file to be server.cfg.
    • Objects of type ServerConfig have to read the file, line by line, and then expose the parameters that were read through methods.
    • The methods in class ServerConfig must throw exceptions if any issues arise when reading or parsing the file:
      1. IOException if the file is missing or it cannot be opened (this exception is already being thrown by stream related classes, so you only need to rethrow it further up the execution stack or declare that it is thrown).
      2. InvalidFormatException if at least one line from the file doesn't match the expected pattern (this type of exception doesn't exist and it needs to be defined).
      3. UnknownKeyException if an unknown property is present in the file (this type of exception doesn't exist and it needs to be defined).
      4. MissingKeyException if one of the expected properties is missing from the file (this type of exception doesn't exist and it needs to be defined).
    • InvalidFormatException, UnknownKeyException and MissingKeyException types are all derived from the ConfigureException type, which is in its turn derived from Exception class, and must also be defined.
    • The ServerConfig class must be immutable. The configuration parameters read from file must be stored as instance constants inside the ServerConfig object.
  1. Only the following properties must exist in the configuration file:
    • TCP_PORT - representing the TCP port on which the server will be listening on.
    • MAX_CLIENTS - the maximum number of clients that the server application may serve.
  2. The property values are integers.
  1. Write a test for this class (which should open a file, read its contents and print the read parameters on the screen) and add it to the main method in class Main.

The configuration file that ServerConfig class constructor is reading is a plain text file, containing lines of the following form:

PROPERTY=value
  • Any line starting with the character #, preceded or not by spaces or "tab" characters is considered to be a comment and will be ignored.
  • Lines that do not contain any printable character but only spaces and/or tabs, or are empty will also be ignored.
  • Spaces or tabs at the beginning and end of lines are ignored.
  • Spaces or tabs around the equal sign are also ignored.

An example of a configuration file:

#this is a config file for John's server

MAX_CLIENT=100
  # changed by Mary

    TCP_PORT = 9000

Notes:

  • The types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend their base classes. they only need to contain a constructor with an argument of type String that only calls the constructor of the super-class.
  • The submitted files will be only Java implementations of the following classes: ServerConfig, Main, ConfigureException, InvalidFormatException, UnknownKeyException and MissingKeyException.
  • The following classes and methods may be of use for your implementation. Look them up in the API documentation:
    • String.trim
    • String.charAt
    • String.contains
    • String.startsWith
    • String.equals
    • String.split
    • Integer.parseInt
    • BufferedReader.readLine
  • Because you can have two different objects of type String (with different references) which contain the same character sequence, and because the operator == is used to compare references, the correct way to compare two Strings is by calling the method equals:
String a = "abc";
String b = new String(a);
if(a == b) { // <- wrong, this is evaluated to false
  //...
}
if(a.equals(b)) { // <- correct, this is evaluated to true
  //...
}

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 4 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.