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.

Slides:



Advertisements
Similar presentations
The ArrayList Class and the enum Keyword
Advertisements

1.A computer game is an example of A.system software; B.a compiler; C.application software; D.hardware; E.none of the above. 2.JVM stands for: A.Java Virtual.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
1-May-15 Java 1.5. Reason for changes “The new language features all have one thing in common: they take some common idiom and provide linguistic support.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
J2SE 5.0 New Features. J2SE 5.0 aka JDK 1.5 aka Tiger.
JAVA 1.5 New Features Dr V. The syntax of the enhanced for loop (for each) for (type variableName : arrayName) { statements } arrayName could be any.
Misc Language Features Features that were added relatively recently that are now in common use.
Java Review Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Generics. Type parameters The definition of “ArrayApplier” in applier.java and applier2.java allows any function from int to int. But suppose you want.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
COMP More About Classes Yi Hong May 22, 2015.
Java Generics.
Generics In Java 1.5 By Manjunath Beeraladinni. Generics ➲ New feature in JDK1.5. ➲ Generic allow to abstract over types. ➲ Generics make the code clearer.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
1162 JDK 5.0 Features Christian Kemper Principal Architect Borland.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Arrays Chapter 8. What if we need to store test scores for all students in our class. We could store each test score as a unique variable: int score1.
Dale Roberts Object Oriented Programming using Java - Enumerations Dale Roberts, Lecturer Computer Science, IUPUI Department.
October 28, 2015ICS102: For Loop1 The for-loop and Nested loops.
Input/Output in Java. Output To output to the command line, we use either System.out.print () or System.out.println() or System.out.printf() Examples.
Java 5 Part 1 CSE301 University of Sunderland Harry Erwin, PhD.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) public static void main(String[]
O AK R IDGE N ATIONAL L ABORATORY U. S. D EPARTMENT OF E NERGY 1 Java Pros and Cons − Roundtable American Nuclear Society Annual Meeting June 16, 2004.
Programming With Java ICS201 1 Chapter 14 Generics and The ArrayList Class.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
CompSci 100E 40.1 Java 5 New Features  Generics  Enhanced for loop  Autoboxing/unboxing  Typesafe enums  Other  Varargs  Static Import  Metadata.
G ENERICS I N J AVA BY: Ankit Goyal Sankalp Singh.
Generics CompSci 230 S Software Construction.
Java 1.5 The New Java Mike Orsega Central Carolina CC.
Objects First With Java A Practical Introduction Using BlueJ Supplementary Material for Java
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Java 5 Part 2 CSE301 University of Sunderland Harry Erwin, PhD.
1 Basic Java Constructs and Data Types – Nuts and Bolts Looking into Specific Differences and Enhancements in Java compared to C.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
GENERICS AND THE JAVA COLLECTIONS FRAMEWORK Lecture 16 CS2110 – Fall 2015 Photo credit: Andrew Kennedy.
Java Generics. It is nice if we could write a single sort method that could sort array of any type of elements: – Integer array, – String array, Solution:
Java Review if Online Time For loop Quiz on Thursday.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
More Java: Static and Final, Abstract Class and Interface, Exceptions, Collections Framework 1 CS300.
Bart van Kuik Application Developer Oracle Corporation.
Introduction to Computational Modeling of Social Systems Prof. Lars-Erik Cederman Center for Comparative and International Studies (CIS) Seilergraben 49,
Methods.
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
19-Mar-16 Collections and ArrayLists.. 2 Collections Why use Collections. Collections and Object-Orientation. ArrayLists. Special Features. Creating ArrayLists.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Loops, Methods, Classes Using Loops, Defining and Using Methods, Using API Classes, Exceptions, Defining Classes Svetlin Nakov Technical Trainer
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Object Oriented Programming Lecture 2: BallWorld.
New Java Features Advanced Programming Techniques.
Sixth Lecture ArrayList Abstract Class and Interface
Chapter No. : 1 Introduction to Java.
TK1114 Computer Programming
While Statement.
The for-loop and Nested loops
An Introduction to Java – Part I, language basics
Java Language Basics.
CS18000: Problem Solving and Object-Oriented Programming
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Object Oriented Programming
A Java Application public class Hello { public static void main(String [] args) { System.out.println("Hello, World!"); } } public class.
Java 5 New Features 1-May-19.
Generic Programming.
Methods/Functions.
Introduction to java Part I By Shenglan Zhang.
Announcements Lab 5 due Wednesday at noon.
Presentation transcript:

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 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 { boolean add(Element o) { // code omitted } Element getFirst(){ // code omitted }

Examples public class Ex2 { private void testCollection() { List list = new ArrayList (); 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 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 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); Input Scanner 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;