\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{Bayan (Bayan.A)} \pdfinfo{ /Title (java-mastery-part-2.pdf) /Creator (Cheatography) /Author (Bayan (Bayan.A)) /Subject (Java Mastery - Part 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}{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 Mastery - Part 2 Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Bayan (Bayan.A)} via \textcolor{DarkBackground}{\uline{cheatography.com/122738/cs/22936/}}} \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}Bayan (Bayan.A) \\ \uline{cheatography.com/bayan-a} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 19th July, 2023.\\ Updated 1st June, 2020.\\ 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{2.58804 cm} x{2.38896 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{if then else}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{`if`}} statement identifies the statement/code block to run based on the expressions value(the specific condition)} \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} Code block is defined by curly braces & \{can contain 1 or more statements\} \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{`else`}} comes after the if, executed when the condition is `false`} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{`else if `}}can test multiple conditions} \tn % Row Count 8 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Example}} \newline \newline `int score = 6000;` \newline `if (score \textgreater{}= 5000) \{` \newline ~ do something; \newline `\} else if (score \textless{}1000 \&\& score \textgreater{}= 500) \{` \newline ~ do something else; \newline `{]} else {[} ` \newline ~ do this if all previous conditions are false \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Method}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Prevent code duplication} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Same as 'function' in other languages} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Set the name of the method, then the data type and name of parameters it will access from the `main` method} \tn % Row Count 5 (+ 3) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Java automatically creates variables with the appropriate data type for us} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Defining a new method:}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`public static void methodName(boolean boolParam, int intParam)` \{ \{\{nl\}\} \}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`void` means to "return nothing"} \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.83689 cm} x{2.14011 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Calling a Method}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{When the method is called, all the code in curly braces \{ \} in the method is executed} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Execute by {\bf{calling}} the method (in `main`) with the required {\bf{arguments}} in the brackets} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} Arguments can be the variable name or the actual values we want to send, as long as it matches the parameters & otherwise an error. \tn % Row Count 9 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`methodName(variable1, variable2);`} \tn % Row Count 10 (+ 1) % Row 4 \SetRowColor{LightBackground} In the example, two arguments expected & datatype need not be specified \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{A variable can be assigned a method result}}} \tn % Row Count 13 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{e.g. \{\{nl\}\} `int highScore = calculateScore(gameOver, score, level, bonus);`} \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{When the variable highScore is printed, the result of the calculations is sent back from the calculateScore method and assigned ot the variable} \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{{\bf{Return variable from method to main:}}}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`public static int methodName(boolean boolParam, int intParam)` \{ \{\{nl\}\} \}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Changing `void` to `int` means we are returning an `int` data type to the Main Method} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The method must explicitly return he variable:} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`return newValue;`} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{An `int` variable is expected, whether or not the statements in the method execute\{\{nl\}\} (i.e. when an if statement is either true or false, something must be returned)} \tn % Row Count 10 (+ 4) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{All the program variations must be accounted for} \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Full example:}} \{\{nl\}\} `public static void main(String{[}{]} args) \{ `\{\{nl\}\} ~` boolean isThisHelpful = true;` \{\{nl\}\} ~ `int points = 15;` \{\{nl\}\} ~ \seqsplit{`methodName(isThisHelpful}, points);` \{\{nl\}\}`\}`} \tn % Row Count 16 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`public static int methodName(boolean isThisHelpful, int points) \{` \newline \newline ~ `if (isThisHelpful) \{` \newline ~ ~ `int finalScore = points + 5` \newline ~ ~ \seqsplit{`System.out.println("Your} final score is: " + finalScore);` \newline ~ ~ `return finalScore;` \newline ~ ~` \}` \newline `return -1; ` \newline // -1 indicates invalid value/not found} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Method Overloading}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Allows us to create multiple methods with the {\bf{same name }}with {\bf{different parameters}}} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Changing the data type of the method will {\bf{not}} make it a unique signature. {\bf{The number of paramaters make it unique.}}} \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The methods can have the same name but {\bf{different implementations}}} \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`sum(int a, int b);` \{\{nl\}\} `sum(int a, int b, int c);` \{\{nl\}\} `sum(int a, int b, int c, int d);`} \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`public static int sum(int a, int b) \{`\{\{nl\}\} `return a + b;` \{\{nl\}\}` \}`} \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`public static int sum(int a, int b, int c) \{`\{\{nl\}\} `return a + b + c; \{\{nl\}\} \}`} \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`public static int sum(int a, int b, int c, int d) \{`\{\{nl\}\} `return a + b + c + d; \{\{nl\}\} \}`} \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{pintln methid is an example of method overloading, as there are 10 methods of the same name} \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{It improves code readability and re-usability} \tn % Row Count 18 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Easier to remember one method name isntead of many} \tn % Row Count 19 (+ 1) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Achieves naming consistency} \tn % Row Count 20 (+ 1) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Gives flexibility to call a similar method with different types of data, based on defined arguments/parameters} \tn % Row Count 23 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.53827 cm} x{2.43873 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Switch}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Similar to the if-then-else statement} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Good for testing values of the same variable} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{The variable that will be changed goes inside the brackets} \tn % Row Count 4 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{the conditions tested will be the {\bf{case}} numbers} \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{break;}} are essential to close off your case comparision. Without it, results will be unpredictable.} \tn % Row Count 8 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{The final switch statement is {\bf{default}}, same as {\bf{else}}} \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} {\bf{Switch statement}} & {\bf{if-then-else equivalent}} \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} `int value = 1;` & `int value = 1;` \tn % Row Count 13 (+ 1) % Row 8 \SetRowColor{LightBackground} `switch(value) \{` & Setting the variable to be changed \tn % Row Count 15 (+ 2) % Row 9 \SetRowColor{white} `case 1:` & `if (value == 1) \{` \tn % Row Count 16 (+ 1) % Row 10 \SetRowColor{LightBackground} \seqsplit{`System.out.println("Value} was 1");` & \seqsplit{`System.out.println("Value} was 1");` \tn % Row Count 18 (+ 2) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`break;`} \tn % Row Count 19 (+ 1) % Row 12 \SetRowColor{LightBackground} `case 2:` & `\} else if (value == 2) \{` \tn % Row Count 21 (+ 2) % Row 13 \SetRowColor{white} \seqsplit{`System.out.println("Value} was 2");` & \seqsplit{`System.out.println("Value} was 2");` \tn % Row Count 23 (+ 2) % Row 14 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`break;`} \tn % Row Count 24 (+ 1) % Row 15 \SetRowColor{white} `default:` & ` \} else \{` \tn % Row Count 25 (+ 1) % Row 16 \SetRowColor{LightBackground} \seqsplit{`System.out.println("Was} not 1 or 2");` & \seqsplit{`System.out.println("Was} not 1 or 2");` \tn % Row Count 28 (+ 3) % Row 17 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`break;`} \tn % Row Count 29 (+ 1) % Row 18 \SetRowColor{LightBackground} `\}` & `\}` \tn % Row Count 30 (+ 1) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.53827 cm} x{2.43873 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Switch (cont)}} \tn % Row 19 \SetRowColor{LightBackground} Case tests can also be on one line, e.g.: & `case 3:case 4:case 5:` \tn % Row Count 3 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.33919 cm} x{2.63781 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Common Methods}} \tn % Row 0 \SetRowColor{LightBackground} variable.{\bf{toLowerCase()}} & Turns the string variable to all lower case \tn % Row Count 3 (+ 3) % Row 1 \SetRowColor{white} variable.{\bf{toUpperCase()}} & Turns the string variable to all UPPERCASE \tn % Row Count 5 (+ 2) % Row 2 \SetRowColor{LightBackground} {\bf{Math.round}}(variable) & Rounds decimal numbers to the nearest value \tn % Row Count 8 (+ 3) % Row 3 \SetRowColor{white} {\bf{String.format("\%.2f",}} variable) & Converts and outputs the variable number with just two decimale points \tn % Row Count 12 (+ 4) % Row 4 \SetRowColor{LightBackground} {\bf{Math.sqrt}}(variable); & Variable must be long. If not, add (long) before the expression \tn % Row Count 15 (+ 3) % Row 5 \SetRowColor{white} {\bf{variable1.equals(variable2)}} & Tests if one String variable is equal to another \tn % Row Count 18 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.29402 cm} x{3.68298 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{For Loop/Statement}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Processes a part of a code block until a condition is satisfied} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Variable created in the for statement only exists in that code block} \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Structure}}:} \tn % Row Count 5 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{for(init; termination; increment)} \tn % Row Count 6 (+ 1) % Row 4 \SetRowColor{LightBackground} {\emph{init:}} & Code {\bf{initialised}} once at the start of the loop \tn % Row Count 8 (+ 2) % Row 5 \SetRowColor{white} {\emph{termination:}} & Determines at what point it exits the for loop \tn % Row Count 10 (+ 2) % Row 6 \SetRowColor{LightBackground} & Once it evaluates to false, it will exit the loop and proceed to the next line \tn % Row Count 13 (+ 3) % Row 7 \SetRowColor{white} {\emph{increment}} & An expression invoked after each iteration \tn % Row Count 15 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Example:}}} \tn % Row Count 16 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`for(int i = 0; i \textless{} 5; i++)`} \tn % Row Count 17 (+ 1) % Row 10 \SetRowColor{LightBackground} int i = 0 & initialise i to zero, \tn % Row Count 18 (+ 1) % Row 11 \SetRowColor{white} i \textless{} 5 & test if i is less than 5 and keeps processing until i is greater than 5 \tn % Row Count 21 (+ 3) % Row 12 \SetRowColor{LightBackground} i++ & add 1 to the value of i \tn % Row Count 22 (+ 1) % Row 13 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Looping forwards or backwards depends on the conditions and ranges} \tn % Row Count 24 (+ 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}{For Statement}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bayan-a_1590424312_for statement.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{while/do-while}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Similar to the for loop} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{instead of looping a certian number of times(as seen in `for`), `while() allows you to loop until the expression is true or false} \tn % Row Count 4 (+ 3) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{the count must be incremented, otherwise you will enter an infinite loop} \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} {\bf{for version}} & {\bf{while version}} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} `for(int i = 0; i != 5; i++) \{`\{\{nl\}\} //do something \} & `int i = 1` \{\{nl\}\} `while(i != 6)` \{ \{\{nl\}\} //do something \} \tn % Row Count 10 (+ 3) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{{\bf{while}}} \tn % Row Count 11 (+ 1) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{while(condition \{ \{\{nl\}\} //statements \{\{nl\}\} \}} \tn % Row Count 12 (+ 1) % Row 7 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`while(true) \{` \{\{nl\}\} `if(i == 5) \{ ` \{\{nl\}\} `break; `\{\{nl\}\} \} \{\{nl\}\} count++;} \tn % Row Count 14 (+ 2) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Do-While}}} \tn % Row Count 15 (+ 1) % Row 9 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{It will always execute once or more depending on defined expressions} \tn % Row Count 17 (+ 2) % Row 10 \SetRowColor{LightBackground} do \{ \{\{nl\}\} //statements \{\{nl\}\} \} while(condition); & {\bf{Remember}}, semi-colon after while condition \tn % Row Count 20 (+ 3) % Row 11 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`count = 1; \{\{nl\}\} do \{ \{\{nl\}\} count++; \{\{nl\}\} \} while(count != 6);`} \tn % Row Count 22 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.69218 cm} x{3.28482 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Object-Oriented Programming}} \tn % Row 0 \SetRowColor{LightBackground} Object & A value of a class type \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} & Has states and behaviours \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} & State is stored in fields \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} & e.g. a dogs name, breed, colour \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} & Behaviour is shown via methods \tn % Row Count 7 (+ 2) % Row 5 \SetRowColor{white} & e.g. a dogs behaviour: barking, wagging tail, running \tn % Row Count 10 (+ 3) % Row 6 \SetRowColor{LightBackground} Class & Describes set of objects with the same behaviour \tn % Row Count 12 (+ 2) % Row 7 \SetRowColor{white} & Can have any number of methods to access the value of different methods \tn % Row Count 15 (+ 3) % Row 8 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Updating a variable using a method instead of directly}}} \tn % Row Count 17 (+ 2) % Row 9 \SetRowColor{white} {\bf{Why?}} & If the variables access modifier is set to `private` for security reasons, we don't want users to directly change the variable by making it 'public' \tn % Row Count 23 (+ 6) % Row 10 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Use `this.` before the class to refer to a particular field} \tn % Row Count 25 (+ 2) % Row 11 \SetRowColor{white} `this.model = model;` & this means to update the field model with the contents of the parameter `model` that was passed through. \tn % Row Count 29 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Classes can have: \{\{nl\}\} {\bf{Local variables}}: Defined in methods, constructors or blocks \{\{nl\}\} {\bf{Instance variables}}: variables withina class but outside any method. Initialised when the class in instantiated. \{\{nl\}\} {\bf{Class variables}}: Variables declared within a class, outside any method, with the static keyword} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.4885 cm} x{2.4885 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Static Variables}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Declared using the keyword `static`} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Also known as {\bf{static member variables}}} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Every class instance shares the same static variable & Changes made to that variable will be seen in other instances \tn % Row Count 6 (+ 4) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Accessible by {\bf{static methods}} directly} \tn % Row Count 7 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Reading user input with {\bf{Scanner}}, `scanner` is declared as a static variable} \tn % Row Count 9 (+ 2) % Row 5 \SetRowColor{white} e.g. `private static String name;` & not a good idea \tn % Row Count 11 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.34379 cm} x{3.63321 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Instance variables}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{They {\bf{don't}} use the static keyword} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Known as fields or member variables} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Belong to an {\bf{instance}} of a class} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Represents the state of an isntance} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} Every instance: & -Has it's own copy of an instance variable \{\{nl\}\} -Can have a different value(state) \tn % Row Count 7 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.09034 cm} x{2.88666 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Static Method}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Declared using a static modifier} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Can't access instance methods and instance variables directly} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} Cannot use the `this` keyword & Usually used for operations that don't require data from an instance of the class (from `this`) \tn % Row Count 8 (+ 5) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{When a method {\bf{does not use instance variables}}, that method should be {\bf{declared as a static method}}} \tn % Row Count 11 (+ 3) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{e.g. main is a static method and is called by JVM when it starts an application} \tn % Row Count 13 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Example}} \newline \newline `public static void printSum(int a, int b) \{` \newline ~ `System.out.println("sum = " + (a + B));` \newline `\}`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4977 cm} p{0.4977 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Instance Methods}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{Belongs to an instance of a class} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{To use, we must instantiate the class first by using the new keyword} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Can}} access instance methods and variables directly} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{Can access static methods and static variables directly} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Example:}}} \tn % Row Count 8 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{2}{x{5.377cm}}{`class Dog \{` \{\{nl\}\} ~ `public void bark() \{` ~ \{\{nl\}\}~ \seqsplit{`System.out.println("woof");`} \{\{nl\}\}~ \} \{\{nl\}\}\}} \tn % Row Count 11 (+ 3) % Row 6 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{`public class Main \{ ` \{\{nl\}\} ~ `public static void main(String {[}{]} args) \{\{\{nl\}\} ~ ~ ``Dog rex = new Dog();` `//create instance` \{\{nl\}\} ~ ~ ``rex.bark(); //call instance method`` \{\{nl\}\} ~ \} \{\{nl\}\} \}} \tn % Row Count 16 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{5.377cm}}{\bf\textcolor{white}{Static or Instance Method?}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{5.377cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bayan-a_1590968049_vlcsnap-2020-06-01-00h31m59s924.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{5.377cm}}{Should a method be static? \newline ~ | \newline ~ V \newline Doies it use any fields(instance variables) or Instance methods? \newline \newline YES? It should probably be an instance method \newline \newline NO? It should probably be a static method} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}