\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{bennetsph} \pdfinfo{ /Title (fundamentals-exam.pdf) /Creator (Cheatography) /Author (bennetsph) /Subject (fundamentals\_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{fundamentals\_exam Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{bennetsph} via \textcolor{DarkBackground}{\uline{cheatography.com/195475/cs/40962/}}} \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}bennetsph \\ \uline{cheatography.com/bennetsph} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 21st October, 2023.\\ Updated 21st October, 2023.\\ 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{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Basics}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Hello, World}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697874535_hello.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Editing, Compiling, and executing}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697874592_developing.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Built in Datatypes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697875165_built-in.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Integers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697875527_int.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Integer Operations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697875574_int-expressions.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Floating Point Numbers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697875682_double.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Floating Point Numbers Operations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697875731_double-expressions.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{String Data Type}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697878084_string-api.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{String operations}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697878569_string-ops.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Commands to Split a String Into Separate Parts}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Scanner scan = new Scanner(System.in); \newline \newline System.out.println("What is your date of birth? (Format: dd mm yyyy) "); \newline String dateBirthday = scan.nextLine(); \newline \newline String{[}{]} parts = dateBirthday.split(" "); \newline \newline // Storing users birthday data \newline int day = Integer.parseInt(parts{[}0{]}); \newline int month = Integer.parseInt(parts{[}1{]}); \newline int year = Integer.parseInt(parts{[}2{]}); \newline \newline // Obtaining today's date: \newline int currentDay = \seqsplit{LocalDate.now().getDayOfMonth();} \newline int currentMonth = \seqsplit{LocalDate.now().getMonthValue();} \newline int currentYear = \seqsplit{LocalDate.now().getYear();}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Math Library}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697876214_math-api.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Type Conversions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697876360_casts.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Parsing Command-Line Arguments}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697876153_parse-api.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Swtich statement}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697877014_switch.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Do-While Loop}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697876979_do-while.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Example Functions}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697877797_function-examples.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Typical Array Problems}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697877182_array-examples.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Formatted Output}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697877490_Screenshot 2023-10-21 at 10.37.44 AM.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Memory Usage of Data Types}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/prpr_1697880945_Picture2.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Command Line Input from File}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697877716_command.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Classes Overview}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697877883_class.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Method to Read a Matrix From a File}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static double{[}{]}{[}{]} readMatrix(String filename) \newline throws java.io.FileNotFoundException\{ \newline File file = new File(filename); \newline Scanner input = new Scanner (file); \newline \newline int rows = input.nextInt(); \newline int columns = input.nextInt(); \newline \newline double{[}{]}{[}{]} matrix = new double{[}rows{]}{[}columns{]}; \newline \newline for (int i=0;i\textless{}rows;i++)\{ \newline for (int j=0;j\textless{}columns;j++)\{ \newline matrix {[}i{]} {[}j{]}= input.nextDouble(); \newline \newline \} \newline \} \newline \newline input.close(); \newline return matrix; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Two first Integers in a file defines the size of a matrix n x m.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Tutorial Content}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Random Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// returns an array of length n containing random integer values \newline public static int{[}{]} randomIntArray(int n)\{ \newline Random random = new Random(); \newline int{[}{]} array = new int{[}n{]}; \newline for(int i=0; i \textless{} n; i++)\{ \newline array{[}i{]} = random.nextInt(); \newline \} \newline return array; \newline \} \newline \newline // returns an array of length n containing random integer values \newline // in the interval {[}lb,ub{]} \newline public static int{[}{]} randomIntArray(int n, int lb, int ub)\{ \newline Random random = new Random(); \newline int{[}{]} array = new int{[}n{]}; \newline for(int i=0; i \textless{} n; i++)\{ \newline array{[}i{]} = random.nextInt(ub-lb+1) + lb; \newline \} \newline return array; \newline \} \newline \newline // returns an array of length n containing random double values \newline public static double{[}{]} randomDoubleArray(int n)\{ \newline Random random = new Random(); \newline double{[}{]} array = new double{[}n{]}; \newline for(int i=0; i \textless{} n; i++)\{ \newline array{[}i{]} = random.nextDouble(); \newline \} \newline return array; \newline \} \newline \newline // returns an array of length n containing random double values \newline // in the interval {[}lb,ub{]} \newline public static double{[}{]} randomDoubleArray(int n, int lb, int ub)\{ \newline Random random = new Random(); \newline double{[}{]} array = new double{[}n{]}; \newline for(int i=0; i \textless{} n; i++)\{ \newline array{[}i{]} = \seqsplit{random.nextDouble(ub-lb+1)} + lb; \newline \} \newline return array; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Read Double Array.}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static double{[}{]} readArray(String filename) \newline throws java.io.FileNotFoundException\{ \newline File file = new File(filename); \newline Scanner input = new Scanner (file); \newline \newline int n = input.nextInt(); \newline \newline double{[}{]} doubleArray = new double{[}n{]}; \newline \newline for (int i=0;i\textless{}n;i++)\{ \newline doubleArray {[}i{]} = input.nextDouble(); \newline \} \newline \newline input.close(); \newline \newline return doubleArray; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{To read other type array, we substitute the "doubles" with int or int{[}{]} or nextInt()} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Digit Sum Method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static int totalDigitSum(int n) \{ \newline \newline int numberOfDigits = \seqsplit{(String.valueOf(n)).length();} \newline int sum = 0; \newline System.out.print("The total digit sum"); \newline \newline while(numberOfDigits != 0) \{ \newline \newline sum = sum + (n\%10); \newline n = n/10; \newline \newline numberOfDigits-{}-; \newline \newline \} \newline \newline return sum; \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Print Prime Factors}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static void printPrimeFactors(int n) \{ \newline if (n \textless{}= 1) \{ \newline System.out.print("No prime factors"); \newline return; \newline \} \newline \newline for (int i = 2; i \textless{}= n; i++) \{ \newline while (n \% i == 0) \{ \newline System.out.print(i + " "); \newline n /= i; \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Method to print the prime factors of a given number} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Method to compute Factorials (n!)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static int{[}{]} computeFactorials(int n) \{ \newline int{[}{]} factorials = new int{[}n{]}; \newline factorials{[}0{]} = 1; \newline \newline for (int i = 1; i \textless{} n; i++) \{ \newline factorials{[}i{]} = factorials{[}i - 1{]} * (i + 1); \newline \} \newline \newline return factorials; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Method Simulating Dice Rolling N times}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static double expectedValue (int numberOfTrials)\{ \newline \newline double sum = 0; \newline \newline for(int i = 1; i \textless{}= numberOfTrials; i++)\{ \newline sum = sum + (int) (Math.random() * 6) +1; \newline \} \newline return sum/numberOfTrials; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Program that computes the expected value of rolling a six sided dice. Input parameter is the number of trials} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Method for Standard Deviation}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// Method that computes standard deviation of an array \newline public static double getStandardDeviation(double{[}{]} array) \{ \newline double sum = 0; \newline double mean = getMean(array); \newline \newline for(double i : array) \{ \newline double temp = (i - mean) * (i - mean); \newline sum += temp; \newline \} \newline \newline double standardDeviation = sum/array.length; \newline standardDeviation = \seqsplit{Math.sqrt(standardDeviation);} \newline return standardDeviation; \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Greatest Common Divisor \& Least Common Multiple}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// GCD \newline public static int getGcd(int n, int m) \{ \newline while(m\%n != 0) \{ \newline int temp = m\%n; \newline m = n; \newline n = temp; \newline \} \newline return n; \newline \} \newline \newline // LCM \newline public static int lcm(int a, int b) \{ \newline int gcd = gcd(a, b); \newline return (a / gcd) * b; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Determening the Season}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{/{\emph{ Program that asks the user for a date and returns in which season this date falls. \newline \newline // method calculating determining the season, given the month and day \newline public int getSeason(int day, int month) \{ \newline \newline // Formula to translate everything on one 'scale' \newline int combinedValue = month}}100 + day; \newline \newline if (combinedValue \textgreater{}= 321 \& combinedValue \textless{}= 620) \{ \newline \newline this.season = 1; \newline \newline \} \newline else if (combinedValue \textgreater{}= 621 \& combinedValue \textless{}= 920) \{ \newline \newline this.season = 2; \newline \newline \} \newline else if (combinedValue \textgreater{}= 921 \& combinedValue \textless{}= 1220) \{ \newline \newline this.season = 3; \newline \newline \} \newline else \{ \newline \newline this.season = 0; \newline \newline \} \newline \newline return this.season; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Sorting Algorithms}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Commands for Random Numbers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{double random1 = Math.random(); \newline \newline System.out.println("A random value between 0 and 1: " + random1); \newline \newline double random2 = Math.random(){\emph{100; \newline \newline System.out.println("A random double value between 0 and 100: " + random2); \newline \newline int random3 = (int) (Math.random()}}100); \newline \newline System.out.println("A random integer value between 0 and 99: " + random3);} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Binary Search Method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static boolean find(int{[}{]} array, int number)\{ \newline \newline int low = 0; \newline int high = array.length-1; \newline \newline while (low+1 \textless{} high) \{ \newline int mid = (high + low) /2; \newline \newline if (array{[}mid{]} == number)\{ \newline return true; \newline \} \newline else if (array{[}mid{]} \textless{} number)\{ \newline low = mid; \newline \} \newline else \{ \newline high = mid; \newline \} \newline \} \newline \newline return ((array{[}low{]} == number) || (array{[}high{]} == number)); \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Random Sorted Int Array}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static int{[}{]} randomSortedIntArray(int n, int startValue, int maxIncrement)\{ \newline Random random = new Random(); \newline int{[}{]} array = new int{[}n{]}; \newline array{[}0{]} = startValue; \newline for(int i=1; i \textless{} n; i++)\{ \newline array{[}i{]} = array{[}i-1{]} + \seqsplit{random.nextInt(maxIncrement);} \newline \} \newline return array; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Random Sorted Int Array (Basis for Binary Search)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Insertion Sort Algorithm}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static void sort(int{[}{]} a) \{ \newline int n = a.length; // Get the length of the input \newline array 'a'. \newline for (int i = 1; i \textless{} n; i++) \{ \newline for (int j = i; j \textgreater{} 0; j-{}-) \{ \newline // This loop iterates from the current \newline position 'i' towards the beginning \newline of the array. \newline if (a{[}j-1{]} \textgreater{} a{[}j{]}) \{ \newline // If the element at the previous \newline position is greater than the \newline current element, \newline // we need to swap them to sort \newline the array in ascending order. \newline swap(a, j-1, j); \newline \} else \{ \newline // If the element at the previous \newline position is not greater than the \newline current element, \newline // it means the array is sorted \newline up to this point, so we break out of the \newline loop. \newline break; \newline \} \newline \} \newline \} \newline \} \newline \newline public static void swap(int{[}{]} arr, int index1, int index2) \{ \newline // This function swaps two elements \newline in the array 'arr' at the given indices 'index1' \newline and 'index2'. \newline int temp = arr{[}index1{]}; \newline arr{[}index1{]} = arr{[}index2{]}; \newline arr{[}index2{]} = temp; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Random Array in Interval}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static int{[}{]} randomArray(int n)\{ \newline int{[}{]} array = new int{[}n{]}; \newline \newline for(int i=0; i\textless{}n;i++)\{ \newline array {[}i{]} = (int) ( Math.random()*1000 - 500); \newline \} \newline return array; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{random array length n, interval int between {[}-500;500{]}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Insertion Sort of an Object w.r.t. String/Int}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// Insertion Sort Algorithm based on First Name \newline public static void sortFirstName(PersonNew{[}{]} personList) \{ \newline \newline for(int i = 1; i \textless{} personList.length; i++) \{ \newline \newline PersonNew currentPerson = personList{[}i{]}; \newline int j = i - 1; \newline boolean flag = true; \newline \newline // We will compare current person's name with other ones \newline while(j \textgreater{}= 0 \&\& flag) \{ \newline \newline String currentName = \seqsplit{currentPerson.getFirstName();} \newline String testName = personList{[}j{]}.getFirstName(); \newline \newline // Checking which name is shorter: \newline int minimum = \seqsplit{Math.min(currentName.length()}, testName.length()); \newline int k = 0; \newline \newline // Now we will check which name is "smaller" based on ascii: \newline while(k \textless{} minimum) \{ \newline \newline char letterCurrent = currentName.charAt(k); \newline char letterTest = testName.charAt(k); \newline \newline // Current Name is larger than other \newline if(letterCurrent \textless{} letterTest) \{ \newline \newline personList{[}j+1{]} = personList{[}j{]}; \newline j-{}-; \newline break; \newline \newline \} \newline // Current Name is already smaller \newline else if(letterTest \textless{} letterCurrent) \{ \newline \newline flag = false; \newline break; \newline \newline \} \newline // They have the same characters, thus we continue going through each string \newline else \{ \newline \newline k++; \newline \newline \} \newline \newline // We reach the end of smaller name, meaning that they have the same name: \newline if(k == minimum) \{ \newline \newline personList{[}j+1{]} = personList{[}j{]}; \newline j-{}-; \newline \newline \} \newline \newline \} \newline \newline \newline \} \newline \newline // Inserting in its correct position. \newline personList{[}j+1{]} = currentPerson; \newline \newline \} \newline \newline \} \newline \newline // Insertion Sort Algorithm based on Height \newline public static void sortHeight(PersonNew{[}{]} personList) \{ \newline \newline for(int i = 1; i \textless{} personList.length; i++) \{ \newline \newline PersonNew currentPerson = personList{[}i{]}; \newline int j = i-1; \newline \newline while((j \textgreater{}= 0) \&\& currentPerson.isTaller(personList{[}j{]})) \{ \newline \newline personList{[}j+1{]} = personList{[}j{]}; \newline j-{}-; \newline \newline \} \newline \newline personList{[}j+1{]} = currentPerson; \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Objects}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Constructor Method for Point / Line}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// constructor for a line through points p and q \newline // sets slope and intercept to Double.NaN for vertical lines \newline public Line(Point p, Point q) \{ \newline \newline // vertical line: \newline if (p.getX() == q.getX()) \{ \newline \newline this.intercept = Double.NaN; \newline this.slope = Double.NaN; \newline \newline \} \newline \newline // otherwise we compute the slope (y2 - y1) / (x2 - x1) \newline // and intercept from y = mx + c, using one of the points \newline else \{ \newline \newline this.slope = (q.getY() - p.getX()) / (q.getX() - p.getX()); \newline this.intercept = p.getY() - this.slope * p.getX(); \newline \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{This Constructor computes the equation of the line, given two points} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Few Methods for Object Date}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// method that returns true if this date is before the date given by the argument \newline public boolean isBefore(Date date) \{ \newline \newline if (this.year \textless{}= date.year) \{ \newline if(this.month \textless{}= date.month) \{ \newline if(this.day \textless{}= date.day) \{ \newline return true; \newline \} \newline return false; \newline \} \newline return false; \newline \} \newline return false; \newline \} \newline \newline // method that increments this date by one day \newline public void increment() \{ \newline \newline // Array storing days per month \newline int{[}{]} daysPerMonth = \{0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31\}; \newline // Figure out if its leap year, to change daysPerMonth{[}2{]} = 29. \newline if(isLeapYear()) \{ \newline daysPerMonth{[}2{]} = 29; \newline \} \newline \newline // Check if its last day of the month \newline if(this.day == daysPerMonth{[}this.month{]}) \{ \newline this.day = 1; \newline \newline // Check if its new year: \newline if(this.month == 12) \{ \newline \newline this.month = 1; \newline year++; \newline \newline \} \newline else \{ \newline month++; \newline \} \newline \} \newline else \{ \newline day++; \newline \} \newline \newline \} \newline \newline // method that returns true if the year of this date is a leap year \newline private boolean isLeapYear() \{ \newline \newline return (this.year \% 4 == 0 \&\& this.year \% 100 != 0) || (this.year \% 400 == 0); \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{We store dd/mm/yyyy in the Object Date. Constructor method is not shown here.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Methods For Rational Numbers (Object Rational)}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// add rational f2 to this rational and return the result. \newline public Rational add(Rational f2) \{ \newline \newline if(this.denominator == f2.getDenominator()) \{ \newline \newline int newNumerator = this.numerator + f2.getDenominator(); \newline return new Rational(newNumerator, this.denominator); \newline \newline \} \newline else \{ \newline \newline // we find the new denominator: \newline int newDenominator = findLCM(this.denominator, f2.getDenominator()); \newline int newNumerator = (this.numerator{\emph{(newDenominator / this.denominator)) + (f2.getNumerator()}}(newDenominator / f2.getDenominator())); \newline \newline return new Rational(newNumerator, newDenominator); \newline \newline \} \newline \newline \} \newline \newline // compute the reciprocal of this Rational and return the result \newline public Rational reciprocal() \{ \newline \newline return new \seqsplit{Rational(this.denominator}, this.numerator); \newline \newline \} \newline \newline // divide this Rational by Rational f2 and return the result \newline public Rational divide(Rational f2) \{ \newline \newline Rational reciprocalf2 = f2.reciprocal(); \newline \newline int newNum = this.numerator {\emph{ \seqsplit{reciprocalf2.getNumerator();} \newline int newDen = this.denominator }} \seqsplit{reciprocalf2.getDenominator();} \newline \newline return new Rational(newNum, newDen); \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Reciprocal: x/y becomes y/x.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Runtime Exercises}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Run Times}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/prpr_1697880773_Picture1.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Knapsack Problem}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static int knapsackAlgorithm(int n, int{[}{]} profits, int{[}{]} volumes, int V) \{ \newline // Create a 2D array 'P' to store the maximum profit for different combinations \newline of items and volumes. \newline int{[}{]}{[}{]} P = new int{[}n + 1{]}{[}V + 1{]}; \newline \newline // Initialize the first row (no items) with zeros, as no profit can be gained. \newline for (int v = 0; v \textless{}= V; v++) \{ \newline P{[}0{]}{[}v{]} = 0; \newline \} \newline \newline // Loop through the items (i) and the available volume (v). \newline for (int i = 1; i \textless{}= n; i++) \{ \newline for (int v = 0; v \textless{}= V; v++) \{ \newline // Check if the volume of the current item can fit in the \newline available volume (v). \newline if (volumes{[}i - 1{]} \textless{}= v) \{ \newline // If it can fit, calculate the maximum profit for two options: \newline // 1. Including the current item and subtracting its volume \newline from the available volume. \newline // 2. Excluding the current item. \newline P{[}i{]}{[}v{]} = Math.max(profits{[}i - 1{]} + P{[}i - 1{]} \newline {[}v - volumes{[}i - 1{]}{]}, P{[}i - 1{]}{[}v{]}); \newline \} else \{ \newline // If the current item cannot fit, take the maximum profit \newline from the previous row. \newline P{[}i{]}{[}v{]} = P{[}i - 1{]}{[}v{]}; \newline \} \newline \} \newline \} \newline \newline // The final element of the 'P' array (bottom-right) contains the \newline maximum profit achievable with the given constraints. \newline return P{[}n{]}{[}V{]}; \newline \} \newline \newline public static int{[}{]} generateRandomArray(int size, int min, int max) \{ \newline // Create an array of the specified size. \newline int{[}{]} array = new int{[}size{]}; \newline Random rand = new Random(); \newline \newline // Fill the array with random integers in the range {[}min, max{]}. \newline for (int i = 0; i \textless{} size; i++) \{ \newline array{[}i{]} = rand.nextInt(max - min + 1) + min; \newline \} \newline \newline // Return the generated array. \newline return array; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Magic Square Problem}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public static boolean isMagic(int{[}{]}{[}{]} matrix) \{ \newline \newline return \seqsplit{(hasUniqueNumbers(matrix)} \&\& hasEqualSums(matrix)); \newline \newline \} \newline \newline // Method that checks if all rows, columns and \newline two diagonals (right \& left) have equal sums: \newline public static boolean hasEqualSums(int{[}{]}{[}{]} matrix) \{ \newline \newline // Variable to store target sum: \newline int targetSum = 0; \newline \newline // Target sum = sum of first row \newline for(int i = 0; i \textless{} matrix.length; i++) \{ \newline \newline targetSum += matrix{[}0{]}{[}i{]}; \newline \newline \} \newline \newline int leftDiagonal = 0; \newline int rightDiagonal = 0; \newline \newline for(int j = 0; j \textless{} matrix.length; j++) \{ \newline \newline int sumColumn = 0; \newline int sumRow = 0; \newline \newline for(int k = 0; k \textless{} matrix{[}0{]}.length; k++) \{ \newline \newline sumRow += matrix{[}j{]}{[}k{]}; \newline sumColumn += matrix{[}k{]}{[}j{]}; \newline \newline // Check if we are on the left diagonal: \newline if(j == k) \{ \newline \newline leftDiagonal += matrix{[}j{]}{[}k{]}; \newline \newline \} \newline // Check if we are on the right diagonal: \newline if(j+k == matrix.length - 1) \{ \newline \newline rightDiagonal += matrix{[}j{]}{[}k{]}; \newline \newline \} \newline \newline \} \newline \newline // Check if the sum is equal to our target one: \newline if (sumRow != targetSum || sumColumn != targetSum) \{ \newline \newline return false; \newline \newline \} \newline \newline \} \newline \newline // Check if diagonal sums are = target \newline if(rightDiagonal != targetSum || leftDiagonal != targetSum) \{ \newline \newline return false; \newline \newline \} \newline \newline return true; \newline \newline \} \newline \newline // Method that checks whether matrix contains \newline only unique numbers from 1 to n{\emph{n. \newline \newline public static boolean hasUniqueNumbers(int{[}{]}{[}{]} matrix) \{ \newline \newline /}} \newline We know that there will be numbers from 1 to n{\emph{n. \newline We create boolean array of size n}}n which \newline will help to flag not unique numbers. \newline Default array element value = false. \newline {\emph{/ \newline int n = matrix.length; \newline boolean{[}{]} seenNumber = new boolean{[}n}}n{]}; \newline int arrayIndex = 0; \newline \newline for(int i = 0; i \textless{} matrix.length; i++) \{ \newline \newline for(int j = 0; j \textless{} matrix{[}0{]}.length; j++) \{ \newline \newline int number = matrix{[}i{]}{[}j{]}; \newline // We check whether the number has \newline already been flagged \&\& whether 1 \textless{}= number \textless{}= n{\emph{n \newline if(!seenNumber{[}number - 1{]} \&\& number \textgreater{}= 1 \&\& number \textless{}= (n}}n)) \{ \newline \newline // Number is being flagged: \newline seenNumber{[}number - 1{]} = true; \newline \newline \} \newline else \{ \newline \newline return false; \newline \newline \} \newline \newline \} \newline \newline \newline \} \newline \newline return true; \newline \newline \} \newline \newline \newline public static int {[}{]}{[}{]} rotate(int{[}{]}{[}{]} matrix)\{ \newline int n = matrix.length; \newline int m = matrix{[}0{]}.length; \newline \newline // Create a new matrix to store the rotated elements \newline int{[}{]}{[}{]} transpose = new int{[}n{]}{[}m{]}; \newline \newline for(int i=0;i\textless{}n;i++)\{ \newline for(int j=0;j\textless{}n;j++)\{ \newline transpose{[}i{]}{[}j{]}=matrix{[}j{]}{[}i{]}; \newline \} \newline \} \newline \newline int{[}{]}{[}{]} rotated = new int{[}n{]}{[}m{]}; \newline int x = n-1; \newline for (int k = 0; k \textless{} n; k++) \{ \newline rotated{[}k{]} = transpose{[}x{]}; \newline x-{}-; \newline \} \newline \newline \newline return rotated; \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Data Structures}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Stack Data Type}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697878702_stack-api.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Intger Stack Class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{import java.util.{\emph{; \newline \newline public class IntegerStack \{ \newline \newline private int size; // size of the stack \newline private Node first; // top of stack \newline \newline // helper linked list class \newline private class Node \{ \newline private int value; \newline private Node next; \newline \} \newline \newline // Constructor for an empty stack \newline \newline public IntegerStack() \{ \newline \newline this.first = null; \newline this.size = 0; \newline \newline \} \newline \newline // Returns true if this stack is empty. \newline \newline public boolean isEmpty() \{ \newline return first == null; \newline \} \newline \newline // returns the size of this stack \newline public int size() \{ \newline return this.size; \newline \} \newline \newline // adds number to the top of the stack \newline public void push(int number) \{ \newline Node oldfirst = first; \newline first = new Node(); \newline first.value = number; \newline first.next = oldfirst; \newline size++; \newline \} \newline \newline public String toString() \{ \newline \newline IntegerStack aux = new IntegerStack(); \newline \newline String stackString = new String(); \newline \newline while (!this.isEmpty()) \{ \newline int x = this.pop(); \newline stackString += x + " "; \newline aux.push(x); \newline \} \newline \newline while (!aux.isEmpty()) \{ \newline this.push(aux.pop()); \newline \} \newline \newline stackString += "\textbackslash{}n"; \newline return stackString; \newline \} \newline \newline /}}{\emph{ \newline }} Removes and returns the item most recently added to this stack. \newline */ \newline public int pop() \{ \newline // if (isEmpty()) throw new \seqsplit{NoSuchElementException("Stack} underflow"); \newline int number = first.value; // save item to return \newline first = first.next; // delete first node \newline size-{}-; \newline return number; // return the saved item \newline \} \newline \newline public void absoluteValue() \{ \newline \newline IntegerStack aux = new IntegerStack(); \newline \newline while (!this.isEmpty()) \{ \newline \seqsplit{aux.push(Math.abs(this.pop()));} \newline \} \newline \newline // put auxiliary stack back on both stacks \newline while (!aux.isEmpty()) \{ \newline this.push(aux.pop()); \newline \} \newline \newline \} \newline \newline // Method that checks if two stacks are exactly the same \newline public boolean isEqual(IntegerStack secondStack) \{ \newline \newline IntegerStack aux = new IntegerStack(); \newline boolean flag = true; \newline \newline // Stacks are of a different size. \newline if(this.size != secondStack.size) \{ \newline \newline flag = false; \newline return flag; \newline \newline \} \newline \newline // Checking whether the two are the same \newline while(!this.isEmpty() \&\& !secondStack.isEmpty()) \{ \newline \newline int thisNumber = this.pop(); \newline int secondNumber = secondStack.pop(); \newline \newline if(!(thisNumber == secondNumber)) \{ \newline \newline // Immediately push back the numbers on top: \newline this.push(thisNumber); \newline \seqsplit{secondStack.push(secondNumber);} \newline \newline flag = false; \newline break; \newline \newline \} \newline \newline aux.push(thisNumber); \newline \newline \} \newline \newline // Restoring both stacks: \newline while (!aux.isEmpty()) \{ \newline \newline int number = aux.pop(); \newline \newline this.push(number); \newline \seqsplit{secondStack.push(number);} \newline \newline \} \newline \newline return flag; \newline \newline \} \newline \newline // Method that reverses the order of the stack: \newline public void reverse() \{ \newline \newline // empty stack: \newline if(this.isEmpty()) \{ \newline \newline \seqsplit{System.out.println("Empty} stack was provided."); \newline return; \newline \newline \} \newline \newline Queue\textless{}Integer\textgreater{} tempQ = new Queue\textless{}Integer\textgreater{}(); \newline \newline // Putting all elements from stack to queue data structure \newline while(!this.isEmpty()) \{ \newline \newline \seqsplit{tempQ.enqueue(this.pop());} \newline \newline \} \newline \newline // Putting everything form queue to stack, to get reverse order \newline while(!tempQ.isEmpty()) \{ \newline \newline \seqsplit{this.push(tempQ.dequeue());} \newline \newline \} \newline \newline \} \newline \newline // Fills stack with -1 and 1 randomly \newline public void fillStackRandom() \{ \newline \newline Random randomNumber = new Random(); \newline \newline for(int i = 0; i \textless{} 100; i++) \{ \newline \newline int number = randomNumber.nextInt(2) == 0 ? 1 : -1; \newline this.push(number); \newline \newline \} \newline \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Queue Data Type}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{p{17.67cm}}{\vspace{1px}\centerline{\includegraphics[width=5.1cm]{/web/www.cheatography.com/public/uploads/bennetsph_1697878720_queue-api.png}}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Queue Class}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{import java.util.Iterator; \newline import \seqsplit{java.util.NoSuchElementException;} \newline \newline public class Queue\textless{}Item\textgreater{} implements Iterable\textless{}Item\textgreater{} \{ \newline private int n; // number of elements on queue \newline private Node first; // beginning of queue \newline private Node last; // end of queue \newline \newline // helper linked list class \newline private class Node \{ \newline private Item item; \newline private Node next; \newline \} \newline \newline // Initializes an empty queue. \newline public Queue() \{ \newline first = null; \newline last = null; \newline n = 0; \newline \} \newline \newline // Returns true if this queue is empty. \newline public boolean isEmpty() \{ \newline return first == null; \newline \} \newline \newline // Returns the number of items in this queue. \newline public int length() \{ \newline return n; \newline \} \newline \newline // Returns the item least recently added to this queue. \newline public Item peek() \{ \newline if (isEmpty()) throw new \seqsplit{NoSuchElementException("Queue} underflow"); \newline return first.item; \newline \} \newline \newline // Add the item to the queue. \newline public void enqueue(Item item) \{ \newline Node oldlast = last; \newline last = new Node(); \newline last.item = item; \newline last.next = null; \newline if (isEmpty()) first = last; \newline else oldlast.next = last; \newline n++; \newline \} \newline \newline // Removes and returns the item on this queue that was least recently added. \newline public Item dequeue() \{ \newline if (isEmpty()) throw new \seqsplit{NoSuchElementException("Queue} underflow"); \newline Item item = first.item; \newline first = first.next; \newline n-{}-; \newline if (isEmpty()) last = null; // to avoid loitering \newline return item; \newline \} \newline \newline // Returns a string representation of this queue. \newline public String toString() \{ \newline StringBuilder s = new StringBuilder(); \newline for (Item item : this) \{ \newline s.append(item); \newline s.append(' '); \newline \} \newline return s.toString(); \newline \} \newline \newline /{\emph{ \newline method to remove all negative integers from a queue. \newline it checks the last element of the queue, if its not negative \newline element goes to the back of the queue, \newline }}/ \newline public void removeNegatives() \{ \newline \newline int sizeQ = this.size(); \newline \newline for(int i = 0; i \textless{} sizeQ; i++) \{ \newline \newline Item lastElement = this.dequeue(); \newline \newline if((int) lastElement \textgreater{}= 0) \{ \newline \newline // it's positive, thus put the item back of the queue \newline \seqsplit{this.enqueue(lastElement);} \newline \newline \} \newline \newline \} \newline \newline \} \newline \newline // Returns an iterator that iterates over the items in this queue in FIFO order. \newline public Iterator\textless{}Item\textgreater{} iterator() \{ \newline return new ListIterator(); \newline \} \newline \newline // an iterator, doesn't implement remove() since it's optional \newline private class ListIterator implements Iterator\textless{}Item\textgreater{} \{ \newline private Node current = first; \newline \newline public boolean hasNext() \{ return current != null; \} \newline public void remove() \{ throw new \seqsplit{UnsupportedOperationException();} \} \newline \newline public Item next() \{ \newline if (!hasNext()) throw new \seqsplit{NoSuchElementException();} \newline Item item = current.item; \newline current = current.next; \newline return item; \newline \} \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{ArrayList data structure Commands}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{ArrayList\textless{}Integer\textgreater{} list = new ArrayList\textless{}\textgreater{}(); \newline \newline // Add an element to the list \newline list.add(x); \newline \newline // Copy list \newline list.clone(); \newline \newline // Element at the index i \newline list.get(i); \newline \newline // Is element present in the list, returns T/F \newline list.contains(x); \newline \newline // Returns the index of the item x \newline list.indexOf(x); \newline \newline // Returns the size of list \newline list.size(); \newline \newline // Check if list is empty, return T/F \newline list.isEmpty(); \newline \newline // Replace an element x at index i with element y \newline list.set(i, y); \newline \newline // ArrayList into array: \newline \newline size of array is same as the ArrayList \newline int{[}{]} arr = new int{[}list.size(){]}; \newline \newline // Convert ArrayList into an array \newline list.toArray(arr); \newline \newline // Convert ArrayList into String, basically ouputs the list \newline list.toString();} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Advanced Tutorial Content}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{} \tn % Row Count 0 (+ 0) \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Call Center Exercise}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{import java.util.ArrayList; \newline \newline public class CallCenter\{ \newline \newline // Constants for the simulation \newline private final int numberOfEmployees = 10; \newline private final int numberOfTimePeriods = 480; \newline private final int maxNumberOfCustomers = 5; \newline private final int maxServiceTime = 5; \newline \newline // Variables to store simulation results \newline private int totalWaitTime = 0; \newline private int \seqsplit{totalNumberOfServedCustomers} = 0; \newline private int finalQueueLength = 0; \newline \newline public static void main (String{[}{]} args)\{ \newline \newline // Create an instance of the CallCenter class \newline CallCenter myCallCenter = new CallCenter(); \newline \newline // Run the call center simulation \newline \seqsplit{myCallCenter.runSimulation();} \newline \newline // Print the simulation statistics \newline \seqsplit{myCallCenter.printStatistics();} \newline \newline \} \newline \newline public void printStatistics()\{ \newline \newline // Printing the simulation statistics. \newline System.out.println("The call center simulation was run with " + numberOfEmployees + " employees."); \newline \seqsplit{System.out.println("Between} 9am and 5pm " + (finalQueueLength + \seqsplit{totalNumberOfServedCustomers)} + " customers have called"); \newline System.out.println("The average wait time was "+ (1.0{\emph{totalWaitTime / \seqsplit{totalNumberOfServedCustomers)} + " minutes."); \newline \seqsplit{System.out.println("Number} of unserved customers: " + finalQueueLength); \newline \newline \} \newline \newline // runs the simulation as described in the assignment \newline public void runSimulation()\{ \newline \newline // We begin with an empty queue \newline Queue\textless{}Customer\textgreater{} myQ = new Queue\textless{}\textgreater{}(); \newline \newline // List of Employees \newline ArrayList\textless{}Employee\textgreater{} listEmployees = \seqsplit{assignEmployee(numberOfEmployees);} \newline \newline // We go through the day: \newline for(int currentTime = 0; currentTime \textless{} numberOfTimePeriods; currentTime++) \{ \newline \newline // Each minute new customers "join the queue" \newline \seqsplit{randomArrivals(maxServiceTime}, maxNumberOfCustomers, currentTime, myQ); \newline \newline // For each employee we assign a new customer if possible: \newline for(Employee employee : listEmployees) \{ \newline \newline // There are customers in queue and employee is available: \newline \seqsplit{if(employee.getAvailableAt()} \textless{}= currentTime \&\& !myQ.isEmpty()) \{ \newline \newline Customer servedCustomer = myQ.dequeue(); \newline \newline // Adding how long customer waited to the total \& recording him as served one. \newline totalWaitTime += (currentTime - \seqsplit{servedCustomer.getArrivalTime());} \newline \seqsplit{totalNumberOfServedCustomers++;} \newline \newline int timeRequired = \seqsplit{servedCustomer.getServiceTime();} \newline \newline // Change employees available time \newline \seqsplit{employee.setAvailableAt(currentTime} + timeRequired); \newline \newline \} \newline \newline \} \newline \newline // After 8 hours, number of people not served: \newline finalQueueLength = myQ.size(); \newline \newline \} \newline \newline \newline \} \newline \newline // helper method to fill employee list \newline public ArrayList\textless{}Employee\textgreater{} assignEmployee(int n) \{ \newline \newline ArrayList\textless{}Employee\textgreater{} list = new ArrayList\textless{}\textgreater{}(); \newline \newline for(int i = 0; i \textless{} n; i++) \{ \newline \newline // Create Employee objects and add them to the list \newline Employee emp = new Employee(i); \newline list.add(emp); \newline \newline \} \newline \newline return list; \newline \newline \} \newline \newline // helper method for simulating callers arriving to the queue: \newline public void randomArrivals(int maxServiceTime, int maxNumberOfCustomers, int timeOfArrival, Queue myQ) \{ \newline \newline int numberOfArrivals = (int) (Math.random()}}maxNumberOfCustomers + 1); \newline \newline for(int i = 0; i \textless{} numberOfArrivals; i++)\{ \newline \newline // each customer will have different service time \newline int serviceTime = (int) \seqsplit{(Math.random()*maxServiceTime} + 1); \newline \newline Customer customer = new Customer(timeOfArrival, serviceTime); \newline \newline // add customer to the end of the queue \newline myQ.enqueue(customer); \newline \newline \} \newline \newline \} \newline \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Employee Class For Call Center}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class Employee\{ \newline \newline private final int id; \newline private int availableAt; \newline \newline // constructs an Employee with an id and initial available time at 0 \newline public Employee (int id)\{ \newline this.id = id; \newline this.availableAt = 0; \newline \} \newline \newline // returns the id of this employee \newline public int getId()\{ \newline return this.id; \newline \} \newline \newline // returns the time at which an employee is available again \newline public int getAvailableAt()\{ \newline return this.availableAt; \newline \} \newline \newline // sets the time at which an employee is available again \newline public void setAvailableAt(int time)\{ \newline this.availableAt = time; \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Customer Class For Call Center}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class Customer\{ \newline \newline private final int arrivalTime; \newline private final int serviceTime; \newline \newline // constructs a Customer with arrival time a and duration d \newline public Customer (int arrivalTime, int serviceTime)\{ \newline this.arrivalTime = arrivalTime; \newline this.serviceTime = serviceTime; \newline \} \newline \newline // returns the arrival time of this customer \newline public int getArrivalTime()\{ \newline return this.arrivalTime; \newline \} \newline \newline // returns the service time of this customer \newline public int getServiceTime()\{ \newline return this.serviceTime; \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Elevator Problem}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// method to solve the number of elevators problem: \newline public static int numberOfElevators(ArrayList\textless{}Person\textgreater{} personList) \{ \newline \newline ArrayList\textless{}Integer\textgreater{} elevators = new ArrayList\textless{}\textgreater{}(); \newline \newline // Initial "bin" is empty: \newline elevators.add(0); \newline int elevatorCount = 1; \newline \newline for(Person person:personList) \{ \newline \newline // flag will tell whether the person fitted in the current elevator: \newline boolean flag = false; \newline int weight = person.getWeight(); \newline \newline \newline // Check if person fits in one of the existing bins (elevators): \newline for (int i = 0; i \textless{} elevatorCount; i++) \{ \newline \newline int currentElevatorWeight = elevators.get(i); \newline \newline // Person fits in the elevator \newline \seqsplit{if((currentElevatorWeight} + weight) \textless{}= 450) \{ \newline \newline elevators.set(i, weight + currentElevatorWeight); \newline flag = true; \newline \newline \} \newline \newline \} \newline \newline // Other elevators didn't have space, we open new "bin": \newline if(!flag) \{ \newline \newline elevators.add(weight); \newline elevatorCount++; \newline \newline \} \newline \newline \} \newline \newline return elevatorCount; \newline \newline \} \newline \newline \newline // helper method to sort the list descending or ascending by weight. char d for descending and a for ascending. \newline public static void insertionSortWeight(ArrayList\textless{}Person\textgreater{} personList, char order)\{ \newline \newline int n = personList.size(); \newline \newline for (int i = 1; i \textless{} n; i++) \{ \newline \newline Person key = personList.get(i); \newline int j = i - 1; \newline \newline // Compare weights for sorting \newline if (order == 'a' || order == 'A') \{ // Ascending order \newline \newline while (j \textgreater{}= 0 \&\& \seqsplit{personList.get(j).getWeight()} \textgreater{} key.getWeight()) \{ \newline \newline personList.set(j + 1, personList.get(j)); \newline j-{}-; \newline \newline \} \newline \newline \} else if (order == 'd' || order == 'D') \{ // Descending order \newline \newline while (j \textgreater{}= 0 \&\& \seqsplit{personList.get(j).getWeight()} \textless{} key.getWeight()) \{ \newline \newline personList.set(j + 1, personList.get(j)); \newline j-{}-; \newline \newline \} \newline \newline \} \newline \newline personList.set(j + 1, key); \newline \} \newline \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Game Of Stacks}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public int runGameStrategy1()\{ \newline \newline // The first step of the game is to create \newline the needed random stack \newline IntegerStack gameStack = \seqsplit{IntegerStack.createRandomStack();} \newline \newline \newline // From here we go to the procedures of the game itself \newline // generate two player scores and ID's \newline int aliceID = 1; \newline int bobID = -1; \newline \newline int aliceScore = 0; \newline int bobScore = 0; \newline \newline \newline // Move tracker that checks whos turn it is \newline int move = 0; \newline \newline // The game starts \newline while (!gameStack.isEmpty())\{ \newline \newline // First we check whos turn it is since both \newline players only take 1 turn every time due to the strategy \newline if (move \% 2 == 0) \{ \newline // Alice starts and ONLY keeps every 1 \newline if (gameStack.pop() == aliceID)\{ \newline aliceScore += 1; \newline \} \newline \} else \{ \newline // Bob starts and ONLY keeps every -1 \newline if (gameStack.pop() == bobID)\{ \newline bobScore += 1; \newline \} \newline \} \newline move += 1; \newline \} \newline \newline if (aliceScore \textgreater{} bobScore) \{ \newline return 1; \newline \} else if (aliceScore == bobScore) \{ \newline return 0; \newline \} else \{ \newline return -1; \newline \} \newline \} \newline \newline \newline public int runGameStrategy2() \{ \newline // The first step of the game is to create \newline the needed random stack \newline IntegerStack gameStack = \seqsplit{IntegerStack.createRandomStack();} \newline \newline int aliceScore = 0; \newline int bobScore = 0; \newline \newline // Move tracker that checks whos turn it is \newline int currentPlayer = 0; \newline \newline // The game starts \newline while (!gameStack.isEmpty()) \{ \newline int top = gameStack.pop(); \newline \newline if( currentPlayer\%2 == 0 ) \{ \newline \newline if (top == 1) \{ \newline int consecOnes = 1; \newline if(gameStack.isEmpty()) break; \newline \newline int next = gameStack.pop(); \newline if (next == 1) \{ \newline consecOnes++; \newline \} else consecOnes = 0; \newline \newline aliceScore += consecOnes; \newline \} \newline \newline \} else if (top == -1) \{ \newline bobScore += 1; \newline \} \newline \newline currentPlayer++; \newline \} \newline \newline if (aliceScore \textgreater{} bobScore) \{ \newline return 1; \newline \} else if (aliceScore == bobScore) \{ \newline return 0; \newline \} else \{ \newline return -1; \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{Average BMI method}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{// Method that calculates average BMI index per age group: \newline public static void averageBMI(ArrayList\textless{}Person\textgreater{} personList) \{ \newline \newline int{[}{]} ageGroup = \{20,30,40,50,60,70\}; \newline \newline // 20-29 group corresponds to index 0, 30-39 to 1 and so on \newline int{[}{]} groupCount = new int{[}ageGroup.length{]}; \newline double{[}{]} groupSumBMI = new double{[}ageGroup.length{]}; \newline \newline for(int i = 0; i \textless{} personList.size(); i++) \{ \newline \newline Person currentPerson = personList.get(i); \newline \newline // age/10 - 2, will give correct index(group number) of the object for its place in the arrays. \newline int personsGroup = \seqsplit{currentPerson.getAge()/10} - 2; \newline groupCount{[}personsGroup{]}++; \newline \newline groupSumBMI{[}personsGroup{]} += currentPerson.getBMI(); \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}