Object Oriented Programming in Java Lecture 14 Review

Object Oriented Programming in Java Lecture 14

Review Quiz 1. Write a method which gets an Object and call its available ‘get’ Methods and print the return values.

Java 5 new Features • Ease of development • Quality monitoring and manageability • Performance and scalability

Generics • Allow to pass types as arguments to classes just as values to methods Why ? - compile time safety - no need in casts C++: templates are handled on macro processor level

Examples public class Linked. List <Element>{ boolean add(Element o) { // code omitted } Element get. First(){ // code omitted } }

Examples public class Ex 2 { private void test. Collection() { List<String> list = new Array. List<String>(); list. add(new String("Hello world!")); list. add(new String("Good bye!")); list. add(""+new Integer(66)); print. Collection(list); }

Examples private void print. Collection(Collection c) { Iterator<String> i = c. iterator(); while(i. has. Next()) { String item = i. next(); System. out. println("Item: "+item); } } public static void main(String argv[]) { Ex 2 e = new Ex 2(); e. test. Collection(); } }

Advantages • Allow to pass types as arguments to classes just as values to methods Why ? - compile time safety - no need in casts

Enhanced loop Syntax for (Formal. Parameter : Expression) Statement Note: Expression must be an array or an instance of a new interface called java. lang. Iterable

public void old. For(Collection c) { for(Iterator i = c. iterator(); i. has. Ntext(); ) { String str = (String) i. next(); System. out. println(str); } } public void new. For(Collection<String> c) { for(String str : c) { System. out. println(str); } }
![Loops for arrays public int sum. Array(int array[]) { int sum = 0; for(int Loops for arrays public int sum. Array(int array[]) { int sum = 0; for(int](http://slidetodoc.com/presentation_image_h2/620b339266189491ae0924b2072a47f9/image-11.jpg)
Loops for arrays public int sum. Array(int array[]) { int sum = 0; for(int i=0; i<array. length; i++) sum += array[i]; return sum; }-------------------------public int sum. Array(int array[]) { int sum = 0; for(int i : array) sum += i; return sum; }

Static imports Enable to import methods and fields from the static members of the class or interface, without qualifying the name import static java. lang. System. out; ………. . out. print("stuff"); out. print("more stuff");

Var-args Multiple arguments can be passed to the routines: public static void my. Method(Object. . . args){ for(Object a: args) System. out. print(""+a+" "); System. out. println(); } … my. Method(23, 34, 78, "Hello", "Goodbye"); System. out. printf("%d %d %d %s %s", 23, 34, 78, "Hello", "Goodbye");

Input/Output • Output System. out. printf("%s %3 d", name, age); • Input Scanner reader = new Scanner(System. in); int n = reader. next. Int();

Enumerations Example: enum Drinks {pepsi, coke, schweppes }; …. Drinks d = Drinks. coke; System. out. print(d); …. Note: In C++ enumerations are just hidden integers. In Java enumerations are generated classes.

Example public enum Shapes{ ins 1(1, 2), ins 2(1, 3) ; private int x; private int y; Shapes(int x, int y){ this. x = x; this. y = y; } } …. . Shapes s = Shapes. ins 1; Shapes s 2 Shapes. ins 2;
- Slides: 16