Joomla Vorlagen by Website Hosting

PL1 - 4th laboratory

lab04.zip

1. Create an interface Valuable that contains the getValue (): double method and implement a class AverageComputer, which has a method for counting the average value of objects in the array it receives as a parameter. Objects are of the same type - Valuable. Make sure you can use AverageComputer to calculate the average size of an area of shapes - rectangle, ellipse, triangle.

2. The project uses CanvasManager to address some of the drawbacks of the previous rendering solution. It can display all objects that implement the Paintable interface. Objects are rendered after they are registered using the add method. Modify the Triangle class to work with the CanvasManager mechanism - use the fillPolygon method in the MyGraphics class to render.

3. Connect the triangle and ellipse creation - ManipulationPanelController.createShape method.

4. Implement the option to delete shapes.

  • To do this, create a Clickable interface that includes the "isInBound (double x, double y): boolean" method, which returns "true" if the given coordinate is inside the object.
  • In the ManipulationPanelController.mousePressed method, add a treatment that if the deleteRB button is selected then all the registered shapes are processed (an array of subjects is returned by method registeredSubjects from CanvasManager)  and if a shape is Clickable (use operator instanceof) then it will verify that we clicked it (isInBounds) and eventually it will be removed.

 

UML

pl1 lab07 01

Average computer.

 

pl1 lab07 02

Graphic application

Solution

lab04-solution.zip

Homework:

Allow working with your shapes - drawing, deleting.

Create a class that will implement the motion algorithm. It will include a method that gets the object as a parameter, the new position, the length, and the time in ms. In this method there will be an algorithm that moves the object from the starting point to the ending in steps, between which there will be a delay specified as the parameter "time". The amount of displacement (the length of the vector by which the displacement occurs) in one step is determined by the "length" parameter. Define a suitable type for this purpose - an interface that allows you to determine the current position and set a new position.

Note: The motion implementing method will actually be a "template method" that calls abstract methods from the interface of the object being passed - see the "Template method" design pattern https://www.kozusznik.cz/resources/msps/gof/hires/pat5jfso.htm.

In contrast, as the pattern is defined, the "template method" is in a different class than the called abstract methods.

PJ1 - 6th laboratory

lab06.zip

Task 1

Create a class ResultInfo that will be Paintable and it will draw info about number of shoots.

pl1 lab06 01

Task 2

Create a class Ufo that will draw image Constant.UFO. Class will be Paintable and it will use GraphicsContext.drawImage.

 

pl1 lab06 02

 

Task 3

Place an object of Ufo on random position in the lower half – dimension of canvas will be set in constructor and position will be generated by Ufo.

 

Task 4

Create an interface Boundary with methods getXOfCorner, getYOfCorner, getWidth, getHeight, overlaps(other:Boundary). The method overlaps will be defined as a default method. The classes Ufo and Ball will implement the interface.

 

pl1 lab06 04

 

Task 5

When the ball hits the Ufo object increase a counter – value of the counter will be also displayed by an object of ResultInfo. For hit detection use the method overlaps. Show the hit counter with the number of shots.

 

Task 6

Place the Ufo object on new position in case that is hit (it will have method hit).

 

Task 7

Inherit from the class Ufo and create DisappearingUfo – it will be after every hit more transparent (10%). Use methods getGlobalAlpha/setGlobalAlpha in GraphicsContext. Use new class in simulation.

 

Solution (Task 1 - 6)

lab06-solution.zip

PJ1 - 9th laboratory (Streams)

This tutorial is based on an Oracle tutorial - https://docs.oracle.com/javase/tutorial/essential/io/streams.html - so you can visit the course website for more insight or self-study.

1. Create a program that copies the specified file using InputStream (FileInputStream) and OutputStream (FileOutputStream). Copying takes place by bytes. Use the faust-goethe-orig.txt file to test it.

2. Modify the previous example to copy using a buffer (use 1MB).

3. Program the previous example using Writer and Reader. Copying is by character and is converted to uppercase.

4. Use the Deflate method for the previous task (the output will be compressed by gzip - GzipOutputStream is used). Properly name the output file - it will contain the extension gz. Examine the file and try decompressing it.

5. Program line-by-line reading (use BufferedReader) to output numbered lines.

6. Use the Scanner class to read words from a file and print them on the console.

7. Use the Scanner class to retrieve double numbers from doubles.txt and calculate their arithmetic mean.

8. Use the DataOutputStream class to write triplets <double, int, String> to the file based on the data:

static final double[] prices = { 19.99, 9.99, 15.99, 3.99, 4.99 };

static final int[] units = { 12, 8, 13, 29, 50 };

static final String[] descs = {

  "Java T-shirt",

  "Java Mug",

  "Duke Juggling Dolls",

  "Java Pin",

  "Java Key Chain"

};

Examine the resulting file.

Use DataInputStream to retrieve and print data.

Homework

Load data from Faust-Goethe-orig.zip. To decompress the data, use ZipInputStream (you must call the getNextEntry method on the ZipInputStream object itself before reading - otherwise the work is the same as with GZIPInputStream); Create an output file (Faust-Goethe-1250.txt.gz) containing text in the format:

<line-number>; <line-length>; <retrieved line from original file>

The file will be in gzip format using the GZIPOutputStream class and the text will be encoded with the windows-1252 character set (you get the character set as “Charset.forName (" windows-1252 ")”). Use the PrintWriter.printf method to write text data.

PJ1 - 8th laboratory (Collections)

pl1-lab08.zip

Use the pl1.lab08.Book class, which has the following methods:

  •     toString - returns the English text of the book "The Old Man and the Sea"
  •     getWords - returns individual words of the text in this book as Collection.

1. Print all words sequentially using: the iterator; foreach construction.

2. Using the iterator, remove all words "the" from the returned collection (ignore capitalization with equalsIgnoreCase)

3. Calculate the average word length, and then print all words with an above-average length.

4. Sort words by length.

5. Print all words from the file without repeating (use Set) and ignore the case (use the toLowerCase method of the String class).

6. Modify the previous task to keep the order of the first occurrence (use LinkedHashSet).

7. Determine word frequencies - use Map (the key will be the word and the value will be the number of repetitions).

Homework

1. Modify task 5. so that the words from a Set are returned in order sorted by length (use TreeSet with the comparator from task 4).

2. Return words sorted by frequency. (Put words without repeating in a List that you sort using a comparator that uses Maps from Task 7).

PL1 - 10th laboratory (Strings)

Work with text in the class „Book“ - it same as in a laboratory 8th.

Work with the project pl1-lab10.zip.

1. A implementation StringLengthComparator replace with:

  • a "nested" class;
  • a local class;
  • an "anonymous" class;
  • alambda expression;
  • a reference method.

Explain why is no suitable an implementation by "inner class".

Following tasks implement using streams  (API in java.util.stream)

2. Print words longer then 2.

3. Print words converted to "lower-case" without duplicates (use a method distinct)

4. Count an average length of words (converted to "lower-case" without duplicates).

5. Overview an implementation provided by a tutor that counts frequencies of words.

6. Print words ordered according their frequencies.

7. Modify previous printing tasks that they return List instead of printing.

Homework

Count average length of words that contains a letter "a".