\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{curae} \pdfinfo{ /Title (java-exam.pdf) /Creator (Cheatography) /Author (curae) /Subject (Java Exam 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}{A3A3A3} \definecolor{LightBackground}{HTML}{F3F3F3} \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 Exam Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{curae} via \textcolor{DarkBackground}{\uline{cheatography.com/71811/cs/18203/}}} \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}curae \\ \uline{cheatography.com/curae} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 9th December, 2018.\\ Updated 9th December, 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*}{3} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Simple Java Program}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public class HelloWorld\{ \newline //Main method must take String{[}{]} args \newline public static void main(String{[}{]} args)\{ \newline //Main functionality here \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Parsing}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{int *name = \seqsplit{Interger.parseInteger(str)}} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{5.377cm}}{double *name = Double.parseDouble(str)} \tn % Row Count 2 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Casting: (int)({\emph{some other type); (double)(}}some other type)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Java Object}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public class ObjectName\{ \newline \newline //variables that are private to this object \newline private {\emph{type }}variableName; \newline \newline //Constructor gets called when new method is created -{}-\textgreater{} Multiple constructors \newline //can exist \newline public ObjectName(*parameters)\{ \newline //Whatever happens in constructor \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.78712 cm} x{2.18988 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Math Methods}} \tn % Row 0 \SetRowColor{LightBackground} Math.pow(a, b) & Math.PI() \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Math.log(x), Math.log10(x) & Math.sqrt(x) \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Math.floor rounds down & Math.ceil() rounds up \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} Math.random() Unif{[}0,1) & Math.min(), Math.max() \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Uniform Random Int between {[}1,6{]} -{}-\textgreater{} (int)(Math.random() + 1)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{IntegerStack}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{IntegerStack intStack = new IntegerStack(); \newline \newline //Add an element to the stack \newline intStack.push(int); \newline \newline //Removing the top element from the stack \newline intStack.pop(); \newline \newline //checking if empty \newline intStack.isEmpty();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Might need to import IntegerStack -{}-\textgreater{} Class specific object -{}-\textgreater{} not java util} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Knapsack}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{P = integer array (n+1, V+1) \newline for(v = 0 ... V) \newline P(0,v) = 0; \newline for (i = 1 ... n) \newline for (v = 0 ... V) \newline if (volumes(i-1) \textless{}= v) \newline P(i,v) = max(profit(i-1) + P(i-1,v-volumes(i-1)), \newline P(i-1,v)); \newline else \newline P(i,v) = P(i-1,v) \newline return P(n,V);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Statements}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\bf{If Statement}} \newline if ( expression ) \{ \newline statements \newline \} else if ( expression ) \{ \newline statements \newline \} else \{ \newline statements \newline \} \newline \newline {\bf{While Loop}} \newline while ( expression ) \{ \newline statements \newline \} \newline \newline {\bf{Do-While Loop}} \newline do \{ \newline statements \newline \} while ( expression ); \newline \newline {\bf{For Loop}} \newline for ( int i = 0; i \textless{} max; ++i) \{ \newline statements \newline \} \newline \newline {\bf{For Each Loop}} \newline for ( var : collection ) \{ \newline statements \newline \} \newline \newline {\bf{Switch Statement}} \newline switch ( expression ) \{ \newline case value: \newline statements \newline break; \newline case value2: \newline statements \newline break; \newline default: \newline statements \newline \} \newline \newline {\bf{Exception Handling}} \newline try \{ \newline statements; \newline \} catch (ExceptionType e1) \{ \newline statements; \newline \} catch (Exception e2) \{ \newline catch-all statements; \newline \} finally \{ \newline statements; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{for loop is more general: for(int i; booleanMethod(i), incrementMethod(i)\{\} \newline incrementing in short: \newline i = i + 1; -{}-\textgreater{} i++; \newline i = i - 1; -{}-\textgreater{} i-{}-; \newline i += a; \newline i -+ a;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Insertion Sort}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Insertion Sort: \newline for (i = 1 ... n-1) \newline for (j = i ... 1) \newline if (a(j-1) \textgreater{} a(j)) \newline swap(a(j-1),a(j)) \newline else break;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{3.68298 cm} x{1.29402 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{String Methods}} \tn % Row 0 \SetRowColor{LightBackground} .toUpperCase(); & \seqsplit{toLowerCase();} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} .substring(i,j) j is excluded & .length() \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} .compareTo(str) *lexicographic ordering (-1, 0, 1) & \seqsplit{.equals(str)} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} .indexOf(e) & \seqsplit{.concat(str)} \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} .charAt(i) & \seqsplit{.contains(e)} \tn % Row Count 10 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{{\emph{type{[}{]} }}arrayName = new {\emph{type{[}}}length{]} \newline E.g. \newline boolean{[}{]} visitedNode = new boolean{[}this.numberOfNodes{]};} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.84149 cm} x{3.13551 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{ArrayList}} \tn % Row 0 \SetRowColor{LightBackground} create & ArrayList\textless{}type\textgreater{} name = new ArrayList\textless{}type\textgreater{}(); \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} access element & list.get(i) \tn % Row Count 4 (+ 1) % Row 2 \SetRowColor{LightBackground} update element & list.set(i, e) \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} return length & list.size() \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} add element {\emph{somewhere}} & list.add(e) \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} add element at i & list.add(i,e) \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} remove element & list.remove(i or e) \tn % Row Count 11 (+ 1) % Row 7 \SetRowColor{white} remove all elements & list.clear() \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{import java.util.ArrayList;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Queues}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Queue\textless{}type\textgreater{} q = new Queue\textless{}type\textgreater{}(); \newline \newline //put element in queue \newline q.enqueue(e); \newline \newline //remove element in queue \newline q.dequeue(); \newline \newline //check if empty \newline q.isEmpty(); \newline \newline //check size \newline q.length(); or q.size();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{We use a class specific Queue method -{}-\textgreater{} not the java utils one} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{GCD}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{public static int GCD (int m, int n)\{ \newline int temp; \newline while (n\%m != 0)\{ \newline temp = m; \newline m = n\%m; \newline n = temp; \newline if (m==0) return 1; \newline \} \newline return m; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}