\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Edgar (edgarjoel21)} \pdfinfo{ /Title (ood.pdf) /Creator (Cheatography) /Author (Edgar (edgarjoel21)) /Subject (OOD Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{15A31F} \definecolor{LightBackground}{HTML}{F0F9F1} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{OOD Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Edgar (edgarjoel21)} via \textcolor{DarkBackground}{\uline{cheatography.com/44289/cs/14900/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Edgar (edgarjoel21) \\ \uline{cheatography.com/edgarjoel21} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 27th February, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{4} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Design Principles}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{1. Javadoc all public classes and methods. Class comment should be at least two sentences, and provide information not already clear from its definition. \newline % Row Count 4 (+ 4) 2. Use interface types over concrete classes wherever possible. Exception: immutable "value" objects. Classes with no interface. \newline % Row Count 7 (+ 3) 3. Fields must always be private. Exception: constants. Methods, classes should be as private as possible. \newline % Row Count 10 (+ 3) 4. Class should never have public methods not in the interface (aside from constructor). \newline % Row Count 12 (+ 2) 5. Composition over inheritance. \newline % Row Count 13 (+ 1) 6. Catch and handle/report errors as early as possible. Use Java compiler checks, enums, final first, runtime checks second. \newline % Row Count 16 (+ 3) 7. Use class types over strings. \newline % Row Count 17 (+ 1) 8. Check inputs. \newline % Row Count 18 (+ 1) 9. Use exceptions only for exceptional situations -{}- not for flow control. \newline % Row Count 20 (+ 2) 10. Checked vs unchecked: checked: reasonable expectation that the program can recover. Unchecked: programmer error (may still be recoverable). \newline % Row Count 23 (+ 3) 11. Don't leave things in an inconsistent state for any substantive length of time. \newline % Row Count 25 (+ 2) 12. Beware of references, copies, and mutation. Make defensive copies. \newline % Row Count 27 (+ 2) 13. Separate responsibilities: one class, one responsibility. \newline % Row Count 29 (+ 2) 14. Use class hierarchies and dynamic dispatch over tagged classes, complex if/switch statements. \newline % Row Count 31 (+ 2) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Design Principles (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{15. Don't duplicate code. \newline % Row Count 1 (+ 1) 16. Open for extension, closed for modification: make changes without modifying existing code; write code to support later changes without modification. \newline % Row Count 5 (+ 4) 17. Extensibility: design to make likely later changes easier. \newline % Row Count 7 (+ 2) 18. Write tests first, cover the range of situations, edge cases. Write code to be testable (avoid System.out); do not expose fields or add public methods just to allow for testing. \newline % Row Count 11 (+ 4) 19. Loose coupling over tight coupling (avoid System.out). Write reusable components when possible. \newline % Row Count 13 (+ 2) 20. You can't change an interface once it's published. \newline % Row Count 15 (+ 2) 21. If you override equals(), override hashCode(), and vice-versa. \newline % Row Count 17 (+ 2) 22. Reuse existing exceptions, classes, libraries, and designs.% Row Count 19 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Scanner Methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`public boolean hasNext() Returns true if the scanner has another token in its input` \newline `public String next() Finds and returns the next complete token from the sc (throws NoSuchElement if no tokens and IllegalState if scanner is closed` \newline `Scanner(Readable source) Constructs a new Scanner that produces values scanned from the specified source.` \newline `Readable r = new StringReader(String);` \newline `Appendable a = new StringBuilder(); THROWS EXCEPTION`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.16722 cm} x{2.26578 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{CLASS INVARIANTS}} \tn % Row 0 \SetRowColor{LightBackground} NOT INVARIANTS & INVARIANT \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} value is small & A logical statement is a claim that is true or false \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} value never decreases & The instantanous state of an object is the combination of values of all its fields at some point in time \tn % Row Count 8 (+ 4) % Row 3 \SetRowColor{white} value is an int & The invariant is ensured by constructors in the sense that whenever a public constructor returns, the logical statement holds \tn % Row Count 13 (+ 5) % Row 4 \SetRowColor{LightBackground} & Preserving the logical statement means that the method doesn't introduce nonsense - instead, we know that if given a object in a good state then it will leave the object in a good state as well \tn % Row Count 21 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{Enables a form of reasoning called rely-guarantee. \newline - If the constructor ensures some property \newline - and every method preserves the property \newline - then every public method, on entry, can rely on the property} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Composition over inheritance}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{- Composition over inheritance \newline - delegate design pattern: take the previous code we want to use and make it a field instead of extending it \newline - has-a instead of is-a \newline - always get copies of every private field instead of passing in the real thing \newline \newline public boolean remove(int i) \{ \newline return deletgate.remove(i); \newline \} \newline \newline public boolean contains(int i) \{ \newline return delegate.contains(i); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Map methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{clear() \newline containsKey(Object key) \newline containsValue(Object value) \newline entrySet() returns a set view of the mappings (Set\textless{}Map.Entry\textless{}K, V\textgreater{}\textgreater{}) \newline equals(Object o) \newline get(Object key) \newline hashCode() \newline isEmpty() \newline keySet() returns a set of the keys Set\textless{}K\textgreater{} \newline put(k key, V value) Associates the value with the key \newline putAll(Map\textless{}? extends K, ? extends V\textgreater{} m) Copies into new map \newline remove(Object key) \newline size() \newline values() Returns a Collection view of the values (Collection\textless{}V\textgreater{})} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Equals}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{@Override \newline public boolean equals(Object that) \{ \newline if (this == that) \{ \newline return true; \newline \} \newline if (!(that instanceof Duration)) \{ \newline return false; \newline \{ \newline return ((Duration) that).inSeconds() == this.inSeconds(); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Stack Methods}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`empty()` Tests if this stack is empty (boolean)| \newline `peek()` Looks at top object of stack without removing it (E) | \newline `pop()` Removes the object at the top of this stack and returns that object (E)| \newline `push(E item)` Pushes an item onto the top of this stack (E) | \newline `search(Object o)` Returns the 1-based position where an object is on this stack (int) | \newline `add, add, addAll, addAll, addElement, capacity, clear, clone, contains, containsAll, copyInto, elementAt, elements, ensureCapacity, equals, firstElement, get, hashCode, indexOf, indexOf, insertElementAt, isEmpty, iterator, lastElement, lastIndexOf, lastIndexOf, listIterator, listIterator, remove, remove, removeAll, removeAllElements, removeElement, removeElementAt, removeRange, retainAll, set, setElementAt, setSize, size, subList, toArray, toArray, toString, trimToSize`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`Deque\textless{}E\textgreater{} is an interface (double ended queue)` \newline `Deque\textless{}Integer\textgreater{} stack = new ArrayDeque\textless{}Integer\textgreater{}();`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{TIps}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{- check for overflow \newline - canonicalize means converting data with multiple representations into a standard or normal form \newline - bug may be that a method used a non-copy of something \newline - interfaces can be extended to add methods to something} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Static}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Static: Java's version of global variables \newline Static Methods: Called on the class rather than an instance and thus dont have a this to work on. Ex: Long.hashCode() \newline Static classes: behave like normal classes just nested in its enclosing classes namespace. Outer.nested is how you refer to it. Outer.nested can see outers private members and vise versa \newline - constants should be public static final and in all caps} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Hashcode}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{@Override \newline public int hashCode() \{ \newline return Object.hash(field, field, field); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{Must use fields that equals uses} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{ABSTRACT TEST}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{protected abstract FreecellOperations\textless{}Card\textgreater{} freecellModel(); \newline \newline public static class SingleMove extends \seqsplit{AbstractFreecellModelTests2} \{ \newline \newline \newline @Override \newline protected FreecellOperations\textless{}Card\textgreater{} freecellModel() \{ \newline return \seqsplit{FreecellModelCreator.create(GameType.SINGLEMOVE);} \newline \} \newline \} \newline \newline public static class MultiMove extends \seqsplit{AbstractFreecellModelTests2} \{ \newline @Override \newline protected FreecellOperations\textless{}Card\textgreater{} freecellModel() \{ \newline return \seqsplit{FreecellModelCreator.create(GameType.MULTIMOVE);} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{- an array is a mutable, fixed-length, constant-time-indexed sequence of values of type t \newline - new int{[}{]}\{2, 4, 6, 8\} gives you fixed size array \newline - new int{[}9{]}\{\} gives you a empty array of 9 spaces with null or 0 \newline - uses length function \newline - mutability (intArray{[}3{]} = 17;) means int at index 3 is now 17 \newline - assertArrayEquals} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}