Advanced Java exercises

Theory slides

The following are links to slideshows that summarise the covered topics.


Create a class with a main method. For all of the exercises below, put code in main that tests the new content.

  1. Create a MyDate class
    1. In it, create enums Month and Weekday
  2. Create methods
    1. Names
      1. Month::getEnglishName
        1. Month::getHunName,
        2. Month::getXName (where X is your favourite language),
        3. replace the above methods using an enum Language
        4. make similar methods for Weekdays
    2. MyDate::getWeekDay
    3. Weekday::isThisDay(Month month, int day)
  3. Write Javadoc documentation for the methods

Anonymous functions (Lambda functions)

In the following exercises, it is your job to decide which (built-in) lambda to use. The most common choices are listed here, and you can also have a look at the documentation of the java.util.function package.

  1. Have some fun with the previously created enums.
  2. Sort the command line arguments based on the number of 'a' characters in them.
  3. Sort the command line arguments so that those arguments that contains names of weekdays come first.
  4. Create the method createArray that fills in an array. It takes the length of the array as an argument, and an int→int function f. Use the f function on each index of the array, and it will produce the value for the index.
  5. Create the method compose that takes two functions and returns their composition, which is another function.
  6. Create the method iterate that takes a function f and a (non-negative) integer n and returns a function that iterates f n times.
  7. Create the method geneticAlgorithm that runs a genetic algorithm.


  1. Print the lengths of the command line arguments in reverse order.
  2. Print the sum of even numbers that are greater than 8 from the command line arguments.
  3. Make a method that generates a stream of the first n prime numbers.
  4. Given some filenames in the command line arguments, print the names on one line, separated with commas, in descending order in the number of lines in the file. If two files have the same number of lines, then the one with more words on its first line should come first.

Unit testing

In the following exercises, test the described functions. Try everything covered in the lecture: descriptions, parameters etc. For each exercise, make several small test cases.

  1. Make a function summer that sums the numbers between its two arguments by adding them together one by one.
  2. Make a function shortSummer that sums the numbers between its two arguments with a formula.
  3. Make a function that takes an English–Hungarian dictionary represented as a Map and a text in English. The function will translate the text word by word, and inserts “?” for all words not in the dictionary.
  4. Test the lambda that produces the first n primes.
  5. Test the lambda that gets a weekday, and returns the weekday n days after that. Use parameters from a @CsvSource or any other source.
  6. Create the class Battleship that represents a state of the Battleship game.
  7. Make a method that takes a file name and an arbitrary number of arguments, and prints the sum of the numbers into the file.
  8. Change geneticAlgorithm so that it also takes an individual as a parameter.
  9. Check with JUnit that the return value of geneticAlgorithm contains at least one individual whose fitness value is not less than that of this individual.
    1. Extract the body of the loop in geneticAlgorithm into a separate function. Make a JUnit test that checks that the size of the population at the beginning of every loop iteration equals populationCount (the first parameter of geneticAlgorithm).


Make a serializable class Book that contains the following information:

The class BookStore stores books in a Map, identifying them by their names. The class implements the following methods.

// Puts some predefined books into the map, to serve as initial data.
void addSomeBooks();

// Writes the books into the given file using serialization.
// Returns whether the save operation was successful.
boolean saveBooks(String fileName);

// Loads the books from the given file.
// Returns whether the load operation was successful.
boolean loadBooks(String fileName);

Make a simple application that tests the above operations.


  1. Make a simple client-server application whose parts communicate using serialization. Use the Book and BookStore classes from the previous exercise.
  2. Make the previous server should accept clients one after the other.
  3. Create a server that receives numbers (as text) from the client. When the client is done sending numbers, the server sends the client their sum, then they disconnect.
  4. Create a server that uses text messages to communicate with the client. The client sends the name of a file; if it exists, the server sends back its contents.
    1. Let the client send many file names. The server sends back the contents of all existing files.
  5. Create a server that remembers how many clients have connected to it so far, and sends this number in a text message to the client. The client receives this number and prints it, but it doesn’t send any messages to the server.


  1. Make two threads that each use System.out.println to print a given text 10000 times. (Give different texts to the threads.)
  2. Now, instead of println, print each text character by character (and an ending newline).
  3. Two goats are trying to cross a narrow bridge from opposite sides. They meet in the middle, and they start pushing each other. Independently of each other, the goats take some time, and then push the other goat one step. Whenever one of the goats is pushed off of the bridge, the game ends. Take the speed of the goats (how much they wait between pushes) and the length of the bridge as parameters; make the program log important actions.
  4. Compute the nth Fibonacci number so that the non-basic cases compute the two halves of the addition on two separate threads.

Sockets with threading

In each of the following exercises, communication can be text based or binary based, it’s your choice.

  1. Make a TCP/IP server that can handle several clients: after accepting them, each connection is handled by a separate thread.
  2. Make a server to which any number of clients can connect. Whenever a client sends a text message, it is immediately sent to all other (currently connected) clients.
  3. Make a program that simulates a stock market. It has two ports: regular clients connect to the first port, admins to the second one.