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

De la WikiLabs
Jump to navigationJump to search
Linia 13: Linia 13:
 
== Requirements ==
 
== Requirements ==
  
* In method '''main''' of class '''TestClass''', instantiate on object of type [http://docs.oracle.com/javase/7/docs/api/java/io/FileOutputStream.html java.io.FileOutputStream] in which to write, on each line, the contents of the fields of the formerly instantiated '''Message''' and '''SystemMessage''' objects, separating fields by the string "<->" (E.g.: "John<->Mary<->Hello!"). The name of the file in which the data is written is given as an argument for the '''main''' method, at runtime. '''NOTE:''' It is permitted to add an additional method in classes '''Message''' and '''SystemMessage''';
+
# 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 '''StreamTester''' which should only contain the '''main''' method which should, just like the previous exercise, take a file name as an argument (the one written by the TestClass above). By using classes [http://docs.oracle.com/javase/7/docs/api/java/io/BufferedReader.html java.io.BufferedReader], [http://docs.oracle.com/javase/7/docs/api/java/io/InputStreamReader.html java.io.InputStreamReader] and [http://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html java.io.FileInputStream], read, line by line, the contents of the file, and then instantiate and print the appropriate objects ('''Message''' or '''SystemMessage'''). '''NOTE:''' You can use method '''split''' in class [http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) java.lang.String].
+
#* The class needs to have two constructors:
* By catching exceptions thrown by the various methods used, if the file to read does not exist, the message "File not found" should be displayed in the console. If this file can't be read or written, "IO operation failed" should be displayed. If any of the lines written by '''StreamTester''' does not match the template, the message "Line <line content> does not match the expected template" should be printed, and the program should skip to the next line in the file.
+
#*# One with an argument of type String, which represents the file to be read
 +
#*# One without any arguments, which will implicitly consider the configuration file to be ''server.conf''.
 +
#* 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:
 +
#*# '''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).
 +
#*# '''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).
 +
#*# '''UnknownKeyException''' if an unknown property is present in 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 created).
 +
#* The class must be immutable.
 +
# The following properties are the only ones valid and 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 an serve.
 +
# 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 class '''ServerConfig''' is reading is a text file, containing lines of the following form:
 +
<syntaxhighlight lang="xorg_conf">
 +
PROPERTY=value
 +
</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.
 +
* Lines that do not contain any printable character but only spaces and/ or tabs will also be ignored.
 +
* Spaces or tabs from the beginning and end of lines are ignored.
 +
An example of a configuration file:
 +
 
 +
<syntaxhighlight lang="xorg_conf">
 +
#this is a config file for the chat server!
 +
      #  this is made by ME!
 +
MAX_CLIENT=100
 +
      TCP_PORT=9000
 +
</syntaxhighlight>
 +
 
 +
 
 +
'''Note:'''
 +
* Cele trei tipuri de excepții care trebuie definite au implementări foarte simple. Trebuie definite ca niște clase care extind clasa '''Exception''', și nu este nevoie să conțină decât definiția unui constructor cu un argument de tip String care nu face altceva decât să apeleze constructorul superclasei.
 +
* Fișierele predate vor fi implementările Java a următoarelor clase: '''ServerConfig''', '''Main''', '''Message''', '''PrivateMessage''', '''InvalidFormatException''', '''UnknownKeyException''' și '''MissingKeyException'''.
 +
* Următoarele clase și metode vă pot fi de folos în realizarea temei. Căutați-le în documentația API:
 +
** String.trim
 +
** String.charAt
 +
** String.contains
 +
** String.startsWith
 +
** String.equals
 +
** String.split
 +
** Integer.parseInt
 +
** BufferedReader.readLine
 +
* Din cauza faptului că puteți avea două obiecte de tip '''String''' diferite (cu referințe diferite) dar care conțin același text, iar operatorul == compară referințele, modul corect prin care se compară două String-uri este prin apelarea metodei equals:
 +
<syntaxhighlight lang="java">
 +
String a = "abc";
 +
String b = new String(a);
 +
if(a == b) { // <- incorect, evaluarea va întoarce false
 +
  //...
 +
}
 +
if(a.equals(b)) { // <- corect, evaluarea va întoarce true
 +
  //...
 +
}
 +
 
 +
</syntaxhighlight >
 +
 
 +
Submitting:
 +
* The assignment is submitted attaching the Java source code files exclusively to an e-mail sent to radu.hobincu@upb.ro.
 +
* The subject of the e-mail will be [POO_3]
 +
* The body of the e-mail will contain the name and group of the student and any additional command you want to make.
 +
* <font color="red">'''Attention'''</font>: Any deviation from these instructions may lead to the loss of the entire amount of points.

Versiunea de la data 23 noiembrie 2014 20:33

Required Tutorials

Requirements

  1. 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:
    • The class needs to have two constructors:
      1. One with an argument of type String, which represents the file to be read
      2. One without any arguments, which will implicitly consider the configuration file to be server.conf.
    • 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).
      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 created).
      3. UnknownKeyException if an unknown property is present in the file (this type of exception doesn't exist and it needs to be created).
      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 created).
    • The class must be immutable.
  2. The following properties are the only ones valid and 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 an serve.
  3. 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 class ServerConfig is reading is a text file, containing lines of the following form:

PROPERTY=value
  • 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.
  • Lines that do not contain any printable character but only spaces and/ or tabs will also be ignored.
  • Spaces or tabs from the beginning and end of lines are ignored.

An example of a configuration file:

#this is a config file for the chat server!
      #   this is made by ME!
MAX_CLIENT=100
       TCP_PORT=9000


Note:

  • Cele trei tipuri de excepții care trebuie definite au implementări foarte simple. Trebuie definite ca niște clase care extind clasa Exception, și nu este nevoie să conțină decât definiția unui constructor cu un argument de tip String care nu face altceva decât să apeleze constructorul superclasei.
  • Fișierele predate vor fi implementările Java a următoarelor clase: ServerConfig, Main, Message, PrivateMessage, InvalidFormatException, UnknownKeyException și MissingKeyException.
  • Următoarele clase și metode vă pot fi de folos în realizarea temei. Căutați-le în documentația API:
    • String.trim
    • String.charAt
    • String.contains
    • String.startsWith
    • String.equals
    • String.split
    • Integer.parseInt
    • BufferedReader.readLine
  • Din cauza faptului că puteți avea două obiecte de tip String diferite (cu referințe diferite) dar care conțin același text, iar operatorul == compară referințele, modul corect prin care se compară două String-uri este prin apelarea metodei equals:
String a = "abc";
String b = new String(a);
if(a == b) { // <- incorect, evaluarea va întoarce false
  //...
}
if(a.equals(b)) { // <- corect, evaluarea va întoarce true
  //...
}

Submitting:

  • The assignment is submitted attaching the Java source code files exclusively to an e-mail sent to radu.hobincu@upb.ro.
  • The subject of the e-mail will be [POO_3]
  • The body of the e-mail will contain the name and group of the student and any additional command you want to make.
  • Attention: Any deviation from these instructions may lead to the loss of the entire amount of points.