Introduction to Programming in Java =================================== 1) Can someone tell me one of the greatest achievements of computer science on the 90's? The issue of portability. A little of the history of Java. The popularity of Java: http://www.tiobe.com/index.php/content/paperinfo/tpci/index.html 2) Why is Java so successful? "simple, object oriented, and familiar". "robust and secure". "architecture neutral and portable". "high performance". 3) How is Java executed? 3.1) What is a VM? 3.2) What are bytecodes? 4) What are primitive types? // Application 1: public class Types { public static void main(String[] args) { short s = 32767; char c = (char)65536; int i = c; // 5) What will be printed here? float f = 1.0F; double d = 2.0; byte b = 127; byte mb = -128; // 4.1 Which other primitive types? boolean and long System.out.println("int = " + i); System.out.println("char = " + c); System.out.println("float = " + f); System.out.println("double = " + d); System.out.println("byte = " + b + ", -byte = " + mb); System.out.println("short = " + s); } } 5) What we notice about this program? - The entry door: main - The code is enclosed in a class. - The code to print stuff: System.out - The C syntax 6) How to write a factorial in Java? public class Fact { public static int fact(int n) { if (n > 1) return n * fact(n - 1); else return 1; } // 6.1) How to read from the input? public static void main(String args[]) { int n = Integer.parseInt(args[0]); System.out.println(fact(n)); } } 6.1) Can you do it using a ternary operator? public static int tf(int n) { return n > 1 ? n * fact(n - 1) : 1; } 7) How to write using while? public static int wf(int n) { int fact = 1; while (n > 1) { fact *= n--; // Does this change the 'n' actual parameter? } return fact; } 8) What about using for? public static int ff(int n) { int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } return fact; } 9) What is an array? 9.1) How to write a program to sum up an array of integer? public class SumArray { public static void main(String args[]) { int a[] = {2, 4, 8, 2, 3}; int sum = 0; for (int i = 0; i < a.length; i++) { sum += a[i]; } System.out.println(sum); } } 10) Contrary to C, Java is strongly typed. What would happen in this C program? #include int main(int argc, char** argv) { int i = 0; int a[2] = {0, 1}; printf("argv[1][0] = %c\n", argv[1][0]); printf("a[0] = %d\n", a[0]); printf("a[1] = %d\n", a[1]); printf("a[2] = %d\n", a[2]); } 11) What is ASCII - American Standard Code for Information Interchange? 11.1) How to write a program to sum up the ASCII code of the letters in the first input argument? public class SumVowels { public static void main(String args[]) { int sum = 0; for (int i = 0; i < args[0].length(); i++) { sum += args[0].charAt(i); } System.out.println(sum); } } 11.2) What if we want only the vowels? public class SumVowels { public static void main(String args[]) { int sumL = 0, sumU = 0; for (int i = 0; i < args[0].length(); i++) { char c = args[0].charAt(i); System.out.println("Summing " + c + " = " + (int)c); switch (c) { case 'a': case 'e': case 'i': case 'o': case 'u': sumL += c; break; case 'A': case 'E': case 'I': case 'O': case 'U': sumU += c; } } System.out.println(sumL); System.out.println(sumU); } } 11.3) Can you make it shorter? public static void main(String args[]) { int sumL = 0, sumU = 0; for (int i = 0; i < args[0].length(); i++) { char c = args[0].charAt(i); System.out.println("Summing " + c + " = " + (int)c); if ("aeiou".indexOf(c) != -1) { sumL += c; } else if ("AEIOU".indexOf(c) != -1) { sumU += c; } } System.out.println(sumL); System.out.println(sumU); } 12) How to find what we can do with strings? - http://download.oracle.com/javase/1.5.0/docs/api/java/lang/String.html#indexOf%28int%29 13) How to swap two variables in java? class Tuple { int a; int b; } public class Swap { public static Tuple swapT(int ap, int bp) { Tuple t = new Tuple(); t.a = bp; t.b = ap; return t; } public static void swap(int ap, int bp) { int tmp = ap; ap = bp; bp = tmp; System.out.println("Swap: a = " + ap + ", b = " + bp); } public static void main(String args[]) { int a = 0, b = 1; System.out.println("a = " + a + ", b = " + b); swap(a, b); System.out.println("a = " + a + ", b = " + b); Tuple t = swapT(a, b); System.out.println("a = " + t.a + ", b = " + t.b); } } 14) How to discover if two strings are anagrams? public class Anagrams { public static void main(String args[]) { if (args.length != 2) { System.err.println("Syntax: java Anagrams s1 s2"); } else { boolean yes = true; String s1 = args[0]; String s2 = args[1]; if (s1.length() != s2.length()) { yes = false; } else { int table[] = new int [128]; for (int i = 0; i < s1.length(); i++) { table[s1.charAt(i)]++; } for (int i = 0; i < s2.length(); i++) { if (table[s2.charAt(i)] == 0) { yes = false; break; } else { table[s2.charAt(i)]--; } } } System.out.println("Are they anagrams? " + yes); } } }