Upcoming SlideShare
×

Programming with Java: the Basics

4,776 views
4,570 views

Published on

8 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

Views
Total views
4,776
On SlideShare
0
From Embeds
0
Number of Embeds
119
Actions
Shares
0
360
0
Likes
8
Embeds 0
No embeds

No notes for slide

Programming with Java: the Basics

1. 1. Programming with Java Jussi Pohjolainen Tampere University of Applied Sciences
2. 2. HELLOWORLD
3. 3. HelloWorld.java <ul><li>public class HelloWorld { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>System.out.println(&quot;Hello World&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
4. 4. BOOLEAN ALGEBRA AND CONDITIONS
5. 5. About Conditions <ul><li>Conditions are used in while and in if – sentences </li></ul><ul><ul><li>if(condition) </li></ul></ul><ul><ul><ul><li>do something </li></ul></ul></ul><ul><li>Condition is a statement that is either true or false </li></ul>
6. 6. AND <ul><li>if(it is raining AND car does not work) </li></ul><ul><ul><li>go to work by bus </li></ul></ul><ul><li>In Java, the AND is marked with && </li></ul><ul><ul><li>if(x >= 4 && x<=10) </li></ul></ul><ul><ul><ul><li>do something </li></ul></ul></ul>
7. 7. AND A B A && B 1 1 1 1 0 0 0 1 0 0 0 0
8. 8. OR <ul><li>if(it is raining OR car does not work) </li></ul><ul><ul><li>Go to work by bus </li></ul></ul><ul><li>In Java OR is marked with || </li></ul><ul><ul><li>if(x == 3 || x == 10) </li></ul></ul><ul><ul><ul><li>do something </li></ul></ul></ul>
9. 9. OR A B A && B 1 1 1 1 0 1 0 1 1 0 0 0
10. 10. Negation <ul><li>Negation turns true to false and wiseversa </li></ul><ul><li>In Java, negation is marked with ! </li></ul><ul><li>if(!rains) </li></ul><ul><ul><li>go to work by bicycly </li></ul></ul><ul><li>if(!(x < 3)) </li></ul><ul><ul><li>do something </li></ul></ul>
11. 11. Negation A !A 1 0 0 1
12. 12. Combining Conditions <ul><li>if(!rains && (temperature > 20C)) </li></ul><ul><ul><li>Walk with your t-shirt on </li></ul></ul><ul><li>Demos </li></ul><ul><ul><li>Conditions.java </li></ul></ul><ul><ul><li>BooleanAlgebra.java </li></ul></ul>
13. 13. PRIMITIVE TYPES
14. 14. About Variables <ul><li>Simple calculator with pseudocode </li></ul><ul><ul><li>print &quot;Give number&quot; </li></ul></ul><ul><ul><li>a := readInput() </li></ul></ul><ul><ul><li>print &quot;Give another number&quot; </li></ul></ul><ul><ul><li>b := readInput() </li></ul></ul><ul><ul><li>sum := a + b </li></ul></ul><ul><ul><li>print sum </li></ul></ul><ul><li>Variables? a, b and sum! </li></ul>
15. 15. Declaring Variables <ul><li>In Java you have to declare a variable before using it </li></ul><ul><li>Declaring? </li></ul><ul><ul><li>What is the variable's name? </li></ul></ul><ul><ul><li>What is the variable's type? </li></ul></ul><ul><li>Type? </li></ul><ul><ul><li>What kind of information will be stored into the variable? </li></ul></ul>
16. 16. Declaring Variables in Pseudocode <ul><li>Integer age </li></ul><ul><li>print &quot;Your age?&quot; </li></ul><ul><li>age := readInput() </li></ul><ul><li>print age; </li></ul>
17. 17. Types <ul><li>Java has two kind of types </li></ul><ul><ul><li>Primitive Types </li></ul></ul><ul><ul><ul><li>int, byte, short, long, double, float, boolean, char </li></ul></ul></ul><ul><ul><li>Class Types </li></ul></ul><ul><ul><ul><li>Everything else, for example </li></ul></ul></ul><ul><ul><ul><ul><li>String, Scanner, Arrays, Vector, JButton, JCheckBox </li></ul></ul></ul></ul>
18. 18. Primitive Types in Java type size Example value byte 8 bit 5 short 16 bit 10000 int 32 bit 200000 long 64 bit 30000000 float 32 bit 1.1234 double 64 bit 1.23487367 boolean 1 bit (undefined) true or false char 16 bit 'a'
19. 19. Declaring Variables with Java <ul><li>Examples </li></ul><ul><ul><li>int number; </li></ul></ul><ul><ul><li>float weight; </li></ul></ul><ul><ul><li>char mycharacter; </li></ul></ul><ul><li>You can declare and set the variable </li></ul><ul><ul><li>char mycharacter = 'a'; </li></ul></ul><ul><li>You can assign a different value to variable after declaring with the = </li></ul>
20. 20. Declaring Variables with Java <ul><li>Declare variable only once! </li></ul><ul><ul><li>int x = 5; </li></ul></ul><ul><ul><li>x = 10; </li></ul></ul><ul><ul><li>System.out.println(x); // prints 10 </li></ul></ul><ul><li>This is wrong! </li></ul><ul><ul><li>int x = 5; </li></ul></ul><ul><ul><li>int x = 10; // Variable already declared! </li></ul></ul><ul><ul><li>System.out.println(x); </li></ul></ul>
21. 21. Final Variable <ul><li>Final variable is a special variable which value cannot be assigned later </li></ul><ul><ul><li>final double PI = 3.14; </li></ul></ul><ul><ul><li>PI = 5.0; // Does not work! </li></ul></ul>
22. 22. Examples <ul><li>int age, shoeSize; </li></ul><ul><li>boolean gender; </li></ul><ul><li>char myCharacter = 'k'; </li></ul><ul><li>double average = 7.7; </li></ul>
23. 23. TYPE CASTING
24. 24. Type Casting? <ul><li>class MyApp { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>int a = 5; </li></ul><ul><li>short b = a; </li></ul><ul><li>System.out.println(b); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>TB308POHJUS-L-2:temp pohjus\$ javac MyApp.java </li></ul><ul><li>MyApp.java:4: possible loss of precision </li></ul><ul><li>found : int </li></ul><ul><li>required: short </li></ul><ul><li>short b = a; </li></ul><ul><li>^ </li></ul><ul><li>1 error </li></ul>
25. 25. Solution <ul><li>class MyApp { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>int a = 5; </li></ul><ul><li>short b = (short) a; </li></ul><ul><li>System.out.println(b); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
26. 26. Why? <ul><li>class MyApp { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>int a = 5; </li></ul><ul><li>long b = 5; </li></ul><ul><li>int result = a * b; </li></ul><ul><li>System.out.println(result); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>MyApp.java:5: possible loss of precision </li></ul><ul><li>found : long </li></ul><ul><li>required: int </li></ul><ul><li>int result = a * b; </li></ul><ul><li>^ </li></ul><ul><li>1 error </li></ul>
27. 27. Why? <ul><li>int a = 5; </li></ul><ul><li>long b = 5; </li></ul><ul><li>int result = a * b; </li></ul>int * long -> long!
28. 28. Example Result of different Calculations Operand Operator Operand Result int + / * - int int long + / * - int, short, long, byte long double + / * - float double double + / * - double double float + / * - float float double + / * - int, short, long, byte double
29. 29. What is the result? <ul><li>double a = 5; </li></ul><ul><li>int b = 5; </li></ul><ul><li>double result = a / b; </li></ul>double / int -> double
30. 30. What is the result? <ul><li>int a = 5; </li></ul><ul><li>int b = 5; </li></ul><ul><li>double result = a / b; </li></ul>int / int -> int !!!
31. 31. Solution <ul><li>int a = 5; </li></ul><ul><li>int b = 5; </li></ul><ul><li>double result = (double) a / b; </li></ul>
32. 32. VISIBILITY OF VARIABLES
33. 33. What is the problem? <ul><li>import java.util.Scanner; </li></ul><ul><li>class MyApp { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Scanner input = new Scanner(System.in); </li></ul><ul><li>int inputVariable; </li></ul><ul><li>inputVariable = input.nextInt(); </li></ul><ul><li>if(inputVariable == 7) { </li></ul><ul><li>int myVariable = 80; </li></ul><ul><li>} </li></ul><ul><li>System.out.println(myVariable); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul><ul><li>TB308POHJUS-L-2:temp pohjus\$ javac MyApp.java </li></ul><ul><li>MyApp.java:15: cannot find symbol </li></ul><ul><li>symbol : variable myVariable </li></ul><ul><li>location: class MyApp </li></ul><ul><li>System.out.println(myVariable); </li></ul><ul><li>^ </li></ul><ul><li>1 error </li></ul><ul><li>TB308POHJUS-L-2:temp pohjus\$ </li></ul>
34. 34. Braces and Variable visibility <ul><li>Variable is visible in it's section(braces) and it's child sections </li></ul><ul><li>Variable is not visible outside of it's section. </li></ul><ul><li>This works: </li></ul><ul><ul><li>int a = 1; </li></ul></ul><ul><ul><li>if(true) { </li></ul></ul><ul><ul><li>System.out.println(a); </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>This does not: </li></ul><ul><ul><li>if(true) { </li></ul></ul><ul><ul><li>int b = 1; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><ul><li>System.out.println(b); </li></ul></ul>
35. 35. Braces? <ul><li>If control statement (if, while, for) contains only one statement you do NOT have to use braces </li></ul><ul><ul><li>if(something) </li></ul></ul><ul><ul><ul><li>doSomething </li></ul></ul></ul>
36. 36. CLASS - TYPES
37. 37. Types <ul><li>Java has two kind of types </li></ul><ul><ul><li>Primitive Types </li></ul></ul><ul><ul><ul><li>int, byte, short, long, double, float, boolean, char </li></ul></ul></ul><ul><ul><li>Class Types </li></ul></ul><ul><ul><ul><li>Everything else, for example </li></ul></ul></ul><ul><ul><ul><ul><li>String, Scanner, Arrays, Vector, JButton, JCheckBox </li></ul></ul></ul></ul>
38. 38. Differences <ul><li>Primitive Type </li></ul><ul><li>first letter lowercase </li></ul><ul><ul><li>int </li></ul></ul><ul><li>Initialized with value </li></ul><ul><ul><li>int x = 0; </li></ul></ul><ul><li>Does not have methods </li></ul><ul><li>Class Type </li></ul><ul><li>first letter uppercase </li></ul><ul><ul><li>Scanner </li></ul></ul><ul><li>Initialized with new </li></ul><ul><ul><li>Scanner x = new Scanner(); </li></ul></ul><ul><li>Does have methods </li></ul><ul><ul><li>x.nextInt(); </li></ul></ul>
39. 39. About String <ul><li>String is a class type with lot of exceptions compared to other class types. </li></ul><ul><li>Usually class types are initialized with new. In String you can initialize also with value </li></ul><ul><ul><li>String example = new String(&quot;Hello World&quot;); </li></ul></ul><ul><ul><li>String example = &quot;Hello World&quot;; </li></ul></ul>
40. 40. Class – type: String <ul><li>String m = &quot;hello&quot;; </li></ul><ul><li>System.out.println(m); </li></ul><ul><li>int length = m.length(); </li></ul><ul><li>String newVariable = m + &quot; world&quot;; </li></ul><ul><li>System.out.println(newVariable); </li></ul>
41. 41. Special Characters <ul><li> = tabulator </li></ul><ul><li> = enter </li></ul><ul><li>&quot; = &quot; </li></ul><ul><li>' = ' </li></ul>
42. 42. INPUT AND OUTPUT JAVA
43. 43. Output <ul><li>System.out is a stream that normally outputs the data you write to the console </li></ul><ul><li>Has different methods: </li></ul><ul><ul><li>print, without enter </li></ul></ul><ul><ul><li>println, with enter </li></ul></ul><ul><li>Usage </li></ul><ul><ul><li>System.out.println(&quot;Hello World!&quot;); </li></ul></ul><ul><ul><li>System.out.print(5); </li></ul></ul><ul><ul><li>System.out.println('a'); </li></ul></ul>
44. 44. Input <ul><li>System.in is an stream connected to keyboard input of console programs </li></ul><ul><li>Problem with System.in is that it can only read one byte at a time from the console. </li></ul><ul><li>If you want to read for example whole line of text, you have to use other classes.. </li></ul>
45. 45. Scanner and System.in <ul><li>Scanner – class and System.in provides easy access to keyboard input </li></ul><ul><li>You need to import the Scanner </li></ul><ul><ul><li>import java.util.Scanner; </li></ul></ul><ul><li>You have to define to Scanner, what stream to be used when reading </li></ul><ul><ul><li>Scanner sc = new Scanner(System.in); </li></ul></ul><ul><li>After creating the Scanner, you can read user input: </li></ul><ul><ul><li>int i = sc.nextInt(); </li></ul></ul>
46. 46. The use of Scanner <ul><li>import java.util.Scanner; </li></ul><ul><li>public class ScannerDemo { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>Scanner scanner = new Scanner(System.in); </li></ul><ul><li>String name; </li></ul><ul><li>int age; </li></ul><ul><li>System.out.println(&quot;Your name: &quot;); </li></ul><ul><li>name = scanner.nextLine(); </li></ul><ul><li>System.out.println(&quot;Your age: &quot;); </li></ul><ul><li>age = scanner.nextInt(); </li></ul><ul><li>System.out.println(&quot;Your name is &quot; + name); </li></ul><ul><li>System.out.println(&quot;Your age is &quot; + age); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
48. 48. COMMENTING CODE
49. 49. Commenting code <ul><li>Comments in code are intended for other programmers </li></ul><ul><li>Three different kind of comments </li></ul><ul><ul><li>One liner </li></ul></ul><ul><ul><li>Multiple lines </li></ul></ul><ul><ul><li>Javadoc </li></ul></ul>
50. 50. Example <ul><li>/* </li></ul><ul><li>This is my beautiful hello world application. </li></ul><ul><li>Made by Jussi </li></ul><ul><li>*/ </li></ul><ul><li>class MyApp { </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>// This prints Hello World </li></ul><ul><li>System.out.println(&quot;Hello World!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
52. 52. Javadoc Example <ul><li>/** </li></ul><ul><li>* Class that provides functionality for printing </li></ul><ul><li>* the &quot;Hello World&quot; String to the console. </li></ul><ul><li>* </li></ul><ul><li>* @author Jussi Pohjolainen </li></ul><ul><li>* @version 2009-10-26 </li></ul><ul><li>*/ </li></ul><ul><li>public class MyApp { </li></ul><ul><li>/** </li></ul><ul><li>* Starting point for the app </li></ul><ul><li>* </li></ul><ul><li>* @param args command line arguments </li></ul><ul><li>*/ </li></ul><ul><li>public static void main(String [] args) { </li></ul><ul><li>// This prints Hello World </li></ul><ul><li>System.out.println(&quot;Hello World!&quot;); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
53. 53. Result
54. 54. More Examples <ul><li>Javadoc slides </li></ul><ul><ul><li>http://home.tamk.fi/~pohjus/java/lectures/javadoc.html </li></ul></ul><ul><li>Java ME Project Works </li></ul><ul><ul><li>http://koti.tamk.fi/~t4hheina/mobiili1/ </li></ul></ul><ul><ul><li>http://koti.tamk.fi/~c5msalo/scorchedtamk/ </li></ul></ul><ul><ul><li>http://koti.tamk.fi/~c6tkoris/mobile/project/ </li></ul></ul><ul><ul><li>http://koti.tamk.fi/~c7msorvo/TsunamiGame/index.html </li></ul></ul>
55. 55. IF, SWITCH, WHILE, DO-WHILE, FOR
56. 56. If <ul><li>if(something) { </li></ul><ul><li>doSomething; </li></ul><ul><li>} </li></ul>
57. 57. if else <ul><li>if(something) { </li></ul><ul><li>doSomething; </li></ul><ul><li>} else { </li></ul><ul><li>doSomethingElse; </li></ul><ul><li>} </li></ul>
58. 58. if else if <ul><li>if(something1) { </li></ul><ul><li>doSomething1; </li></ul><ul><li>} else if(something2) { </li></ul><ul><li>doSomething2; </li></ul><ul><li>} </li></ul>
59. 59. if else if else <ul><li>if(something1) { </li></ul><ul><li>doSomething1; </li></ul><ul><li>} else if(something2) { </li></ul><ul><li>doSomething2; </li></ul><ul><li>} else { </li></ul><ul><li>doSomething3; </li></ul><ul><li>} </li></ul>
60. 60. if else if else if else <ul><li>if(something1) { </li></ul><ul><li>doSomething1; </li></ul><ul><li>} else if(something2) { </li></ul><ul><li>doSomething2; </li></ul><ul><li>} else if(something3) { </li></ul><ul><li>doSomething3 </li></ul><ul><li>} else { </li></ul><ul><li>doSomething4; </li></ul><ul><li>} </li></ul>
61. 61. Intro to Switch Case <ul><li>int a = 1; </li></ul><ul><li>if(a == 1) { </li></ul><ul><li>System.out.println(&quot;you gave one&quot;); </li></ul><ul><li>} else if(a == 2) { </li></ul><ul><li>System.out.println(&quot;you gave two&quot;); </li></ul><ul><li>} </li></ul>
62. 62. Switch Case (same than previous) <ul><li>switch(a) </li></ul><ul><li>{ </li></ul><ul><li>case 1: </li></ul><ul><li>System.out.println(&quot;you gave one&quot;); </li></ul><ul><li>break; </li></ul><ul><li>case 2: </li></ul><ul><li>System.out.println(&quot;you gave two&quot;); </li></ul><ul><li>break; </li></ul><ul><li>} </li></ul>
63. 63. Switch Case <ul><li>switch(a) </li></ul><ul><li>{ </li></ul><ul><li>case 1: </li></ul><ul><li>System.out.println(&quot;you gave one&quot;); </li></ul><ul><li>break; </li></ul><ul><li>case 2: </li></ul><ul><li>System.out.println(&quot;you gave two&quot;); </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>System.out.println(&quot;You did NOT give 1 or 2&quot;); </li></ul><ul><li>} </li></ul>
64. 64. Switch Case <ul><li>switch(a) </li></ul><ul><li>{ </li></ul><ul><li>case 1: </li></ul><ul><li>case 2: </li></ul><ul><li>System.out.println(&quot;you gave one or two&quot;); </li></ul><ul><li>break; </li></ul><ul><li>default: </li></ul><ul><li>System.out.println(&quot;You did NOT give 1 or 2&quot;); </li></ul><ul><li>} </li></ul>
65. 65. while <ul><li>int i = 0; </li></ul><ul><li>while(i < 5) { </li></ul><ul><li>System.out.println(i); </li></ul><ul><li>i = i + 1; </li></ul><ul><li>} </li></ul>
66. 66. while <ul><li>int i = 5; </li></ul><ul><li>while(i >= 0) { </li></ul><ul><li>System.out.println(i); </li></ul><ul><li>i = i - 1; </li></ul><ul><li>} </li></ul>
67. 67. while to for <ul><li>int i = 5; </li></ul><ul><li>while(i >= 0) { </li></ul><ul><li>System.out.println(i); </li></ul><ul><li>i = i - 1; </li></ul><ul><li>} </li></ul><ul><li>=> </li></ul><ul><li>for(int i=5; i>=0; i = i – 1) { </li></ul><ul><li>System.out.println(i); </li></ul><ul><li>} </li></ul>
68. 68. Incremental <ul><li>i = i + 1; </li></ul><ul><ul><li>i++; </li></ul></ul><ul><li>i = i – 1; </li></ul><ul><ul><li>i--; </li></ul></ul><ul><li>i = i + 2; </li></ul><ul><ul><li>i += 2; </li></ul></ul><ul><li>i = i – 2; </li></ul><ul><ul><li>i -= 2; </li></ul></ul>
69. 69. while to for <ul><li>for(int i=0; i<5; i++) { </li></ul><ul><li>System.out.println(i); </li></ul><ul><li>} </li></ul>
70. 70. do-while <ul><li>int i = 0; </li></ul><ul><li>do { </li></ul><ul><li>System.out.println(&quot;Hello&quot;); </li></ul><ul><li>i++; </li></ul><ul><li>} while(i < 3); </li></ul>
71. 71. EXAMPLES