Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object...

16
Object Oriented Programming in Java Lecture 14

Transcript of Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object...

Page 1: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Object Oriented Programmingin

Java

Lecture 14

Page 2: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Review Quiz1. Write a method which gets an Object

and call all its available ‘get’ Methods and print the return values.

Page 3: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Java 5 new Features

• Ease of development

• Quality monitoring and manageability

• Performance and scalability

Page 4: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

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

Page 5: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Examples

public class LinkedList <Element>{

boolean add(Element o) {

// code omitted

}

Element getFirst(){

// code omitted

}

}

Page 6: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Examplespublic class Ex2 {

private void testCollection() { List<String> list = new ArrayList<String>(); list.add(new String("Hello world!")); list.add(new String("Good bye!")); list.add(""+new Integer(66)); printCollection(list); }

Page 7: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

private void printCollection(Collection c) { Iterator<String> i = c.iterator(); while(i.hasNext()) { String item = i.next(); System.out.println("Item: "+item); } }

public static void main(String argv[]) { Ex2 e = new Ex2(); e.testCollection(); }}

Examples

Page 8: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Advantages

• Allow to pass types as arguments to classes just as values to methods

Why ?

- compile time safety

- no need in casts

Page 9: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Enhanced loop

Syntax

for (FormalParameter : Expression) Statement

Note:Expression must be an array or an instance of a new interface called java.lang.Iterable

Page 10: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

public void oldFor(Collection c) { for(Iterator i = c.iterator(); i.hasNtext(); ) { String str = (String) i.next(); System.out.println(str); }}

public void newFor(Collection<String> c) { for(String str : c){

System.out.println(str); }

}

Page 11: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

public int sumArray(int array[]) { int sum = 0; for(int i=0;i<array.length;i++)

sum += array[i]; return sum; }-------------------------------------------------- public int sumArray(int array[]) {

int sum = 0; for(int i : array)

sum += i;

return sum; }

Loops for arrays

Page 12: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

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");

Page 13: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Var-args

Multiple arguments can be passed to the routines:

public static void myMethod(Object ... args){ for(Object a:args) System.out.print(""+a+" "); System.out.println();} … myMethod(23, 34, 78,"Hello", "Goodbye"); System.out.printf("%d %d %d %s %s",23, 34, 78,"Hello",

"Goodbye");

Page 14: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Input/Output

• Output

System.out.printf("%s %3d", name, age);

• InputScanner reader = new Scanner(System.in);

int n = reader.nextInt();

Page 15: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

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.

Page 16: Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object and call all its available ‘get’ Methods and print.

Example

public enum Shapes{ins1(1,2),ins2(1,3) ;private int x;private int y;Shapes(int x, int y){

this.x = x;this.y = y;

}}

…..

Shapes s = Shapes.ins1;

Shapes s2 Shapes.ins2;