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