Java Tutorial Ashkan Taslimi Saarland University Tutorial 3 September 6, 2011 1
Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements 2
Review Programming Style and Documentation Comments Good Naming Balanced Brackets Space and Indent
Review Primitive Data Types Kinds of values that can be stored and manipulated Integral Types: byte, int, long (-30, 0, 4) Floating-Point Types: float, double (3.14, 2.0, -0.9) Constants final double PI = 3.14159; Casting Large range types to a smaller range conversion Int intvalue = (int) 10.1;
Review Variables Variables have different names according to their location Static Variable Instance Variables Local Variable Parameter
Review Operators Concatenating (+) Example: String a = Winter ; int b = 2012; String c = a + Semester + b; System.out.println(c); // Winter Semester 2012
Review Operators Division operator ( / ) operates differently on integers and doubles Example: 5 / 9 = 0; 5 and 9 are considered as integer 5.0 / 9 = 0.55; 5.0 is double so the result will be double
Review Input/Output classes Console: Output: System.out.println(some String) Input: Scanner Class and next() method for String Dialog Box - JOptionPane class Output: showinputdialog method Input: showinputdialog method Exercise
Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements Format Specifier Loops 9
Access Level Modifiers Determine whether other classes can use a particular field or invoke a particular method. public class MyClass { private int gear = 0; public void changegear(int newvalue){ gear = newvalue;
Access Level Modifiers public modifier the field is accessible from all classes. private modifier the field is accessible only within its own class.
Access Level Modifiers Visibility of Alpha variables Modifier Alpha Beta Alphasub Gamma public Y Y Y Y private Y N N N
Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements Format Specifier Loops 13
Methods Return Type Parameter public static void main(string[] arguments) { System.out.println( Welcome ); Method Body
Adding Method public static void NAME() { STATEMENTS To call Method: NAME();
Example public static void printhello{ System.out.println( Hello ); public static void main(string[] args) { printhello();
Example public static void printhello{ System.out.println( Hello ); public static void main(string[] args) { printhello();
Parameters public static void NAME(TYPE NAME) { STATEMENTS To call: NAME(ARGUMENT);
Example public static void printword(string x){ System.out.println(x); public static void main(string[] arguments){ printword( Students ); printword( Teachers );
Example public static void printword(string x){ System.out.println(x); public static void main(string[] arguments){ printword( Students ); printword( Teachers );
Multiple Parameters [ ] NAME(TYPE NAME, TYPE NAME) { STATEMENTS To call: NAME(arg1, arg2);
Example public static void addnumber(int x, int y){ System.out.println(x + y); public static void main(string[] arguments){ addnumber(22, 6);
Example public static void addnumber(int x, int y){ System.out.println(x + y); public static void main(string[] arguments){ addnumber (22, 6);
Return Values public static TYPE NAME() { STATEMENTS return EXPRESSION; void means no type so no return
Example public static int addnumber(int x, int y){ return (x + y); public static void main(string[] arguments){ System.out.println(addNumber(22, 6));
Example public static int addnumber(int x, int y){ return (x + y); public static void main(string[] arguments){ System.out.println(addNumber(22, 6));
Variable Scope Variables live in the block ({) where they are defined (scope) Method parameters are like defining a new variable in the method
Invoking an Object s Method public class NAME{ public void METHOD(){ STATEMENT public static void main(string[] arguments){ NAME object = new NAME(); object. METHOD();
Example public class Calculator{ public static int addnumber(int x, int y){ return (x + y); public static void main(string[] arguments){ Calculator object = new Calculator(); object.addnumber(22, 6));
Mathematical Methods Math.sin(x) Math.random() Math.pow(2, 3) Math.sqrt(4) Exercise
Outline Tutorial 2 Review Access Level Modifiers Methods Selection Statements Format Specifier Loops 31
Selection Statements If-then Statement If-then-else Statement Switch Statement Conditional Expressions
If-then Statement
Example public static void main(string[] arguments){ test(3); test(5); test(7); public static void test(int number){ if (number > 5){ System.out.println(number + " is > 5");
If-then-else
if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else if (CONDITION) { STATEMENTS else { STATEMENTS else if
Example public static void main(string[] arguments){ test(4); public static void test(int x){ if (x > 5){ System.out.println(x + " is > 5"); else if (x == 5){ System.out.println(x + " equals 5"); else { System.out.println(x + " is < 5");
Switch Statement
Switch Syntax switch (switch-expression) { case value1: statement1; break; case value2: statement2; break; case valuen: statementn; break; default: statement(s)-for-default;
Switch Rules The switch-expression must yield a value of char, byte, short, or int type. The value1,..., and valuen must have the same data type as the value of the switch-expression. The keyword break is optional. The default case, which is optional. The case statements are checked in sequential order
Example switch (ch) { case 'a': System.out.println(ch); break; case 'b': System.out.println(ch); case 'c': System.out.println(ch); default: System.out.println( Not found! );
Conditional Expressions booleanexpression? expression1 : expression2; The result of this conditional expression is expression1 if booleanexpression is true; otherwise the result is expression2.
Example if (x > 0) y = 1; else y = -1; OR y = (x > 0)? 1 : -1; Exercise