Introduction to Java. Module 4: Advanced Language Features. Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Σχετικά έγγραφα
ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

Διδάσκων: Παναγιώτης Ανδρέου

API: Applications Programming Interface

The Simply Typed Lambda Calculus

ιαδικτυακές Εφαρµογές

Αντικειμενοστρεφής Προγραμματισμός

Η λέξη κλειδί this. Γαβαλάς Δαμιανός

Εργαστήριο Java. Διδάσκουσα: Εργαστηριακοί Συνεργάτες:

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 5: Component Adaptation Environment (COPE)

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

Διάλεξη 16-17: Πολυμορφισμός (Polymorphism) Διδάσκων: Παναγιώτης Ανδρέου

Αντικειμενοστρεφής Προγραμματισμός

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Αρχές Τεχνολογίας Λογισμικού Εργαστήριο

Homework 3 Solutions

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Κλάσεις και Αντικείμενα Αναφορές

ΠΛΗΡΟΦΟΡΙΚΗ ΙΙ (JAVA) 18/3/2008

Διάλεξη 3: Προγραμματισμός σε JAVA I. Διδάσκων: Παναγιώτης Ανδρέου

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αναφορές Στοίβα και Σωρός Μνήμης Αντικείμενα ως ορίσματα

(Διαφάνειες Νίκου Βιδάκη)

Βασικά της γλώσσας JAVA

Wrapper Classes, Abstract Classes and Interfaces

Πρόβλημα 1: Αναζήτηση Ελάχιστης/Μέγιστης Τιμής

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Εισαγωγή στη Java III

SOAP API. Table of Contents

The challenges of non-stable predicates

Εισαγωγή στον Αντικειμενοστρέφή Προγραμματισμό Διάλεξη #13

Διάλεξη 5: Κλάσεις και Αντικείμενα. Διδάσκων: Παναγιώτης Ανδρέου

! Ο καλός προγραμματισμός επιβάλλει την αποδοτική χρήση. ! Είναι σημαντικό να καταλαβαίνουμε τις διαδικασίες

Συστήματα Διαχείρισης Βάσεων Δεδομένων

Βασίλης Χριστοφίδης Επαναληπτική Εξέταση (3 ώρες) Ηµεροµηνία: 21 Σεπτεµβρίου 2012

Αντικειμενοστρεφής Προγραμματισμός

Διδάσκων: Παναγιώτης Ανδρέου

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Υπάρχουσες κλάσεις και αντικείμενα στην Java Strings Wrapper Classes Δομές

CRASH COURSE IN PRECALCULUS

ΕΙΣΑΓΩΓΗ ΣΤΟN ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ ΚΑΙ ΠΛΗΡΟΦΟΡΙΚΗΣ

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

PARTIAL NOTES for 6.1 Trigonometric Identities

Elements of Information Theory

Δομές Δεδομένων - Εργαστήριο 2. Λίστες

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

Finite Field Problems: Solutions

ΕΠΛ221: Οργάνωση Υπολογιστών και Συμβολικός Προγραμματισμός. Εργαστήριο Αρ. 2

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Οντοκεντρικός Προγραμματισμός

Μηχανική Μάθηση Hypothesis Testing

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

ΠΛΗΡΟΦΟΡΙΚΗ Ι JAVA Τμήμα θεωρίας με Α.Μ. σε 3, 7, 8 & 9 25/10/07

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Εισαγωγή στη Java III

Αντικειμενοστρεφής Προγραμματισμός

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

5. Ανασκόπηση αντικειμενοστραφούς προγραμματισμού

2 Composition. Invertible Mappings

Block Ciphers Modes. Ramki Thurimella

Αντικειμενοστρεφής Προγραμματισμός

Approximation of distance between locations on earth given by latitude and longitude

Congruence Classes of Invertible Matrices of Order 3 over F 2

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

ΠΛΗΡΟΦΟΡΙΚΗ ΙΙ (JAVA) 11/3/2008

ΠΛΗΡΟΦΟΡΙΚΗ Ι JAVA Τμήμα θεωρίας με Α.Μ. σε 3, 7, 8 & 9 22/11/07

C.S. 430 Assignment 6, Sample Solutions

Κατανεμημένα Συστήματα. Javascript LCR example

8. Μέθοδοι (Methods)

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Other Test Constructions: Likelihood Ratio & Bayes Tests

ΕΠΛ233 Βασικά στοιχεία προγραµµατισµού στη JAVA

Section 9.2 Polar Equations and Graphs

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Section 8.3 Trigonometric Equations

Αντικειµενοστρεφής Προγραµµατισµός

ΕΠΛ131 Αρχές Προγραμματισμού

Statistical Inference I Locally most powerful tests

Abstract classes, Interfaces ΦΡΟΝΤΙΣΤΗΡΙΟ JAVA

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

Αντικειμενοστρεφής Προγραμματισμός

EE512: Error Control Coding

Fractional Colorings and Zykov Products of graphs

ΠΑΡΑΜΕΤΡΟΙ ΕΠΗΡΕΑΣΜΟΥ ΤΗΣ ΑΝΑΓΝΩΣΗΣ- ΑΠΟΚΩΔΙΚΟΠΟΙΗΣΗΣ ΤΗΣ BRAILLE ΑΠΟ ΑΤΟΜΑ ΜΕ ΤΥΦΛΩΣΗ

(C) 2010 Pearson Education, Inc. All rights reserved.

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αναφορές Στοίβα και Σωρός Αναφορές-Παράμετροι

Στοιχεία εξεταζόµενου Αριθµός Απάντησης Βαθµολογία. Σύνολο (Θέµα 4 ο )

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΣΧΟΛΗ ΘΕΤΙΚΩΝ ΕΠΙΣΤΗΜΩΝ ΤΜΗΜΑ ΜΑΘΗΜΑΤΙΚΩΝ ΙΠΛΩΜΑΤΙΚΗ ΕΡΓΑΣΙΑ

Διάλεξη 4: Προγραμματισμός σε JAVA IΙ. Διδάσκων: Παναγιώτης Ανδρέου

Introduction to Java. Module 2.a: Read and Write Introduction. Prepared by Costantinos Costa for EPL 233. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

ΑΤΕΙ ΘΕΣΣΑΛΟΝΙΚΗΣ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ ΠΛΗΡΟΦΟΡΙΚΗΣ Αλγοριθμική και Προγραμματισμός. Παναγιώτης Σφέτσος.

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αναφορές Στοίβα και Σωρός Αναφορές-Παράμετροι String Interning

Mεταβλητές (variables) και Σταθερές (constants)

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΕΙΡΑΙΩΣ ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ. Βάσεις Δεδομένων (4 ο εξάμηνο) Εργαστήριο MySQL #2

Αντικειμενοστραφής Προγραμματισμός I (5 ο εξ) Εργαστήριο #2 ο : Ανατομία προγραμμάτων εφαρμογών, η

ΔΟΜΕΣ ΔΕΔΟΜΕΝΩΝ & ΑΛΓΟΡΙΘΜΟΙ ΕΡΓΑΣΤΗΡΙΟ

Math 6 SL Probability Distributions Practice Test Mark Scheme

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αναφορές Μέθοδοι που επιστρέφουν αντικείμενα Deep and Shallow Copies

Areas and Lengths in Polar Coordinates

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αντικείμενα με πίνακες. Constructors. Υλοποίηση Στοίβας

Galatia SIL Keyboard Information

Lab 1: C/C++ Pointers and time.h. Panayiotis Charalambous 1

SUPERPOSITION, MEASUREMENT, NORMALIZATION, EXPECTATION VALUES. Reading: QM course packet Ch 5 up to 5.6

Αντικειμενοστραφής Προγραμματισμός I(5 ο εξ) Εργαστήριο #2 ο : Ανατομία προγραμμάτων εφαρμογών, η

ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Αναφορές Στοίβα και Σωρός μνήμης Αντικείμενα παράμετροι String Interning

Transcript:

Introduction to Java Module 4: Advanced Language Features Prepared by Costantinos Costa for EPL 233 ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 1

Constructors Constructor is the method called when an object is initialized with the new keyword Constructors can also be overridden (they usually are) Constructors can invoke constructors from the superclass Example: public class redcircle extends Circle {... //constructor public redcircle(int x, int y,int radius) { super(x,y,radius); color=color.red; ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 2

Static variables & methods Static methods No need to create an instance of the class containing the method to use it Example: double x = Console.readDouble(); double x = Math.pow(3, 0.1); This works because the method readdoubleof the Consoleclass and the method powof the Mathclass are declared as static I.e. public static double readdouble() { ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 3

Static variables & methods Static variables No need to create an instance of the class to access them Example: public class defaults { public static String hostname= java.sun.com public class anotherclass{ System.out.println( The hostname is + defaults.hostname); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 4

Static variables & methods We need to create a new object to access data and methods of the specific class. What if we need to access those data and methods without create an object?? Use static Static variables and methods are called class-data, class-methods. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 5

Static variables & methods public class WithStaticData { static int y; int x; public static void main(string[] args) { WithStaticData a = new WithStaticData(); WithStaticData b = new WithStaticData(); WithStaticData c = new WithStaticData(); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 6

Memory Text: binary code of the process s program Data: constants of the program BSS(Block Started by Symbol ):Globaland static variables Stack: stack, local variables Heap: dynamic memory ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 7

Final classes, variables and methods A finalclass cannot become a parent class Example:finalclass Card{ Specific methods in a class can be declared as final. A finalmethod cannot be overridden Example:public finalvoid dosomethind() { A finalvariable cannot change value Example:public finalintx=15; Using finalimproves performance ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 8

Wrapper classes An instance of the Doubleclass wraps the doubletype, Integerthe inttype and so on Example: Suppose we need a vector of Double. Simply adding numbers to the vector won t work: Vector v = new Vector(); v.addelement(3.14);//error The floating-point number 3.14 is not an object. Here we can use the Double wrapper class to create a Doubleobject and add it to the vector: v.addelement(new Double(3.14)); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 9

Working with Strings Java.lang.String Create: String x = new String( a string ); Concat: x=x+, another string ;(x= a string, another string ) Length: int stringlength = x.length(); Comparing if (x.equals( a string )) if (x.compareto( a string )) ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 10

Working with Strings Other useful methods: indexof(string) Returns the index within this string of the first occurrence of the specified character. replace(char, char) Returns a new string resulting from replacing all occurrences of oldcharin this string with newchar startswith(string) Tests if this string starts with the specified prefix. trim() Removes white space from both ends of this string. Check the API documentation for the complete list of functions ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 11

Working with Strings Java provides three classes for working with Strings String, StringBuffer and StringBuilder Class Stringis used for strings that remain constant(their value doesn t change) Class StringBufferandStringBuilderis used for strings that may change E.g. Reading the contents of a text file Check out the Java API for StringBuffer and StringBuilder Using Stringis more efficient when our strings remain unchanged as they are constants that can be jointly used by other code in our program StringBufferandStringBuilderis more efficient when we have changing strings as we create only one object StringBuffer is synchronized, StringBuilder is not. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 12

Concatenating: + Operator vs StringBuffer vs StringBuilder public class StringConcatanations { public static int MAX_ITER = 100; public static void main(string[] args) { for (int i = 1; i <=5; i++) { MAX_ITER=(int)Math.pow(10.0,i); concatenate(); concatenatewithstringbuffer(); concatenatewithstringbuilder(); public static void concatenate() { System.out.println("Concatanating using the + operator"); String s1 = ""; long s1time = getnanotime(); for(int i=0;i<max_iter;i++) {s1 = s1 + "abc"; long e1time = getnanotime(); System.out.println("Time: " + (e1time - s1time)); public static void concatenatewithstringbuffer() { System.out.println("Concatanating using StringBuffer"); StringBuffer sb = new StringBuffer(); long s2time = getnanotime(); for(int i=0;i<max_iter;i++) {sb.append("abc"); long e2time = getnanotime(); System.out.println("Time: " + (e2time - s2time)); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 13

Concatenating: + Operator vs StringBuffer vs StringBuilder public static void concatenatewithstringbuilder() { System.out.println("Concatanating using StringBuilder"); StringBuilder sbuilder = new StringBuilder(); long s3time = getnanotime(); for(int i=0;i<max_iter;i++) { sbuilder.append("abc"); long e3time = getnanotime(); System.out.println("Time: " + (e3time - s3time)); public static long getnanotime() { return System.nanoTime(); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 14

Concatenating: + Operator vs StringBuffer vs StringBuilder 2,5E+10 2E+10 1,5E+10 time(ns) 1E+10 Concatanating using the + operator Concatanating using StringBuffer Concatanating using StringBuilder 5E+09 0 1 2 3 4 5 Iteration 10 x ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 15

Working with Strings class SameString{ public static void main(string[] args) { String s1 = dog ; String s2 = It s a dog s life ; String s3 = dog ; if (s1 == s2) System.out.println( s1 == s2 ); // FALSE if (s1 == s3) System.out.println( s1 == s3 ); // TRUE if (s1 == dog ) System.out.println( s1 == \ dog \ ); //TRUE String doggy = new String(s1); if (s1 == doggy) System.out.println( s1 == doggy ); //FALSE ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 16

Example-Exercise Checking whether a string is a palindrome. Palindromea string that reads the same forward and backward. E.g Level, civic, νίψον ανομήματα μη μόναν oψιν ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 17

Example-Exercise Checking whether a string is a palindrome. Without using a loop!!! Palindromea string that reads the same forward and backward. E.g Level, civic, νίψον ανομήματα μη μόναν oψιν ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 18