Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object...
-
Upload
vivien-bruce -
Category
Documents
-
view
212 -
download
0
Transcript of Object Oriented Programming in Java Lecture 14. Review Quiz 1. Write a method which gets an Object...
Object Oriented Programmingin
Java
Lecture 14
Review Quiz1. Write a method which gets an Object
and call all 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 LinkedList <Element>{
boolean add(Element o) {
// code omitted
}
Element getFirst(){
// code omitted
}
}
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); }
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
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 (FormalParameter : Expression) Statement
Note:Expression must be an array or an instance of a new interface called java.lang.Iterable
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); }
}
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
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 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");
Input/Output
• Output
System.out.printf("%s %3d", name, age);
• InputScanner reader = new Scanner(System.in);
int n = reader.nextInt();
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{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;