Composition in Java

Composition in Java

“Composition in Java” is a somewhat extraordinary textbook for teaching Java programming on strong foundations. It includes the comprehensive material for a 14-week undergraduate Java programming course. “Composition in Java” consists of 14 workbooks and 12 labs, and it comes with JTamaro, an educational functional programming library for Java.

Workbooks - Interleaved Theory and Exercises

Each week of the course is supported by a workbook. Ideally students receive the printed workbook at the start of the week, which they are to complete by the end of the week. During the lessons, the instructor takes the students on a tour through the workbook; students complete the exercises, individually or in pairs, during the lecture, and the instructor provides guidance and feedback.

Week   Workbook Title
1 Methods, Expressions, Parameters
2 Types, Composition, Conditionals
3 Repetitive Data
4 Repetitive Computation
5 Instance Fields, Methods, Type Casts
6 Interfaces, Subtyping Polymorphism
7 Abstraction
8 More Records, Anonymous Functions
9 Statements & Local Mutation
10 More Mutation, Data Flow
11 Inheritance
12 Non-Local Mutation
13 Mutable Lists & Maps
14 Null, Arrays, Streams

An average workbook consists of about 12 pages which provide between 180 and 270 minutes of classroom experience (e.g., two or three 90-minute class sessions).

Labs - Programming Practice

Each week of the course (except for the last two weeks) includes a homework assignment, called a “lab”. Students receive the lab at the start of the week. The lab includes a sequence of challenges. Students are expected to complete all the challenges in a lab.

Week   Lab Challenges
1 Pacman, Toolbelt, House, Colors, Swiss Flag, Rhombus, Ski Slope, Promote to Toolbelt, Eyes, Flower, Yin and Yang, Me
2 Flags, Target, Expand Your Toolbelt, Heart, Clover, Rounded Rectangle, Isosceles Trapezoid, Swiss Railway Clock
3 Music, Pacman animation, Printing Sequences, Odds, Counting, Pacman maze
4 Mapping, Ghost, Mapping, Rotation, Color Hues, Employees
5 Fibonacci, TimeStamp, TimeInterval
6 AST, Bytecode, Tests
7 Reductions, Mappings, Filterings, Animated Pacman with IO.animate, Animated Pacman with IO.interact, Interactive Pacman Application with IO.interact
8 Data Science (Fancy Bar Chart), Pacman Game (First Part)
9 Memory Diagram to Code, CFG to Code, Random Access, Reductions with Loops, Mappings with Loops, Filterings with Loops, Pacman Game (Finish)
10 Mouse Visualizer, Dice Game
11 Lexical Analyzer
12 Recursive Descent Parser

Most of the labs include challenges involving graphics, interaction, and fundamental data structures.

JTamaro - A Minimal Core Library

The course material is based on the JTamaro library. The goal of JTamaro is to provide a minimalist but powerful library that supports the “Composition in Java” approach. Using JTamaro, students get immediate, motivating visual results when starting to program with a minimal subset of Java language features.

JTamaro is a close cousin to the PyTamaro compositional graphics library for Python. Unlike PyTamaro, JTamaro also includes simple purely functional data structures (with types like Sequence, Option, and Pair), and functionality for developing purely-functional interactive applications (with a fluent API to configure and run an Interaction). JTamaro provides a minimal API, much smaller than the Java API surface covered in standard Java programming courses:

  • Instead of the large interfaces provided by Java’s Collection and Stream, JTamaro provides Sequence, consisting of a clean set of features that are necessary for learning the fundamental ideas.
  • Instead of Java’s large Optional class, JTamaro provides a minimalist Option interface focused on the essence.
  • Instead of Java’s many functional interfaces (Function, BiFunction, Consumer, Supplier, …), JTamaro provides simply Function1, Function2, …
  • Instead of Java’s complex GUI APIs like AWT and Swing, JTamaro’s Graphics and IO classes provide a purely functional API for developing well structured and testable interactive applications.

The library promotes purity, immutability, parametric polymorphism, subtyping polymorphism, and higher-order functions. It does this by focusing on Java records, interfaces, and generics, and by a design that encourages immutability and the composition of independent components.

The booklets gradually introduce the library, and the labs heavily depend on it. The library provides just a minimal core, on which the students build throughout the course. The use of a minimal core library keeps students focused on the essential concepts. The solid understanding of essentials students gain in the course serves as a foundation for quickly learning the more complicated existing and future class libraries.

Notional Machines

“Composition in Java” uses several complementary visual representations to aid in explanation and assessment.

  • Expression Tree (see also Expression Tutor)
  • Memory Diagram (Stack, Heap, Globals)
  • Dynamic Call Tree
  • Class Diagram
  • Control-Flow Graph

Throughout the course, students learn to reason about their code by translating back and forth between these visual representations and code.

History and Contributors

“Composition in Java” is being developed in a collaborative effort at the LuCE research lab at USI. The original version was written by Matthias Hauswirth for the 2023 edition of the “Programming Fundamentals 2” course in the Bachelor of Informatics at USI.

The structure and content was influenced by the experience of teaching Java programming using traditional textbooks and a rich and growing set of resources for almost two decades. “Composition in Java” was an effort to restart from a clean slate, and to focus on building up a thorough understanding of the essential concepts that lie at the core of any programming language. The book was greatly inspired by the programming education research conducted at LuCE, such as the work on sound notional machines by Igor Moreno Santos and the work on teaching problem decomposition and programming with graphics by Luca Chiodini.

In 2024 “Composition in Java” was completely revised and improved greatly with key contributions from Luca Chiodini, Igor Moreno Santos, and Joey Bevilacqua throughout the workbooks, labs, library, and notional machines.