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.
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.
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
andStream
, JTamaro providesSequence
, 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 minimalistOption
interface focused on the essence. - Instead of Java’s many functional interfaces (
Function
,BiFunction
,Consumer
,Supplier
, …), JTamaro provides simplyFunction1
,Function2
, … - Instead of Java’s complex GUI APIs like AWT and Swing, JTamaro’s
Graphics
andIO
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.