\documentclass[12pt,a4paper]{article}

\usepackage[utf8]{inputenc}
\usepackage[margin=2cm]{geometry}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{lmodern}

\title{Howework 3 -- Programming Fundamentals 3}
\author{Claudio Maggioni}

\lstset{
    basicstyle=\small\ttfamily,
    frame=shadowbox,
    rulesepcolor=\color{black},
    columns=fullflexible,
    commentstyle=\color{gray},
    keywordstyle=\bfseries,
    escapeinside={\%*}{*)},
    aboveskip=2em,
    captionpos=b,
    abovecaptionskip=1em,
    belowcaptionskip=1em
}

\begin{document}
\maketitle
\tableofcontents

\section{Exercise 1}
\subsection{Exercise 1.1}
\paragraph{Example 1:}
The code will compile and run normally since Integer implements Number
and therefore it is a satisfiable parameter for
\texttt{Box<Number>.put(Number)}.

\paragraph{Example 2:}
The code will compile, but it will terminate with a NullPointerException.
The implicit unboxing of the return value of \texttt{iBox.get()} will call
the method \texttt{intValue()} on the object returned. But, since the Box object
contains an empty reference, this call will fail and thus cause a
\texttt{NullPointerException}.

\paragraph{Example 3:}
This code will not compile since \texttt{Box<Integer>.put()} requires an integer
as an argument, but a \texttt{double} is provided.

\subsection{Exercise 1.2}
\begin{lstlisting}[language=Java, caption=New signature of Box.put(...)]
public void put(Box<? extends T> box) { ... };
\end{lstlisting}


\subsection{Exercise 1.3}
\begin{lstlisting}[language=Java, caption=New signature of Box.put(...)]
public boolean containsSame(Box<? extends T> o,
                               EqualityComparator<? super T> c) { ... };
\end{lstlisting}


\section{Exercise 2}
\subsection{Exercise 2.1}
\begin{lstlisting}[language=Java, caption=BiPredicate with lambda]
BiPredicate<String, String> firstStartsWithSecond = (s, s2) -> s.startsWith(s2);
\end{lstlisting}

\begin{lstlisting}[language=Java, caption=BIPredicate with method reference]
BiPredicate<String, String> firstStartsWithSecond = String::startsWith;
\end{lstlisting}

\end{document}
