Java Seminar (2011/2012)

(redirected from Main.Java)

Annotation from SIS:
Basic syntax - primitive types, classes, interfaces, objects, inheritance, packages, operators. Differences between Java and C++. Exception handling: try, catch, throw. Arrays. Strings. Java memory management and garbage collection. Virtual machine and runtime environment. Development tools. Threads and synchronization. Overview of the packages in the standard Java distribution - accessing files, collection library, networking,...

12:20 Tue, SU2

NPRG013 - Winter semester 2011/2012

For more information see:

Information for Czech students

  • konzultácie po dohode e-mailom, Malá Strana, druhé poschodnie, dvere č. 204

Project Ideas


Lab 1: 2011-10-04

Lab 2: 2011-10-11

  • Getting acquainted with the Eclipse IDE (download at - Eclipse IDE for Java Developers)
    • You can also use Netbeans, just do not forget to create symlinks from your home directory to home/BIG (you can use quota_free script)
    • You can use multiple workspaces at a time. All workspaces should be located in the BIG dir.
    • To see source code of the standard Java classes in Eclipse, you've got to find the file.
      • On Ubuntu: apt-get install openjdk-6-source, it should be located in /usr/lib/jvm/java-6-openjdk/
  • Separation of specification from implementation
    • Specification:
      • Printer interface
      • MyCollection interface
    • Implementation:
      • GenericPrinter implements IPrinter
      • TimestampPrinter extends GenericPrinter
      • PrefixPrinter extends GenericPrinter
      • Logger + Logger with a queue
      • FixedCollection
      • ResizeableCollection

Lab 3: 2011-10-18

  • Recap from the last lecture: iterfaces, exceptions, inner classes, shell args, exit codes
  • Iterable/Iterator (btw. could you describe the difference ? :-) )
    • Task 1: Implement an iterable collection that reallocated its buffer on demand
    • Task 2: Implement an iterable binary search tree
  • Few "tests":
    • calling a method with different signatures (null also matches long[], rather than Object)
    • behaviour of try-finally on normal method return and on System.exit()
    • IMPORTANT: static methods in Java are NOT virtual

Lab 4: 2010-10-26

  • again the Iterator/Iterable approach, now with generic types e.g. Iterable<String>
  • reading from files - reading chars (FileReader class), reading lines (BufferedReader class)
  • Three simple tasks:
    • Task 1: Implement MyString with an iterator over individual characters.
      • Here, you should practice strings handling. You can use private String buf; or private char[] buf; as a buffer
      • Implement the iterator at the very beginning.
    • Task 2: Implement a file reader that reads words from a file delimited by a set of characters e.g. whitespaces. Implement your class using the iterator.
      • Use FileReader in such a way that you always remember one character ahead.
      • Store regular characters into the buffer and ignore all sequences of delimiters.
      • The iterator should be implemented at the very end in order to prevent changes in the original implementation. This allows for better reusability of your code.
    • Task 3: Reuse the previous implementation and create a word-wrapper

Lab 5: 2011-11-01

  • implement MyHashTable class
    • KEY: String
    • VALUE: Object
    • methods: get(key), set(key, value)
    • Iterable + Iterator over elements
  • Splitter class
    • takes an instance of Reader in the constructor (Note: only reset(), read() and close() methods are needed from the Reader class, in Java: Object->Reader->InputStreamReader->FileReader)
    • supports a set of delimiters (e.g. newline, tab, space ...) - setDelimiters(String) method
    • Iterable + Iterator over individual words
  • WordWrapAlignLeft, WordWrapJustify
    • setLineLength() method
    • iterator over lines
    • last line should not be justified
  • Counter
    • takes an instance of Reader in the constructor
    • number of characters
    • number of words
    • number of lines
    • method setMinWordLength()
    • occurrences of every word (word:count)

Lab 6: 2011-11-08

  • Finishing tasks from the previous lecture
  • The word-wrapper should should write output text to a file
  • Implement a command similar to adduser in UNIX

Lab 7: 2011-11-15

  • Synchronization in Java
  • lock + queue of waiting threads
  • Two types of locks in Java:
    • per class
      • static synchronized mymethod() {...}
      • synchrnized(MyClass.class) {...}
    • per instance
      • synchronized mymethod() {...}
      • synchronized(myinstance) {...}
      • synchronized(this) {...}
  • Two types of creating a thread:
    • Thread (if you want to extend a class) - new MyThread().start()
    • Runnable (if you want to implement an interface)
  • Queue of waiting threads:
    • wait() - put the current thread into the queue
    • notify() - notify the first thread in the queue
    • notifyAll() - notify all threads in the queue
  • More synchrnoziation primitives in java.util.concurrent. There are also non-blocking synchronization data structures such as ConcurrentLinkedQueue.
  • Implement a thread-safe RoundBuffer
    • synchronized put() and get() methods
    • readers calling the get() method will be queued when the buffer empty
    • writers calling the put() method will be queued when the buffer full


synchronized void put(...) {
  while( isFull() ) {
synchronized ... get() {
  while( isEmpty() ) {

Note: You can also come up with a more elaborate implementation using multiple partially ordered locks: top-level lock on the whole buffer, separate lock for readers and separate lock for writers writers.

Lab 8: 2011-11-22

  • finished the RoundBuffer implementation
  • comparator/comparable

Lab 9: 2011-11-29

  • basic principles of TCP/UDP communication in Java and in general
  • Task: write a multithreaded TCP server + TCP client that implement a remote Todo list
  • Simple TCP echoing server and client available here: Attach:Java2009/ExampleTcpCode.tgz
    • Multithreaded TCP server
    • contains ANT build file and also works as an Eclipse project
    • ant - compiles the sources
    • ant client - runs the client
    • ant server - runs the server

Lab 10: 2011-12-06

Lab 11: 2011-12-13

Lab 12: 2011-12-20

  • Finish previous assignment
  • Simple Web Downloader: Attach:Java2010/ExampleWebDownloader.tgz
    • URL.openStream allows for downloading content through HTTP protocol (and more)
    • SwingWorker can handle time-consuming operations in background
    • JScrollPane + JTextArea, JTextField
    • BorderLayout (NORTH, SOUTH, CENTER ...)

Lab 13: 2011-12-20