\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{NoxLupus (NoxLupus)} \pdfinfo{ /Title (java-code-exsamples.pdf) /Creator (Cheatography) /Author (NoxLupus (NoxLupus)) /Subject (Java code exsamples 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 code exsamples Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{NoxLupus (NoxLupus)} via \textcolor{DarkBackground}{\uline{cheatography.com/46432/cs/14884/}}} \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}NoxLupus (NoxLupus) \\ \uline{cheatography.com/noxlupus} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 20th April, 2018.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{tabularx}{17.67cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{17.67cm}}{\bf\textcolor{white}{simple}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class FirstJavaProgram \{ \newline public static void main(String{[}{]} args)\{ \newline System.out.println("This is my first program in java"); \newline \}//End of main \newline \}//End of FirstJavaProgram Class} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{This is my first program in java} \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}{Operators}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class ArithmeticOperatorDemo \{ \newline public static void main(String args{[}{]}) \{ \newline int num1 = 100; \newline int num2 = 20; \newline \newline System.out.println("num1 + num2: " + (num1 + num2) ); \newline System.out.println("num1 - num2: " + (num1 - num2) ); \newline System.out.println("num1 {\emph{ num2: " + (num1 }} num2) ); \newline System.out.println("num1 / num2: " + (num1 / num2) ); \newline System.out.println("num1 \% num2: " + (num1 \% num2) ); \newline \newline System.out.println("5.0 \% 10.0: " + (5.0 \% 10.0) ); \newline System.out.println("num1 + num2: " + num1 + num2 ); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{num1 + num2: 120 \newline num1 - num2: 80 \newline num1 {\emph{ num2: 2000 \newline num1 / num2: 5 \newline num1 \% num2: 0 \newline \newline 5.0 \% 10.0: 0.0~~~ }} the output is 0.5 so it gose to 0.0 even thoe it is a double* \newline num1 + num2: 10020} \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}{Abstract}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//abstract class \newline abstract class Sum\{ \newline // Two {\bf{abstract methods}} \newline public abstract int sumOfTwo(int n1, int n2); \newline public abstract int sumOfThree(int n1, int n2, int n3); \newline \newline //{\bf{Regular method }} \newline public void disp()\{ \newline System.out.println("Method of class Sum"); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\emph{This code needs to be over rid by its child. it is a way to force the subclass to make certain methods.}}} \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}{super}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//Parent class or Superclass or base class \newline class Superclass \newline \{ \newline int num = 100; \newline \} \newline //Child class or subclass or derived class \newline class Subclass extends Superclass \newline \{ \newline {\emph{ //The same variable num is declared in the Subclass}} \newline \newline int num = 110; \newline void printNumber()\{ \newline System.out.println(num); \newline \seqsplit{System.out.println(super.num);} \newline \} \newline public static void main(String args{[}{]})\{ \newline Subclass obj= new Subclass(); \newline obj.printNumber(); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{110 \newline 100} \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}{Inheritance}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\bf{Class X}} \newline \{ \newline public void methodX() \newline \{ \newline \seqsplit{System.out.println("Class} X method"); \newline \} \newline \} \newline \newline {\bf{Class Y extends X}} \newline \{ \newline public void methodY() \newline \{ \newline System.out.println("class Y method"); \newline \} \newline \} \newline \newline {\bf{Class Z extends Y}} \newline \{ \newline public void methodZ() \newline \{ \newline \seqsplit{System.out.println("class} Z method"); \newline \} \newline public static void main(String args{[}{]}) \newline \{ \newline Z obj = new Z(); \newline obj.methodX(); {\emph{//calling grand parent class method}} \newline obj.methodY(); {\emph{//calling parent class method}} \newline obj.methodZ(); {\emph{//calling local method}} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class X method \newline class Y method \newline class Z method} \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}{if else}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class IfStatementExample \{ \newline public static void main(String args{[}{]})\{ \newline int num=70; \newline if( num \textless{} 100 ) \{ \newline \seqsplit{System.out.println("number} is less than 100"); \newline \}else \{ \newline \seqsplit{System.out.println("number} is more less than 100"); \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}{while}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class Test \{ \newline \newline public static void main(String args{[}{]}) \{ \newline int x = 10; \newline \newline while( x \textless{} 20 ) \{ \newline System.out.print("value of x : " + x ); \newline x++; \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{value of x : 10 \newline value of x : 11 \newline value of x : 12 \newline value of x : 13 \newline value of x : 14 \newline value of x : 15 \newline value of x : 16 \newline value of x : 17 \newline value of x : 18 \newline value of x : 19} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class Test \{ \newline \newline public static void main(String args{[}{]}) \{ \newline int x = 20; \newline \newline do \{ \newline System.out.print("value of x : " + x ); \newline x++; \newline System.out.print("\textbackslash{}n"); \newline \}while( x \textless{} 20 ); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{value of x : 20} \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}{for}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class ForLoopExample \{ \newline public static void main(String args{[}{]})\{ \newline for(int i=10; i\textgreater{}1; i-{}-)\{ \newline System.out.println("The value of i is: "+i); \newline \} \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{The value of i is: 10 \newline The value of i is: 9 \newline The value of i is: 8 \newline The value of i is: 7 \newline The value of i is: 6 \newline The value of i is: 5 \newline The value of i is: 4 \newline The value of i is: 3 \newline The value of i is: 2} \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}{swich}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class Test \{ \newline \newline public static void main(String args{[}{]}) \{ \newline // char grade = args{[}0{]}.charAt(0); \newline char grade = 'C'; \newline \newline switch(grade) \{ \newline case 'A' : \newline \seqsplit{System.out.println("Excellent}!"); \newline break; \newline case 'B' : \newline case 'C' : \newline System.out.println("Well done"); \newline break; \newline case 'D' : \newline System.out.println("You passed"); \newline case 'F' : \newline \seqsplit{System.out.println("Better} try again"); \newline break; \newline default : \newline \seqsplit{System.out.println("Invalid} grade"); \newline \} \newline System.out.println("Your grade is " + grade); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Well done \newline Your grade is C} \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}{Arrays}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class TestArray \{ \newline \newline public static void main(String{[}{]} args) \{ \newline double{[}{]} myList = \{1.9, 2.9, 3.4, 3.5\}; \newline \newline // Print all the array elements \newline for (int i = 0; i \textless{} myList.length; i++) \{ \newline System.out.println(myList{[}i{]} + " "); \newline \} \newline \newline // Summing all elements \newline double total = 0; \newline for (int i = 0; i \textless{} myList.length; i++) \{ \newline total += myList{[}i{]}; \newline \} \newline \seqsplit{System.out.println("Total} is " + total); \newline \newline // Finding the largest element \newline double max = myList{[}0{]}; \newline for (int i = 1; i \textless{} myList.length; i++) \{ \newline if (myList{[}i{]} \textgreater{} max) max = myList{[}i{]}; \newline \} \newline System.out.println("Max is " + max); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{1.9 \newline 2.9 \newline 3.4 \newline 3.5 \newline Total is 11.7 \newline Max is 3.5} \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}{quizes}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Condition 1: (x \textless{} y \&\& x \textgreater{} 0) false \newline Condition 2: (a != d || x != 5) true \newline Condition 3: !(true \&\& false) true \newline Condition 4: (x \textgreater{} y || a == 'A' || d != 'A') true \newline for x = 5, y = 3, and a and d are char variables with a = 'a' and d = 'A' \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline if (score \textgreater{}= 90) \newline grade = 'A'; \newline if (score \textgreater{}= 80) \newline grade = 'B'; \newline if (score \textgreater{}= 70) \newline grade = 'C'; \newline if (score \textgreater{}= 60) \newline grade = 'D'; \newline else \newline grade = 'F'; \newline //will work correctly only if score \textless{} 70, because it dosent brake. a A is also a C,D \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline if (x \textgreater{} 0) \newline x++; \newline else \newline if (x \textless{} 0) \newline x-{}-; \newline //adds one if x is pos and subs on if x is neg \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline if (count != 0 \&\& total / count \textgreater{} max) \newline max = total / count; \newline //count = 0, The condition short circuits and the assignment statement is not executed nor dose it get to divitoin by 0 \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline if (x \textless{} 0) \newline y = x; \newline else \newline y = 0; \newline // or \newline y = (x \textless{} 0) ? x : 0; \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline //Start: X = 1 end: x = 128, but if x = 0 it is an infinite lop \newline while (x \textless{} 100) \newline x *= 2; \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline //will execute 10 times: \newline int x = 10; \newline while (x \textgreater{} 0) \newline \{ \newline System.out.println(x); \newline x-{}-; \newline \} \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline //end: x = 10: 1+2+3+4 \newline for (int i=0; i\textless{}5; i++) \newline x += i; \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline int x = 10; \newline do \{ \newline System.out.println(x); \newline x-{}-; \newline \} while (x \textgreater{} 0); \newline //executes 1 time \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline for (int j = 0; j \textless{} 1000; ) X++; \newline //is a infinite loop \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline for (int j = s.length( ); j \textgreater{} 0; j-{}-) \newline \seqsplit{System.out.print(s.charAt(j-1));} \newline //it prints s out backwards with the last character \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline int{[} {]} arr = new int{[}5{]}; \newline arr.length = 5 \newline int{[} {]} list = \{5, 10, 15, 20, 6\}; \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline BankAccount{[} {]} firstEmpireBank; \newline firstEmpireBank = new BankAccount{[}1000{]}; \newline //will creat 1,000 reference variables, each of which could point to a BankAccount object \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}{variable}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{int numberOfDays; \newline byte nextInStream; \newline short hour; \newline long totalNumberOfStars; \newline float reactionTime; \newline double itemPrice;} \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}{variable, Method and Class cunsructers}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class XYZ\{ \newline final void demo()\{ \newline System.out.println("XYZ HI"); \newline \} \newline \} \newline \newline class ABC extends XYZ\{ \newline void demo()\{ \newline System.out.println("ABC HI"); \newline \} {\emph{ /* this method can not be crreated since the parnts method of the sasm name is final. howver we can just let it get inherited down.*/ }} \newline \newline public static void main(String args{[}{]})\{ \newline ABC obj= new ABC(); \newline obj.demo(); \newline \} \newline \} \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline package abcpackage; \newline \newline public class Addition \{ \newline \newline public int addTwoNumbers(int a, int b)\{ \newline return a+b; \newline \} \newline \} \newline \newline package xyzpackage; \newline import abcpackage.*; \newline class Test\{ \newline public static void main(String args{[}{]})\{ \newline Addition obj = new Addition(); \newline \seqsplit{System.out.println(obj.addTwoNumbers(100}, 1)); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{XYZ HI \newline -{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}- \newline 101} \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}{garbage collection}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{BeginnersBook obj1 = new BeginnersBook(); \newline BeginnersBook obj2 = new BeginnersBook(); \newline obj2 = obj1; \newline \newline char{[}{]} sayhello = \{ 'h', 'e', 'l', 'l', 'o'\}; \newline String str = new String(sayhello); \newline str = null;} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{str and obj2 {\emph{are now available for garbage collection.}} \newline {\emph{which means the instance (object) pointed by (referenced by) obj2 is not reachable }}} \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}{Encapsulation in Java}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class EncapsulationDemo\{ \newline private String empName; \newline public String getEmpName()\{ \newline return empName; \newline \} \newline public void setEmpName(String newValue)\{ \newline empName = newValue; \newline \} \newline \} \newline \newline public class EncapsTest\{ {\emph{// this class can not see empName}} \newline public static void main(String args{[}{]})\{ \newline EncapsulationDemo obj = new EncapsulationDemo(); \newline obj.setEmpName("Mario"); \newline \seqsplit{System.out.println("Employee} Name: " + obj.getEmpName()); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Employee Name: Mario \newline {\emph{1) Make the instance variables private so that they cannot be accessed directly}} {\emph{from outside the class. You can only set and get values of these variables through the methods of the class.}} \newline {\emph{2) Have getter and setter methods in the class to set and get the values of the}} {\emph{fields.}}} \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}{DecimalFormat}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{DecimalFormat \newline void applyPattern(String pattern) \newline String format(Double number)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{creates formatting object \newline applies a pattern to DecimalFormatobj \newline returns a string containing the number format according to current pattern} \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}{tax}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{//{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}} \newline // Purchase.java Author: Lewis/Loftus \newline // \newline // Demonstrates the use of the NumberFormat class to format output. \newline //}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}{\bf{}}}}{\bf{{\emph{}}}} \newline \newline import java.util.Scanner; \newline import java.text.NumberFormat; \newline \newline public class Purchase \newline \{ \newline //-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline // Calculates the final price of a purchased item using values \newline // entered by the user. \newline //-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-{}-- \newline public static void main (String{[}{]} args) \newline \{ \newline final double TAX\_RATE = 0.06; // 6\% sales tax \newline \newline int quantity; \newline double subtotal, tax, totalCost, unitPrice; \newline \newline Scanner scan = new Scanner (System.in); \newline \newline NumberFormat fmt1 = \seqsplit{NumberFormat.getCurrencyInstance();} \newline NumberFormat fmt2 = \seqsplit{NumberFormat.getPercentInstance();} \newline \newline System.out.print ("Enter the quantity: "); \newline quantity = scan.nextInt(); \newline \newline System.out.print ("Enter the unit price: "); \newline unitPrice = scan.nextDouble(); \newline \newline subtotal = quantity {\emph{ unitPrice; \newline tax = subtotal }} TAX\_RATE; \newline totalCost = subtotal + tax; \newline \newline // Print output with appropriate formatting \newline System.out.println ("Total: " + fmt1.format(totalCost)); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{enter quantity: 5 \newline total: \%20.51} \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}{ramdom}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{random() \newline float nextFloat() \newline int nextInt() \newline int nextInt(int num)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{instagts random object \newline ranNum{[}0.0,0.1) \newline ranNum(all = or - n whole\#) \newline ranNum(0,num-1)} \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}{enum}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public enum Directions\{ \newline EAST, \newline WEST, \newline NORTH, \newline SOUTH \newline \} \newline \newline public static void main(String{[}{]} args) \{ \newline EnumDemo obj1 = new \seqsplit{EnumDemo(Directions.EAST);} \newline obj1.getMyDirection(); \newline Directions.charAt(2) \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{{\emph{the getMyDirection command is made to output the direction of the object}} \newline EAST \newline NORTH} \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}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{public class Example\{ \newline public static void main(String args{[}{]})\{ \newline \newline //creating a string by{\bf{ java string literal}} \newline String str = "Beginnersbook"; \newline char arrch{[}{]}=\{'h','e','l','l','o'\}; \newline \newline //converting {\bf{char array arrch{[}{]} to string}} str2 \newline String str2 = new String(arrch); \newline \newline //Displaying all the three strings \newline System.out.println(str); \newline System.out.println(str2); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{Beginnersbook \newline hello} \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}{constructor}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class Example2 \newline \{ \newline private int var; \newline //default constructor \newline public Example2() \newline \{ \newline this.var = 10; \newline \} \newline //parameterized constructor \newline public Example2(int num) \newline \{ \newline this.var = num; \newline \} \newline \newline public static void main(String args{[}{]}) \newline \{ \newline Example2 obj = new Example2(); \newline Example2 obj2 = new Example2(100); \newline System.out.println("var is: "+obj.getValue()); \newline System.out.println("var is: "+obj2.getValue()); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{var is: 10 \newline var is: 100} \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}{Static}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{class JavaExample\{ \newline static int i = 100; \newline static String s = "Beginnersbook"; \newline //Static method \newline static void display() \newline \{ \newline \seqsplit{System.out.println("i:"+i);} \newline \seqsplit{System.out.println("i:"+s);} \newline \} \newline \newline //non-static method \newline void funcn() \newline \{ \newline //Static method called in non-static method \newline display(); \newline \} \newline //static method \newline public static void main(String args{[}{]}) \newline \{ \newline JavaExample obj = new JavaExample(); \newline //You need to have object to call this non-static method \newline obj.funcn(); \newline \newline //Static method called in another static method \newline display(); \newline \} \newline \}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{i:100 \newline i:Beginnersbook \newline i:100 \newline i:Beginnersbook} \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}{intager}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{integer(int value) \newline prim typeValue() \newline // dyte byteValue() retuns number in primative data \newline static int parseInt(String str) \newline staic String toBinary\{hex, Octal\}String(int num)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{~~~~creates new integer obj storing the value \newline ~~~~returns number in primitive data \newline ~~~~returns an int with value in the sting \newline ~~~~returns string with the specified integer value in the corresponding base} \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}{format}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{String format(double number) \newline static NumberFormat getCurrencyInstamce() \newline static NumberFormat getPercenctInstamce() \newline \newline //creates obj \newline // then do obj.format(Foo)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{17.67cm}}{returns string with specified \#format \newline returns object that represents current currency standers \newline returns object that represents current percentages standers} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \end{document}