CSSE2002 exam material
Author:
John Owen
Last Updated:
9 years ago
License:
Creative Commons CC BY 4.0
Abstract:
.
\begin
Discover why 18 million people worldwide trust Overleaf with their work.
\begin
Discover why 18 million people worldwide trust Overleaf with their work.
\documentclass[a4paper]{article}
\usepackage{amsmath} % Yay, math
%\usepackage{fancyvrb} % Gives a better vertbatim environmnt, Verbatim
\usepackage[margin=1in]{geometry} % 1 inch margins
\usepackage{fixltx2e} % For \textsubscript
\usepackage[T1]{fontenc} % So we can use pretty T1 fonts
\usepackage{libertine} % Nicer serif font
\usepackage[scaled=0.85]{beramono} % Boldable mono font
\usepackage{parskip} % Replaces paragraph indents with vspaces
\usepackage{csquotes} % Use " to quote things
\newcommand\codeitem[1]{\item[\texttt{#1}:]}
\newcommand\methoditem[1]{\item[\texttt{#1}] \hfill \\}
\newcommand\sub[1]{\textsubscript{#1}}
\begin{document}
\setlength{\abovedisplayshortskip}{-1ex}
\title{CSSE2002 exam material\vspace{-2ex}}
\author{John Owen}
\date{\vspace{-3ex}}
\maketitle
\section{Important bits from the \texttt{Object} class}
\begin{description}
\methoditem{boolean equals(Object obj)}
Indicates whether some other object is "equal to" this one.
Usually, you override \texttt{equals()} iff your class is immutable and it hasn't already been overridden by a superclass.
Rules \texttt{equals()} must abide by:
\begin{itemize}
\item Reflexivity: \texttt{x.equals(x)}.
\item Symmetry: \texttt{x.equals(y) == y.equals(x)}.
\item Transitivity: \texttt{x.equals(y) \&\& y.equals(z) == x.equals(z)}.
\item \texttt{x.equals(null)} is false.
\end{itemize}
If you override \texttt{equals()}, you must also override \texttt{hashCode()}.
\methoditem{int hashCode()}
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by HashMap.
The general contract of hashCode is:
\begin{itemize}
\item Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
\item If two objects are equal according to the equals(Object) method, then calling the hashCode method on each of the two objects must produce the same integer result.
\item It is not required that if two objects are unequal according to the equals(java.lang.Object) method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
\end{itemize}
\methoditem{String toString()}
Returns a string representation of the object.
\end{description}
\section{Good implementation of \texttt{hashCode()}}
\subsection{General case}
\def\fsubi{\texttt{f\sub{i}}}
\def\hsubi{\texttt{h\sub{i}}}
First, for every field \fsubi\ (\texttt{f\sub{0}} is the first field and so on), calculate its hash code \hsubi using
\begin{equation*}
\mathtt{h_i} = \begin{cases}
\text{\texttt{(\fsubi\ ?\ 1\ :\ 0)}} & \text{if \fsubi\ is a \texttt{boolean}}\\
\text{\texttt{(int)\ \fsubi}} & \text{if \fsubi\ is a \texttt{byte}, \texttt{char}, \texttt{short}, \texttt{long}, or \texttt{int}}\\
\text{\texttt{Float.floatToIntBits(\fsubi)}} & \text{if \fsubi\ is a \texttt{float} or \texttt{double}}\\
\text{\texttt{\fsubi\ ==\ null\ ?\ 0 :\ \fsubi.hashCode()}} & \text{if \fsubi\ is a \texttt{Object}}\\
\text{\texttt{Arrays.hashCode(\fsubi)}} & \text{if \fsubi\ is an array}.
\end{cases}
\end{equation*}
To calculate the hash code, we use $\mathtt{hashCode} = \sum_\mathtt{i} \mathtt{h_i}31^\mathtt{i}$.
\subsection{Example implementation}
\begin{verbatim}
public class Employee {
int employeeId;
String name;
Department dept;
// ...
public int hashCode() {
return employeeId + name.hashCode() * 31 + dept.hashCode() * 31 ^ 2;
}
}
\end{verbatim}
\section{Important bits from the \texttt{List<E>} interface}
\begin{description}
\methoditem{boolean add(E e)}
Appends the specified element to the end of this list (optional operation).
\methoditem{void add(int index, E element)}
Inserts the specified element at the specified position in this list (optional operation).
\methoditem{boolean addAll(Collection<? extends E> c)}
Appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's iterator (optional operation).
\methoditem{boolean addAll(int index, Collection<? extends E> c)}
Inserts all of the elements in the specified collection into this list at the specified position (optional operation).
\methoditem{void clear()}
Removes all of the elements from this list (optional operation).
\methoditem{boolean contains(Object o)}
Returns true if this list contains the specified element.
\methoditem{boolean containsAll(Collection<?> c)}
Returns true if this list contains all of the elements of the specified collection.
\methoditem{boolean equals(Object o)}
Compares the specified object with this list for equality.
\methoditem{E get(int index)}
Returns the element at the specified position in this list.
\methoditem{int hashCode()}
Returns the hash code value for this list.
\methoditem{int indexOf(Object o)}
Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element.
\methoditem{boolean isEmpty()}
Returns true if this list contains no elements.
\methoditem{Iterator<E> iterator()}
Returns an iterator over the elements in this list in proper sequence.
\methoditem{int lastIndexOf(Object o)}
Returns the index of the last occurrence of the specified element in this list, or -1 if this list does not contain the element.
\methoditem{ListIterator<E> listIterator()}
Returns a list iterator over the elements in this list (in proper sequence).
\methoditem{ListIterator<E> listIterator(int index)}
Returns a list iterator over the elements in this list (in proper sequence), starting at the specified position in the list.
\methoditem{E remove(int index)}
Removes the element at the specified position in this list (optional operation).
\methoditem{boolean remove(Object o)}
Removes the first occurrence of the specified element from this list, if it is present (optional operation).
\methoditem{boolean removeAll(Collection<?> c)}
Removes from this list all of its elements that are contained in the specified collection (optional operation).
\methoditem{void replaceAll(UnaryOperator<E> operator)}
Replaces each element of this list with the result of applying the operator to that element.
\methoditem{boolean retainAll(Collection<?> c)}
Retains only the elements in this list that are contained in the specified collection (optional operation).
\methoditem{E set(int index, E element)}
Replaces the element at the specified position in this list with the specified element (optional operation).
\methoditem{int size()}
Returns the number of elements in this list.
\methoditem{void sort(Comparator<? super E> c)}
Sorts this list according to the order induced by the specified Comparator.
\methoditem{List<E> subList(int fromIndex, int toIndex)}
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive.
\methoditem{Object[] toArray()}
Returns an array containing all of the elements in this list in proper sequence (from first to last element).
\end{description}
\section{Important bits from the \texttt{java.util.Map<K,V>} interface}
\begin{description}
\methoditem{void clear()}
Removes all of the mappings from this map (optional operation). Removes all of the mappings from this map (optional operation).
\methoditem{boolean containsKey(Object key)}
Returns true if this map contains a mapping for the specified key. Returns true if this map contains a mapping for the specified key.
\methoditem{boolean containsValue(Object value)}
Returns true if this map maps one or more keys to the specified value. Returns true if this map maps one or more keys to the specified value.
\methoditem{Set<Map.Entry<K,V>\,> entrySet()}
Returns a Set view of the mappings contained in this map. Returns a Set view of the mappings contained in this map.
\methoditem{boolean equals(Object o)}
Compares the specified object with this map for equality. Compares the specified object with this map for equality.
\methoditem{V get(Object key)}
Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.
\methoditem{int hashCode()}
Returns the hash code value for this map. Returns the hash code value for this map.
\methoditem{boolean isEmpty()}
Returns true if this map contains no key-value mappings. Returns true if this map contains no key-value mappings.
\methoditem{Set<K> keySet()}
Returns a Set view of the keys contained in this map. Returns a Set view of the keys contained in this map.
\methoditem{V put(K key, V value)}
Associates the specified value with the specified key in this map (optional operation). Associates the specified value with the specified key in this map (optional operation).
\methoditem{void putAll(Map<? extends K,? extends V> m)}
Copies all of the mappings from the specified map to this map (optional operation). Copies all of the mappings from the specified map to this map (optional operation).
\methoditem{V remove(Object key)}
Removes the mapping for a key from this map if it is present (optional operation). Removes the mapping for a key from this map if it is present (optional operation).
\methoditem{boolean remove(Object key, Object value)}
Removes the entry for the specified key only if it is currently mapped to the specified value. Removes the entry for the specified key only if it is currently mapped to the specified value.
\methoditem{V replace(K key, V value)}
Replaces the entry for the specified key only if it is currently mapped to some value. Replaces the entry for the specified key only if it is currently mapped to some value.
\methoditem{boolean replace(K key, V oldValue, V newValue)}
Replaces the entry for the specified key only if currently mapped to the specified value. Replaces the entry for the specified key only if currently mapped to the specified value.
\methoditem{int size()}
Returns the number of key-value mappings in this map. Returns the number of key-value mappings in this map.
\methoditem{Collection<V> values()}
Returns a Collection view of the values contained in this map. Returns a Collection view of the values contained in this map.
\end{description}
\section{Important bits from the \texttt{Comparable<T>} interface}
\begin{description}
\methoditem{int compareTo(T o)}
Compares this object with the specified object for order. Returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
The implementor must ensure \texttt{sgn(x.compareTo(y)) == -sgn(y.compareTo(x))} for all \texttt{x} and \texttt{y}. (This implies that \texttt{x.compareTo(y)} must throw an exception iff \texttt{y.compareTo(x)} throws an exception.)
The implementor must also ensure that the relation is transitive: (\texttt{x.compareTo(y)>0 \&\& y.compareTo(z)>0}) implies \texttt{x.compareTo(z)>0}.
Finally, the implementor must ensure that \texttt{x.compareTo(y)==0} implies that \texttt{sgn(x.compareTo(z)) == sgn(y.compareTo(z))}, for all \texttt{z}.
It is strongly recommended, but not strictly required that \texttt{(x.compareTo(y)==0) == (x.equals(y))}. Generally speaking, any class that implements the Comparable interface and violates this condition should clearly indicate this fact. The recommended language is "Note: this class has a natural ordering that is inconsistent with equals."
In the foregoing description, the notation \texttt{sgn(expression)} designates the mathematical signum function, which is defined to return one of $-1$, 0, or 1 according to whether the value of expression is negative, zero or positive.
\end{description}
\section{Important unchecked exceptions in \texttt{java.lang}}
\begin{description}
\codeitem{ArithmeticException} Arithmetic error, such as divide-by-zero.
\codeitem{ArrayIndexOutOfBoundsException} Array index is out-of-bounds.
\codeitem{ArrayStoreException} Assignment to an array element of an incompatible type.
\codeitem{ClassCastException} Attempt to cast an object of type A to something that isn't a superclass of A.
\codeitem{IllegalArgumentException} Illegal argument used to invoke a method.
\codeitem{IllegalStateException} Environment or application is in incorrect state.
\codeitem{IndexOutOfBoundsException} Some type of index is out-of-bounds.
\codeitem{NullPointerException} Invalid use of a null reference.
\codeitem{NumberFormatException} Invalid conversion of a string to a numeric format.
\codeitem{StringIndexOutOfBounds} Attempt to index outside the bounds of a string.
\codeitem{UnsupportedOperationException} An unsupported operation was encountered.
\codeitem{RuntimeException} Is the superclass of those exceptions that can be thrown during the normal operation of the Java Virtual Machine.
\end{description}
\end{document}