Προγραμματισμός σε Java

download Προγραμματισμός σε Java

of 62

  • date post

    14-Aug-2015
  • Category

    Documents

  • view

    26
  • download

    0

Embed Size (px)

description

Προγραμματισμός σε Java

Transcript of Προγραμματισμός σε Java

JAVA252

Strings

Strings

( primitive data types) Java : int: ,

.. int i = -143; .. double d = 143.8832; .. char c = E;

double: ,

char: ,

primitive long, boolean, float, long, short byte.

: http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

Strings

10

: ; class ArrayDemo { public static void main(String[] args) { int[] anArray; // anArray = new int[5]; // 5 anArray[0] = 100; // anArray[1] = 200; // anArray[2] = 300; // . anArray[3] = 400; anArray[4] = 500; System.out.println("Element at index 0: " + anArray[0]); System.out.println("Element at index 1: " + anArray[1]); System.out.println("Element at index 2: " + anArray[2]); System.out.println("Element at index 3: " + anArray[3]); System.out.println("Element at index 4: " + anArray[4]); } }

: :h y 2 5 2

: :h y 2 5 2

: 1. , 2. -

: :h y 2 5 2

h: 1. , 2. -

: :h y 2 5 2

h y: 1. , 2. -

: :h y 2 5 2

h y 2 5 2: 1. , 2. -

: : :

class ArrayDemo2{ public static void main(String[] args) { char[] copyFrom = new char[] { h', y', 2', 5', 2'-; char[] copyTo = new char[copyFrom.length]; for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = copyFrom[i]; } } }: 1. , 2. -

: : :

class ArrayDemo2{ public static void main(String[] args) {

char[] copyFrom = new char[] { h', y', 2', 5', 2'-; char[] copyTo = new char[copyFrom.length]; for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = copyFrom[i]; } } }: 1. , 2. -

: : :

class ArrayDemo2{ public static void main(String[] args) {

. copyFrom 5

char[] copyFrom = new char*+ , h', y', 2', 5', 2'-; char[] copyTo = new char[copyFrom.length]; for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = copyFrom[i]; } } }: 1. , 2. -

: : :

class ArrayDemo2{ public static void main(String[] args) {

char[] copyFrom = new char*+ , h', y', 2', 5', 2'-; char[] copyTo = new char[copyFrom.length]; for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = copyFrom[i]; } } }: 1. , 2. -

: : :

class ArrayDemo2{ public static void main(String[] args) { char[] copyFrom = , h', y', 2', 5', 2'-;

char[] copyTo = new char[copyFrom.length]; for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = copyFrom[i]; } } }: 1. , 2. -

: : :

class ArrayDemo2{ public static void main(String[] args) { char[] copyFrom = , h', y', 2', 5', 2'-;

i copyFrom copyTo

char[] copyTo = new char[copyFrom.length]; for (int i = 0; i < copyFrom.length; i++) { copyTo[i] = copyFrom[i]; } } }: 1. , 2. -

(max) ;24 31 5 52 9 :

max =

24

1. ( ) max, 2. max 3. , max

(max) ;24 31 5 52 9 : 1. ( ) max, 2. max 3. , max

31 > 24?

max =

24

(max) ;24 31 5 52 9 : 1. ( ) max, 2. max 3. , max

31 > 24? !

max =

24

31

(max) ;24 31 5 52 9 : 1. ( ) max, 2. max 3. , max

5> 31? !

max =

31

(max) ;24 31 5 52 9 : 1. ( ) max, 2. max 3. , max

52 > 31? !

max =

31

52

(max) ;24 31 5 52 9 : 1. ( ) max, 2. max 3. , max

9 > 52? !

max =

52

(max) ;24 31 5 52 9 :

max =

52

1. ( ) max, 2. max 3. , max

(max) ; :

class ArrayDemo3 { public static void main(String[] args) { int[] array= { 24, 31, 5, 52, 9}; int max = array[0]; for (int i = 0; i < array.length; i++) { if (array[i] > max) { max = array[i]; } } } }

: 1. ( ) max, 2. max 3. , max

(min) (sum)

Strings

( )

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { return n1; } else { return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { return n1; ; (for/while) } else { . return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 n2) { == ; return n1; (n1n2) { n1 + ((n1+1)n2) } else return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { ; n1=n2. return n1; n1=n2; } else { return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { return n1; } else { return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { return n1; } else { return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { .. if (n1 == n2) { sum(7, 7) return n1; 7. } else { return n1 + sum(n1 + 1, n2); } }

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { return n1; } else { return n1 + sum(n1 + 1, n2); .. } sum(3, 7) }

3 + sum(4, 7)

: n1n2. .. n1 = 3 n2 = 7, 3 + 4 + + 7 = 25 public static int sum(int n1, int n2) { if (n1 == n2) { : return n1; } else { ; return n1 + sum(n1 + 1, n2); } }

Strings

STRINGS string (.. java) (.. java) primitive types string : char c = j; strings char ;

STRINGS

strings char ; char! char*+ string = hy252;

h y 2 5 2

C Java Strings:

String str = hy252

STRINGS

String h y 2 5 2

i :

str.get(i)

..String str1 = kostas; String str2 = java; char c1 = str1.get(5); // c1 = s char c2 = str2.get(0); // c2 = j

STRINGS

: String str1 = h y 2 5 2

str2 =

h y 2 4 0

STRINGS

: String str1 = h y 2 5 2 1 : 1. strings ( !) 2. - 3. false

str2 =

h y 2 4 0

21 = 5 2 = 5 1 == 2; !

STRINGS

: String str1 = h y 2 5 2 h == h; ! : 1. strings ( !) 2. - 3. false

str2 =

h y 2 4 0

STRINGS

: String str1 = h y 2 5 2 y == y; ! : 1. strings ( !) 2. - 3. false

str2 =

h y 2 4 0

STRINGS

: String str1 = h y 2 5 2 2 == 2; ! : 1. strings ( !) 2. - 3. false

str2 =

h y 2 4 0

STRINGS

: String str1 = h y 2 5 2 5 == 4; ! : 1. strings ( !) 2. - 3. false

str2 =

h y 2 4 0

STRINGS

: String str1 = h y 2 5 2 5 == 4; ! : 1. strings ( !) 2. - 3. false

str2 =

h y 2 4 0

: strings

STRINGS: String : public static boolean stringEqual(String str1, String str2) { int l1 = str1.size(); int l2 = str2.size(); : if (l1 != l2) { return false; 1. strings ( } for (int i = 0; i < l1; i++) { !) if (str1.get(i) != str2.get(i)) { 2. - return false; } 3. } } false

STRINGS String . .. strings :

boolean result = str1.equals(str2);

H equals String true strings false :public String[] split(String regex) .. String str1 = this is a test; String*+ result = str1.split( );

this is a test

STRINGS

strings. :String str1 = this is ; String str2 = a test; String enwsi = str1 + str2;

this is a test

Strings

3 :

command line arguments

COMMAND LINE ARGUMENTSCommand line arguments: pico :

> pico

pico :

> pico file.txt

, pico file.txt command line argument

COMMAND LINE ARGUMENTS

java , command line arguments :

java MyProgram argument1 argument2

MyProgram argument1 argument, argumnet2 . arguments , ,

COMMAND LINE ARGUMENTS

, ( ) arguments , String[] args main :

public static void main(String[] args)

COMMAND LINE ARGUMENTS

: public static void main(String[] args) { String result = ; for (int i = 0; i < args.length; i++) { result = result + + args[i]; } System.out.println(result); }

: ;

: Scanner in = new Scanner(System.in); String line = in.nextLine();

H Scanner , , , . .. Scanner in = new Scanner(System.in); , String line = in.nextLine(); . int akeraios = in.nextInt(); double dekadikos = in.nextDouble(); (Exception:

)

.