Introduction to Programming in Java =================================== 1) Let's review the characteristics of imperative languages: 1.1) How do imperative languages differ from functionals? 2) Normally we say that a functional language has expressions, and an imperative language has commands and expressions. What is a command? 2.1) There are three major types of commands: - Branches - Conditionals - Iterations - Sequences - Blocks - Assignments 3) 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 4) Why is Java so successful? "simple, object oriented, and familiar". "robust and secure". "architecture neutral and portable". "high performance". 5) How is Java executed? 5.1) What is a VM? 5.2) What are bytecodes? 6) 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); } } 6.1) What are the operators of the language? exp++ exp-- ++exp --exp +exp -exp ~ ! * / % + - << >> >>> < > <= >= instanceof == != & ^ | && || ? : = += -= *= /= %= &= ^= |= <<= >>= >>>= 7) We see many assignments. An assignment has a left side and a right side. What can exist on the right side? 7.1) What about the left side? 8) 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; } // 5.1) How to read from the input? public static void main(String args[]) { int n = Integer.parseInt(args[0]); System.out.println(fact(n)); } } 9) How to write using while? public static int wf(int n) { int fact = 1; while (n > 1) { fact *= n--; // 6.1 Does this change the 'n' actual parameter? } return fact; } 10) What about using for? public static int ff(int n) { int fact = 1; for (int i = 1; i <= n; i++) { fact *= i; } return fact; } 11) What is an array? 11.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); } } 12) What is ASCII? 12.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); } } 13.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); } } 13.1) What about "indexOf"? Information in Java is organized in structures called classes. * The class is the implementation of a type, that is, it contains attributes that define the state of that type, and operations that can be applied on that type. * Every time we instantiate a class, we produce an object. Objects are run-time entities. They are data of a certain class type. They have a state that can be checked and possibly updated at any time. * The implementation of the operations that we can apply on an object are called methods. * The state of the object is defined by the state of its attributes, also called fields. 14) Implement a ML like list type as a Java class: public class ConsCell { private E head; private ConsCell tail; public ConsCell(E h, ConsCell t) { head = h; tail = t; } public E getHead() { return head; } public ConsCell getTail() { return tail; } } 15) From which point a ML program starts? What about a C program? Can anyone give me examples of other languages? From which point a Java program starts? 16) Implement the main method for our ConsCell example: public static void main(String argc[]) { ConsCell c1 = new ConsCell(1, null); ConsCell c2 = new ConsCell(2, c1); ConsCell c3 = new ConsCell(3, c2); ConsCell c4 = new ConsCell(4, c3); System.out.println(c4.length()); System.out.println("Printing c4"); c4.print(); System.out.println("Printing c2"); c2.print(); } 17) Add to the ConsCell class a method that computes its length: public int length() { if (tail == null) { return 1; } else { return 1 + tail.length(); } } 18) Now, implement a method that prints the contents of the class: public void print() { System.out.println(head); if (tail != null) { tail.print(); } } ================================================================================ 19) 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); } } }