\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 (java-fundies-2.pdf) /Creator (Cheatography) /Author (Edgar (edgarjoel21)) /Subject (Java Fundies 2 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}{4B10A3} \definecolor{LightBackground}{HTML}{F3F0F9} \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{Java Fundies 2 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Edgar (edgarjoel21)} via \textcolor{DarkBackground}{\uline{cheatography.com/44289/cs/13117/}}} \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 12th October, 2017.\\ 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}{Contains (CartPt)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{` public boolean contains(CartPt pt) \{ return (this.nw.x \textless{}= pt.x) \&\& (pt.x \textless{}= this.nw.x + this.size) \&\& (this.nw.y \textless{}= pt.y) \&\& (pt.y \textless{}= this.nw.y + this.size); \} \}`} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{This occurs when we have CartPt nw as a field} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{2.0598 cm} x{1.3732 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{String Methods}} \tn % Row 0 \SetRowColor{LightBackground} s.length() & s.charAt(/) \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} s.substring(start, end) & s.toUpperCase() \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} s.toLowerCase() & s.indexOf(x) \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} s.replace(old, new) & s.split(regex) \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} s.trim() & s.equals(s2) \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} s.compareTo(s2) (Compares two strings Lexiographically) & \seqsplit{s.contains(CharSequence} cs) \tn % Row Count 8 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.50027 cm} x{0.81623 cm} p{0.50027 cm} x{0.81623 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{3.833cm}}{\bf\textcolor{white}{Arithmetic Operators}} \tn % Row 0 \SetRowColor{LightBackground} x + y & add & x - y & subtract \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} x * y & Multiply & x/y & divide \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} x \% y & modulo & & \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{2.30011 cm} p{1.13289 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Boolean Operators}} \tn % Row 0 \SetRowColor{LightBackground} ! x & not \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} x \&\& y & and \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} x || y & or \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Abstracted Sameness}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`abstract class AFoo implements IFoo \{ public boolean sameX(X that) \{ return false; \} public boolean sameY(Y that) \{ return false; \} public boolean sameZ(Z that) \{ return false; \} \} class X extends AFoo \{ public boolean sameFoo(IFoo that) \{ return that.sameX(this); \} public boolean sameX(X that) \{ ... compares two X values ... \} \} class Y extends AFoo \{ public boolean sameFoo(IFoo that) \{ return that.sameY(this); \} public boolean sameY(Y that) \{ ... compares two Y values ... \} \} class Z extends AFoo \{ public boolean sameFoo(IFoo that) \{ return that.sameZ(this); \} public boolean sameZ(Z that) \{ ... compares two Z values ... \} \}`} \tn % Row Count 14 (+ 14) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.34229 cm} p{0.71091 cm} p{0.34229 cm} x{1.23751 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{3.833cm}}{\bf\textcolor{white}{Comparisons}} \tn % Row 0 \SetRowColor{LightBackground} x \textless{} y & less & x \textless{}= y & less than or equal \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} x \textgreater{} y & greater than & x \textgreater{}= y & greater than or equal \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} x == y & Equal & ! x & Not equal \tn % Row Count 6 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Distance formula (Origin)}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{` public double distanceToOrigin() \{ return Math.sqrt(this.x {\emph{ this.x + this.y }} this.y) - this.radius; \} \}`} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{` - this.radius ` is only for circle} \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}{DistanceTo Formula}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`class CartPt \{ ... double distanceTo(CartPt that) \{ return Math.sqrt( (this.x - that.x) {\emph{ (this.x - that.x) + (this.y - that.y) }} (this.y - that.y)); \} \}`} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{2.64341 cm} p{0.78959 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Insert()}} \tn % Row 0 \SetRowColor{LightBackground} `public ILoBook insert(Book b) \{ if \seqsplit{(this.first.cheaperThan(b))} \{ return new ConsLoBook(this.first, this.rest.insert(b)); \} else \{ return new ConsLoBook(b, this); \} \}` & \seqsplit{ConsLoBook()} \tn % Row Count 7 (+ 7) % Row 1 \SetRowColor{white} `public ILoBook insert(Book b) \{ return new ConsLoBook(b, this); \}` & \seqsplit{MtLoBook()} \tn % Row Count 10 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.7165 cm} x{1.7165 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{append(ILoItem other)}} \tn % Row 0 \SetRowColor{LightBackground} `public ILoItem allNumbers() \{ return \seqsplit{this.left.allNumbers().append(this.right.allNumbers());} \}` & `// In interface-{}- ILoItem append(ILoItem other);` \tn % Row Count 5 (+ 5) % Row 1 \SetRowColor{white} `// in consLoItem -{}- public ILoItem append(ILoItem other) \{ new \seqsplit{ConsLoItem(this.first}, \seqsplit{this.rest.append(other));} \}` & `// MtLoItem -{}-{}- public ILoItem append(ILoItem other) \{ return other; \}` \tn % Row Count 11 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.34229 cm} x{1.76411 cm} p{0.2633 cm} p{0.2633 cm} } \SetRowColor{DarkBackground} \mymulticolumn{4}{x{3.833cm}}{\bf\textcolor{white}{Younger IAT (In IAT)}} \tn % Row 0 \SetRowColor{LightBackground} In IAT & `IAT youngerIAT(IAT other); -{}-{}-{}-{}-{}-{}- IAT youngerIATHelp(IAT other, int otherYob);` & & \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}----} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Younger IAT and Helper}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`public IAT youngerIAT(IAT other) \{return \seqsplit{other.youngerIATHelp(this}, this.yob);`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{`IAT youngerIATHelp(IAT other, int otherYob) \{ if (this.yob \textgreater{} otherYob) \{ return this; \} else \{ return other; \} \}`} \tn % Row Count 5 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Youngest Parent}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`public IAT youngestParent() \{ return \seqsplit{this.mom.youngerIAT(this.dad);} \}`} \tn % Row Count 2 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Youngest GrandParent}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`public IAT youngestGrandparent() \{ return \seqsplit{this.mom.youngestParent().youngerIAT(this.dad.youngestParent());} \}`} \tn % Row Count 3 (+ 3) \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 Interface}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`abstract class AShape implements IShape \{ CartPt loc; String color;   AShape(CartPt loc, String color) \{ this.loc = loc; this.color = color; \} \}`} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{p{0.3433 cm} p{0.3433 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Abstract Class}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{`class Circle extends AShape \{ int radius;   Circle(CartPt center, int radius, String color) \{ super(center, color); this.radius = radius; \} \}  `} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Subclass entension}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`class Square extends Rect \{   Square(CartPt nw, int size, String color) \{ super(nw, size, size, color); \}`} \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{size, size represent length and width. \newline You would need to override the method to use size rather than length/width} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.7165 cm} x{1.7165 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Tips}} \tn % Row 0 \SetRowColor{LightBackground} {\bf{Don't:}} & {\bf{Do:}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Casting & Design Recipe \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Field of Field & Tests: test helpers \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} isFoo() (isEmpty()) & Purpose statements \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Getters & Shorthand \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} & Dynamic Dispatch \tn % Row Count 6 (+ 1) \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 with Range in constructor}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`interface ITetrisPiece \{ int SCREEN\_HEIGHT = 30; \} `} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{3.833cm}}{`abstract class ATetrisPiece implements ITetrisPiece \{ ... ATetrisPiece(int x, int y) \{ this.xPos = x; this.yPos = y; \} ATetrisPiece(int x) \{ this(x, SCREEN\_HEIGHT); \} \}`} \tn % Row Count 6 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{3.833cm}}{\bf\textcolor{white}{Utilis Class}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`class Utils \{ int checkRange(int val, int min, int max, String msg) \{ if (val \textgreater{}= min \&\& val \textless{}= max) \{ return val; \} else \{ throw new \seqsplit{IllegalArgumentException(msg);} \} \} \}`} \tn % Row Count 5 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{This allows a constructor to be general} \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}{Illegal Exception Message in Constructor}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`// In class Date -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-Date(int year, int month, int day) \{ this.year = new Utils().checkRange(year, 1500, 2100, "Invalid year: " + Integer.toString(year)); this.month = new Utils().checkRange(month, 1, 12, "Invalid month " + \seqsplit{Integer.toString(month));} this.day = new Utils().checkRange(day, 1, 31, "Invalid day: " + Integer.toString(day));`} \tn % Row Count 8 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{\seqsplit{`integer.toString(year))`} changes the invalid integer to a string and combines (+)} \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}{Testing Exceptions}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{3.833cm}}{`// In Tester -{}-{}-{}- boolean \seqsplit{checkConstructorException(Exception} e, String className, ... constr args ...);`} \tn % Row Count 4 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{3.833cm}{x{1.7165 cm} x{1.7165 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{3.833cm}}{\bf\textcolor{white}{Sameness Interface}} \tn % Row 0 \SetRowColor{LightBackground} `interface IShape \{ boolean sameShape(IShape that); boolean sameCircle(Circle that); boolean sameRect(Rect that); boolean sameSquare(Square that); \}` & `// In Circle: public boolean sameShape(IShape that) \{ return \seqsplit{that.sameCircle(this);} \}` \tn % Row Count 8 (+ 8) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{3.833cm}}{Others would be false in this class} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}