FANDOM


Chapter 1 -- The Class

A Java Class is a compiled program (.java file) which has been converted to a different program type (.class file) that the JDK reads and runs. The most basic Java class is composed of the following: Java codes for beginners</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l1 level1 lfo2;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Public class <ClassName></p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l1 level1 lfo2;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args)</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none">public class FirstClass {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">as you can see, in the basic program above we have a class statement and a main method*. Furthermore, you may have noticed the curly brackets, when creating a class/method you must show where it starts and ends with the curly brackets ({ }) inside these brackets is where the code goes. As you can see, the main method is within the class brackets.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">[REMEMBER] -- The first letter for a class name should always be capatalised</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public class FirstClass { <-- class start</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) { <-- main start</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none"> } <-- main end</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 56.0pt 84.0pt 112.0pt 140.0pt 168.0pt 196.0pt 224.0pt 3.5in 280.0pt 308.0pt 336.0pt; mso-layout-grid-align:none;text-autospace:none">} <-- class end</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> The Main Method: the main method is used in every single program. This is where the compiler will start to read and perform the actions written in your program.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">method*: A Java method is a collection of statements that are grouped together to perform an operation. When you call the print method (chapter 3), for example the system actually executes several statements in order to display message on the console.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">A method is made like this:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none">modifier returnValueType methodName(list of parameters) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> // Method body;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:12.0pt; margin-left:.5in;text-indent:-.5in;mso-pagination:none;mso-list:l2 level1 lfo3; tab-stops:11.0pt .5in;mso-layout-grid-align:none;text-autospace:none"> Modifiers: The modifier, which is optional, tells the compiler how to call the method. This defines the access type of the method.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:12.0pt; margin-left:.5in;text-indent:-.5in;mso-pagination:none;mso-list:l2 level1 lfo3; tab-stops:11.0pt .5in;mso-layout-grid-align:none;text-autospace:none"> Return Type: A method may return a value. The returnValueType is the data type of the value the method returns. Some methods perform the desired operations without returning a value. In this case, the returnValueType is the keyword void.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:12.0pt; margin-left:.5in;text-indent:-.5in;mso-pagination:none;mso-list:l2 level1 lfo3; tab-stops:11.0pt .5in;mso-layout-grid-align:none;text-autospace:none"> Method Name: This is the actual name of the method. The method name and the parameter list together constitute the method signature.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:12.0pt; margin-left:.5in;text-indent:-.5in;mso-pagination:none;mso-list:l2 level1 lfo3; tab-stops:11.0pt .5in;mso-layout-grid-align:none;text-autospace:none"> Parameters: A parameter is like a placeholder. When a method is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a method. Parameters are optional; that is, a method may contain no parameters.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:12.0pt; margin-left:.5in;text-indent:-.5in;mso-pagination:none;mso-list:l2 level1 lfo3; tab-stops:11.0pt .5in;mso-layout-grid-align:none;text-autospace:none"> Method Body: The method body contains a collection of statements that define what the method does.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 2: Comments</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">A comment is a line in the code for the programmer to read and remind him/her about what the line of code is, what the programmer needs to do, etc. Comments are not read by the compiler thus, they can be anything.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">How to create a comment: There are 2 types of comments in Java, the single line comment and the multi-line comment. To create a single line comment you start with // and then put anything you want.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">A multi-line comment is a bit more complex than the single line comment. A multiline comment looks like this:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">/* start of comment</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">* all lines except for start and end</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">* should start like this</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">end of comment is like this */</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">public class FirstClass {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> //Single line comment!</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> /*This is the first line</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> *second line</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> *third line</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> end*/</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 3: Hello World!</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">The Hello World program is a generic program in all languages, it is a simple console-print out program that says "Hello World!". However, our Hello World program will be a bit more badass than that.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none">public class FirstClass {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("********************************\n");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("* *\n"); </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("* *\n"); </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("* Hello World! *\n"); </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("* *\n"); </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("* *\n");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.print("********************************\n");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("********************************");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("* *");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("* *");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("* Goodbye World *");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("* *");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("* *");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> System.out.println("********************************");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> } </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;mso-layout-grid-align: none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> As you can see I used two different types of print out statements:</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l3 level1 lfo4;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print -- This prints out whatever is in the quotations in a single line.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l3 level1 lfo4;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println -- This prints out whatever is in the quotations in a new line.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Furthermore, you may notice that after each line of code you can see a ';'. In Java, each line of code ends with ;. Also, in all the print lines you may see '\n' this is called a special sequence and will be covered in the next chapter.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 4: Java Special Keys</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> The Java language supports numerous special escape sequences for String* and char ** literals. They are as follows:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\n - new line</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\r - carriage return</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\f - Formfeed</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\b - backspace</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\s - space</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\t - tab</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\" - Double quote</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\' - single quote</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\\ - backslash (allows you to print out "\")</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\ddd - octal character</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">\uxxxx - Hexadecimal UNICODE character</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">String*: a string is a line of text like "Hello world"</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">char**: a char is a single character like 'a' or '1' or '-'. a char is specified with '<char>' instead of "<char>"</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 5: Primitive data types</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">In Java there are nine primitive data types. Primitive data types are predefined by the language and named by a key word. Let us now look into detail about the nine primitive data types:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> byte - a byte is a small whole number like 68, or 73. They byte has a minimum value of -128 and a maximum value of 127 (inclusive).</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> short - the short is a larger whole number than a byte with a minimum value of -32,768 and a maximum value of 32,767 (inclusive).</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int - the int is the most common type of whole number primitive with a minimum value of -2,157,483,648 and a maximum value of 2,147,483,647.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> long - the long is a data typed used for only extremely long numbers. With its capability of going from -9,223,272,036,854,775,808 all the way up to 9,223,372,036,854,775,807 the long is the largest whole number sequence in Java</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> float - a float is a data type that can hold non-whole number numbers (10.7, 149.12) and is commonly used instead of the double. The range of the float is beyond the scope of this discussion</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> double - the double is a non-whole number (10.7, 1349.432) with the range value beyond the scope of this discussion. This data type, other than the int, is usually the default choice for number sequences.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> boolean - the boolean data type has only two possible values: true or false. you use this data type for simple flags that track true/false condtions.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> char - the char is a single 16-bit unicode character.</p> <p class="MsoNormal" style="margin-top:0in;margin-right:-.5in;margin-bottom:0in; margin-left:.5in;margin-bottom:.0001pt;text-indent:-.5in;mso-pagination:none; mso-list:l4 level1 lfo5;tab-stops:11.0pt .5in 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> String - the string is a line of text</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 6: variables</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">A variable is a primitive data type used in code. some examples of variables are strings printed out to form text or integers added, subtracted, multiplied or divided and printed out.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">public class FirstClass {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> String Hello = "hello java";</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> char h = 'h';</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> char e = 'e';</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> char y = 'y';</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int A = 12;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int B = 2;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int Sum = A + B;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int Divide = A/B;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int Multiply = A*B;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int Subtract = A-B;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> double D = 123.456;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println(Hello);//will print out "hello java"</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(h);//will print out "h"</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(e);//will print out "e"</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(y);//will print out "y"</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print("\n");//will make a new line</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println(A + " " + B);//will print out 12 2</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Sum - " + Sum);//will print out the sum of A + B</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Divide - " + Divide);//will print out the answer of A / B</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Multiply - " + Multiply);//will print out the answer of A * B</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Subtract - " + Subtract);//will print out the answer of A - B</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println(D);//will print out 123.456</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 7: user input</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> In Java, user input is done using 'Scanners' A scanner is an object that takes anything the user inputs and sets it as a string (unless an int is specifically defined):</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">import java.util.Scanner;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">public class UserInput {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> String text;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print("Enter some text: ");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> text = scan.next();</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println(text);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Lets have a look at this code piece by piece:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">______</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> This creates a new Scanner object called 'scan' and uses the System's console (System) as the input device (.in).</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">______</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> text = scan.next();</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> This sets the previously created string named 'text' to the next string inputted by the scanner. Along with .next() which means next thing inputted, the scanner also has:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .nextLine() -- looks at the line of input after the current line</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .hasNextInt() -- reads for the next integer inputted</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .hasNextDouble() -- reads for the next double inputted</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .skip(String) -- skips any word put in the parenthesis</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Procedure 1: simple calculator</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">This is not a new chapter, Procedures will be small programs created using what we have learned in the earlier chapters. In this procedure, we will be using a simple calculator using user input and variables.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">import java.util.Scanner;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">public class UserInput {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> int a, b, answer;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print("Enter first number: ");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> a = scan.nextInt();//reads for next integer</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print("Enter second number: ");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> b = scan.nextInt();//reads for next integer</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> answer = a + b;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Answer = " + answer);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> answer = a - b;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Answer = " + answer);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> answer = a / b;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Answer = " + answer);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> answer = a * b;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Answer = " + answer);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> answer = a % b;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.println("Answer = " + answer);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">When this program is run the output will be the following:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Enter first number: 12</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Enter second number: 5</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Answer = 17</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Answer = 7</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Answer = 2</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Answer = 60</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Answer = 2</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">24.182.108.156!!! End Of Unit 1 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">24.182.108.156!!! Start Of Unit 2 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 8: IF Statements</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">An if statement is a conditional statement that says: if<conditional>{codeblock will happen}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">There are three ‘types’ of if statements:

 if <conditional> {code block will happen} – basic If-then statement</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Else if <conditional> {code block will happen} – says ‘If the first if statement does not apply, do this.’</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Else {code block will happen} – says, ‘if the above statements do not apply, do this.’</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">A good example of if statements would be:

public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Int x = 10;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> If (x > 10){</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(“X is greater than 10.”); </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Else if (x < 10){</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(“X is less than 10.”);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Else{</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(“X is equal to 10.”);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">If one takes the above code and puts it into basic sentences, it will look like:

 The integer ‘x’ is equal to 10.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> If x is greater than 10, print out “X is greater than 10.”</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Otherwise, if x is less than 10, print out “X is less than 10.”</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> If neither of the above statements apply to x, print out “X is equal to 10.”</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">If statements can be used not only with static (never changing) conditions but also with things like user input. For example: </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">import java.util.Scanner;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">public class UserInput {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> public static void main(String[] args) {</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> String password = “xyxy”;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> String text;</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print("Enter A Password: ");</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> text = scan.next();</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> if(text != password){</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(“Access denied.”);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> Else{</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> System.out.print(“Access granted.”);</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> }</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">The above program asks the user for a password, if the user enters “xyxy”, access is granted. However, if the user does not enter “xyxy”, access is denied.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Along with != (does not equal) and == (exactly equals), The if statement has a variety of other conditionals that can be used. Some of these are:</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .EqualsIgnoreCase(String) –</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> If (somestring.EqualsIgnoreCase(anotherstring)){code block will happen}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> This conditional throws away upper case and lower case.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .Equals(String) –</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> If (Somestring.Equals(anotherstring)){code block will happen}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> This conditional is like == but is more precise.</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> .Contains(String) –</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> If (somestring.contains(anotherstring)){code block will happen}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> This conditional check if one string contains another string, for example: If (anatomy.contains(tom){code block will happen}</p> <p class="MsoNormal" style="margin-right:-.5in;mso-pagination:none;tab-stops: 28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">Chapter 9: Loops</p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none"> </p> <p class="MsoNormal">In java there are loops that allow the programmer to have a block of code repeated while a conditional is true/false. There are three main types of loops, these are:
The while loop:
</p> <p class="MsoListParagraph" style="margin-left:1.0in;mso-add-space:auto; text-indent:-.25in;mso-list:l9 level1 lfo8">· The while loop says while <conditional> {code block will happen}. The while loops is useful for long and fast loops. One example of this type of loop is:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> int x = 10;</p> <p class="MsoNormal"> while(x != 0){</p> <p class="MsoNormal"> System.out.println("X = " + x);</p> <p class="MsoNormal"> x--;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> The for loop:</p> <p class="MsoListParagraph" style="margin-left:1.0in;mso-add-space:auto; text-indent:-.25in;mso-list:l9 level1 lfo8">· The for loop is like a while loop but it is used for less repetitions. To write this type of loop we use: for (conditional; conditional >/</<=/>=/etc.;conditional++/conditional--/++conditional/--conditional). One example of this typeof loops is:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> for (int i = 10; i > 0; i--){</p> <p class="MsoNormal"> System.out.println("i = " + i);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">The do-while loop:</p> <p class="MsoListParagraph" style="margin-left:1.0in;mso-add-space:auto; text-indent:-.25in;mso-list:l9 level1 lfo8">· The do while loop is like a large for loop, It has the same speed and the same durability but it takes more lines of code. To write this type of loop we use: do {code block}while<conditional>. One example of this loop is:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> int x = 10;</p> <p class="MsoNormal"> do{</p> <p class="MsoNormal"> System.out.println("X =" + x);</p> <p class="MsoNormal"> x--;</p> <p class="MsoNormal"> }while(x != 0);</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">As you can see, each of these loops is created differently but they are all able to do the same thing. It is up to the programmer which one he/she wants to use. Another factor to think about when using loops is called ‘Nested looping’. A nested loop is a loop within a loop. The nested loops have the same syntax as the original loop. Here are some examples of nested loops:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoListParagraph" style="margin-left:1.0in;mso-add-space:auto; text-indent:-.25in;mso-list:l9 level1 lfo8">· Do While loop</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> int x = 10;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> do{</p> <p class="MsoNormal"> int y = 0;</p> <p class="MsoNormal"> System.out.println("X =" + x);</p> <p class="MsoNormal"> x--;</p> <p class="MsoNormal"> do{</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.println("\tY =" + y);</p> <p class="MsoNormal"> y++;</p> <p class="MsoNormal"> }while(y != 10);</p> <p class="MsoNormal"> }while(x != 0);</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoListParagraph" style="margin-left:1.0in;mso-add-space:auto; text-indent:-.25in;mso-list:l9 level1 lfo8">· While loop</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> int x = 10;</p> <p class="MsoNormal"> int y = 0;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> while(x != 0){</p> <p class="MsoNormal"> while(y != 10){</p> <p class="MsoNormal"> System.out.println("X = " + x);</p> <p class="MsoNormal"> x--;</p> <p class="MsoNormal"> System.out.println("Y = " + y);</p> <p class="MsoNormal"> y++;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoListParagraph" style="margin-left:1.0in;mso-add-space:auto; text-indent:-.25in;mso-list:l9 level1 lfo8">· For loop</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> for (int i = 0; i < 10; i++){</p> <p class="MsoNormal"> System.out.println("I = " + i);</p> <p class="MsoNormal"> for (int j = 10; j > 0; j--){</p> <p class="MsoNormal"> System.out.println("J = " + j);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 10: Switch Statements</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">A Switch statement in java is like an ‘if statement’ but more organized. However, in spite of the organization of the switch statement, these types of conditional statements take up a lot of lines of code. One example of switch statements is:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class switch {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> int day = 5;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> switch (day) {</p> <p class="MsoNormal"> case 1: </p> <p class="MsoNormal"> System.out.println("Monday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> case 2: </p> <p class="MsoNormal"> System.out.println("Tuesday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> case 3: </p> <p class="MsoNormal"> System.out.println("Wednesday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> case 4: </p> <p class="MsoNormal"> System.out.println("Thursday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> case 5: </p> <p class="MsoNormal"> System.out.println("Friday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> case 6: </p> <p class="MsoNormal"> System.out.println("Saturday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> case 7: </p> <p class="MsoNormal"> System.out.println("Sunday"); </p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> default: </p> <p class="MsoNormal"> System.out.println("Invalid Day.");</p> <p class="MsoNormal"> break;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above code will print out “Friday”. As you can see, this is just like an If statement but it takes up a lot of lines of code.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 11: Multiple Methods</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">Contrary to what you may be thinking, there are multiple methods in java. To create a method we do public/private void methodname(). Public/private defines whether or not the contents of said methods can be seen by other methods. Along with void, methods be type specific. For example, public int methodname(), public double methodname(). Methods can also have operators in their parenthesis. For example, public void methodName(String a, String b, int c, double d){code block}. One example of multiple methods is:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.Scanner;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class loops {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> int a, b;</p> <p class="MsoNormal"> System.out.print("Enter first number: ");</p> <p class="MsoNormal"> a = scan.nextInt();</p> <p class="MsoNormal"> System.out.print("Enter second number: ");</p> <p class="MsoNormal"> b = scan.nextInt();</p> <p class="MsoNormal"> System.out.print("A+B= " + add(a,b));</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public static int add(int a, int b){//static means never changing and is used for syntax</p> <p class="MsoNormal"> return a + b;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above code does the following:</p> <p class="MsoNormal"> </p> <p class="MsoListParagraphCxSpFirst" style="text-indent:-.25in;mso-list:l6 level1 lfo9">1. Sets a to an inputted number.</p> <p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;mso-list:l6 level1 lfo9">2. Sets b to an inputted number.</p> <p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;mso-list:l6 level1 lfo9">3. Calls the method ‘add’ and substitutes a and b in the method parenthesis with a and b define in the main method.</p> <p class="MsoListParagraphCxSpMiddle" style="text-indent:-.25in;mso-list:l6 level1 lfo9">4. Returns the sum of a and b (return a + b)</p> <p class="MsoListParagraphCxSpLast" style="text-indent:-.25in;mso-list:l6 level1 lfo9">5. Goes back to the method that it was called and prints out the returned answer along with “A+B= ”.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Procedure 2: Menu Program</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">In this procedure we will be making a menu program using input, if statements and loops.</p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.Scanner;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class Menu {</p> <p class="MsoNormal"> static String password;</p> <p class="MsoNormal"> static int logintries = 5;</p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> run();//goes to a method named "run"</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private static void run() {</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> String loginreg;</p> <p class="MsoNormal"> System.out.print("Login or Register?: ");</p> <p class="MsoNormal"> loginreg = scan.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (loginreg.equalsIgnoreCase("Login")){</p> <p class="MsoNormal"> login();</p> <p class="MsoNormal"> }else if (loginreg.equalsIgnoreCase("register")){</p> <p class="MsoNormal"> register();</p> <p class="MsoNormal"> }else{</p> <p class="MsoNormal"> run();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private static void register() {</p> <p class="MsoNormal"> Scanner reg = new Scanner(System.in);</p> <p class="MsoNormal"> String regpass;</p> <p class="MsoNormal"> System.out.print("Enter Desired Password: ");</p> <p class="MsoNormal"> regpass = reg.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> password = regpass;//sets the password to the input recieved</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (password == ""){</p> <p class="MsoNormal"> register();//if the passwords length is 0 (no letters or whitespace) make a new one</p> <p class="MsoNormal"> }else{</p> <p class="MsoNormal"> logintries = 5;</p> <p class="MsoNormal"> login();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private static void login() {</p> <p class="MsoNormal"> if (password != ""){//if the password is larger then an empty string</p> <p class="MsoNormal"> if (logintries != 0){</p> <p class="MsoNormal"> Scanner pass = new Scanner(System.in);</p> <p class="MsoNormal"> String chance;</p> <p class="MsoNormal"> System.out.print("Enter your password: ");</p> <p class="MsoNormal"> chance = pass.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (chance.equals(password)){</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else{//if chance != password</p> <p class="MsoNormal"> logintries--;</p> <p class="MsoNormal"> login();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }else{//if tries = 0;</p> <p class="MsoNormal"> register();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }else{//if password.length == 0</p> <p class="MsoNormal"> register();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private static void Menu() {</p> <p class="MsoNormal"> Scanner menuchoice = new Scanner(System.in);</p> <p class="MsoNormal"> int choice;</p> <p class="MsoNormal"> System.out.println("**************************************");</p> <p class="MsoNormal"> System.out.println("* *");</p> <p class="MsoNormal"> System.out.println("* Menu *");</p> <p class="MsoNormal"> System.out.println("* *");</p> <p class="MsoNormal"> System.out.println("* 1.Calculator *");</p> <p class="MsoNormal"> System.out.println("* 2.Change password *");</p> <p class="MsoNormal"> System.out.println("* 3.Log out *");</p> <p class="MsoNormal"> System.out.println("* *");</p> <p class="MsoNormal"> System.out.println("* *");</p> <p class="MsoNormal"> System.out.println("**************************************");</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> choice = menuchoice.nextInt();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if(choice == 1){</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if(choice == 2){</p> <p class="MsoNormal"> register();</p> <p class="MsoNormal"> }else if (choice == 3){</p> <p class="MsoNormal"> System.exit(0);//shuts down the program</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else{</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private static void calculator() {</p> <p class="MsoNormal"> Scanner type = new Scanner(System.in);</p> <p class="MsoNormal"> String mathtype;</p> <p class="MsoNormal"> System.out.print("Enter math type(mod,/,*,+,-): ");</p> <p class="MsoNormal"> mathtype = type.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if(mathtype.equals("mod")){</p> <p class="MsoNormal"> Scanner math = new Scanner(System.in);</p> <p class="MsoNormal"> double a,b;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Enter first number: ");</p> <p class="MsoNormal"> a = math.nextDouble();</p> <p class="MsoNormal"> System.out.print("Enter second number: ");</p> <p class="MsoNormal"> b = math.nextDouble();</p> <p class="MsoNormal"> System.out.println("Answer: " + mod(a,b));</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Scanner again = new Scanner(System.in);</p> <p class="MsoNormal"> String yesno;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Use calculator again?(y/n): ");</p> <p class="MsoNormal"> yesno = again.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (yesno.equalsIgnoreCase("y")){</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }else if (yesno.equalsIgnoreCase("n")){</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if(mathtype.equals("+")){</p> <p class="MsoNormal"> Scanner math = new Scanner(System.in);</p> <p class="MsoNormal"> double a,b;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Enter first number: ");</p> <p class="MsoNormal"> a = math.nextDouble();</p> <p class="MsoNormal"> System.out.print("Enter second number: ");</p> <p class="MsoNormal"> b = math.nextDouble();</p> <p class="MsoNormal"> System.out.println("Answer: " + add(a,b));</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Scanner again = new Scanner(System.in);</p> <p class="MsoNormal"> String yesno;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Use calculator again?(y/n): ");</p> <p class="MsoNormal"> yesno = again.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (yesno.equalsIgnoreCase("y")){</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }else if (yesno.equalsIgnoreCase("n")){</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if(mathtype.equals("-")){</p> <p class="MsoNormal"> Scanner math = new Scanner(System.in);</p> <p class="MsoNormal"> double a,b;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Enter first number: ");</p> <p class="MsoNormal"> a = math.nextDouble();</p> <p class="MsoNormal"> System.out.print("Enter second number: ");</p> <p class="MsoNormal"> b = math.nextDouble();</p> <p class="MsoNormal"> System.out.println("Answer: " + sub(a,b));</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Scanner again = new Scanner(System.in);</p> <p class="MsoNormal"> String yesno;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Use calculator again?(y/n): ");</p> <p class="MsoNormal"> yesno = again.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (yesno.equalsIgnoreCase("y")){</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }else if (yesno.equalsIgnoreCase("n")){</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if(mathtype.equals("*")){</p> <p class="MsoNormal"> Scanner math = new Scanner(System.in);</p> <p class="MsoNormal"> double a,b;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Enter first number: ");</p> <p class="MsoNormal"> a = math.nextDouble();</p> <p class="MsoNormal"> System.out.print("Enter second number: ");</p> <p class="MsoNormal"> b = math.nextDouble();</p> <p class="MsoNormal"> System.out.println("Answer: " + mult(a,b));</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Scanner again = new Scanner(System.in);</p> <p class="MsoNormal"> String yesno;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Use calculator again?(y/n): ");</p> <p class="MsoNormal"> yesno = again.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (yesno.equalsIgnoreCase("y")){</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }else if (yesno.equalsIgnoreCase("n")){</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if(mathtype.equals("/")){</p> <p class="MsoNormal"> Scanner math = new Scanner(System.in);</p> <p class="MsoNormal"> double a,b;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Enter first number: ");</p> <p class="MsoNormal"> a = math.nextDouble();</p> <p class="MsoNormal"> System.out.print("Enter second number: ");</p> <p class="MsoNormal"> b = math.nextDouble();</p> <p class="MsoNormal"> System.out.println("Answer: " + div(a,b));</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Scanner again = new Scanner(System.in);</p> <p class="MsoNormal"> String yesno;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Use calculator again?(y/n): ");</p> <p class="MsoNormal"> yesno = again.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (yesno.equalsIgnoreCase("y")){</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }else if (yesno.equalsIgnoreCase("n")){</p> <p class="MsoNormal"> Menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else{</p> <p class="MsoNormal"> calculator();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public static double mod(double a, double b){</p> <p class="MsoNormal"> return a % b;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public static double add(double a, double b){</p> <p class="MsoNormal"> return a + b;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public static double mult(double a, double b){</p> <p class="MsoNormal"> return a * b;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public static double sub(double a, double b){</p> <p class="MsoNormal"> return a - b;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public static double div(double a, double b){</p> <p class="MsoNormal"> return a / b;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal">The above program requires the user to login/register a password then login to go to a menu, in the menu, they can choose to change their password, go to a calculator, or shut down the program. </p> <p align="center" class="MsoNormal" style="margin-right:-.5in;text-align:center; mso-pagination:none;tab-stops:28.0pt 1.0in 1.5in 2.0in 2.5in 3.0in 3.5in 4.0in 4.5in 5.0in 5.5in 6.0in; mso-layout-grid-align:none;text-autospace:none">24.182.108.156!!! End Of Unit 2 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">24.182.108.156!!! Start Of Unit 3 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 12: Arrays/ArrayLists</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">Arrays and ArrayLists are two topics in programming that are very important to the fundamentals of every programming language. An array is a preset group of integers, strings, characters, etc. There are three ways to form an array:</p> <p class="MsoNormal"> 1. DataType[] arrayname = new DataType[ArraySize];</p> <p class="MsoNormal" style="text-indent:.5in">2. DataType arrayname[] = {item 0, item 1, item 2, item 3…item n};</p> <p class="MsoNormal" style="text-indent:.5in">3. DataType[] arrayname = {item 0, item 1, item 2, item 3…item n}; </p> <p class="MsoNormal">As you can see, the integer definitions of items in an array always start with ‘0’ instead of ‘1’. An example of an array is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.*;//imports everything in the java.util package</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class Array{</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> static Scanner input = new Scanner(System.in);</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args) {</p> <p class="MsoNormal"> int numNames = getInt("Number of names?");//calls the method “getint()”</p> <p class="MsoNormal"> String[] names = new String[numNames];//the array (takes the size of the array as the number of names inputed)</p> <p class="MsoNormal"> for (int i = 0; i < names.length; i++) {</p> <p class="MsoNormal"> names[i] = getString("Enter name #" + (i + 1));//sets each item of the array as a name inputed</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> for (int i = 0; i < names.length; ++i) {</p> <p class="MsoNormal"> System.out.println(names[i]);//prints out name at the index number (i) in the array</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static int getInt(String prompt) {</p> <p class="MsoNormal"> System.out.print(prompt + " ");</p> <p class="MsoNormal"> int integer = input.nextInt();</p> <p class="MsoNormal"> input.nextLine(); </p> <p class="MsoNormal"> return integer;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static String getString(String prompt) {</p> <p class="MsoNormal"> System.out.print(prompt + " ");</p> <p class="MsoNormal"> return input.nextLine();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">In the above program, the user is asked to enter a number of names to enter. Once the number is entered, the number is set to the size of an array. Next, the program asks the user for names to fill in the array; once the array is full (the amount of names equals the number inputted) all the names in the array will be printed out. If one wants to print out a full array all together (instead of the items in it 1 by 1) you must use the .toString() function which changes the array to a string.For example:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print(ArrayName.toString());</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Along with arrays, array lists are a huge part of programming. Array lists are like arrays but you can add things, remove things, etc. To create an array list you do the following:</p> <p class="MsoNormal"> ArrayList<Data Type> ArrayName = new ArrayList<Data Type>();</p> <p class="MsoNormal">To add Things to an array one must use the .add function. For example:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> ArrayList<String> names = new ArrayList<String>();</p> <p class="MsoNormal"> names.add(“Daniel”);</p> <p class="MsoNormal"> names.add(“Charlie”);</p> <p class="MsoNormal"> names.add(“Tracy”);</p> <p class="MsoNormal"> </p> <p class="MsoNormal">To remove items from an array one must use the remove function. For example;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> names.remove(“Daniel”);</p> <p class="MsoNormal"> names.remove(“Charlie”);</p> <p class="MsoNormal"> names.remove(“Tracy”);</p> <p class="MsoNormal"> </p> <p class="MsoNormal">One example of array list use is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.*;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class ArrayList{</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public static void main(String args[]) {</p> <p class="MsoNormal">// create an array list</p> <p class="MsoNormal">ArrayList al = new ArrayList();</p> <p class="MsoNormal">System.out.println("Initial size of al: " +</p> <p class="MsoNormal">al.size());</p> <p class="MsoNormal">// add elements to the array list</p> <p class="MsoNormal">al.add("C");</p> <p class="MsoNormal">al.add("A");</p> <p class="MsoNormal">al.add("E");</p> <p class="MsoNormal">al.add("B");</p> <p class="MsoNormal">al.add("D");</p> <p class="MsoNormal">al.add("F");</p> <p class="MsoNormal">al.add(1, "A2");</p> <p class="MsoNormal">System.out.println("Size of al after additions: " +</p> <p class="MsoNormal">al.size());</p> <p class="MsoNormal">// display the array list</p> <p class="MsoNormal">System.out.println("Contents of al: " + al);</p> <p class="MsoNormal">// Remove elements from the array list</p> <p class="MsoNormal">al.remove("F");</p> <p class="MsoNormal">al.remove(2);</p> <p class="MsoNormal">System.out.println("Size of al after deletions: " +</p> <p class="MsoNormal">al.size());</p> <p class="MsoNormal">System.out.println("Contents of al: " + al);</p> <p class="MsoNormal" style="text-indent:.5in">}</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above program’s output is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Initial size of al: 0</p> <p class="MsoNormal">Size of al after additions: 7</p> <p class="MsoNormal">Contents of al: [C, A2, A, E, B, D, F]</p> <p class="MsoNormal">Size of al after deletions: 5</p> <p class="MsoNormal">Contents of al: [C, A2, E, B, D]</p> <p class="MsoNormal"> </p> <p class="MsoNormal">ArrayLists can also be used with user input: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.*;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class InputArrayList{</p> <p class="MsoNormal">public static void main(String args[]) {</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> ArrayList al = new ArrayList();</p> <p class="MsoNormal"> System.out.print("Amount of objects to put in arraylist?: ");</p> <p class="MsoNormal"> int size = scan.nextInt();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> for (int i = 1; i < size + 1; i++){</p> <p class="MsoNormal"> Scanner items = new Scanner(System.in);</p> <p class="MsoNormal"> String addItem;</p> <p class="MsoNormal"> System.out.println(i+": Add something to the arraylist: ");</p> <p class="MsoNormal"> addItem = items.next();</p> <p class="MsoNormal"> al.add(addItem);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Size of ArrayList is: " +al.size()+ "\n");</p> <p class="MsoNormal"> System.out.print("Contenets of ArrayList are: " + al);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above program does almost the same thing as the array program we made. It asks the user for a size, then it prompts the user to add things to the ArrayList. Once the amount of things in the ArrayList equal the size, the ArrayList is printed out.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 13: 2D arrays</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">A two-dimensional array is an array who’s items are arrays as well. To form one of these array you use: </p> <p class="MsoNormal" style="text-indent:.5in">DataType[][] Arrayname = {{item,item,item…},</p> <p class="MsoNormal" style="text-indent:.5in"> {item,item,item…},</p> <p class="MsoNormal" style="text-indent:.5in"> {item,item,item…}};</p> <p class="MsoNormal"> </p> <p class="MsoNormal">As you can see, all the items in the array “Arrayname” are put in other arrays. This is useful for things like maps, draw-outs, etc. Furthermore, you can easily pinpoint an item on the ‘graph’ of a two-dimensional array by using:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Arrayname[YvalIndex][XvalIndex] </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">For example:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> String[][] Names = {{"Daniel", "John", "Willis"},</p> <p class="MsoNormal"> {"David", "Alex", "Andrew"},</p> <p class="MsoNormal"> {"Alec", "Adam", "Anthony"}};</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print(Names[1][2].toString());</p> <p class="MsoNormal"> </p> <p class="MsoNormal">The Above will print out “Andrew” because it is at the 2nd Array (first array has index 0) and is the 2nd item in the array (first item has index 0).</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 14: OOP (Object Oriented Programming).</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">Object oriented programming is programming with multiple classes. This keeps your code neat and organized. When working with mulitiple classes, you must create an object of other classes being used (hence, the name: ‘Object oriented’ programming).To do this, we use:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> ClassName CustomName = new ClassName();</p> <p class="MsoNormal"> </p> <p class="MsoNormal">One example of object oriented programming is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Main Class:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.*;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class Main{</p> <p class="MsoNormal">public static void main(String args[]) {</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> int size;</p> <p class="MsoNormal"> System.out.print("Enter size of arrayList: ");</p> <p class="MsoNormal"> size = scan.nextInt();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> arraylist list = new arraylist();//creates an object of the arraylist class</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> for (int i = 1; i < size + 1; i++){</p> <p class="MsoNormal"> Scanner _scan = new Scanner(System.in);</p> <p class="MsoNormal"> String item;</p> <p class="MsoNormal"> System.out.print(i+": ");</p> <p class="MsoNormal"> item = _scan.next();</p> <p class="MsoNormal"> list.arli.add(item);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> list.al();//will run the method 'al' in arraylist class</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">arraylist Class:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.ArrayList;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class arraylist {</p> <p class="MsoNormal"> ArrayList arli = new ArrayList();</p> <p class="MsoNormal"> public void al(){</p> <p class="MsoNormal"> System.out.println("List size: "+ arli.size());</p> <p class="MsoNormal"> System.out.println("Items: "+arli.toString());</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above program will do the same thing as the Array-list program did two chapters ago. However, This program utilizes a second class called “arraylist” to optimize the organization of our code.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 15: Threads</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Threads in java allow the program to run faster and more efficiently then programs without friends, a program that once took two second to compile and run can take 0.5 seconds using threads. What a thread does is take the load of the compiler by going through a certain path in the program by itself while letting other threads (or the compiler itself) to look at the other lines of code more productively. For example, one thread could take the role of an ‘If statement’ while another could have the role of an ‘Else statement’. One example of threads is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">class Thread</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static void main (String [] args)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> MyThread mt = new MyThread ();</p> <p class="MsoNormal"> mt.start ();//starts up the thread</p> <p class="MsoNormal"> for (int i = 0; i < 50; i++)//this is the threads function/role</p> <p class="MsoNormal"> System.out.println ("i = " + i + ", i * i = " + i * i);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal">class MyThread extends Thread//is the thread</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public void run ()</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> for (int count = 1, row = 1; row < 20; row++, count++)//does this seperatly than the above class but run simultaniously</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> for (int i = 0; i < count; i++)</p> <p class="MsoNormal"> System.out.print ('*');</p> <p class="MsoNormal"> System.out.print ('\n');</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">the output of this program is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">i = 0, i * i = 0</p> <p class="MsoNormal">i = 1, i * i = 1</p> <p class="MsoNormal">i = 2, i * i = 4</p> <p class="MsoNormal">i = 3, i * i = 9</p> <p class="MsoNormal">i = 4, i * i = 16</p> <p class="MsoNormal">i = 5, i * i = 25</p> <p class="MsoNormal">i = 6, i * i = 36</p> <p class="MsoNormal">i = 7, i * i = 49</p> <p class="MsoNormal">i = 8, i * i = 64</p> <p class="MsoNormal">i = 9, i * i = 81</p> <p class="MsoNormal">i = 10, i * i = 100</p> <p class="MsoNormal">i = 11, i * i = 121</p> <p class="MsoNormal">i = 12, i * i = 144</p> <p class="MsoNormal">i = 13, i * i = 169</p> <p class="MsoNormal">i = 14, i * i = 196</p> <p class="MsoNormal">i = 15, i * i = 225</p> <p class="MsoNormal">i = 16, i * i = 256</p> <p class="MsoNormal">i = 17, i * i = 289</p> <p class="MsoNormal">i = 18, i * i = 324</p> <p class="MsoNormal">i = 19, i * i = 361</p> <p class="MsoNormal">i = 20, i * i = 400</p> <p class="MsoNormal">i = 21, i * i = 441</p> <p class="MsoNormal">i = 22, i * i = 484</p> <p class="MsoNormal">*</p> <p class="MsoNormal">**</p> <p class="MsoNormal">***</p> <p class="MsoNormal">****</p> <p class="MsoNormal">*****</p> <p class="MsoNormal">******</p> <p class="MsoNormal">*******</p> <p class="MsoNormal">********</p> <p class="MsoNormal">*********</p> <p class="MsoNormal">*****i = 23, i * i = 529</p> <p class="MsoNormal">i = 24, i * i = 576</p> <p class="MsoNormal">i = 25, i * i = 625</p> <p class="MsoNormal">i = 26, i * i = 676</p> <p class="MsoNormal">i = 27, i * i = 729</p> <p class="MsoNormal">i = 28, i * i = 784</p> <p class="MsoNormal">i = 29, i * i = 841</p> <p class="MsoNormal">i = 30, i * i = 900</p> <p class="MsoNormal">i = 31, i * i = 961</p> <p class="MsoNormal">i = 32, i * i = 1024</p> <p class="MsoNormal">i = 33, i * i = 1089</p> <p class="MsoNormal">i = 34, i * i = 1156</p> <p class="MsoNormal">i = 35, i * i = 1225</p> <p class="MsoNormal">i = 36, i * i = 1296</p> <p class="MsoNormal">i = 37, i * i = 1369</p> <p class="MsoNormal">i = 38, i * i = 1444</p> <p class="MsoNormal">i = 39, i * i = 1521</p> <p class="MsoNormal">i = 40, i * i = 1600</p> <p class="MsoNormal">i = 41, i * i = 1681</p> <p class="MsoNormal">i = 42, i * i = 1764</p> <p class="MsoNormal">i = 43, i * i = 1849</p> <p class="MsoNormal">i = 44, i * i = 1936</p> <p class="MsoNormal">i = 45, i * i = 2025</p> <p class="MsoNormal">i = 46, i * i = 2116</p> <p class="MsoNormal">i = 47, i * i = 2209</p> <p class="MsoNormal">i = 48, i * i = 2304</p> <p class="MsoNormal">i = 49, i * i = 2401</p> <p class="MsoNormal">*****</p> <p class="MsoNormal">***********</p> <p class="MsoNormal">************</p> <p class="MsoNormal">*************</p> <p class="MsoNormal">**************</p> <p class="MsoNormal">***************</p> <p class="MsoNormal">****************</p> <p class="MsoNormal">*****************</p> <p class="MsoNormal">******************</p> <p class="MsoNormal">*******************</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Without threading, this program would take much longer then it did to compile and run (This program took approximately 0.15 seconds to compile and run).</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 16: Multi-Threading</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Multi-Threading is using multiple threads in one program to run the program as efficiently as possible. For example, a multi-threaded GUI (Graphical User Interface) can keep responding to user inputs while it does something else, like print a document or send an email etc. One example of multi-threading is: </p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.Random;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class thread</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static void main (String [] args)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> FinTrans ft = new FinTrans ();</p> <p class="MsoNormal"> TransThread tt1 = new TransThread (ft, "Deposit Thread");</p> <p class="MsoNormal"> TransThread tt2 = new TransThread (ft, "Withdrawal Thread");</p> <p class="MsoNormal"> tt1.start ();</p> <p class="MsoNormal"> tt2.start ();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal">class FinTrans</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static String transName;</p> <p class="MsoNormal"> public static double amount;</p> <p class="MsoNormal">}</p> <p class="MsoNormal">class TransThread extends Thread</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> private FinTrans ft;</p> <p class="MsoNormal"> TransThread (FinTrans ft, String name)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> super (name); // Save thread's name</p> <p class="MsoNormal"> this.ft = ft; // Save reference to financial transaction object</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> public void run ()</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> for (int i = 0; i < 100; i++)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> if (getName ().equals ("Deposit Thread"))</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> // Start of deposit thread's critical code section</p> <p class="MsoNormal"> ft.transName = "Deposit";</p> <p class="MsoNormal"> try</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> Thread.sleep ((int) (Math.random () * 1000));//pauses the thread for a random amount of time</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> catch (InterruptedException e)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> Random r = new Random();</p> <p class="MsoNormal"> ft.amount = r.nextInt(6000)+1;</p> <p class="MsoNormal"> System.out.println (ft.transName + " " + ft.amount);</p> <p class="MsoNormal"> // End of deposit thread's critical code section</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> // Start of withdrawal thread's critical code section</p> <p class="MsoNormal"> ft.transName = "Withdrawal";</p> <p class="MsoNormal"> try</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> Thread.sleep ((int) (Math.random () * 1000));//pauses the thread for a randomamount of time</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> catch (InterruptedException e)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> Random r = new Random();</p> <p class="MsoNormal"> ft.amount = r.nextInt(5000)+1;</p> <p class="MsoNormal"> System.out.println (ft.transName + " " + ft.amount);</p> <p class="MsoNormal"> // End of withdrawal thread's critical code section</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above program uses multithreading to “Withdraw” and “Deposit” a random amount. This program shows how multiple threads can be run at once and still give out results efficiently.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Procedure 3: Bank Program</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">In this procedure we will be using threading and Array-Lists to create a bank program.</p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.ArrayList;</p> <p class="MsoNormal">import java.util.Random;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class bank {</p> <p class="MsoNormal"> public static void main(String[] args){</p> <p class="MsoNormal"> banker bk = new banker();</p> <p class="MsoNormal"> bankarray bka = new bankarray();</p> <p class="MsoNormal"> bk.start();</p> <p class="MsoNormal"> bk.run();</p> <p class="MsoNormal"> bka.start();</p> <p class="MsoNormal"> bka.run();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.print("Recent history: " + bka.bankal.toString());</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class banker extends Thread {</p> <p class="MsoNormal"> public void run(){</p> <p class="MsoNormal"> Random r = new Random();</p> <p class="MsoNormal"> int money = r.nextInt(10000);</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> bankarray bka = new bankarray();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> for(int i = 0; i < 11; i++){</p> <p class="MsoNormal"> bka.bankal.add(money);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">class bankarray extends Thread{</p> <p class="MsoNormal"> ArrayList bankal = new ArrayList();</p> <p class="MsoNormal"> public void run(){</p> <p class="MsoNormal"> if (bankal.size() == 10){</p> <p class="MsoNormal"> System.out.println("Finished creating history");</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">The following will print out:</p> <p class="MsoNormal"> </p> <p class="MsoNormal" style="text-indent:.5in">Finished creating history.</p> <p class="MsoNormal" style="text-indent:.5in">Recent History: [randomnumber,randomnumber,randomnumber….]</p> <p class="MsoNormal"> </p> <p class="MsoNormal">This program took about 0.25 seconds to compile and run</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center"> 24.182.108.156!!! End Of Unit 3 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">24.182.108.156!!! Start Of Unit 4 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 17: Recursion</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">Recursion is when a method calls upon itself. An example of recursion is:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Public void recurse(int i){</p> <p class="MsoNormal">If (I == 0)//if I = 0, end the method</p> <p class="MsoNormal"> Return;</p> <p class="MsoNormal">System.out.print(i+” “);//if I != 0 do this</p> <p class="MsoNormal">Recurse(i -1);//subtract 1 from i</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">If the original call to recurse() is: recurse(10); then the program will print out the numbers 10-1 in reverse order with a space between them:</p> <p class="MsoNormal"> </p> <p class="MsoNormal" style="text-indent:.5in">10 9 8 7 6 5 4 3 2 1</p> <p class="MsoNormal"> </p> <p class="MsoNormal">When building a recursive method, there are 3 main parts:</p> <p class="MsoNormal"> </p> <p class="MsoListParagraphCxSpFirst" style="margin-left:.75in;mso-add-space:auto; text-indent:-.25in;mso-list:l8 level1 lfo10">1) the Parameters -- What values need to be kept track of during my recursion? In the recurse() method above, that value is just the number that needs to be printed.</p> <p class="MsoListParagraphCxSpMiddle" style="margin-left:.75in;mso-add-space: auto;text-indent:-.25in;mso-list:l8 level1 lfo10">2) The checks -- Every recursive method must have a base case. That is, a case in which the final solution has been found and recursion should stop. In recurse(), that was when i=0. Testing for the base cases are called the checks, because they check whether or not the method should do anything.</p> <p class="MsoListParagraphCxSpLast" style="margin-left:.75in;mso-add-space:auto; text-indent:-.25in;mso-list:l8 level1 lfo10">3) The calls -- For a method to be recursive, it must call itself. That’s done in the calls section of the method. The calls run new methods with updated values. It is important to remember that ANY objects passed in these calls will change in the method from which they originated. So if you’re going to be updating an object at any point, ALWAYS send a copy of it in the recursive calls. For example, when sending an ArrayList<String> called list DO NOT put list in your parameters. Sending something like new ArrayList<String>(list); this constructor of the ArrayList class creates as new ArrayList with the same objects as the original.</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Public void recurse(int i/*the parameters*/){</p> <p class="MsoNormal"> If (I == 0)//the check: have I found my stopping point?</p> <p class="MsoNormal"> Return;</p> <p class="MsoNormal"> System.out.print(i + “ “);</p> <p class="MsoNormal"> Recurse(i - 1);//The Call(s)</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 18: Algorithms</p> <p class="MsoNormal"> </p> <p class="MsoNormal">An Algorithm is a different way of making a program do something. One basic example of an algorithm would be:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Turn left.</p> <p class="MsoNormal"> Look at wall.</p> <p class="MsoNormal"> Move forward.</p> <p class="MsoNormal"> Turn left.</p> <p class="MsoNormal"> </p> <p class="MsoNormal">As you can see, the outcome of the above algorithm would be you looking at the back wall, while standing next to the left wall.</p> <p class="MsoNormal"> </p> <p class="MsoNormal">An example of algorithms within java would be sorts. A sort is a program that organizes and unorganized array. Some types of sorts are:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> The quick sort:</p> <p class="MsoNormal"> public class Quicksort {</p> <p class="MsoNormal"> private int[] numbers;</p> <p class="MsoNormal"> private int number;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public void sort(int[] values) {</p> <p class="MsoNormal"> // Check for empty or null array</p> <p class="MsoNormal"> if (values ==null || values.length==0){</p> <p class="MsoNormal"> return;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> this.numbers = values;</p> <p class="MsoNormal"> number = values.length;</p> <p class="MsoNormal"> quicksort(0, number - 1);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private void quicksort(int low, int high) {</p> <p class="MsoNormal"> int i = low, j = high;</p> <p class="MsoNormal"> // Get the pivot element from the middle of the list</p> <p class="MsoNormal"> int pivot = numbers[low + (high-low)/2];</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> // Divide into two lists</p> <p class="MsoNormal"> while (i <= j) {</p> <p class="MsoNormal"> // If the current value from the left list is smaller then the pivot</p> <p class="MsoNormal"> // element then get the next element from the left list</p> <p class="MsoNormal"> while (numbers[i] < pivot) {</p> <p class="MsoNormal"> i++;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> // If the current value from the right list is larger then the pivot</p> <p class="MsoNormal"> // element then get the next element from the right list</p> <p class="MsoNormal"> while (numbers[j] > pivot) {</p> <p class="MsoNormal"> j--;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> // If we have found a values in the left list which is larger then</p> <p class="MsoNormal"> // the pivot element and if we have found a value in the right list</p> <p class="MsoNormal"> // which is smaller then the pivot element then we exchange the</p> <p class="MsoNormal"> // values.</p> <p class="MsoNormal"> // As we are done we can increase i and j</p> <p class="MsoNormal"> if (i <= j) {</p> <p class="MsoNormal"> exchange(i, j);</p> <p class="MsoNormal"> i++;</p> <p class="MsoNormal"> j--;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> // Recursion</p> <p class="MsoNormal"> if (low < j)</p> <p class="MsoNormal"> quicksort(low, j);</p> <p class="MsoNormal"> if (i < high)</p> <p class="MsoNormal"> quicksort(i, high);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private void exchange(int i, int j) {</p> <p class="MsoNormal"> int temp = numbers[i];</p> <p class="MsoNormal"> numbers[i] = numbers[j];</p> <p class="MsoNormal"> numbers[j] = temp;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above sorting type is the fastest type of sorting.</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Another type of sort is the Bubble Sort:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class bubblesort(){</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public static void main(String[] args) {</p> <p class="MsoNormal"> int unsortedArray[] = {10, 97, 6, 23, 0, -45, 697, -1000, 1, 0}; //Random set of numbers for example.</p> <p class="MsoNormal"> int i;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> bubbleSort(unsortedArray, unsortedArray.length); //Pass the array to be sorted and its length.</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.println("After sorting, the list elements are: "); //Just to show you it worked. :)</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> for(i=0; i<unsortedArray.length; i++) {</p> <p class="MsoNormal"> System.out.print(unsortedArray[i] + " ");</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> private static void bubbleSort(int[] unsortedArray, int length) {</p> <p class="MsoNormal"> int temp, counter, index;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.</p> <p class="MsoNormal"> for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.</p> <p class="MsoNormal"> if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not.</p> <p class="MsoNormal"> temp = unsortedArray[index]; //These three lines just swap the two elements:</p> <p class="MsoNormal"> unsortedArray[index] = unsortedArray[index+1];</p> <p class="MsoNormal"> unsortedArray[index+1] = temp;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">} </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Though this sorting type may seem quick. It is considerably slower than the quicksort.</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Another, more advanced algorithm is Djikstra’s shortest path algorithm which finds the shortest path between given points:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.util.PriorityQueue;</p> <p class="MsoNormal">import java.util.List;</p> <p class="MsoNormal">import java.util.ArrayList;</p> <p class="MsoNormal">import java.util.Collections;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class Vertex implements Comparable<Vertex></p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public final String name;</p> <p class="MsoNormal"> public Edge[] adjacencies;</p> <p class="MsoNormal"> public double minDistance = Double.POSITIVE_INFINITY;</p> <p class="MsoNormal"> public Vertex previous;</p> <p class="MsoNormal"> public Vertex(String argName) { name = argName; }</p> <p class="MsoNormal"> public String toString() { return name; }</p> <p class="MsoNormal"> public int compareTo(Vertex other)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> return Double.compare(minDistance, other.minDistance);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class Edge</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public final Vertex target;</p> <p class="MsoNormal"> public final double weight;</p> <p class="MsoNormal"> public Edge(Vertex argTarget, double argWeight)</p> <p class="MsoNormal"> { target = argTarget; weight = argWeight; }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class Dijkstra</p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static void computePaths(Vertex source)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> source.minDistance = 0.;</p> <p class="MsoNormal"> PriorityQueue<Vertex> vertexQueue = new PriorityQueue<Vertex>();</p> <p class="MsoNormal"> vertexQueue.add(source);</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> while (!vertexQueue.isEmpty()) {</p> <p class="MsoNormal"> Vertex u = vertexQueue.poll();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> // Visit each edge exiting u</p> <p class="MsoNormal"> for (Edge e : u.adjacencies)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> Vertex v = e.target;</p> <p class="MsoNormal"> double weight = e.weight;</p> <p class="MsoNormal"> double distanceThroughU = u.minDistance + weight;</p> <p class="MsoNormal"> if (distanceThroughU < v.minDistance) {</p> <p class="MsoNormal"> vertexQueue.remove(v);</p> <p class="MsoNormal"> v.minDistance = distanceThroughU ;</p> <p class="MsoNormal"> v.previous = u;</p> <p class="MsoNormal"> vertexQueue.add(v);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static List<Vertex> getShortestPathTo(Vertex target)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> List<Vertex> path = new ArrayList<Vertex>();</p> <p class="MsoNormal"> for (Vertex vertex = target; vertex != null; vertex = vertex.previous)</p> <p class="MsoNormal"> path.add(vertex);</p> <p class="MsoNormal"> Collections.reverse(path);</p> <p class="MsoNormal"> return path;</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> public static void main(String[] args)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> Vertex v0 = new Vertex("Redvile");</p> <p class="MsoNormal"> Vertex v1 = new Vertex("Blueville");</p> <p class="MsoNormal"> Vertex v2 = new Vertex("Greenville");</p> <p class="MsoNormal"> Vertex v3 = new Vertex("Orangeville");</p> <p class="MsoNormal"> Vertex v4 = new Vertex("Purpleville");</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> v0.adjacencies = new Edge[]{ new Edge(v1, 5),</p> <p class="MsoNormal"> new Edge(v2, 10),</p> <p class="MsoNormal"> new Edge(v3, 8) };</p> <p class="MsoNormal"> v1.adjacencies = new Edge[]{ new Edge(v0, 5),</p> <p class="MsoNormal"> new Edge(v2, 3),</p> <p class="MsoNormal"> new Edge(v4, 7) };</p> <p class="MsoNormal"> v2.adjacencies = new Edge[]{ new Edge(v0, 10),</p> <p class="MsoNormal"> new Edge(v1, 3) };</p> <p class="MsoNormal"> v3.adjacencies = new Edge[]{ new Edge(v0, 8),</p> <p class="MsoNormal"> new Edge(v4, 2) };</p> <p class="MsoNormal"> v4.adjacencies = new Edge[]{ new Edge(v1, 7),</p> <p class="MsoNormal"> new Edge(v3, 2) };</p> <p class="MsoNormal"> Vertex[] vertices = { v0, v1, v2, v3, v4 };</p> <p class="MsoNormal"> computePaths(v0);</p> <p class="MsoNormal"> for (Vertex v : vertices)</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> System.out.println("Distance to " + v + ": " + v.minDistance);</p> <p class="MsoNormal"> List<Vertex> path = getShortestPathTo(v);</p> <p class="MsoNormal"> System.out.println("Path: " + path);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Output:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">Distance to Redvile: 0.0</p> <p class="MsoNormal">Path: [Redvile]</p> <p class="MsoNormal">Distance to Blueville: 5.0</p> <p class="MsoNormal">Path: [Redvile, Blueville]</p> <p class="MsoNormal">Distance to Greenville: 8.0</p> <p class="MsoNormal">Path: [Redvile, Blueville, Greenville]</p> <p class="MsoNormal">Distance to Orangeville: 8.0</p> <p class="MsoNormal">Path: [Redvile, Orangeville]</p> <p class="MsoNormal">Distance to Purpleville: 10.0</p> <p class="MsoNormal">Path: [Redvile, Orangeville, Purpleville]</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">Other algorithms are:</p> <p class="MsoNormal"> </p> <p class="MsoNormal">- quicksort (generally the fastest sort) </p> <p class="MsoNormal">- mergesort (another fast sort, with some other nice properties) </p> <p class="MsoNormal">- sieve method for finding prime numbers </p> <p class="MsoNormal">- linked lists (and other list implementations) </p> <p class="MsoNormal">- trees and common tree-traversals (prefix, infix, postfix) </p> <p class="MsoNormal">- balanced trees (AVL is the easiest one) </p> <p class="MsoNormal">- B-trees (important in file systems) </p> <p class="MsoNormal">- tries (these are cool, but pretty specialized) </p> <p class="MsoNormal">- Breadth-First Search and Depth-First Search for graph traversal</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Chapter 19: Files</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">In java, writing and reading from files is very useful for storing information such as usernames, passwords, names, numbers, email addresses, etc. To do this we create a file, open it, read/write to it and close the file:</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Creating a file in java:</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">import java.io.*;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class CreateFile{</p> <p class="MsoNormal"> public static void main(String[] args) throws IOException{</p> <p class="MsoNormal"> File f;</p> <p class="MsoNormal"> f=new File("myfile.txt");</p> <p class="MsoNormal"> if(!f.exists()){</p> <p class="MsoNormal"> f.createNewFile();</p> <p class="MsoNormal"> System.out.println("New file \"myfile.txt\" has been created </p> <p class="MsoNormal"> to the current directory");</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">In the above program, we create a new file called “myfile.txt” by checking if the file exists or now, if it does not exist, we use the .createNewFile() function.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Reading from a file</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">import java.io.*;</p> <p class="MsoNormal">class FileRead </p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static void main(String args[])</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> try{</p> <p class="MsoNormal"> // Open the file that is the first </p> <p class="MsoNormal"> // command line parameter</p> <p class="MsoNormal"> FileInputStream fstream = new FileInputStream("textfile.txt");</p> <p class="MsoNormal"> // Get the object of DataInputStream</p> <p class="MsoNormal"> DataInputStream in = new DataInputStream(fstream);</p> <p class="MsoNormal"> BufferedReader br = new BufferedReader(new InputStreamReader(in));</p> <p class="MsoNormal"> String strLine;</p> <p class="MsoNormal"> //Read File Line By Line</p> <p class="MsoNormal"> while ((strLine = br.readLine()) != null) {</p> <p class="MsoNormal"> // Print the content on the console</p> <p class="MsoNormal"> System.out.println (strLine);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> //Close the input stream</p> <p class="MsoNormal"> in.close();</p> <p class="MsoNormal"> }catch (Exception e){//Catch exception if any</p> <p class="MsoNormal"> System.err.println("Error: " + e.getMessage());</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">In the above program we read text from a file by using:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> A FileInputStream -- allows the user to access the file</p> <p class="MsoNormal"> A DataInputStream -- allows the data within the file to be transferred</p> <p class="MsoNormal"> A BufferedReader -- reads the text within the file</p> <p class="MsoNormal"> </p> <p class="MsoNormal">If the ‘textfile.txt’ had the text:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> This</p> <p class="MsoNormal"> Is</p> <p class="MsoNormal"> The</p> <p class="MsoNormal"> Text </p> <p class="MsoNormal"> </p> <p class="MsoNormal">The output would be:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> This</p> <p class="MsoNormal"> Is</p> <p class="MsoNormal"> The</p> <p class="MsoNormal"> Text </p> <p class="MsoNormal"> </p> <p class="MsoNormal">This is because the reader reads line-by-line, not all together at once.</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">Writing to a file</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal"> import java.io.*;</p> <p class="MsoNormal">class FileWrite </p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static void main(String args[])</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> try{</p> <p class="MsoNormal"> // Create file </p> <p class="MsoNormal"> FileWriter fstream = new FileWriter("out.txt");</p> <p class="MsoNormal"> BufferedWriter out = new BufferedWriter(fstream);</p> <p class="MsoNormal"> out.write("Hello Java");</p> <p class="MsoNormal"> //Close the output stream</p> <p class="MsoNormal"> out.close();</p> <p class="MsoNormal"> }catch (Exception e){//Catch exception if any</p> <p class="MsoNormal"> System.err.println("Error: " + e.getMessage());</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal">In the above program, we write “Hello Java” to the file “out.txt”. To do this, we use:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> A FileWriter -- allows for access to the file</p> <p class="MsoNormal"> A BufferedWriter -- writes what we want it to in the file</p> <p class="MsoNormal"> </p> <p class="MsoNormal">If we combined this program with the file reader the output would be:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> Hello Java</p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">User input and files</p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">In java, you can use a scanner to write to a file:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> import java.io.*;</p> <p class="MsoNormal"> import java.util.*;</p> <p class="MsoNormal"> </p> <p class="MsoNormal">class ScannerFileWrite </p> <p class="MsoNormal">{</p> <p class="MsoNormal"> public static void main(String args[])</p> <p class="MsoNormal"> {</p> <p class="MsoNormal"> try{</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> String text;</p> <p class="MsoNormal"> System.out.print("Enter Text: ");</p> <p class="MsoNormal"> text = scan.next();</p> <p class="MsoNormal"> // Create file </p> <p class="MsoNormal"> FileWriter fstream = new FileWriter("out.txt");</p> <p class="MsoNormal"> BufferedWriter out = new BufferedWriter(fstream);</p> <p class="MsoNormal"> out.write(text);</p> <p class="MsoNormal"> //Close the output stream</p> <p class="MsoNormal"> out.close();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.println(text+ " " + "is now in file: out.txt");</p> <p class="MsoNormal"> }catch (Exception e){//Catch exception if any</p> <p class="MsoNormal"> System.err.println("Error: " + e.getMessage());</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">The above program takes user input and adds it to the file out.txt.</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p align="center" class="MsoNormal" style="text-align:center">Procedure 4: Login System</p> <p class="MsoNormal">In this procedure we will be using files and algorithms to make an advanced login system:</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center"> </p> <p class="MsoNormal">import java.io.BufferedReader;</p> <p class="MsoNormal">import java.io.BufferedWriter;</p> <p class="MsoNormal">import java.io.DataInputStream;</p> <p class="MsoNormal">import java.io.File;</p> <p class="MsoNormal">import java.io.FileInputStream;</p> <p class="MsoNormal">import java.io.FileWriter;</p> <p class="MsoNormal">import java.io.IOException;</p> <p class="MsoNormal">import java.io.InputStreamReader;</p> <p class="MsoNormal">import java.util.Scanner;</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal">public class LoginSystem {</p> <p class="MsoNormal"> static File usernames,passwords;</p> <p class="MsoNormal"> public static void main(String[] args) throws IOException{</p> <p class="MsoNormal"> run();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> private static void run() throws IOException {</p> <p class="MsoNormal"> usernames=new File("usernames.txt");</p> <p class="MsoNormal"> passwords = new File("passwords.txt");</p> <p class="MsoNormal"> if(!usernames.exists()){</p> <p class="MsoNormal"> try {</p> <p class="MsoNormal"> usernames.createNewFile();</p> <p class="MsoNormal"> } catch (IOException e) {</p> <p class="MsoNormal"> e.printStackTrace();//prints error</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> if (!passwords.exists()){</p> <p class="MsoNormal"> try{</p> <p class="MsoNormal"> passwords.createNewFile();</p> <p class="MsoNormal"> }catch(IOException e){</p> <p class="MsoNormal"> e.printStackTrace();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> private static void menu() throws IOException {</p> <p class="MsoNormal"> Scanner Scan = new Scanner(System.in);</p> <p class="MsoNormal"> System.out.print("*********************************\n");</p> <p class="MsoNormal"> System.out.print("* *\n");</p> <p class="MsoNormal"> System.out.print("* *\n");</p> <p class="MsoNormal"> System.out.print("* Menu *\n");</p> <p class="MsoNormal"> System.out.print("* *\n");</p> <p class="MsoNormal"> System.out.print("* 1.Login *\n");</p> <p class="MsoNormal"> System.out.print("* 2.Register *\n");</p> <p class="MsoNormal"> System.out.print("* 3.Exit *\n");</p> <p class="MsoNormal"> System.out.print("*********************************\n");</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> int choice = Scan.nextInt();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if(choice == 1){</p> <p class="MsoNormal"> login();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if (choice == 2){</p> <p class="MsoNormal"> register();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else if (choice == 3){</p> <p class="MsoNormal"> System.exit(0);</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else{</p> <p class="MsoNormal"> menu();</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> private static void login() throws IOException {</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> FileInputStream fstream = new FileInputStream("usernames.txt");</p> <p class="MsoNormal"> DataInputStream in = new DataInputStream(fstream);</p> <p class="MsoNormal"> BufferedReader br = new BufferedReader(new InputStreamReader(in));</p> <p class="MsoNormal"> FileInputStream stream = new FileInputStream("passwords.txt");</p> <p class="MsoNormal"> DataInputStream inp = new DataInputStream(stream);</p> <p class="MsoNormal"> BufferedReader brl = new BufferedReader(new InputStreamReader(inp));</p> <p class="MsoNormal"> String strLine;</p> <p class="MsoNormal"> String str;</p> <p class="MsoNormal"> while ((strLine = br.readLine()) != null){</p> <p class="MsoNormal"> while((str = brl.readLine()) != null){</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> System.out.print("Enter Username: ");</p> <p class="MsoNormal"> String user = scan.next();</p> <p class="MsoNormal"> System.out.print("Enter Password: ");</p> <p class="MsoNormal"> String pass = scan.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> if (strLine.equalsIgnoreCase(user) && str.equalsIgnoreCase(pass)){</p> <p class="MsoNormal"> System.err.print("Logged in. Congratulations on finishing this book!"); </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> else{</p> <p class="MsoNormal"> login(); </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> in.close();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> private static void register() {</p> <p class="MsoNormal"> try{</p> <p class="MsoNormal"> Scanner scan = new Scanner(System.in);</p> <p class="MsoNormal"> String user,pass;</p> <p class="MsoNormal"> System.out.print("Enter Desired Username: ");</p> <p class="MsoNormal"> user = scan.next();</p> <p class="MsoNormal"> System.out.print("Enter Desired Password: ");</p> <p class="MsoNormal"> pass = scan.next();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> FileWriter fstream = new FileWriter("usernames.txt");</p> <p class="MsoNormal"> BufferedWriter out = new BufferedWriter(fstream);</p> <p class="MsoNormal"> out.write(user);</p> <p class="MsoNormal"> out.close();</p> <p class="MsoNormal"> FileWriter stream = new FileWriter("passwords.txt");</p> <p class="MsoNormal"> BufferedWriter write = new BufferedWriter(stream);</p> <p class="MsoNormal"> write.write(pass);</p> <p class="MsoNormal"> write.close();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> System.out.println("Username: " + user + " " + "Password: "+ pass);</p> <p class="MsoNormal"> login();</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }catch (Exception e){//Catch exception if any</p> <p class="MsoNormal"> System.err.println("Error: " + e.getMessage());</p> <p class="MsoNormal"> }</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p class="MsoNormal"> }</p> <p class="MsoNormal">}</p> <p class="MsoNormal"> </p> <p class="MsoNormal"> </p> <p align="center" class="MsoNormal" style="text-align:center">24.182.108.156!!! End Of Unit 4 !!!24.182.108.156</p> <p align="center" class="MsoNormal" style="text-align:center"> </p>

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.