-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjava.html
More file actions
executable file
·305 lines (273 loc) · 27.4 KB
/
java.html
File metadata and controls
executable file
·305 lines (273 loc) · 27.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Programming - Java</title>
<meta name="description" content="Programming Tutorial Website">
<meta name="author" content="Chris Munley and Aiden Trainer">
<link rel="stylesheet" type="text/css" href="style.css">
<link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500" rel="stylesheet">
<script src="menu.js"></script>
</head>
<body>
<div id="header">
<div id="logo"><img src="img/logo.png" alt="Logo"></div>
<div id="author">by Chris Munley & Aiden Trainer</div>
<div id="navbar">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="java.html" class="selected">Java</a></li>
<li><a href="cpp.html">C++</a></li>
</ul>
</div>
</div>
<div id="body">
<div id="menu">
<div class="section">Intro</div>
<ul>
<li class="mi selected" onclick="window.location='#intro'">What is Java</li>
<li class="mi" onclick="window.location='#starting'">Getting Started</li>
</ul>
<div class="section">Basics</div>
<ul>
<li class="mi selected" onclick="window.location='#helloworld'">Hello World</li>
<li class="mi" onclick="window.location='#comments'">Comments</li>
<li class="mi" onclick="window.location='#variables'">Variables</li>
<li class="mi" onclick="window.location='#operators'">Operators</li>
<li class="mi" onclick="window.location='#review1'">Review 1</li>
</ul>
<div class="section">Decision Making</div>
<ul>
<li class="mi" onclick="window.location='#decisionbasics'">Basics</li>
<li class="mi" onclick="window.location='#if'">if</li>
<li class="mi" onclick="window.location='#else'">else and else if</li>
<li class="mi" onclick="window.location='#switch'">Switches</li>
<li class="mi" onclick="window.location='#review2'">Review 2</li>
</ul>
<div class="section">Iteration</div>
<ul>
<li class="mi" onclick="window.location='#iterationbasics'">Basics</li>
<li class="mi" onclick="window.location='#for'">for</li>
<li class="mi" onclick="window.location='#while'">while and do...while</li>
<li class="mi" onclick="window.location='#controlstatements'">Control Statements</li>
<li class="mi" onclick="window.location='#review3'">Review 3</li>
</ul>
</div>
<div id="page">
<div id="intro">
<h1>What is Java</h1>
<p>Java is a general-purpose, object-oriented programming language developed by James Gosling in the mid 1990s while he was working at Sun Microsystems. One main feature of java that sets it apart from others is that it is machine-independent, meaning that a Java program can be written and run on any platform (Windows, Mac, Linux, etc). Java is capable of running on any platform because a compiler is used to run it.</p>
<p>The compiler takes java code and converts it into a code known as bytecode. The compiler also checks for syntax errors. If there are no errors, the compiler generates a new program based on your java code that is readable by a Java virtual machine (JVM). The JVM is where the Java program actually runs, since it translates the bytecode into machine code. JVM is part of the Java Runtime Environment(JRE). <div class="block yellow">It is not extremely important to understand compilers, bytecode, the JVM, or the JRE right now, but it is always good to know what you are actually working with.</div>Now that you know what Java is, let's jump right into it!</p>
</div>
<div id="starting">
<h1>Getting started</h1>
<p>What do I need to start making my own Java programs? Well, you need somewhere to write, compile, and run code. For this, you have a few options. The best place to program in Java is in an IDE. An IDE, or Integrated Development Environment, is a simple piece of software that aids you in writing code.<div class="block blue">A few great IDE's include Eclipse, IntelliJ, NetBeans. You cant go wrong with any of these.</div> An alternative to downloading an IDE is to go to <a href="repl.it/languages/java">repl.it/languages/java</a>, which basically serves as an online IDE where you can write, compile and run code. This option does not have as many features to aid you in writing code, but it will run code just the same. Once you have an IDE or a similar alternative, you are ready to learn Java!</p>
</div>
<div class="separator"></div>
<br>
<div id="helloworld">
<h1>Hello World</h1>
<p>A hello world program is traditionally the first computer program that a beginning Java programmer writes.</p>
<div class="block code">
<pre style="color:#000020;"><span style="color:#200080; font-weight:bold; ">public</span> <span style="color:#200080; font-weight:bold; ">class</span> HelloWorld <span style="color:#406080; ">{</span><br> <span style="color:#200080; font-weight:bold; ">public</span> <span style="color:#200080; font-weight:bold; ">static</span> <span style="color:#7779bb; ">void</span> main<span style="color:#308080;">(</span><span style="color:#6679aa; font-weight:bold; ">String</span><span style="color:#308080; ">[</span><span style="color:#308080; ">]</span> args<span style="color:#308080; ">)</span> <span style="color:#406080; ">{</span><br> <span style="color:#6679aa; font-weight:bold; ">System</span><span style="color:#308080; ">.</span>out<span style="color:#308080; ">.</span>println<span style="color:#308080; ">(</span><span style="color:#1060b6; ">"Hello World!"</span><span style="color:#308080; ">)</span><span style="color:#406080; ">;</span><br> <span style="color:#406080;">}</span><br><span style="color:#406080;">}</span></pre>
</div>
<p>To begin writing a Java program, you must first make a class. This is done with "public class HelloWorld { }". In order to run a Java program, there must be a main method. "public static void main(String[] args) { }" is how you create a main method. Don't worry too much about what each term means now, we will go over that in depth later.</p>
<div class="block blue"><p>To print something to the console in a Java program, you must use either "System.out.println()" or "System.out.print()". The only difference between the two is that the latter does not create a new line after printing whatever is inside the parentheses.</p></div>
</div>
<div id="comments">
<h1>Comments</h1>
<p>Before we get into specific details about java, we should talk about a good programming practice: commenting. Commenting your code is an essential part of being a good programmer. If you want to come back to your work later in time, you will be glad you used comments, as it will make your code so much easier to understand. If you ever collaborate with people in the future, you are going to want them to be able to understand your code. You can guide their thinking with comments. However, you shouldn't overcomment your code, as it can distract from the code and make harder. Only put comments where necessary and where it won't distract anyone. <div class="block green">Comments are ignored by the compiler; they are solely for the programmer.</div>Inline comments in Java are created with "//". Inline means that only the line that // is on will be commented out. Also, only things that come after // will be ignored by the compiler, and stuff that comes before it on the same line will be read as regular code.<br>The syntax for multi line comments in Java is "/* comments go here, across multiple lines */". You can start and close a comment section with /* and */. Anything in between these will be ignored. These type of comments can span across many lines.
<div class="block code">
<pre style='color:#000000;background:#ffffff;'><span style='color:#3f7f59; '>// this is a single line comment</span><br><br>int i = 5; <span style='color:#3f7f59; '>// this is a comment after some code</span><br><br><span style='color:#3f7f59; '>/* </span><br><span style='color:#3f7f59; '>    This is a multi line comment</span><span style='color:#3f7f59; '><br>    int i = 0; this code is ignored, will not affect the program</span><br><span style='color:#3f7f59; '>*/</span></pre>
</div>
<div class="block yellow">In many IDEs, if you highlight some text and hit "ctrl+/", all of the text will be commented out for you so you don't have to do it by hand. This is useful if you want to see how the code runs when you take out some code, but you don't wan't to remove the code for good.</div> </p>
</div>
<div id="variables">
<h1>Variables</h1>
<p>
Variables store data that can be used later. Each variable is given a name, or an identifier, that explains what the variable holds. Each variable has a type: <ul><li>int: stores whole numbers only ex. 12, 156, 8722</li><li>double: stores floating point numbers (decimals) and whole numbers ex. 3.1415, -2.0, 3.5</li><li>String: stores text such as "Hello World!". Text string are enclosed in double quotes.</li><li>boolean: stores true or false values only</li><li>char: char stands for character, and it holds only one character. chars are surrounded in single quotes.</li></ul>
<div class="block code"><pre style='color:#000000;background:#ffffff;'>String name = "chris";<br><br>int grade = 12;<br><br>double gpa = 4.6;<br><br>boolean goodAtJava = true;<br><br>char grade = 'A';</pre></div>
<div class="block green">You can declare multiple variables of the same type on the same line: "int a = 5, b = 7, c = 9;" is competely valid.</div>
There are two general types of variable in Java: primitive and non-primitive. The main difference between these are the way data is stored. In a primitive variable, the data is stored directly in the variable, as you would imagine. With a non-primitive variable, the variable actually doesn't hold the data itself, but rather a reference to an object (they reference a specific memory location). Non-primitive data types are not defined by the programming language, but rather by the programmer, usually through classes. A non-primitive data type is usually just called an Object. Java has eight primitive data types: byte, short, int, long, char, boolean, double, and float.
<div class="block green">If a data type starts with a lower case letter (such as int, boolean, etc.), it is a primitive data type. Non-primitive type variables start with a captial letter (such as String).</div>
</p>
</div>
<div id="operators">
<h1>Operators</h1>
<p>
Java provides many arithmetic operators: +, -, *, /, and % perform addition, subtraction, multiplication, division, and modulo operations respectively. When using these math operations in java you must be aware of what type of variables you are working with, and how the operator will be interpreted.
</p>
<h3>'+' for Addition and Concatenation</h3>
<p>The + operator has two functions: to add numeric quantities and to concatenate strings. When + is used on two numbers, they will be added together. However when you use + on String variables it concatenates them, or puts them together.
<div class="block blue">
A very important thing to note about the + operator is that if only one of the two operands (things being added together) is a String and the other is a number, the number will be converted to and treated as a String. It follows that "System.out.println("Health: " + playerHealth);" would work as you would expect, and you don't have to put quotes around the variable name or if it was an actual number, since it is converted to a string.<br><br>System.out.println("30 + 50 = " 30 + 50); // this prints '30 + 50 = 3050'<br>If you wanted the + operator to work as a numerical operation, you could put () around 30 + 50 in the above print statement, since parentheses are treated with higher priority just as in a calculator or math class.
</div></p>
<h3>Subtraction and Multiplication</h3>
<p>The - operator actually serves two purposes as well; it can be used as a regular math operator to subtract two numbers, and it can also be used to make a number negative.<br><br>The * symbol is simply a multiplication operator, for example 10 * 10 = 100.</p>
<h3>Division with '/'</h3>
<p>
Division in Java is relatively straight forward. If you divide two integer numbers,the largerst whole number will be returned and any remainder will be forgotten. If one is a floating point, or decimal number, the result will be a decimal number. If you divide by zero in java, an exception will be thrown. The best way to learn what results you will get from dividing two numberic values is to go into your ide and test a bunch of situations out. You can only learn if you practice!
</p>
<h3>The modulus operator</h3>
<p>I think the modulus operator scares a lot of new programmers because they have probably never seen it in a calculator or math class before. Thhe modulus % simply returns the remainder of a division. For example, 7 % 3 returns 1, and 12 % 3 returns 0.<br><br>A common way to use the modulus operator is to check if a number is even or a multiple of something: for example, any number % 2 will return either 0 or 1. If 0, the number is divisible by 2 and is an even number, else it is an odd number.
</p>
</div>
<div id="review1">
<h1>Review 1</h1>
<ul>
<li>Java can run on any platform</li>
<li>Java is a general-purpose, object-oriented computer programming language</li>
<li>Java has thousands of built in classes that anyone can use</li>
<li>An integrated development environment is a piece of software that allows you to write programs more easily</li>
<li>HelloWorld is traditionally the first computer program that someone starting to learn Java writes</li>
<li>"System.out.println()" prints whatever is inside () to the console, followed by a new line</li>
</ul>
</div>
<div class="separator"></div>
<br>
<div id="decisionbasics">
<h1>Decision-making basics</h1>
<p>In this section we will discuss conditional statements which are the simplest way to make your program actually do more than one thing. They will make it so that your program can do something different than normal if a condition is satisfied.</p>
<p>There are 2 types of conditional statements in Java, as in many other high-level programming languages. They are:</p>
<ul>
<li>If statements</li>
<li>Switches</li>
</ul>
<div class="block blue">
If statement blocks can look for more than one condition at a time. This is done by the use of else and else if statments as well as nested ifs.
</div>
</div>
<div id="if">
<h1>if</h1>
<p>The if statement is the most basic conditional statement. If the condition is true, it will run the code inside the block. If the condition is not true, the code inside the block will not run. Take a look at this example:</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>int x = 10;<br><br>if( x < 20 ) {<br>    System.out.println("20 is greater than 10, so this will get printed");<br>}<br><br>if( x < 5 ) {<br>    <span style='color:#3f7f59; '>// any code here will NOT run because 5 is not greater than 10</span><br>}</pre>
</div>
<p>A lonely if statement would be used when you do not want to do anything special when a condition is not met.</p>
</div>
<div id="else">
<h1>else and else if</h1>
<p>If you do want to something different when the condition is false, you can use an else statement.</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>if( x == 17 ) {<br>    System.out.println("x was 17");<br>} else {<br>    System.out.println("in every other case this would be printed instead");<br>}</pre>
</div>
<p>In the event that you have more than one condition to check as well as an "else" fallback, you can use an else if block</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>if( x == 17 ) {<br>    System.out.println("x was 17");<br>} else if( x == 18 ){<br>    System.out.println("x was 18");<br>} else {<br>    System.out.println("in every other case this would be printed instead");<br>}</pre>
</div>
<div class="block green">
Both if and else if can evaluate two conditions at a time using the && operator. For instance if( x < 10 && x > 8 ) { ... } will be true for a number like 8.2 or 9 but not 7.6 or 10.
</div>
</div>
<div id="switch">
<h1>Switches</h1>
<p>A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.</p>
<div class="block red">
A switch will continue looking even after it has found a match! You can use the optional break statements to stop this from happening.
</div>
<p>Switches in programming in general are a bit of a misnomer. You have to think of them a lot more like multi-track railway switches, rather than light switches, as there are usually a lot more cases than just 2.</p>
<p>Also, it is worth bearing in mind that switches can only be used when you know exactly what sort of values you expect; they are no good for evaluating if a value has a certain property. For instance, they will happily check if your string is "hello world", but will me useless for checking if your string has 11 characters.</p>
<p>The "default" case will be used if no other match was found.</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>switch(name) {<br>    case "Chris" :<br>        System.out.println("Awesome name Chris!");<br>    case "Aidan" :<br>        System.out.println("Nice one Aidan!");<br>    default :<br>        System.out.println("You have some other name.");<br>}</pre>
</div>
</div>
<div id="review2">
<h1>Review 2</h1>
<ul>
<li>Conditional statements are the easiest way to make your application do something based of varing input.</li>
<li>Java has 2 types of conditional statements; if-elseif-else blocks and switches.</li>
<li>If statements can be used to determine if a condition is true.</li>
<li>Switches can be used to check if a value is the same as something else.</li>
</ul>
</div>
<div class="separator"></div>
<br>
<div id="iterationbasics">
<h1>Iteration basics</h1>
<p>Iteration in programming is the running a computer program for a defined number of repetitions. A block of statements is said to be iterated; you can also refer to that block of statements as one "iteration".</p>
<div class="block green">
"Iteration" is just another word for looping.
</div>
<p>Using a loop is very useful when you want to perform a certain operation or code segment a specific number of times or until something changes. They are also commonly used when traversing arrays.</p>
<p>A good way to know that you should be using iteration is if you find yourself rewriting or copying and pasting a large amount of code in one place. This common rookie mistake often takes place when printing, manipulating strings and arrays as well as mathematical operations,</p>
<p>There are 3 common types of loops:</p>
<ul>
<li>for loops - most common</li>
<li>while loops</li>
<li>do...while loops - least common, though useful sometimes</li>
</ul>
<p>Let's take a deeper look at each one and how and when to use them.</p>
</div>
<div id="for">
<h1>for</h1>
<p>The for loop is typically used where we already know how many iterations we will require. In Java, like many other languages, the for loop consists of three parts in the head of the loop. They are, in the order the are used:</p>
<ul>
<li>intialization</li>
<li>condition</li>
<li>incrementation</li>
</ul>
<p>Here are all the parts in their correct places in the loop:</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>for(intialization; condition; incrementation) {<br>    <span style='color:#3f7f59; '>// do something</span><br>}</pre>
</div>
<p>Let's break that down in a worked example.</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>for(int i = 1; i < 10; i++) {<br>    System.out.println( i );<br>}</pre>
</div>
<div class="block blue">
i++ means the same thing as i = i + 1
</div>
<p>In the intialization step we create our iterator, the (in this case) number that we will use to determine how many time we have already exectured the code in the loop.</p>
<p>The condition part works a lot like a conditional statemtent that we discussed in section 2. The only difference is that the condition will be checked against every time you go round the loop. At the point where the condition is not resolved as true, no more code inside the loop will use run.</p>
<p>The incrementation will increment the iterator</p>
<div class="block green">
You can also use decrimentation by using a negative increment like i-- which is the same as i = i - 1.
</div>
<p>In this example out iterator is the variable i, and we want to keep going round the loop as long as 10 is greater than i, adding 1 to i every time. Meanwhile, on every iteration we simply want to print out i. This example will result in the output of "123456789".</p>
<div class="block yellow">
See if you can try to make a program that print out all odd numbers smaller than 40. Hint: you might need to use i = i + 2 somewhere in your incrementation.
</div>
</div>
<div id="while">
<h1>while and do...while</h1>
<p>While loops are very well explain buy simply taking their name at face value. You will often find that programming concepts become quite easy to grasp if you say exactly what they do in english. "While a condition is true, run some code". It's that simple; while loops are almost like glorified if statements. Let's take a look at how they work in practice.</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>int i = 1;<br><br>while(i < 10) {<br>    System.out.println( i );<br>    i++;<br>}</pre>
</div>
<p>This example will output the exact same thing as the example in the for loop section; "123456789".</p>
<p>You will notice that in a while loop you are expected to do the intialization and incrementation on your own.</p>
<p>The main learning point on while loops is that the loop might never run. When the expression is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.</p>
<p>Let's investigate the use of do...while loops which in contrast are guaranteed to run at least once. All a do...while loop is is a a flipped while loop. Here is the same example:</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>int i = 1;<br><br>do {<br>    System.out.println( i );<br>    i++;<br>}while (i < 10);</pre>
</div>
<div class="block red">
Be very careful when using do...while loops as they are very prone to <a href="https://en.wikipedia.org/wiki/Off-by-one_error">off-by-one errors</a>, if you are not familiar with their inverted nature.
</div>
</div>
<div id="controlstatements">
<h1>Control Statements</h1>
<p>There are two control statements related to iteration in Java. They are:</p>
<ul>
<li>break</li>
<li>continue</li>
</ul>
<p>Break and continue will be familiar to many programmers. Break exits a for, while, or switch structure. Continue exits the current pass in the loop and begins with the next. Their correct usage is as follows:</p>
<div class="block code">
<pre style='color:#000000;background:#ffffff;'>break;<br><br>continue;</pre>
</div>
</div>
<div id="review3">
<h1>Review 3</h1>
<ul>
<li>You can use interation to consisely perform repetative tasks.</li>
<li>There are 3 types of loops: for, while and do...while.</li>
<li>Pay attention to off-by-one errors, specifically by ensuring you use the right condition for the loop.</li>
<li>Control statements like break and continue can help you navigate a loop in a non-standard way.</li>
</ul>
</div>
</div>
</div>
</body>
</html>