\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{phon} \pdfinfo{ /Title (java-basic-intro.pdf) /Creator (Cheatography) /Author (phon) /Subject (Java basic intro 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}{000000} \definecolor{LightBackground}{HTML}{F7F7F7} \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 basic intro Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{phon} via \textcolor{DarkBackground}{\uline{cheatography.com/25871/cs/8971/}}} \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}phon \\ \uline{cheatography.com/phon} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 5th September, 2016.\\ Updated 12th March, 2019.\\ 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Swap}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{public static void swap(int{[}{]} list, int e1, int e2) \{ \newline % Row Count 2 (+ 2) int{[}{]} mylist = \{1, 2, 3, 4, 5\}; \newline % Row Count 3 (+ 1) int{[}{]} mylist2 = new int{[}9{]}; \newline % Row Count 4 (+ 1) int temp; \newline % Row Count 5 (+ 1) temp = mylist{[}e1{]}; \newline % Row Count 6 (+ 1) mylist2{[}e2{]} = mylist{[}e1{]}; \newline % Row Count 7 (+ 1) mylist2{[}e2{]} = temp; \newline % Row Count 8 (+ 1) for (int i : mylist)\{ \newline % Row Count 9 (+ 1) System.out.println(i); \newline % Row Count 10 (+ 1) \}% Row Count 11 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lab04 MyDate}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{public class MyDate \{ \newline private int year; \newline private int month; \newline private int day; \newline private int objectNumber; \newline static int objectCounter; \newline static String{[}{]} strMonths = \{ "January", "February", \newline "March", "April", "May", "June", "July", "August", "September", "October", "November", \newline "December"\}; \newline MyDate()\{ \newline this.setDate(1900,1,1); \newline objectCounter++; \newline objectNumber = objectCounter; \newline \} \newline MyDate(int aYear,int aMonth,int aDay)\{ \newline this.setDate(aYear, aMonth, aDay); \newline objectCounter++; \newline objectNumber = objectCounter; \newline \} \newline \newline public int getObjectNumber() \{ \newline return objectNumber; \newline \} \newline public void setDate(int aYear,int aMonth,int aDay) \{ \newline this.setYear(aYear); \newline this.setMonth(aMonth); \newline this.setDay(aDay); \newline \} \newline public void setYear(int aYear) \{ \newline year = aYear; \newline \} \newline public void setMonth(int aMonth) \{ \newline month = aMonth; \newline \} \newline public void setDay(int aDay) \{ \newline day = aDay; \newline \} \newline public int getYear() \{ \newline return year; \newline \} \newline public int getMonth() \{ \newline return month; \newline \} \newline public int getDay() \{ \newline return day; \newline \} \newline public static int yearDiff (MyDate start, MyDate end) \{ \newline int amonth = start.month; \newline int ayear = start.year; \newline int counter = 0; \newline int result = 0; \newline if (end.year \textgreater{}= start.year) \{ \newline boolean process = true; \newline while(process == true) \{ \newline if(amonth == 12) \{ \newline amonth = 1; \newline ayear++; \newline counter++; \newline \} \newline else if (amonth == end.month \&\& ayear == end.year) \{ \newline process = false; \newline \} \newline else \{ \newline amonth++; \newline counter++; \newline \} \newline \} \newline if (start.day \textgreater{} end.day) \{ \newline counter-{}-; \newline \} \newline result = counter / 12; \newline \} \newline else \{ \newline result = -1; \newline \} \newline return result; \newline \} \newline \newline @Override \newline public String toString() \{ \newline return day +" "+ strMonths{[}month - 1{]} +" "+ year; \newline \} \newline \newline MyDate nextDay() \{ \newline if (this.getMonth() == 12) \{ \newline this.setDate(this.getYear() + 1,1,1); \newline \} \newline else \{ \newline if(this.getMonth() == 4 || this.getMonth() == 6 || this.getMonth() == 9 || this.getMonth() == 11) \{ \newline if(this.getDay() == 30) \{ \newline this.setMonth(this.getMonth() + 1); \newline this.setDay(1); \newline \} \newline else \{ \newline this.setDay(this.getDay() + 1); \newline \} \newline \} \newline else if(this.getMonth() != 2)\{ \newline if(this.getDay() == 31) \{ \newline this.setDay(1); \newline this.setMonth(this.getMonth() + 1); \newline \} \newline else \{ \newline this.setDay(this.getDay() + 1); \newline \} \newline \} \newline else \{ \newline if \seqsplit{(isLeapYear(this.getYear())} == true \&\& this.getDay() == 29) \{ \newline this.setMonth(this.getMonth() + 1); \newline this.setDay(1); \newline \} \newline else if \seqsplit{(isLeapYear(this.getYear())} == false \&\& this.getDay() == 28) \{ \newline this.setMonth(this.getMonth() + 1); \newline this.setDay(1); \newline \} \newline else \{ \newline this.setDay(this.getDay() + 1); \newline \} \newline \} \newline \} \newline return this; \newline \} \newline \newline MyDate nextMonth()\{ \newline if (this.getMonth() == 12) \{ \newline this.setMonth(1); \newline this.setYear(this.getYear() + 1); \newline \} \newline else if (this.getMonth() == 3 || this.getMonth() == 5 || this.getMonth() == 8 || this.getMonth() == 10) \{ \newline if(this.getDay() == 31) \{ \newline this.setDate(this.getYear(), this.getMonth() + 1, 30); \newline \} \newline else \{ \newline this.setMonth(this.getMonth() + 1); \newline \} \newline \} \newline else if (this.getMonth() == 1)\{ \newline if \seqsplit{(isLeapYear(this.getYear())} == true \&\& this.getDay() \textgreater{}= 29) \{ \newline this.setDate(this.getYear(),this.getMonth() + 1, 29); \newline \} \newline else \{ \newline this.setDate(this.getYear(),this.getMonth() + 1, 28); \newline \} \newline \} \newline else\{ \newline this.setMonth(this.getMonth() + 1); \newline \} \newline return this; \newline \} \newline \newline MyDate nextYear() \{ \newline if \seqsplit{(isLeapYear(this.getYear())} == true \&\& this.getDay() == 29 \&\& this.getMonth() == 2) \{ \newline this.setDate(this.getYear() + 1, 2 , 28); \newline \} \newline else \{ \newline this.setYear(this.getYear() + 1); \newline \} \newline return this; \newline \} \newline MyDate previousDay() \{ \newline if (this.getMonth() == 1 \&\& this.getDay() == 1) \{ \newline this.setYear(this.getYear() - 1); \newline this.setMonth(12); \newline this.setDay(31); \newline \} \newline else \{ \newline if(this.getMonth() == 5 || this.getMonth() == 7 || this.getMonth() == 10 || this.getMonth() == 12) \{ \newline if (this.getDay() == 1) \{ \newline this.setMonth(this.getMonth() - 1); \newline this.setDay(30); \newline \} \newline else \{ \newline this.setDay(this.getDay() - 1); \newline \} \newline \} \newline else if (this.getMonth() != 3) \{ \newline if (this.getDay() == 1) \{ \newline this.setDate(this.getYear(), this.getMonth() - 1, 31); \newline \} \newline else \{ \newline this.setDay(this.getDay() - 1); \newline \} \newline \} \newline else \{ \newline if(isLeapYear(this.getYear()) == true \&\& this.getDay() == 1)\{ \newline this.setDate(this.getYear(),this.getMonth() - 1, 29); \newline \} \newline else if (this.getDay() == 1) \{ \newline this.setDate(this.getYear(), this.getMonth() - 1, 28); \newline \} \newline else \{ \newline this.setDay(this.getDay() - 1); \newline \} \newline \} \newline \} \newline return this; \newline \} \newline \newline MyDate previousMonth() \{ \newline if (this.getMonth() == 1) \{ \newline this.setMonth(12); \newline this.setYear(this.getYear() - 1); \newline \} \newline else if (this.getMonth() == 5 || this.getMonth() == 7 || this.getMonth() == 10 || this.getMonth() == 12) \{ \newline if(this.getDay() == 31) \{ \newline this.setDate(this.getYear(), this.getMonth() - 1, 30); \newline \} \newline else \{ \newline this.setMonth(this.getMonth() - 1); \newline \} \newline \} \newline else if (this.getMonth() == 3)\{ \newline if \seqsplit{(isLeapYear(this.getYear())} == true \&\& this.getDay() \textgreater{}= 29) \{ \newline this.setDate(this.getYear(),this.getMonth() - 1, 29); \newline \} \newline else \{ \newline this.setDate(this.getYear(),this.getMonth() - 1, 28); \newline \} \newline \} \newline else\{ \newline this.setMonth(this.getMonth() - 1); \newline \} \newline return this; \newline \} \newline \newline MyDate previousYear() \{ \newline if \seqsplit{(isLeapYear(this.getYear())} == true \&\& this.getDay() == 29 \&\& this.getMonth() == 2) \{ \newline this.setDate(this.getYear() - 1, 2 , 28); \newline \} \newline else \{ \newline this.setYear(this.getYear() -1); \newline \} \newline return this; \newline \} \newline static boolean isLeapYear(int Year) \{ \newline if (Year \% 4 != 0) \{ \newline return false; \newline \} \newline else if (Year \% 100 != 0) \{ \newline return true; \newline \} \newline else if (Year \% 400 != 0) \{ \newline return false; \newline \} \newline else \{ \newline return true; \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Range of Data Types}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{Byte = – 128 to 127 {[}-2\textasciicircum{}7 to 2\textasciicircum{}7 - 1{]} \newline % Row Count 1 (+ 1) Short = – 32,768 to 32,767 {[}-2\textasciicircum{}15 to 2\textasciicircum{}15 - 1{]} \newline % Row Count 3 (+ 2) Int = –2,147,483,648 to 2,147,483,647 {[}-2\textasciicircum{}31 to 2\textasciicircum{}31 - 1{]} \newline % Row Count 5 (+ 2) Long = –9,223,372,036,854,775,808 to 9 ,223,372,036,854,775,807 \newline % Row Count 7 (+ 2) = {[}-2\textasciicircum{}63 to 2\textasciicircum{}63 -1{]}% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lab04 Person}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{import java.time.LocalDate; \newline \newline public class Person \{ \newline private String firstname; \newline private String lastname; \newline private MyDate birthday = new MyDate(); \newline \newline Person(String aFirstname, String aLastname)\{ \newline firstname = aFirstname; \newline lastname = aLastname; \newline \} \newline \newline Person(String aFirstname, String aLastname, int aYear, int aMonth, int aDay)\{ \newline firstname = aFirstname; \newline lastname = aLastname; \newline birthday.setDate(aYear, aMonth, aDay); \newline \} \newline public int getAge(MyDate aDate) \{ \newline int age = MyDate.yearDiff(birthday, aDate); \newline return age; \newline \} \newline public boolean isEligible(MyDate elecDate) \{ \newline if \seqsplit{(MyDate.yearDiff(this.birthday}, elecDate) \textgreater{}= 18) \{ \newline return true; \newline \} \newline else \{ \newline return false; \newline \} \newline \} \newline public void printPersonInfo() \{ \newline System.out.println("Person: "+firstname+" "+lastname); \newline System.out.println("Birthday: "+birthday.getDay()+" "+MyDate.strMonths{[}birthday.getMonth() - 1{]}+" "+birthday.getYear()); \newline \} \newline \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Lab04}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{import java.util.Scanner; \newline public class ElectionTester \{ \newline public static void main(String{[}{]} args) \{ \newline MyDate election = new MyDate(2019, 3, 24); \newline Person a = new Person("Lalisa", "Manoban", 1997, 3, 27); \newline printPersonElectionInfo(a, election); \newline Person b = new Person("Nuda", "Inter", 2012, 1, 16); \newline printPersonElectionInfo(b, election); \newline Person c = new Person("Hallo", "World",1998,2,1); \newline printPersonElectionInfo(c, election); // \newline boolean processing = true; \newline while(processing == true) \{ \newline String firstname; \newline String lastname; \newline int year; \newline int month; \newline int day; \newline Scanner in = new Scanner(System.in); \newline System.out.print("Enter firstname or type 'q' to exit:"); \newline String dataA = in.nextLine(); \newline if(dataA.equals("q") == true) \{ \newline processing = false; \newline break; \newline \} \newline else \{ \newline firstname = dataA; \newline Scanner in1 = new Scanner(System.in); \newline System.out.print("Enter lastname:"); \newline lastname = in1.nextLine(); \newline Scanner in3 = new Scanner(System.in); \newline System.out.print("Enter year of birthday: "); \newline year = in3.nextInt(); \newline Scanner in4 = new Scanner(System.in); \newline System.out.print("Enter month of birthday: "); \newline month = in4.nextInt(); \newline Scanner in5 = new Scanner(System.in); \newline System.out.print("Enter day of birthday: "); \newline day = in5.nextInt(); \newline Person data = new Person(firstname,lastname,year,month,day); \newline printPersonElectionInfo(data, election); \newline \} \newline \} \newline System.out.println("Exit the program. Thank you."); \newline \} \newline public static void \seqsplit{printPersonElectionInfo(Person} p, MyDate election) \{ \newline p.printPersonInfo(); \newline System.out.println("Age: " + p.getAge(election)); \newline if(p.isEligible(election)) \newline System.out.println("This person is eligible to vote."); \newline else \newline System.out.println("This person is NOT eligible to vote"); \newline \newline System.out.println("-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}--"); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{1.52 cm} p{0.76 cm} x{5.32 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{Code}} \tn % Row 0 \SetRowColor{LightBackground} // & = & / \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} /t & = & tap \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} /* & = & comment \tn % Row Count 3 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Loop}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{while loop}} \newline % Row Count 1 (+ 1) int x = 1; \newline % Row Count 2 (+ 1) while (x \textless{} 10) \{ \newline % Row Count 3 (+ 1) System.out.println(x); \newline % Row Count 4 (+ 1) x++; \newline % Row Count 5 (+ 1) {\bf{for loop}} \newline % Row Count 6 (+ 1) for (int a = 0; a \textless{} 10; a++)\{ \newline % Row Count 7 (+ 1) System.out.println(a); \newline % Row Count 8 (+ 1) {\bf{Enchanted for loop}} \newline % Row Count 9 (+ 1) int number{[}{]} = \{ 2, 3, 4, 5, 6, 7, 8, 9, 10, 11\}; \newline % Row Count 11 (+ 2) for (int a: number) \{ \newline % Row Count 12 (+ 1) System.out.println(a); \newline % Row Count 13 (+ 1) {\bf{while..do loop}} \newline % Row Count 14 (+ 1) int x = 1; \newline % Row Count 15 (+ 1) do\{ \newline % Row Count 16 (+ 1) System.out.println( x ); \newline % Row Count 17 (+ 1) x++; \newline % Row Count 18 (+ 1) \} \newline % Row Count 19 (+ 1) while( x \textless{} 10 );% Row Count 20 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}