Final report

This commit is contained in:
Claudio Maggioni 2022-10-23 14:39:34 +02:00
parent 6d56bcc5ce
commit 2940fa290c
5 changed files with 200 additions and 0 deletions

BIN
hw1/assignment1.pdf Normal file

Binary file not shown.

BIN
hw2/Assignment2AJP.pdf Normal file

Binary file not shown.

BIN
hw2/logo_usi.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.1 KiB

BIN
hw2/report.pdf Normal file

Binary file not shown.

200
hw2/report.tex Normal file
View File

@ -0,0 +1,200 @@
\documentclass[a4paper]{article}
\usepackage{graphicx}
\usepackage[hidelinks]{hyperref}
\usepackage[english]{babel}
\usepackage{fancyhdr}
\usepackage{listings}
\usepackage{xcolor}
\usepackage{float}
\usepackage{lmodern}
\usepackage[margin=2cm,top=2.5cm]{geometry}
\hypersetup{pdfborder={0 0 0}}
\usepackage[nomessages]{fp}
\lstset{
basicstyle=\small\ttfamily,
%frame=shadowbox,
rulesepcolor=\color{black},
columns=fullflexible,
commentstyle=\color{gray},
keywordstyle=\color{blue},
mathescape=true,
aboveskip=1em,
captionpos=b,
abovecaptionskip=1em,
belowcaptionskip=1em
}
\fancyhead[R]{Advanced Java Programming -- Assignment 2 -- Federico Lagrasta,
Claudio Maggioni}
\pagestyle{fancy}
\begin{document}
\begin{titlepage}
\scshape
\centering
\includegraphics[width=3cm, keepaspectratio]{logo_usi.png}
\par
\vspace{0.1cm}
\texttt{FACULTY OF INFORMATICS}
\vspace{1cm}
\raisebox{-\baselineskip}{\rule{\textwidth}{1px}}
\rule{\textwidth}{1px}
\vspace{0.2cm}
{\LARGE{{ADVANCED JAVA PROGRAMMING}}}\par \vspace{0.1cm}
Assignment 2
\rule{\textwidth}{2px}
\vspace{1cm}
\begin{tabular}{lp{1cm}l}
Author & \rule{0pt}{3ex} & FEDERICO \mbox{LAGRASTA}\\
&&\\
& \rule{0pt}{3ex} & CLAUDIO \mbox{MAGGIONI}\\
\end{tabular}
\vspace{1.3cm}
\vfill
\today
\end{titlepage}
\section*{A}
\subsection*{1}
The snippet would cause a compile time error at line 4 since it is not possible
to call \texttt{get(...)} on an lower bounded \texttt{List} (i.e.\ a variable
with \texttt{List<?\ super SOMETHING>} static type, where \texttt{SOMETHING} can
be any class), in this case on \texttt{src}. This is due to the get-put
principle. If the code snippet were legal, the following example would have been
legal but not be type safe:
\begin{lstlisting}[caption=Type unsafe example of covariant access on a
\texttt{List}
with a lower bounded wildcard., language=java]
List<Object> objects = new ArrayList<>();
objects.put(new Object());
List<? super String> strings = objects;
// not type safe, as strings[0], i.e. objects[0] is an Object and not a String
String s = strings.get(0);
\end{lstlisting}
Additionally, the snippet would cause another compile time error at line 5,
since it is also not possible to call \texttt{set(...)} on an upper bounded
\texttt{List} (i.e.\ a \texttt{List<?\ extends SOMETHING>}). The following
example would not be type safe otherwise:
\begin{lstlisting}[caption=Type unsafe example of contravariant access on a
\texttt{List}
with an upper bounded wildcard., language=java]
List<Integer> ints = new ArrayList<>();
ints.put(42);
List<? extends Number> numbers = ints;
numbers.set(0, 50.0);
// not type safe, as numbers[0], i.e. ints[0] is a Double and not an Integer
Integer i = ints.get(0);
\end{lstlisting}
\subsection*{2}
The code does not compile.
\section*{B}
\subsection*{1}
No, as the compiler will trust our cast. However, an ``unchecked cast''
compiler warning will be reported.
\subsection*{2}
Yes, as arrays can not be downcasted. Specifically, a
\texttt{ClassCastException} will be thrown at line 8, where the
\texttt{Object[]} instance would be needed to be downcasted to \texttt{String[]}.
Note that the exception is not thrown inside \texttt{myArrayGenerator(...)} as
\texttt{T} is erased to \texttt{Object} during compliation, making the explicit
cast redundant, but in turn making the implicit cast added by the use of
generics illegal.
\section*{C}
\subsection*{1}
The compiler would report 3 compile time errors (at lines 3, 5 and 7)
as generic type argument \texttt{T} cannot be referenced in static contexts,
i.e.\ in static field declarations, static method signatures or static method
bodies. This compiler rule is a side effect of erasure.
\subsection*{2}
The code does not compile.
\section*{D}
\subsection*{1}
The code will not compile, as the two methods included in the class have the
same signature after erasure, since \texttt{Class<?>} is erased to the type
\texttt{Class} and the types \texttt{T} and \texttt{U} will be erased to
\texttt{Object}. Therefore, the class after erasure would look like this:
\marginpar[right text]{\color{white}\url{https://youtu.be/RUN6Kqd9xgs}}
\begin{lstlisting}[caption=Class \texttt{Couple<T, U>} after erasure.,
language=java]
public class Couple {
public Class getType(Object t) {
return t.getClass();
}
public Class getType(Object u) {
return u.getClass();
}
}
\end{lstlisting}
thus making the two methods have an identical signature, which is illegal.
\subsection*{2}
The code does not compile.
\section*{E}
\subsection*{1}
It will print \texttt{I am a class} since the keyword \texttt{super} prioritizes
the parent class over interfaces. The implemented interface would have to be
referred as \texttt{Second.super} (i.e.\ changing line 17 to
\texttt{Second.super.doSomething();} would result in the \texttt{I am an
interface} output).
\subsection*{2}
It won't compile. \texttt{First.doSomething()} would be inherited by the child
class \texttt{Third} over \texttt{Second.doSomething()} since parent classes
have priority interfaces with default methods w.r.t.\ interfaces.
However, \texttt{First.doSomething()} has default or ``package-private''
visibility while the interface \texttt{Second} requires \texttt{doSomething()}
to be public (as method signatures in interfaces are by default public).
Therefore, a compile-time error is reported due to assigning ``weaker access
privilegies'' to the \texttt{doSomething()} method than required by interface
\texttt{Second}.
\end{document}