Skip to content

FloMueh/02-classes-interfaces

 
 

Repository files navigation

This is an assignment to the class Programmieren 3 at the University of Applied Sciences Rosenheim.

Assignment 2: Classes and interfaces revisited

In this assignment we'll be looking at

  • static and regular inner classes
  • anonymous classes
  • lambda expressions and @FunctionalInterface by implementing a basic linked list.

A linked list consists of a controller (the list class) and elements that contain the actual payload (the container or element class). The controller class maintains a reference to the first list element (head), and every element points to its successor (or to a null value, if it's the last). The following UML shows all classes of this assignment (including the interfaces java.util.Iterator<T> and java.lang.Iterable<T>; we'll cover ).

Note: the concept of an iterator will be discussed in detail in a few weeks.

Classes

Setup

  1. Create a fork of this repository.
  2. Clone your fork to get a local working copy.
  3. Import the project to your IDE (choose Gradle project)

Static and regular inner classes

  1. What is a static class?
    • Refresh your knowledge on UML (e.g. here)
    • What's the difference between a regular inner and static inner class?
      • Erstellung der Objekte. Ein statisch Objekt wird nur einmal in den Speicher geladen und wieder verwendet.
    • Can you think of some use cases for both?
  2. Implement Element as static inner class of SimpleListImpl.
    • Why is this class static, and ideally private?
      • Static, da das Element im speicher nur einmal benötigt wird.
      • Privat, da nur im Context der SimpleListImpl darauf zugegriffen werden muss.
  3. Implement the Iterator interface as inner class of SimpleListImpl.
    • Why is it helpful to make this class non-static?
      • Die Klasse muss auf Objekte Zugreifen die nicht static sind. Konkret handelt es sich dabei um das "head" Objekt.
      • Java static nested classes können nur static data members und methods verwenden
      • Alternativ könnte man die Klasse static deklarieren, wenn man parallel dazu das Object "head" static deklariert.
  4. Add the Iterable interface to your SimpleListImpl, and implement the required methods.
    • Why is implementing the Iterable interface essential for a (good) list implementation? (Hint: Check the test cases!)
      • Damit die verwendete forkorrekt iterieren kann.
    • Are there any language definition constraints to this?
      • Gute frage..?

Anonymous (inner) classes and lambda expressions

  1. Implement the filter method for your SimpleListImpl class (see SimpleFilter interface).
  2. Check the given test suite for an example on
    • how to use an anonymous class with an interface.
    • how an anonymous class can be replaced by a lambda expression.
  3. Add some test methods and implement another filter logic (e.g. every third number, or any number smaller than a certain value).
  4. Review anonymous classes and lambdas.
    • Lambda expressions look very convenient; can you think of a scenario where they should not be used?
    • Recall how scoping works for anonymous (inner or local) classes; can you think of a scenario where to avoid them?

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%