OOP Lab Task 4

De la WikiLabs

Required Tutorials


  1. Add to the labutil package a class named ServerConfig whose purpose is to hold 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 must have two constructors:
      1. One with an argument of type String, which is the name of the file to read from;
      2. One without arguments, which assumes that the name of the configuration file is server.cfg (use exactly this name in your code).
    • Upon its instantiation the ServerConfig object reads the file, line by line, until its end, and retains the values of the congiguration parameters. If the configuration file cannot be accessed or does not obey its format, the instantiation fails and throws an exception according to the cause of failure.
    • The constructors in class ServerConfig must throw exceptions if any issue arises 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). This exception retains also the offending name as its detail message string.
      4. MissingKeyException if one of the valid properties is missing from the file (this type of exception doesn't exist and it needs to be defined).
    • The ServerConfig class must be immutable. The configuration parameters read from file must be stored as instance constants inside the ServerConfig object.
    • The class must have a public method, named read, with only one argument, a string. When called with a valid configuration parameter name the method returns the value of that parameter, otherwise it throws an exception of type UnknownKeyException, with the invalid name given as the exception's detail message string.
  2. 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 types of exceptions which must be defined have very simple implementations. You need to define them as classes that extend their base classes. Only the UnknownKeyException class has something in its body: a constructor with an argument of type String needed to get the invalid/unknown configuration parameter name, and that constructor will simply call the constructor of the super-class with that name as argument.
  3. Only these following configuration parameter names are allowed in the configuration file:
    • TCP_PORT - the TCP port on which the server will be listening on.
    • MAX_CLIENTS - the maximum number of clients that the server application may serve.
  4. All property values are integers.
  5. 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 the ServerConfig class constructor reads is a plain text file, containing only comments, blank spaces, and parameter-value pairs.

  • A parameter-value pair must be written as an assignment:
  • Spaces or tabs around the equal sign are also 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, or are empty will also be ignored.
  • Spaces or tabs at the beginning and end of lines are ignored.

An example of a configuration file:

#this is a config file for John's server

  # changed by Mary

    TCP_PORT = 9000


  • Be carefull to name your classes exactly ServerConfig, Main, ConfigureException, InvalidFormatException, UnknownKeyException and MissingKeyException. Also the public method of the ServerConfig class must be named read.
  • All these classes are part of the labutil package.
  • Some of 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
  • To be able to read from a text file, instantiate a BufferedReader object and use a BufferedReader reference, so that to be able to call its method readLine. At each call this method returns the next line of the file as a string (but without the newline character). After the end of file is reached the method returns a null string.
  • For the BufferedReader object you need only its constructor and the methods readLine and close.
  • Instantiate the BufferedReader object with the following constructor:
BufferedReader(new FileReader(filename))

where filename is a String variable that holds the name of the configuration file.

  • To close the configuration file simply call the close method for the BufferedReader object.


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