Skip to content

benko325/Java-pb162-seminar-project

Repository files navigation

Tenth iteration

Exercise focused on working with input and output.

Modify the LabeledPolygon.Builder class to implement the PolygonReadable interface. Modify the LabeledPolygon class to implement the PolygonWritable interface.

  1. The read(InputStream) method takes an open input containing named vertices, reads the vertices, and adds them to the existing vertices of the polygon. For any input/output error or input data format error, the method must atomically fail and throw an IOException. (atomically = loading all or nothing) The input data format is as follows:

    • The input is text.
    • Each vertex is on one line.
    • Each line is in the format "x y vertex name", i.e. first the coordinates of the vertex separated by a space and then the name of the vertex (the name can contain spaces). See, for example, the file polygon-ok.txt.
  2. The write(OutputStream) method writes vertices to a given output stream. The output format is the same as for the previous method.

  3. The write(File) and read(File) methods will work the same as before, however, they will work with the file instead of the I/O stream. Avoid code repetition!

  4. Create a writeJson(OutputStream os) method that will write a map in JSON format to the output stream.

    • Use external library gson. For maven, you need to add a dependency to the pom.xml file in the <dependencies> section.
    • Read the Gson class documentation.
    • According to documentation use the so-called pretty print.
    • An object of type Gson can be reused, so you only need to create it once.
  5. Edit the Demo class as follows:

    • The class creates LabeledPolygon from the file polygon-ok.txt.
    • The polygon will also contain a vertex named vertex x with coordinates [123, 456].
    • Write the output to the output stream System.out in JSON format.
    • To check that the output stream is still open then print Hello World!.
  6. Running the Draw class loads polygon-ok.txt and draws on the screen.

Hints

  • Only close streams/files that you have opened.
  • Use try with resources.
  • Study the methods Writer#flush(), Reader#ready().
  • Creating a file: new File("soubor.txt").
  • Demo.main can throw IOException.
  • Instead of \n, use the universal line break separator, System.lineSeparator().
  • The tests create a polygon-out.txt file.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages