├── README.md ├── img ├── c5f015.png ├── f03c15.png └── ffa500.png ├── lab-java-01 ├── HelloWorld.java ├── HelloWorld_1.java ├── HelloWorld_2.java ├── HelloWorld_3.java ├── README.md └── codecheck.png ├── lab-java-02 └── README.md ├── lab-java-03 └── README.md ├── lab-java-04 └── README.md ├── lab-java-05 └── README.md ├── lab-java-06 └── README.md ├── lab-java-07 └── README.md ├── lab-java-08 └── README.md ├── lab-java-09 └── README.md └── lecture-examples ├── 02-essentials ├── ArgsReader.java ├── Blocks.java ├── Blocks2.java ├── Blocks3.java ├── CompFloat.java ├── Constants.java ├── Defaults.java ├── DoMath.class ├── DoMath.java ├── GetUserInfo.java ├── GetUserInfo_NB.java ├── IntReader.java ├── IntScanner.java ├── Literals.java ├── Op.java ├── TypeCast.java └── WhileEffi.java ├── 03-classes-objects-intro ├── 01-member-access │ └── Baby.java ├── 02-accessor-methods │ └── Baby.java ├── 03-accessor-methods-cont-more │ ├── Baby.class │ ├── Baby.java │ ├── Other.class │ └── Other.java ├── 03-accessor-methods-cont │ ├── Baby.class │ ├── Baby.java │ └── Other.class ├── 04-compare │ └── Baby.java ├── 05-compare-more.1 │ └── Baby.java ├── 05-compare-more │ └── Baby.java ├── 06-equals │ ├── Baby.java │ ├── EqualsNull.java │ └── EqualsString.java ├── 07-toString │ └── Baby.java └── 08-stringbuilder-stringbuffer │ ├── Baby.java │ └── StringEfficiency.java ├── 04-methods └── examples │ ├── 01.static │ └── Calculator.java │ ├── 01 │ ├── Calculator.java │ └── ClassMethods.java │ ├── 02 │ └── Calculator.java │ ├── 03 │ ├── MethodParams.class │ ├── MethodParams.java │ ├── Point.class │ ├── Point.java │ └── StackAndHeap.java │ ├── 04 │ └── Baby.java │ ├── 05 │ ├── Calculator.java │ └── ConfussedCalculator.java │ └── 06-scopeerror │ └── ScopeError.java ├── 05-arrays-collections └── examples │ ├── ArgsLoop.java │ ├── ArrayAsParams.java │ ├── ArrayAssignment.class │ ├── ArrayAssignment.java │ ├── ArrayEquality.java │ ├── ArrayListDemo.java │ ├── ArrayLoop.java │ ├── ArrayLoopMulti.java │ ├── ArrayObjects.java │ ├── ArrayObjectsLoop.java │ ├── Arrays.java │ ├── ArraysMethods.java │ ├── ListIterator.class │ ├── ListIterator.java │ ├── ListRemove.class │ ├── ListRemove.java │ ├── MapsDemo.class │ ├── MapsDemo.java │ ├── SetsDemo.java │ ├── Wrappers.class │ └── Wrappers.java ├── 06-inheritance └── examples │ ├── 01 │ ├── Main.java │ ├── PhotoPost.java │ └── Post.java │ ├── 02 │ ├── Main.java │ ├── PhotoPost.java │ ├── Post.java │ └── TextPost.java │ ├── 03 │ ├── Main.java │ ├── PhotoPost.java │ ├── Post.java │ └── TextPost.java │ ├── 04 │ ├── Main.java │ ├── PhotoPost.java │ └── Post.java │ ├── 05 │ ├── Main.java │ ├── PhotoPost.java │ └── Post.java │ ├── 06 │ ├── Main.java │ ├── MyWall.java │ ├── PhotoPost.java │ ├── Post.java │ └── TextPost.java │ └── 07 │ ├── Main.java │ ├── MyWall.java │ ├── PhotoPost.java │ ├── Post.java │ └── TextPost.java ├── 07-interfaces-abstract ├── examples-1 │ ├── 01 │ │ ├── Main.java │ │ ├── MyWall.java │ │ ├── PhotoPost.java │ │ ├── Post.java │ │ └── TextPost.java │ ├── 02 │ │ ├── HTMLPhotoPost.java │ │ ├── HTMLTextPost.java │ │ ├── Main.java │ │ ├── MyWall.java │ │ ├── PhotoPost.java │ │ ├── Post.java │ │ └── TextPost.java │ ├── 03 │ │ ├── Book.java │ │ ├── Dislikable.java │ │ ├── Likable.java │ │ ├── Main.java │ │ ├── MyWall.java │ │ ├── PhotoPost.java │ │ ├── Post.java │ │ └── TextPost.java │ ├── 04.1 │ │ ├── MediaSelector.java │ │ └── MediaTypes.java │ └── 04 │ │ ├── MediaSelector.java │ │ └── MediaTypes.java └── examples-2 │ ├── 01 │ ├── Employee.java │ ├── Lecturer.java │ ├── LecturerWithKids.java │ ├── LecturerWork.java │ ├── Main.java │ ├── Parent.java │ └── Person.java │ └── 02 │ ├── Employee.java │ ├── Lecturer.java │ ├── LecturerWithKids.java │ ├── LecturerWork.java │ ├── Main.java │ ├── Parent.java │ ├── Person.java │ ├── Programmer.java │ ├── ProgrammerWithKids.java │ └── ProgrammerWork.java ├── 08-exceptions └── examples │ ├── 01 │ ├── IntDivisor.class │ ├── IntDivisor.java │ └── Main.java │ ├── 02.1 │ └── IntDivisor.java │ ├── 02 │ └── IntDivisor.java │ ├── 03 │ ├── IntDivisor.html │ ├── IntDivisor.java │ ├── allclasses-frame.html │ ├── allclasses-noframe.html │ ├── constant-values.html │ ├── deprecated-list.html │ ├── help-doc.html │ ├── index-all.html │ ├── index.html │ ├── overview-tree.html │ ├── package-frame.html │ ├── package-list │ ├── package-summary.html │ ├── package-tree.html │ ├── resources │ │ ├── background.gif │ │ ├── tab.gif │ │ ├── titlebar.gif │ │ └── titlebar_end.gif │ └── stylesheet.css │ ├── 04 │ └── IntDivisor.java │ └── 05 │ ├── IntDivisor.class │ └── IntDivisor.java ├── 09-streams └── examples │ ├── 10 │ ├── FilesPaths.class │ ├── FilesPaths.java │ └── file.txt │ ├── 01 │ ├── 01-Intro-copy.pdf │ ├── 01-Intro.pdf │ ├── 09-Exceptions-copy.pdf │ ├── 09-Exceptions.pdf │ ├── CopyBytes.java │ ├── in.txt │ └── out.txt │ ├── 02 │ ├── CopyToUppercase.java │ ├── in.txt │ ├── out.txt │ └── up.txt │ ├── 03 │ ├── CopyLinesReversed.java │ ├── in.txt │ ├── out.txt │ └── rev.txt │ ├── 04 │ ├── 01-Intro-copy.pdf │ ├── 01-Intro.pdf │ ├── 09-Exceptions-copy.pdf │ ├── 09-Exceptions.pdf │ └── CopyBuffered.java │ ├── 05 │ └── Scanning.java │ ├── 06 │ ├── StockDataReader.java │ ├── StockDataWriter.java │ └── stock.out │ ├── 07 │ ├── Stock.java │ ├── StockReader.java │ ├── StockWriter.java │ ├── mystock.txt │ └── stocks.out │ ├── 08.1 │ ├── Stock.java │ ├── StockJsonReader.java │ ├── StockJsonWriter.java │ ├── gson-2.8.0.jar │ └── stocks.json │ ├── 08.2 │ ├── Stock.java │ ├── StockJsonReader.java │ ├── StockJsonWriter.java │ ├── gson-2.8.0.jar │ ├── stocks.json │ └── stocks_parser.py │ ├── 08 │ ├── Order.java │ ├── OrderJsonWriter.java │ ├── Stock.java │ ├── StockJsonReader.java │ ├── StockJsonWriter.java │ ├── gson-2.8.0.jar │ ├── myorder.txt │ ├── out.js │ └── stock.json │ ├── 09.1 │ ├── 01-Intro.pdf │ ├── 09-Exceptions.pdf │ ├── CopyBuffered.class │ └── CopyBuffered.java │ ├── 09.2 │ ├── 01-Intro-copy.pdf │ ├── 01-Intro.pdf │ ├── 09-Exceptions.pdf │ ├── CopyBuffered.class │ ├── CopyBuffered.java │ ├── MyBufferedInputStream.class │ └── MyBufferedOutputStream.class │ └── 09 │ ├── 01-Intro.pdf │ ├── 09-Exceptions.pdf │ ├── CopyBuffered.class │ └── CopyBuffered.java └── 10-streams └── examples ├── 01 ├── CountCountries.class ├── CountCountries.java ├── CountCountriesStream.class ├── CountCountriesStream.java └── ListCountries.class ├── 02 ├── LimitCountriesStream.class └── LimitCountriesStream.java ├── 03 ├── PrintCountriesStream.class └── PrintCountriesStream.java ├── 04 ├── ConcatCountriesStream.class ├── ConcatCountriesStream.java ├── GroupByCountriesStream.class └── GroupByCountriesStream.java ├── 05 ├── ProcessCountriesStream.class └── ProcessCountriesStream.java ├── 06 ├── ProcessCountriesStream.class └── ProcessCountriesStream.java └── 07 ├── ProcessOptionalCountriesStream.class └── ProcessOptionalCountriesStream.java /README.md: -------------------------------------------------------------------------------- 1 | # Αντικειμενοστραφής προγραμματισμός με τη γλώσσα προγραμματισμού Java 2 | ### _update: 2023-24_ 3 | 4 | Υλικό μαθήματος [HY-130] Αντικειμενοστρεφής Προγραμματισμός, Τμ. Πληροφορικής, Ιόνιο Πανεπιστήμιο 5 | https://opencourses.ionio.gr/courses/DDI171/ 6 | 7 | Το υλικό μελέτης κάθε εβδομάδας βρίσκεται στον αντίστοιχο φάκελο. 8 | -------------------------------------------------------------------------------- /img/c5f015.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/img/c5f015.png -------------------------------------------------------------------------------- /img/f03c15.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/img/f03c15.png -------------------------------------------------------------------------------- /img/ffa500.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/img/ffa500.png -------------------------------------------------------------------------------- /lab-java-01/HelloWorld.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση HelloWord προγράμματος 3 | * Αρχείο πηγαίου κώδικα HelloWorld.java 4 | * 5 | * Πρώτο πρόγραμμα σε Java. Εμφανίζει το κείμενο Hello, World στο τερματικό. 6 | * 7 | * Compilation: javac HelloWorld.java 8 | * Execution: java HelloWorld 9 | * 10 | ******************************************************************************/ 11 | 12 | public class HelloWorld { 13 | 14 | public static void main(String[] args) { 15 | // Prints "Hello, World" to the terminal window. 16 | System.out.println("Hello, World"); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /lab-java-01/HelloWorld_1.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση HelloWord προγράμματος 3 | * Αρχείο πηγαίου κώδικα HelloWorld.java 4 | * 5 | * Πρώτο πρόγραμμα σε Java. Εμφανίζει το κείμενο Hello, World στο τερματικό. 6 | * 7 | * Compilation: javac HelloWorld.java 8 | * Execution: java HelloWorld 9 | * 10 | ******************************************************************************/ 11 | 12 | public class HelloWorld { 13 | 14 | public static void main(String[] args) { 15 | // Prints "Hello, World" to the terminal window. 16 | System.out.println("Hello, World _1"); 17 | } 18 | } -------------------------------------------------------------------------------- /lab-java-01/HelloWorld_2.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση HelloWord προγράμματος 3 | * Αρχείο πηγαίου κώδικα HelloWorld.java 4 | * 5 | * Πρώτο πρόγραμμα σε Java. Εμφανίζει το κείμενο Hello, World στο τερματικό. 6 | * 7 | * Compilation: javac HelloWorld.java 8 | * Execution: java HelloWorld 9 | * 10 | ******************************************************************************/ 11 | 12 | public class HelloWorld_2 { 13 | 14 | public static void main(String[] args) { 15 | // Prints "Hello, World" to the terminal window. 16 | System.out.printlr("Hello, World _2"); 17 | } 18 | } -------------------------------------------------------------------------------- /lab-java-01/HelloWorld_3.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση HelloWord προγράμματος 3 | * Αρχείο πηγαίου κώδικα HelloWorld.java 4 | * 5 | * Πρώτο πρόγραμμα σε Java. Εμφανίζει το κείμενο Hello, World στο τερματικό. 6 | * 7 | * Compilation: javac HelloWorld.java 8 | * Execution: java HelloWorld 9 | * 10 | ******************************************************************************/ 11 | 12 | public static void main(String[] args) { 13 | // Prints "Hello, World" to the terminal window. 14 | System.out.printlr("Hello, World _3"); 15 | } 16 | -------------------------------------------------------------------------------- /lab-java-01/README.md: -------------------------------------------------------------------------------- 1 | ### Εργαστήριο #1 - Εισαγωγή στη Java, τη JVM και το JDK 2 | ___ 3 | Σκοπός του πρώτου εργαστηρίου είναι να εξοικειωθείτε με τη Java VM, το Java Development Kit στο περιβάλλον επεξεργασίας πηγαίου κώδικα IDE Atom ή στο GoormIDE. 4 | 5 | ___ 6 | #### Προετοιμασία (_πριν το εργαστήριο_) #### 7 | 8 | * **[Desktop: Linux/Mac/Windows]** Βεβαιωθείτε ότι στην **επιφάνεια εργασίας** που εργάζεστε είναι εγκατεστημένα και ρυθμισμένα τα ακόλουθα: 9 | * **Java Virtual Machine & Development Kit** 10 | * εκτελέστε σε ένα τερματικό την εντολή `java -version`, αν λάβετε απάντηση με ένα αριθμό έκδοσης, τότε η **JVM** είναι εγκατεστημένη 11 | * εκτελέστε σε ένα τερματικό την εντολή `javac -version`, αν λάβετε απάντηση με ένα αριθμό έκδοσης, τότε το **JDK** (compiler και άλλα εργαλεία) είναι εγκατεστημένο 12 | * **αν δεν είναι** εγκατεστημένο κάτι από τα δύο (ή είναι σε παλαιότερη έκδοση) κάντε λήψη και εγκατάσταση από: https://adoptopenjdk.net/ 13 | 14 | * **Pulsar IDE + packages** (λήψη από: https://pulsar-edit.dev) 15 | * βεβαιωθείτε ότι είναι εγκατεστημένο το Atom IDE 16 | * ελέγξτε (από το μενού `Packages > Settings View > Install Packages/Themes`) ότι έχουν εγκατασταθεί τα : 17 | - script 18 | - tool-bar 19 | - tool-bar-atom 20 | - terminus 21 | - basic-java-compile 22 | - autocomplete-java 23 | - linter-java και dependencies 24 | 25 | * **[Cloud]** _Εναλλακτικά_ για το cloud based IDE GoormIDE βεβαιωθείτε ότι έχετε δημιουργήσει ένα java based container, ακολουθώντας τις οδηγίες στις διαφάνειες της πρώτης διάλεξης. 26 | * Ελέξτε όπως πιο πάνω την ύπαρξης **σωστής έκδοσης** **JVM** και **JDK**. 27 | 28 | ___ 29 | #### Μελέτη (_πριν έρθετε στο εργαστήριο_): #### 30 | * Μελετήστε και εκτελέστε τα παραδείγματα του: 31 | https://www.freecodecamp.org/news/command-line-for-beginners/ 32 | - Σε περιβάλλον linux ή mac εκτελούνται στο terminal 33 | - Στο cloud IDE goorm εκτελούνται στο παρεχόμενο terminal εντός του browser 34 | - Σε περιβάλλον win εκτελούνται σε περιβάλλον WLS (Windows Linux Subsystem) 35 | * 😱 Help needed? https://github.com/riggas-ionio/java/discussions 36 | 37 | ___ 38 | #### Ασκήσεις #### 39 | * Στο περιβάλλον codeckeck λύστε το Parsons puzzle `Hello World`: 40 | https://codecheck.io/files/2210040845ehutdcsp7wbjhky9u663v9xdx 41 | * Όταν πετύχετε να κάνετε `compile` την κλάση: 42 | ![Download from codecheck](codecheck.png) 43 | 1. κάντε **Download** το αποτέλεσμα 44 | 2. μετονομάστε το **στο αριθμητικό μέρος του ΑΜ σας** (πχ `2000123.zip`) και 45 | 3. ανεβάστε το στη μη αξιολογούμενη εργασία της εβδομάδας **στο opencourses** 46 | * Σε όποιο περιβάλλον δουλεύετε (desktop ή GoormIDE): 47 | * Κατεβάστε αυτό το repo για να έχετε τον κώδικά του (και να λαμβάνεται τον κώδικα των επόμενων εβδομάδων) 48 | * Προτείνεται να το κατεβάσετε εκτελώντας (_εντός του φακέλου που θα δουλεύετε!!_) 49 | `git clone https://github.com/courses-ionio/java.git` 50 | Αν η εντολή git δεν είναι εγκατεστημένη στο σύστημά σας: 51 | https://www.atlassian.com/git/tutorials/install-git 52 | * Εναλλακτικά, κατεβάστε όλο το branch ως zip αρχείο (_..αλλά θα πρέπει κάθε εβδομάδα να κάνετε το ίδιο.. :-(_ ) από 53 | https://github.com/courses-ionio/java/archive/refs/heads/master.zip 54 | * Μετακινηθείτε **από το τερματικό** σας στο φάκελο της πρώτης εβδομάδας `lab-java-1` 55 | * **Aπό το τερματικό** κάνετε `compile` και στη συνέχεια εκτελέστε την κλάση HelloWorld που βρίσκεται στο φάκελο `lab-java-1`. 56 | * **Στον editor** όποιου IDE χρησιμοποιείτε, μελετήστε τα σφάλματα του compiler για τις κλάσεις `HelloWorld_`(1,2,3 αντίστοιχα), διορθώστε τα (_στον editor_) και στη συνέχεια κάντε `compile` τις κλάσεις και εκτελέστε τες. 57 | -------------------------------------------------------------------------------- /lab-java-01/codecheck.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lab-java-01/codecheck.png -------------------------------------------------------------------------------- /lab-java-03/README.md: -------------------------------------------------------------------------------- 1 | ### Εργαστήριο #3 - Κλάσεις & Αντικείμενα 2 | ___ 3 | Σκοπός του δεύτερου εργαστηρίου είναι να εξοικειωθείτε με βασικά στοιχεία της γλώσσας προγραμματισμού Java: Κλάσεις & Αντικείμενα. 4 | Οι ασκήσεις αυτής της εβδομάδας είναι self-paced και υλοποιούνται μέσω του codecheck. 5 | Όπως και την περασμένη εβδομάδα, **υποβάλετε στο opencourses** τον αρχείο που κάνετε `Download` για την **τελευταία (μόνο) άσκηση (=αυτοαξιολόγησης)**. 6 | 7 | ___ 8 | #### Ασκήσεις εξάσκησης #### 9 | 10 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 11 | Τροποποιήστε τη μέθοδο `withdraw` (γραμμές κώδικα 40-48), ώστε σε κάθε ανάληψη να αφαιρεί και `$1.00` επιπλέον ως τραπεζικά έξοδα. 12 | https://codecheck.io/files/wiley/codecheck-bj-4-class-101 13 | 14 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 15 | Τροποποιήστε τις μεθόδους `deposit` και `withdraw` (γραμμές κώδικα 33-41 και 47-55), ώστε σε κάθε συναλλαγή να καταγράφει το πλήθος τους (στο μέλος δεδομένων `transactions`). 16 | https://codecheck.io/files/wiley/codecheck-bj-4-class-102 17 | 18 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 19 | Συμπληρώστε τους constructors ώστε σε κάθε άνοιγμα λογαριασμού να δίνεται bonus `$10.00`. 20 | https://codecheck.io/files/wiley/codecheck-bj-4-class-103 21 | 22 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 23 | Συμπληρώστε τον κώδικα της κλάσης, κατασκευαστή και μεθόδους, προσέχοντας ιδιαίτερα τη μορφή που πρέπει να έχει η επιστρεφόμενη τιμή της `getTime()`. 24 | https://codecheck.io/files/wiley/codecheck-bj-4-class-104 25 | 26 | 3. ![HARD](https://via.placeholder.com/15/f03c15/f03c15.png) `HARD` 27 | Συμπληρώστε τον κώδικα της κλάσης, κατασκευαστή και μεθόδους, προσέχοντας ιδιαίτερα τη μορφή που πρέπει να έχει η επιστρεφόμενη τιμή και οι παράμετροι της `setStartTime()`. 28 | https://codecheck.io/files/wiley/codecheck-bj-4-class-105 29 | 30 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 31 | Συμπληρώστε τον κώδικα της κλάσης, κατασκευαστές και μεθόδους, η οποία αναπαριστά ένα ορθογώνιο ή τετράγωνο, η διαφορά τους είναι ότι το τετράγωνο έχει ίδιο πλάτος και ύψος. Θυμηθείτε ο τύπος της περιμέτρου είναι 32 | $${2 * width + 2 * height}$$ 33 | ενώ του εμβαδού 34 | $${width * height}$$ 35 | https://codecheck.io/files/wiley/codecheck-bjlo-1-class-103 36 | 37 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 38 | Συμπληρώστε τον κώδικα της κλάσης, κατασκευαστές και μεθόδους, η οποία αναπαριστά ένα τετράγωνο, και η οποία έχει και μια μέθοδο `grow()` η οποία αυξάνει το μήκος πλευράς κατά ένα ποσοστό. 39 | https://codecheck.io/files/wiley/codecheck-bjlo-1-class-110 40 | 41 | 42 | #### Άσκηση αυτοαξιολόγησης #### 43 | _**Επίλυση > codecheck > Download > Υποβολή στον opencourses**_ 44 | 45 | * ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 46 | Συμπληρώστε τον κώδικα της κλάσης, κατασκευαστές και μεθόδους, η οποία αναπαριστά ένα ορθογώνιο παραλληλεπίπεδο (_ένα κουτί_) και παρέχει μεθόδους για τον υπολογισμό του όγκου 47 | $${ width * height * depth }$$ 48 | 49 | και της επιφάνειας 50 | $${2 * (width * height + height * depth + depth * width)}$$ 51 | 52 | του κουτιού. 53 | 54 | https://codecheck.io/files/wiley/codecheck-bjlo-1-class-111 55 | -------------------------------------------------------------------------------- /lab-java-04/README.md: -------------------------------------------------------------------------------- 1 | ### Εργαστήριο #4 - Μέθοδοι 2 | ___ 3 | Σκοπός του τέταρτου εργαστηρίου είναι να εξοικειωθείτε με βασικά στοιχεία της γλώσσας προγραμματισμού Java: Μέθοδοι. 4 | Οι ασκήσεις αυτής της εβδομάδας είναι self-paced και υλοποιούνται μέσω του codecheck. 5 | Όπως και τις περασμένες εβδομάδες, **υποβάλετε στο opencourses** τον αρχείο που κάνετε `Download` για την **τελευταία (μόνο) άσκηση (=αυτοαξιολόγησης)**. 6 | 7 | ___ 8 | #### Ασκήσεις εξάσκησης #### 9 | 10 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 11 | Συμπληρώστε την υλοποίηση της μεθόδου `decimalDegrees` ώστε να μετατρέπει μια αναπαράσταση γωνίας σε μοίρες, πρώτα και δεύτερα λεπτά σε δεκαδική. 12 | Hint: διαιρείτε τα πρώτα λεπτά με εξήντα και ανάλογα τα δεύτερα λεπτά. 13 | Hint: προσοχή σε θετικές και αρνητικές γωνίες. 14 | https://codecheck.io/files/wiley/codecheck-bj-4-fundat-106 15 | 16 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 17 | Συμπληρώστε την υλοποίηση της μεθόδου `evaluate` ώστε να επιστρέφει τον υπολογισμό που υποδεικνύει η πρώτη παράμετρος. 18 | https://codecheck.io/files/wiley/codecheck-bjlo-1-funct-111 19 | 20 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 21 | Συμπληρώστε την υλοποίηση της μεθόδου `countSevens` ώστε να επιστρέφει το πλήθος των ψηφίων `7` που εμφανίζονται σε ένα αριθμό. 22 | https://codecheck.io/files/wiley/codecheck-bjlo-1-funct-109 23 | 24 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 25 | Συμπληρώστε την υλοποίηση της μεθόδου `convert` ώστε να μετατρέπει μια αναπαράσταση γωνίας από δεκαδική σε μοίρες, πρώτα και δεύτερα λεπτά. 26 | Hint: η `Math.round(double)` στρογγυλοποιεί ένα αριθμό κινητής υποδιαστολής, στον κοντινότερο ακέραιο αλλά ο τύπος παραμένει κινητής υποδιαστολής. Αν θέλετε τον κοντινότερο ακέραιο χρειάζεται explicit casting. 27 | https://codecheck.io/files/wiley/codecheck-bj-4-fundat-107 28 | 29 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 30 | Συμπληρώστε τον κώδικα της κλάσης, υλοποιώντας μία **στατική** μέθοδο με όνομα `sumOddsInRange` και δύο παραμέτρους τύπου `int` για πάνω και κάτω όριο. Εντός της main υπολογίστε το ζητούμενο (άθροισμα όλων των περιττών αριθμών μεταξύ των δύο τιμών που δίνει ο χρήστης) με κλήση της μεθόδου που υλοποιήσατε. 31 | https://codecheck.io/files/wiley/codecheck-bjlo-1-loop-102 32 | 33 | 3. ![HARD](https://via.placeholder.com/15/f03c15/f03c15.png) `HARD` 34 | Συμπληρώστε τον κώδικα της κλάσης, υλοποιώντας μία **στατική** μέθοδο με όνομα `binaryDigits` και παράμετρο τύπου `int`, η οποία μετατρέπει την παράμετρο από δεκαδική σε δυαδική μορφή (με όποιο τρόπο ξέρετε/μπορείτε) και επιστρέφει το πλήθος των δυαδικών ψηφίων του αριθμού. 35 | https://codecheck.io/files/wiley/codecheck-bj-4-class2-104 36 | 37 | 3. ![HARD](https://via.placeholder.com/15/f03c15/f03c15.png) `HARD` 38 | Συμπληρώστε τον κώδικα της κλάσης, υλοποιώντας μία **αναδρομική στατική** μέθοδο με όνομα `factorial` και παράμετρο μία παράμετρο τύπου `int`, έστω `n`. Η μέθοδος `factorial` που υλοποιείτε θα πρέπει να επιστρέφει το γινόμενο $${ n * factorial(n-1) }$$ για `n > 1` ή `1` αλλιώς. Εντός της main καλέστε τη `factorial` με παράμετρο την τιμή που δίνει ο χρήστης. 39 | https://codecheck.io/files/wiley/codecheck-bjlo-1-loop-107 40 | 41 | 42 | #### Άσκηση αυτοαξιολόγησης #### 43 | _**Επίλυση > codecheck > Download > Υποβολή στον opencourses**_ 44 | 45 | * ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 46 | Συμπληρώστε τον κώδικα της μεθόδου `countPerfectSquares` ώστε να επιστρέφει το πλήθος των τέλειων τετραγώνων στο εύρος των δύο παραμέτρων `a` και `b` της μεθόδου. 47 | Hint: ένα τέλειο τετράγωνο είναι ένας φυσικός αριθμός που είναι τετράγωνο ενός φυσικού αριθμού. Πώς μπορεί η `Math.sqrt` να σας βοηθήσει; 48 | https://codecheck.io/files/wiley/codecheck-bj-4-loop-104 49 | -------------------------------------------------------------------------------- /lab-java-07/README.md: -------------------------------------------------------------------------------- 1 | ### Εργαστήριο #7 - Αφηρημένες κλάσεις - Διεπαφές - Απαριθμήσεις 2 | ___ 3 | Σκοπός του πέμπτου εργαστηρίου είναι να εξοικειωθείτε με βασικά στοιχεία της γλώσσας προγραμματισμού Java: Αφηρημένες κλάσεις - Διεπαφές - Απαριθμήσεις. 4 | Οι ασκήσεις αυτής της εβδομάδας είναι self-paced και υλοποιούνται μέσω του codecheck. 5 | Δεν υπάρχει αυτοαξιολογούμενη άσκηση προς υποβολή. Είναι εβδομάδα ενδιάμεσης αξιολόγησης. 6 | 7 | ___ 8 | #### Ασκήσεις εξάσκησης #### 9 | 10 | 11 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 12 | Συμπληρώστε την κλάση `Sentence` έτσι ώστε να υλοποιεί το `interface Measurable`. Προσέξτε τα μέλη δεδομένων που χρειάζονται στην κλάση `Sentence` δηλώνονται στις γραμμές 42, 43, δεν λείπουν. 13 | https://codecheck.io/files/wiley/codecheck-bj-4-inter-101 14 | 15 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 16 | Παρέχετε τον κώδικα της κλάσης `WordMeasurer` η οποία πρέπει να υλοποιεί το `interface Measurer`. 17 | Hint: Η `measure()` που θα υλοποιεί η `WordMeasurer` θα πρέπει να παίρνει ως παράμετρο αντικείμενο της κλάσης Object (αφού το `interface` δεν είναι generic) όμως εντός της μεθόδου θέλετε να μετράτε τις συλλαβές κάθε λέξης αξιοποιώντας την `countSyllables()` της `Word`. Επιλύστε αυτή την ασυμφωνία με explicit casting. 18 | https://codecheck.io/files/wiley/codecheck-bj-4-inter-102 19 | 20 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 21 | Συμπληρώστε την υλοποίηση της απαρίθμησης `Element`. Για κάθε χημικό στοιχείο χρειάζεται να αποθηκεύετε το όνομά του, το σύμβολό του και το σημείο τήξης του. Στον κώδικα που δίνεται, συμπληρώστε την υλοποίηση του constructor της απαρίθμησης, κάντε τη δήλωση όλων των χημικών στοιχείων ως τιμές της απαρίθμησης και υλοποιήστε και τη μέθοδο `getMeltedElements()` η οποία παίρνει ως παράμετρο μια θερμοκρασία και από όλα τα στοιχεία της απαρίθμησης επιστρέφει σε δομή `ArrayList` όσα έχουν μικρότερη ή ίση θερμοκρασία τήξης. 22 | https://codecheck.io/files/wiley/codecheck-bj-4-inher-108 23 | 24 | 3. ![HARD](https://via.placeholder.com/15/f03c15/f03c15.png) `HARD` 25 | Δίνονται δύο κλάσεις, η `Fluorescent` και η `Incandescent` που αναπαριστούν λάμπες φθορίου και πυρακτώσεως. Ζητείται από εσάς να συμπληρώσετε την κλάση πρόγονό τους `LightBulb`. Η `LightBulb` πρέπει να είναι `abstract`. 26 | _Hint_: και οι δύο constructor των υποκλάσεων καλούν την `super(lumens)`, άρα τι constructor πρέπει να παρέχει η κοινή υπερκλάση; 27 | _Food for though_: η `main` καλεί τη μέθοδο `getDescription()` επί αντικειμένων των δύο υποκλάσεων. Όμως.. καμία από τις δύο υποκλάσεις (τις οποίες δεν μπορείτε να επεξεργαστείτε) δεν περιλαμβάνει τέτοια μέθοδο. Πώς καλείται αυτή; 28 | _Food for though_: H `getDescription()` της υπερκλάσης χρειάζεται να περιλαμβάνει το `shape` κάθε υποκλάσης, αλλά η υπερκλάση δεν έχει τέτοιο μέλος δεδομένων και ακόμα και δεν έχει νόημα να το προσθέσετε την υπερκλάση αφού δεν μπορείτε να επεξεργαστείτε τις υποκλάσεις. Μπορείτε μια `abstract` μέθοδος στην υπερκλάση να λύσει το αδιέξοδο; 29 | _Hint_: Θυμηθείτε τη String.format για να μορφοποιήσετε το κείμενο όπως ακριβώς ζητείται. 30 | https://codecheck.io/files/wiley/codecheck-bj-4-inher-109 31 | -------------------------------------------------------------------------------- /lab-java-09/README.md: -------------------------------------------------------------------------------- 1 | ### Εργαστήριο #9 - Διαχείριση ροών εισόδου-εξόδου 2 | ___ 3 | Σκοπός του όγδοου εργαστηρίου είναι να εξοικειωθείτε με βασικά στοιχεία της γλώσσας προγραμματισμού Java: Διαχείριση ροών εισόδου-εξόδου. 4 | Οι ασκήσεις αυτής της εβδομάδας είναι self-paced και υλοποιούνται μέσω του codecheck. 5 | Δεν υπάρχει αυτοαξιολογούμενη άσκηση προς υποβολή. 6 | Είναι εβδομάδα ενδιάμεσης αξιολόγησης. 7 | ___ 8 | #### Ασκήσεις εξάσκησης #### 9 | 10 | 11 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 12 | Συμπληρώστε την υλοποίηση της κλάσης `LineReverser` έτσι ώστε να διαβάζει τις γραμμές ενός αρχείου κειμένου και να τις αποθηκεύει σε ένα άλλο αρχείο κειμένου _με αντίστροφη σειρά_. 13 | https://codecheck.io/files/wiley/codecheck-bj-4-io-101 14 | 15 | 1. ![EASY](https://via.placeholder.com/15/c5f015/c5f015.png) `EASY` 16 | Συμπληρώστε τον κώδικα της μεθόδου `longestWord` έτσι ώστε να διαβάζει ένα αρχείο κειμένου και να επιστρέφει τη λέξη με το μεγαλύτερο μήκος. Αν υπάρχουν πολλές λέξεις με το ίδιο μέγιστο μήκος να επιστρέφει την πρώτη. 17 | https://codecheck.io/files/wiley/codecheck-bj-4-io-102 18 | 19 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 20 | Συμπληρώστε την υλοποίηση της κλάσης `ExamAverage` έτσι ώστε να διαβάζει τις γραμμές ενός αρχείου κειμένου το οποίο περιέχει βαθμολογίες, να τις επεξεργάζεται και να τις αποθηκεύει σε ένα άλλο αρχείο κειμένου προσθέτοντας στο τέλος το πλήθος των βαθμολογιων που διάβασε και το μέσο όρο τους. 21 | https://codecheck.io/files/wiley/codecheck-bj-4-io-106 22 | 23 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 24 | Συμπληρώστε την υλοποίηση της κλάσης `UpcaseNames` έτσι ώστε να διαβάζει τις γραμμές ενός αρχείου κειμένου το οποίο περιέχει ονόματα, να τις επεξεργάζεται και να τις αποθηκεύει σε ένα άλλο κάνοντας κεφαλαίο το πρώτο γράμμα κάθε λέξης σε κάθε γραμμή. 25 | https://codecheck.io/files/wiley/codecheck-bj-4-io-107 26 | 27 | 2. ![MEDI](https://via.placeholder.com/15/ffa500/ffa500.png) `MEDI` 28 | Συμπληρώστε την υλοποίηση της κλάσης `WordCounter` έτσι ώστε να διαβάζει τις γραμμές ενός αρχείου κειμένου και να τις αποθηκεύει σε ένα άλλο αρχείο κειμένου προσθέτοντας μπροστά από κάθε γραμμή το πλήθος των λέξεων της γραμμής και τρεις κενούς χαρακτήρες. 29 | _Hint_: μπορείτε να χρησιμοποιήσετε τη μέθοδο [String.split()](https://docs.oracle.com/javase/7/docs/api/java/lang/String.html#split(java.lang.String) για να χωρήσετε τις λέξεις, θεωρήστε ότι το χώρισμα γίνεται μόνο στα κενά. 30 | https://codecheck.io/files/wiley/codecheck-bjlo-1-io-101 31 | 32 | 3. ![HARD](https://via.placeholder.com/15/f03c15/f03c15.png) `HARD` 33 | Συμπληρώστε την υλοποίηση της κλάσης `ReplaceVowels` έτσι ώστε να διαβάζει τις γραμμές ενός αρχείου κειμένου και να τις αποθηκεύει σε ένα άλλο αρχείο κειμένου αντικαθιστώντας όλα τα λατινικά φωνήεντα (καφαλαί και μικρά) με κάτω παύλα (`_`). 34 | https://codecheck.io/files/wiley/codecheck-bjlo-1-io-102 35 | -------------------------------------------------------------------------------- /lecture-examples/02-essentials/ArgsReader.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση ArgsReader προγράμματος 3 | * Αρχείο πηγαίου κώδικα ArgsReader.java 4 | * 5 | * Πρόγραμμα σε Java το οποίο διαβάζει μια παράμετρο από το τερματικό 6 | * και την εμφανίζει πάλι σε αυτό. 7 | * 8 | * Compilation: javac ArgsReader.java 9 | * Execution: java ArgsReader 10 | * 11 | ******************************************************************************/ 12 | 13 | public class ArgsReader { 14 | 15 | public static void main(String[] args){ 16 | 17 | for ( int i=0; i< args.length; i++) 18 | { 19 | String s1 = args[i]; 20 | System.out.println("Δώσατε παράμετρο: "+s1); 21 | } 22 | 23 | } 24 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Blocks.java: -------------------------------------------------------------------------------- 1 | public class Blocks { 2 | 3 | public static void main(String[] args){ 4 | int i = 100000; 5 | { 6 | int i2 = i+1; 7 | System.out.println(i2); 8 | } 9 | 10 | System.out.println(i); 11 | } 12 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Blocks2.java: -------------------------------------------------------------------------------- 1 | public class Blocks2 { 2 | 3 | public static void main(String[] args){ 4 | int i = 100000; 5 | int i2 = i+1; 6 | { 7 | int i3 = i2+1; 8 | System.out.println(i3); 9 | } 10 | 11 | System.out.println(i2); 12 | System.out.println(i); 13 | 14 | } 15 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Blocks3.java: -------------------------------------------------------------------------------- 1 | public class Blocks3 { 2 | 3 | public static void main(String[] args){ 4 | 5 | boolean b = true; 6 | int i; 7 | if (b==true) { 8 | i =5; // Σχολιάστε αυτή τη γραμμή και δείτε το αποτέλεσμα 9 | } 10 | else { 11 | i =10; 12 | } 13 | 14 | System.out.println(i); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lecture-examples/02-essentials/CompFloat.java: -------------------------------------------------------------------------------- 1 | public class CompFloat { 2 | 3 | public static void main(String[] args){ 4 | 5 | float f1, f2; 6 | f1=0.001F; 7 | f2= 1.001F - 1.0F; 8 | // f2 = 0.001F; 9 | System.out.println("Είναι f1==f2 "+(f1==f2)); 10 | 11 | for (float f=0.0F; f!=1.0F; f+=0.1F ) { 12 | System.out.println("F is "+f); 13 | if ( f>1.1F) break; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Constants.java: -------------------------------------------------------------------------------- 1 | public class Constants { 2 | final static double VAT = 0.23; // ποσοστό ΦΠΑ 3 | 4 | public static void main(String[] args) { 5 | final double REDUCTION; 6 | REDUCTION = 0.15; // 15% έκπτωση 7 | 8 | double price = 60.0; // 60€ καθαρή αξία 9 | double ppv = price + price * VAT; 10 | double pay = ppv - ppv * REDUCTION; 11 | System.out.println("Καθαρή αξία: "+price); 12 | System.out.println("Aξία + ΦΠΑ: "+ppv); 13 | System.out.println("Πληρωτέο μετά την έκπτωση: "+pay); 14 | } 15 | 16 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Defaults.java: -------------------------------------------------------------------------------- 1 | public class Defaults { 2 | 3 | static int member; 4 | static boolean b; 5 | 6 | public static void main(String[] args){ 7 | int local=-1; 8 | 9 | final int f ; 10 | 11 | System.out.println(member); 12 | System.out.println(b); 13 | System.out.println(local); 14 | 15 | f = 5; 16 | // f = 6; 17 | System.out.println(f); 18 | int k; 19 | for (k = 0; k<3; k++) { 20 | System.out.println(k); 21 | } 22 | System.out.println(k); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/DoMath.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/02-essentials/DoMath.class -------------------------------------------------------------------------------- /lecture-examples/02-essentials/DoMath.java: -------------------------------------------------------------------------------- 1 | import java.lang.Math; 2 | 3 | class DoMath { 4 | public static void main(String args[]) 5 | { 6 | double a = 30; 7 | double b = 2; 8 | double c = 12.75; 9 | double d = Math.PI; 10 | 11 | System.out.println(Math.pow(a, b)); 12 | System.out.println(Math.sqrt(a)); 13 | System.out.println(Math.floor(c)); 14 | System.out.println(Math.cos(d)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lecture-examples/02-essentials/GetUserInfo.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class GetUserInfo 3 | { 4 | public static void main(String[] args) 5 | { 6 | String name; 7 | int age; 8 | Scanner inputDevice = new Scanner(System.in); 9 | System.out.print("Please enter your name >> "); 10 | name = inputDevice.nextLine(); 11 | System.out.print("Please enter your age >> "); 12 | age = inputDevice.nextInt(); 13 | System.out.println("Your name is " + name + 14 | " and you are " + age + " years old."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lecture-examples/02-essentials/GetUserInfo_NB.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | public class GetUserInfo_NB 3 | { 4 | public static void main(String[] args) 5 | { 6 | String name; 7 | int age; 8 | Scanner inputDevice = new Scanner(System.in); 9 | System.out.print("Please enter your age >> "); 10 | age = inputDevice.nextInt(); 11 | // Δείτε τι συμβαίνει αν παραληφθεί η επόμενη εντολή: 12 | inputDevice.nextLine(); 13 | System.out.print("Please enter your name >> "); 14 | name = inputDevice.nextLine(); 15 | System.out.println("Your name is " + name + 16 | " and you are " + age + " years old."); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /lecture-examples/02-essentials/IntReader.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση IntReader προγράμματος 3 | * Αρχείο πηγαίου κώδικα IntReader.java 4 | * 5 | * Πρόγραμμα σε Java το οποίο διαβάζει μια παράμετρο από το τερματικό 6 | * και την εμφανίζει πάλι σε αυτό. 7 | * @author Dimitrios Ringas 8 | * Compilation: javac IntReader.java 9 | * Execution: java IntReader 10 | * 11 | ******************************************************************************/ 12 | 13 | public class IntReader { 14 | 15 | public static void main(String[] args){ 16 | 17 | int i1 = Integer.parseInt( args[0] ); 18 | System.out.println("Δώσατε παράμετρο τον ακέραιο: "+i1); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/IntScanner.java: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * Τεκμηρίωση IntScanner προγράμματος 3 | * Αρχείο πηγαίου κώδικα IntScanner.java 4 | * 5 | * Πρόγραμμα σε Java το οποίο προτρέπει το χρήστη να δώσει ένα ακέραιο αριθμό 6 | * και τον εμφανίζει πάλι στο τερματικό. 7 | * 8 | * Compilation: javac IntScanner.java 9 | * Execution: java IntScanner 10 | * 11 | ******************************************************************************/ 12 | import java.util.Scanner; 13 | 14 | public class IntScanner { 15 | 16 | public static void main(String[] args){ 17 | 18 | Scanner sc = new Scanner(System.in); 19 | 20 | System.out.println("Πληκτρολογήστε ένα ακέραιο: "); 21 | 22 | int i1 = sc.nextInt(); 23 | System.out.println("Πληκτρολογήσατε τον ακέραιο: "+i1); 24 | 25 | } 26 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Literals.java: -------------------------------------------------------------------------------- 1 | public class Literals { 2 | 3 | static byte b; 4 | static short s; 5 | static int i; 6 | static long l; 7 | 8 | static float f; 9 | static double d, d2; 10 | 11 | 12 | public static void main(String[] args){ 13 | int i = 100000; 14 | long l = 100000L; 15 | byte b = (byte)127; 16 | short s = (short) 32000; 17 | 18 | float f = 123.4F; 19 | double d = 123.4000; 20 | double d2 = 1.234e2; 21 | 22 | long l2 = 317_000_000_000L; 23 | 24 | char capitalC = 'C'; 25 | char lowerC = 'c'; 26 | 27 | char sQuotes = '\''; 28 | char dQuotes = '\"'; 29 | 30 | System.out.println(b); 31 | System.out.println(s); 32 | System.out.println(i); 33 | System.out.println(s); 34 | 35 | System.out.println(f); 36 | System.out.println(d); 37 | System.out.println(d2); 38 | 39 | System.out.println(l2); 40 | 41 | System.out.println(capitalC); 42 | System.out.println(lowerC); 43 | 44 | System.out.println(sQuotes); 45 | System.out.println(dQuotes); 46 | } 47 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/Op.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | public class Op { 5 | 6 | public static void main(String[] args){ 7 | 8 | 9 | System.out.println( "Η παράσταση είναι: " + ((4>5)?"Αληθές":"Ψευδές") ); 10 | 11 | } 12 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/TypeCast.java: -------------------------------------------------------------------------------- 1 | public class TypeCast { 2 | 3 | public static void main(String[] args){ 4 | int a = 4/2; 5 | int b = 5/2; 6 | 7 | int b2 = (int)5.0/2; 8 | 9 | double c= 4.0/2.0; 10 | double d = 5/2; 11 | 12 | System.out.println(a); 13 | System.out.println(b); 14 | System.out.println(b2); 15 | System.out.println(c); 16 | System.out.println(d); 17 | 18 | 19 | } 20 | } -------------------------------------------------------------------------------- /lecture-examples/02-essentials/WhileEffi.java: -------------------------------------------------------------------------------- 1 | public class WhileEffi { 2 | 3 | public static void main(String[] args){ 4 | 5 | int REPEAT = 300_000_000; 6 | long start, end; 7 | start = System.currentTimeMillis(); 8 | 9 | for ( int x=0; x0.0) 21 | this.weight = weight; 22 | } 23 | public void addTooth(){ 24 | this.numTeeth ++; 25 | } 26 | public int getNumTeeth(){ 27 | return this.numTeeth; 28 | } 29 | public void removeOtherChildsTooth(Baby other){ 30 | other.numTeeth--; 31 | } 32 | 33 | public static void main(String[] args) { 34 | // Create a new baby 35 | Baby george = new Baby("George", true); 36 | george.numTeeth = 3; 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/03-accessor-methods-cont-more/Other.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/03-classes-objects-intro/03-accessor-methods-cont-more/Other.class -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/03-accessor-methods-cont-more/Other.java: -------------------------------------------------------------------------------- 1 | 2 | public class Other { 3 | public static void main(String[] args){ 4 | Baby george = new Baby("George", true); 5 | Baby nancy = new Baby("Nancy", false); 6 | 7 | //george.numTeeth = 3; // error: numTeeth has private access in Baby 8 | nancy.addTooth(); nancy.addTooth(); 9 | george.removeOtherChildsTooth(nancy); 10 | 11 | System.out.println( 12 | "New baby is " + nancy.getName() + 13 | ", weight is " + nancy.getWeight() + 14 | ", has "+nancy.getNumTeeth()+" teeth."); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/03-accessor-methods-cont/Baby.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/03-classes-objects-intro/03-accessor-methods-cont/Baby.class -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/03-accessor-methods-cont/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public double getWeight () { 17 | return this.weight; 18 | } 19 | public void setWeight (double weight) { 20 | if (weight>0.0) 21 | this.weight = weight; 22 | } 23 | public void addTooth(){ 24 | this.numTeeth ++; 25 | } 26 | public int getNumTeeth(){ 27 | return this.numTeeth; 28 | } 29 | 30 | public static void main(String[] args) { 31 | // Create a new baby 32 | Baby george = new Baby("George", true); 33 | // Set/alter some attributes 34 | george.setWeight(3.420); 35 | george.addTooth(); 36 | // Print baby data 37 | System.out.println( 38 | "New baby is " + george.getName() + 39 | ", weight is " + george.getWeight() + 40 | ", has "+george.getNumTeeth()+" teeth."); 41 | // Prints: 42 | // New baby is George, weight is 3.420 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/03-accessor-methods-cont/Other.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/03-classes-objects-intro/03-accessor-methods-cont/Other.class -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/04-compare/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public double getWeight () { 17 | return this.weight; 18 | } 19 | public void setWeight (double weight) { 20 | this.weight = weight; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // Δημιουργούμε το πρώτο μωρό 25 | Baby george1 = new Baby("George", true); 26 | // Δημιουργούμε το δεύτερο μωρό 27 | Baby george2 = new Baby("George", true); 28 | 29 | // Συγκρίνουμε τα δυο αντικείμενα 30 | System.out.println(george1 == george2); 31 | // Τι θα εμφανίσει; 32 | } 33 | } 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/05-compare-more.1/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public double getWeight () { 17 | return this.weight; 18 | } 19 | public void setWeight (double weight) { 20 | this.weight = weight; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // Δημιουργούμε το πρώτο μωρό 25 | Baby george1 = new Baby("George", true); 26 | // Δημιουργούμε το δεύτερο μωρό 27 | Baby george2 = new Baby("George", true); 28 | 29 | Baby george3 = george1; 30 | 31 | double w1 = 4.300; 32 | double w2 = w1; 33 | w2 = 3.600; 34 | 35 | george1.setWeight(w1); 36 | george2.setWeight(w2); 37 | george3.setWeight(4.550); 38 | 39 | System.out.println( 40 | "Baby 1 is " + george1.getName() + 41 | ", weight is " + george1.getWeight()); 42 | 43 | george3 = george2; 44 | george3.setWeight(6.300); 45 | 46 | System.out.println( 47 | "Baby 2 is " + george2.getName() + 48 | ", weight is " + george2.getWeight()); 49 | 50 | 51 | } 52 | } 53 | 54 | 55 | 56 | 57 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/05-compare-more/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public double getWeight () { 17 | return this.weight; 18 | } 19 | public void setWeight (double weight) { 20 | this.weight = weight; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // Δημιουργούμε το πρώτο μωρό 25 | Baby george1 = new Baby("George", true); 26 | // Δημιουργούμε το δεύτερο μωρό 27 | Baby george2 = new Baby("George", true); 28 | 29 | Baby george3 = george1; 30 | 31 | 32 | // Συγκρίνουμε τα δυο αντικείμενα 33 | System.out.println(george1 == george2); 34 | // Συγκρίνουμε τα δυο αντικείμενα 35 | System.out.println(george1 == george3); 36 | // Τι θα εμφανίσει; 37 | } 38 | } 39 | 40 | 41 | 42 | 43 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/06-equals/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public double getWeight () { 17 | return this.weight; 18 | } 19 | public void setWeight (double weight) { 20 | this.weight = weight; 21 | } 22 | 23 | public static void main(String[] args) { 24 | // Δημιουργούμε ένα μωρό με όνομα George 25 | Baby george1 = new Baby("George", true); 26 | 27 | // Συγκρίνουμε το όνομα με ένα άλλο String George 28 | System.out.println(george1.getName() == new String("George")); 29 | System.out.println(george1.getName().equals( new String("George") )); 30 | System.out.println(george1.getName() == "George"); 31 | // Τι θα εμφανίσει; 32 | } 33 | } 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/06-equals/EqualsNull.java: -------------------------------------------------------------------------------- 1 | public class EqualsNull { 2 | public static void main(String[] args) { 3 | // Δημιουργούμε το πρώτο μωρό 4 | Baby george1 = new Baby("George", true); 5 | // Δημιουργούμε το δεύτερο μωρό 6 | Baby george2 = new Baby("George", true); 7 | 8 | Baby george3 = null; 9 | 10 | // Συγκρίνουμε τα δυο αντικείμενα 11 | System.out.println(george1.equals(george2)); 12 | // Συγκρίνουμε τα δυο αντικείμενα 13 | System.out.println(george1.equals(george3)); 14 | // Συγκρίνουμε τα δυο αντικείμενα 15 | System.out.println(java.util.Objects.equals(george3, george1)); 16 | System.out.println(java.util.Objects.equals(george1, george3)); 17 | System.out.println(george1.equals(george3)); 18 | // Αυτό όμως αποτυγχάνει με NullPointerException 19 | System.out.println(george3.equals(george1)); 20 | 21 | // Τι θα εμφανίσει; 22 | } 23 | 24 | } 25 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/06-equals/EqualsString.java: -------------------------------------------------------------------------------- 1 | public class EqualsString { 2 | public static void main(String[] args) { 3 | String hello = "Hello", lo = "lo"; 4 | final String const_lo = "lo"; 5 | System.out.println((hello == "Hello")); 6 | System.out.println((Other.hello == hello)); 7 | System.out.println((hello == ("Hel"+"lo"))); 8 | System.out.println((hello == ("Hel"+lo))); 9 | System.out.println((hello == ("Hel"+const_lo))); 10 | } 11 | } 12 | class Other { static String hello = "Hello"; } 13 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/07-toString/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public boolean getIsABoy() { 17 | return this.isABoy; 18 | } 19 | public double getWeight () { 20 | return this.weight; 21 | } 22 | public void setWeight (double weight) { 23 | this.weight = weight; 24 | } 25 | public int getNumTeeth () { 26 | return this.numTeeth; 27 | } 28 | public void addTooth () { 29 | this.numTeeth ++; 30 | } 31 | 32 | public String toString() { 33 | return "Baby is named "+this.name 34 | +" is " + (this.isABoy?"a boy":"a girl") 35 | +" weights " +this.weight 36 | +" and has " +this.numTeeth +" teeth"; 37 | } 38 | public static void main(String[] args) { 39 | // Δημιουργούμε ένα μωρό με όνομα George 40 | Baby george1 = new Baby("George", true); 41 | george1.addTooth(); george1.addTooth(); george1.addTooth(); 42 | 43 | // Εκτυπώνουμε με χρήση μεθόδων πρόσβασης 44 | System.out.println("Baby is named "+george1.getName() 45 | +" is " + ((george1.getIsABoy())?"a boy":"a girl") 46 | +" weights " +george1.getWeight() 47 | +" and has " +george1.getNumTeeth() +" teeth" 48 | ); 49 | // Τι θα εμφανίσει; 50 | 51 | // Δημιουργούμε ένα άλλο μωρό με όνομα Nancy 52 | Baby nancy = new Baby("Nancy", false); 53 | nancy.setWeight(3.200); 54 | nancy.addTooth(); nancy.addTooth(); 55 | // Εκτυπώνουμε με χρήση μεθόδων πρόσβασης 56 | System.out.println( nancy ); 57 | // Baby nancy2 = new Baby("Nancy2", false); 58 | // System.out.println( nancy2 ); 59 | // Τι θα εμφανίσει; 60 | 61 | Baby dimitris = new Baby("Dimitrios", true); 62 | System.out.println( dimitris ); 63 | 64 | } 65 | } 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/08-stringbuilder-stringbuffer/Baby.java: -------------------------------------------------------------------------------- 1 | public class Baby { 2 | private String name; 3 | private double weight = 4.0; 4 | private boolean isABoy; 5 | private int numTeeth = 0; 6 | 7 | public Baby(String name, boolean boy) 8 | { 9 | this.name = name; 10 | this.isABoy = boy; 11 | } 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | public boolean getIsABoy() { 17 | return this.isABoy; 18 | } 19 | public double getWeight () { 20 | return this.weight; 21 | } 22 | public void setWeight (double weight) { 23 | this.weight = weight; 24 | } 25 | public int getNumTeeth () { 26 | return this.numTeeth; 27 | } 28 | public void addTooth () { 29 | this.numTeeth ++; 30 | } 31 | 32 | public String toString() { 33 | StringBuilder sb = new StringBuilder(); 34 | sb.append("Baby is named ").append(this.name) 35 | .append(" is ").append( this.isABoy?"a boy":"a girl" ) 36 | .append(" weights ").append(this.weight) 37 | .append(" and has ").append(this.numTeeth).append(" teeth"); 38 | return sb.toString(); 39 | } 40 | public static void main(String[] args) { 41 | // Δημιουργούμε ένα μωρό με όνομα George 42 | Baby george1 = new Baby("George", true); 43 | george1.addTooth(); george1.addTooth(); george1.addTooth(); 44 | System.out.println( george1 ); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /lecture-examples/03-classes-objects-intro/08-stringbuilder-stringbuffer/StringEfficiency.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | public class StringEfficiency { 3 | public static void main(String[] args) { 4 | int N = 100_000; 5 | long t; 6 | Random r = new Random(); 7 | System.out.println("Append "+N+" times; times in millisec:"); 8 | 9 | { 10 | String sb = new String(); 11 | t = System.currentTimeMillis(); 12 | for (int i = N; i-- > 0 ;) { 13 | sb = sb.concat(Integer.toString(r.nextInt(1000))); 14 | } 15 | System.out.print(sb.getClass()+": "); 16 | System.out.println(System.currentTimeMillis() - t); 17 | } 18 | 19 | { 20 | StringBuffer sb = new StringBuffer(); 21 | t = System.currentTimeMillis(); 22 | for (int i = N; i-- > 0 ;) { 23 | sb.append(Integer.toString(r.nextInt(1000))); 24 | } 25 | System.out.print(sb.getClass()+": "); 26 | System.out.println(System.currentTimeMillis() - t); 27 | } 28 | 29 | { 30 | StringBuilder sb = new StringBuilder(); 31 | t = System.currentTimeMillis(); 32 | for (int i = N; i-- > 0 ; ) { 33 | sb.append(Integer.toString(r.nextInt(1000))); 34 | } 35 | System.out.print(sb.getClass()+": "); 36 | System.out.println(System.currentTimeMillis() - t); 37 | } 38 | } 39 | } 40 | -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/01.static/Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Calculator { 4 | public static int add(int i1, int i2) { 5 | int sum = i1 +i2; 6 | return sum; 7 | } 8 | 9 | public static void main (String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | int param1, param2; 12 | System.out.println("Provide two integers:"); 13 | param1 = sc.nextInt(); 14 | param2 = sc.nextInt(); 15 | 16 | int res = Calculator.add( param1, param2); 17 | System.out.println( param1 +" plus "+ param2 + " equals " +res); 18 | } 19 | } -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/01/Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Calculator { 4 | public int add(int i1, int i2) { 5 | int sum = i1 +i2; 6 | return sum; 7 | } 8 | 9 | public static void main (String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | int param1, param2; 12 | System.out.println("Provide two integers:"); 13 | param1 = sc.nextInt(); 14 | param2 = sc.nextInt(); 15 | 16 | Calculator clt = new Calculator(); 17 | int res = clt.add( param1, param2); 18 | System.out.println( param1 +" plus "+ param2 + " equals " +res); 19 | } 20 | } -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/01/ClassMethods.java: -------------------------------------------------------------------------------- 1 | public class ClassMethods { 2 | 3 | public static void hello(){ System.out.print("Hello "); } 4 | public static void main(String[] args){ 5 | hello(); 6 | world(); 7 | } 8 | public static void world(){ System.out.println("world!"); } 9 | } 10 | -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/02/Calculator.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Calculator { 4 | public int add(int i1, int i2) { 5 | int sum = i1 +i2; 6 | return sum; 7 | } 8 | //τραγική υλοποίηση πολλαπλασιασμού 9 | //κατάλληλη μόνο για φυσικούς αριθμούς 10 | public int multiply(int i1, int i2) { 11 | int multi =0; 12 | for ( int i=1; i<=i1; i++) 13 | multi = add(multi, i2); 14 | return multi; 15 | } 16 | 17 | public static void main (String[] args) { 18 | Scanner sc = new Scanner(System.in); 19 | int param1, param2; 20 | System.out.println("Provide two positive integers:"); 21 | param1 = sc.nextInt(); 22 | param2 = sc.nextInt(); 23 | 24 | Calculator clt = new Calculator(); 25 | int res = clt.add( param1, param2); 26 | System.out.println( param1 +" plus "+ param2 + " equals " +res); 27 | res = clt.multiply( param1, param2); 28 | System.out.println( param1 +" times "+ param2 + " equals " +res); 29 | } 30 | } -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/03/MethodParams.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/04-methods/examples/03/MethodParams.class -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/03/MethodParams.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class MethodParams { 4 | 5 | public static void swap(int a, int b) { 6 | System.out.printf("(x,y)= (%d, %d) IN swap(int, int) BERORE %n", a, b); 7 | int temp = a; 8 | a=b; 9 | b=temp; 10 | System.out.printf("(x,y)= (%d, %d) IN swap(int, int) AFTER %n", a, b); 11 | } 12 | public static void swap(Point p) { 13 | int temp = p.x; 14 | p.x=p.y; 15 | p.y=temp; 16 | } 17 | public static void main(String[] args){ 18 | Random rand = new Random(); 19 | int x = rand.nextInt(50); 20 | int y = rand.nextInt(50); 21 | // System.out.printf("(x,y)= (%d, %d) before swap(int, int) %n", x, y); 22 | // swap(x, y); 23 | // System.out.printf("(x,y)= (%d, %d) after swap(int, int) %n", x, y); 24 | 25 | // Point p = new Point(x, y); 26 | 27 | // System.out.println("Point before swap(int, int) "+p); 28 | // swap(p.x, p.y); 29 | // System.out.println("Point after swap(int, int) "+p); 30 | // swap(p); 31 | // System.out.println("Point after swap(Point) "+p); 32 | // p.swap(); 33 | // System.out.println("Point after swap(Point) "+p); 34 | 35 | Point p1 = new Point(rand.nextInt(50), rand.nextInt(50)); 36 | Point p2 = new Point(p1.x, p1.y); 37 | 38 | System.out.println(p1); 39 | System.out.println(p2); 40 | 41 | System.out.println( p1.equals(p2) ); 42 | 43 | } 44 | } 45 | 46 | class Point{ 47 | int x, y; 48 | public Point(int x, int y){ 49 | this.x = x; 50 | this.y = y; 51 | } 52 | public String toString() { 53 | return super.toString() + " ("+x+", "+y+")"; 54 | } 55 | 56 | public boolean equals(Point other){ 57 | return (this.x == other.x) && (this.y == other.y); 58 | } 59 | 60 | public void swap(){ 61 | int temp =x; 62 | this.x = this.y; 63 | this.y = temp; 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/03/Point.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/04-methods/examples/03/Point.class -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/03/Point.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | class Point{ 4 | int x, y; 5 | public Point(){ 6 | Random rand = new Random(); 7 | x = rand.nextInt(10); 8 | y = rand.nextInt(10); 9 | } 10 | public String toString() { 11 | return "("+x+","+y+")"; 12 | } 13 | public boolean equals(Point other){ 14 | return this.x==other.x && this.y==other.y; 15 | } 16 | public static void main(String[] args){ 17 | System.out.println( new Point() ); 18 | 19 | Point p1 = new Point(); 20 | p1.x=5; p1.y=7; 21 | Point p2 = new Point(); 22 | p2.x=5; p2.y=7; 23 | 24 | System.out.println( p1+"=="+p2+" ? "+(p1.equals(p2))); 25 | 26 | 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /lecture-examples/04-methods/examples/03/StackAndHeap.java: -------------------------------------------------------------------------------- 1 | import java.util.Arrays; 2 | public class StackAndHeap { 3 | 4 | public static void accumulate(int p1, int p2) { 5 | System.out.println("Entering accumulate(): p1= "+ p1 +", p2= " +p2); 6 | p1 = p1 + p2; 7 | System.out.println("Exiting accumulate(): p1= "+ p1 +", p2= " +p2); 8 | } 9 | 10 | public static void sum(int[] arrayParam) { 11 | System.out.println("Entering sum(): arrayParam= "+ Arrays.toString(arrayParam)); 12 | System.out.println("Entering sum(): arrayParam= "+ arrayParam); 13 | for ( int i=0; i0 16 | Point p = array[0]; 17 | for (int i=0; i list = new ArrayList(); 10 | for (String a : args) 11 | list.add(a); 12 | 13 | Collections.shuffle(list); 14 | System.out.println("Shuffled: "+ list); 15 | Collections.sort(list); 16 | System.out.println("Sorted: "+ list); 17 | 18 | System.out.println("Contains '5'? "+ list.contains("5")); 19 | System.out.println("indexOf '5'? "+ list.indexOf("5")); 20 | 21 | list.remove("5"); 22 | System.out.println("Contains '5'? "+ list.contains("5")); 23 | System.out.println("indexOf '5'? "+ list.indexOf("5")); 24 | 25 | System.out.println("Before add size is: "+ list.size()); 26 | list.add("last"); 27 | System.out.println("After add size is: "+ list.size()); 28 | System.out.println("After add list is: "+ list); 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/ArrayLoop.java: -------------------------------------------------------------------------------- 1 | public class ArrayLoop { 2 | 3 | public static void main(String[] args){ 4 | 5 | int[] nums = new int[10]; 6 | 7 | for (int i=0; i<10; i++) { 8 | nums[i] = i* 100; 9 | } 10 | 11 | int i = 9; 12 | while ( i >= 0) { 13 | System.out.println(nums[i--]); 14 | } 15 | 16 | for ( i=0; i= 0) { 22 | System.out.println(nums[i--]); 23 | } 24 | 25 | for ( int element: nums) { 26 | System.out.println("Element "+element); 27 | } 28 | 29 | } 30 | } -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/ArrayLoopMulti.java: -------------------------------------------------------------------------------- 1 | public class ArrayLoopMulti { 2 | 3 | public static void iterArray( int[][] array) { 4 | System.out.println("Classic-for:"); 5 | for (int i=0; i= 0) 41 | System.out.println("Found 5 at element in intArray at index"+ location); 42 | else 43 | System.out.println("5 not found in intArray"); 44 | 45 | location = Arrays.binarySearch(intArray, 10); 46 | if (location >= 0) 47 | System.out.println("Found 10 at element in intArray at index"+ location); 48 | else 49 | System.out.println("10 not found in intArray"); 50 | 51 | System.out.println("\n\n"); 52 | } 53 | } -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/ListIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/05-arrays-collections/examples/ListIterator.class -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/ListIterator.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ListIterator{ 4 | 5 | public static void main(String[] args){ 6 | ArrayList list = new ArrayList(); 7 | Random rand = new Random(); 8 | 9 | for (int i = 0; i < 10; i++) 10 | list.add(rand.nextInt(100)); 11 | System.out.println(list); 12 | Iterator iter = list.iterator(); 13 | 14 | while (iter.hasNext()) { 15 | int i = (Integer)iter.next(); 16 | System.out.print(i + " "); 17 | if (i % 2 != 0) 18 | iter.remove(); 19 | } 20 | System.out.println("\n"+list); 21 | } 22 | 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/ListRemove.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/05-arrays-collections/examples/ListRemove.class -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/ListRemove.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class ListRemove{ 4 | 5 | public static void main(String[] args){ 6 | ArrayList list = new ArrayList(); 7 | Random rand = new Random(); 8 | 9 | for (int i = 0; i < 10; i++) 10 | list.add(rand.nextInt(100)); 11 | 12 | System.out.println(list); 13 | 14 | int l = list.size(); 15 | int j = 0; 16 | while (j < l ) { 17 | int i = list.get(j); 18 | System.out.print(i + " "); 19 | if (i % 2 != 0) 20 | list.remove(j); 21 | j++; 22 | } 23 | System.out.println("\n"+list); 24 | } 25 | 26 | } 27 | 28 | 29 | -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/MapsDemo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/05-arrays-collections/examples/MapsDemo.class -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/MapsDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.Map; 2 | import java.util.TreeMap; 3 | import java.util.HashMap; 4 | 5 | 6 | public class MapsDemo { 7 | 8 | public static void main(String[] args) { 9 | Map m = new TreeMap(); 10 | 11 | // Initialize frequency table from command line 12 | for (String a : args) { 13 | Integer freq = m.get(a); 14 | // m.put(a, (freq == null) ? 1 : freq + 1); 15 | System.out.println("Map get key "+a+": "+freq); 16 | if ( freq==null ) 17 | m.put(a, 1); // java promotes int 1 to Integer(1) 18 | else 19 | m.put(a, freq+1); //java allows Integer object +1 20 | } 21 | 22 | // System.out.println("Count parameters: "+args.length); 23 | System.out.println("Distinct parameters: "+m.size()); 24 | System.out.println(m); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/SetsDemo.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.Set; 3 | import java.util.TreeSet; 4 | import java.util.HashSet; 5 | 6 | 7 | public class SetsDemo { 8 | 9 | public static void main(String[] args) { 10 | Set hashSet = new HashSet(); 11 | TreeSet treeSet = new TreeSet(); 12 | for (String a : args) { 13 | hashSet.add(a); 14 | treeSet.add(a); 15 | } 16 | 17 | System.out.println("HashSet: "+ hashSet); 18 | System.out.println("TreeSet: "+ treeSet); 19 | 20 | System.out.println("HashSet Contains '5'? "+ hashSet.contains("5")); 21 | hashSet.remove("5"); 22 | System.out.println("HashSet Contains '5'? "+ hashSet.contains("5")); 23 | System.out.println("HashSet: "+ hashSet); 24 | 25 | System.out.println("First and last in TreeSet? "+ treeSet.first() + " "+ treeSet.last()); 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/Wrappers.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/05-arrays-collections/examples/Wrappers.class -------------------------------------------------------------------------------- /lecture-examples/05-arrays-collections/examples/Wrappers.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.Random; 3 | 4 | public class Wrappers{ 5 | public static void main(String[] args){ 6 | ArrayList integers = new ArrayList(); 7 | Random r = new Random(); 8 | for (int i=0; i<5; i++){ 9 | int value = r.nextInt(); 10 | integers.add(value); //autoboxing int to Integer 11 | } 12 | for (int i: integers){ // auto-unboxing Integer to int 13 | System.out.println(i); 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/01/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Post p1 = new Post(); 4 | p1.username = "George"; 5 | p1.text = "My first post"; 6 | p1.timestamp = 2016_11_14_0910L; 7 | 8 | PhotoPost pp1 = new PhotoPost(); 9 | pp1.filename = "selfie.png"; 10 | pp1.caption = "Wow...."; 11 | pp1.username = "Nancy"; 12 | pp1.timestamp = 2016_11_14_0912L; 13 | } 14 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/01/PhotoPost.java: -------------------------------------------------------------------------------- 1 | public class PhotoPost extends Post { 2 | String filename; 3 | String caption; 4 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/01/Post.java: -------------------------------------------------------------------------------- 1 | public class Post { 2 | String username; 3 | String text; 4 | long timestamp; 5 | int likes; 6 | 7 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/02/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | TextPost tp1 = new TextPost(); 4 | tp1.username = "George"; 5 | tp1.text = "My first post"; 6 | tp1.timestamp = 2016_11_14_0910L; 7 | 8 | PhotoPost pp1 = new PhotoPost(); 9 | pp1.filename = "selfie.png"; 10 | pp1.caption = "Wow...."; 11 | pp1.username = "Nancy"; 12 | pp1.timestamp = 2016_11_14_0912L; 13 | } 14 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/02/PhotoPost.java: -------------------------------------------------------------------------------- 1 | public class PhotoPost extends Post { 2 | String filename; 3 | String caption; 4 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/02/Post.java: -------------------------------------------------------------------------------- 1 | public class Post { 2 | String username; 3 | long timestamp; 4 | int likes; 5 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/02/TextPost.java: -------------------------------------------------------------------------------- 1 | public class TextPost extends Post { 2 | String text; 3 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/03/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost pp1 = new PhotoPost(); 4 | pp1.filename = "selfie.png"; 5 | pp1.caption = "Wow...."; 6 | pp1.username = "Nancy"; 7 | pp1.increaseLikes(); 8 | pp1.increaseLikes(); 9 | pp1.increaseLikes(); 10 | System.out.println("Photo ["+pp1.filename+"] \n"+ 11 | pp1.caption+" \n"+ 12 | "by "+pp1.username+" \n"+ 13 | "Liked "+pp1.getLikes()+" times."); 14 | } 15 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/03/PhotoPost.java: -------------------------------------------------------------------------------- 1 | public class PhotoPost extends Post { 2 | String filename; 3 | String caption; 4 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/03/Post.java: -------------------------------------------------------------------------------- 1 | public class Post { 2 | String username; 3 | long timestamp; 4 | private int likes; 5 | 6 | public void increaseLikes() { 7 | this.likes++; 8 | } 9 | public int getLikes() { 10 | return this.likes; 11 | } 12 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/03/TextPost.java: -------------------------------------------------------------------------------- 1 | public class TextPost extends Post { 2 | String text; 3 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/04/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost pp1 = new PhotoPost("Nancy"); 4 | //pp1.username = "Nancy"; πλεόν είναι private 5 | pp1.filename = "selfie.png"; 6 | pp1.caption = "Wow...."; 7 | pp1.increaseLikes(); 8 | pp1.increaseLikes(); 9 | pp1.increaseLikes(); 10 | System.out.println("Photo ["+pp1.filename+"] \n"+ // πλέον είναι private 11 | pp1.caption+" @ "+pp1.getTimestamp()+"\n"+ 12 | "by "+pp1.getUsername()+" \n"+ 13 | "Liked "+pp1.getLikes()+" times."); 14 | } 15 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/04/PhotoPost.java: -------------------------------------------------------------------------------- 1 | public class PhotoPost extends Post { 2 | String filename; 3 | String caption; 4 | 5 | public PhotoPost(String username) { 6 | super(username); 7 | } 8 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/04/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class Post { 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public void increaseLikes() { 16 | this.likes++; 17 | } 18 | public int getLikes() { 19 | return this.likes; 20 | } 21 | public String getUsername() { 22 | return this.username; 23 | } 24 | public long getTimestamp() { 25 | return this.timestamp; 26 | } 27 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/05/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost pp1 = new PhotoPost("Nancy", "selfie.png"); 4 | pp1.setCaption("Wow...."); 5 | pp1.increaseLikes(); 6 | pp1.increaseLikes(); 7 | pp1.increaseLikes(); 8 | System.out.println("Photo ["+pp1.getPhoto()+"] \n"+ // πλέον είναι private 9 | pp1.getCaption()+" @ "+pp1.getTimestamp()+"\n"+ 10 | "by "+pp1.getUsername()+" \n"+ 11 | "Liked "+pp1.getLikes()+" times."); 12 | pp1.reset("new_selfie.png"); 13 | System.out.println("Photo ["+pp1.getPhoto()+"] \n"+ // πλέον είναι private 14 | pp1.getCaption()+" @ "+pp1.getTimestamp()+"\n"+ 15 | "by "+pp1.getUsername()+" \n"+ 16 | "Liked "+pp1.getLikes()+" times."); 17 | 18 | } 19 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/05/PhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class PhotoPost extends Post { 6 | private String filename; 7 | private String caption; 8 | 9 | public PhotoPost(String username, String filename) { 10 | super(username); 11 | this.filename = filename; 12 | } 13 | 14 | public void reset(String filename) { 15 | this.filename = filename; 16 | super.reset(); 17 | } 18 | public String getPhoto() { 19 | return this.filename; 20 | } 21 | 22 | public void setCaption(String caption) { 23 | this.caption = caption; 24 | } 25 | public String getCaption() { 26 | return this.caption; 27 | } 28 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/05/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class Post { 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public void increaseLikes() { 16 | this.likes++; 17 | } 18 | public int getLikes() { 19 | return this.likes; 20 | } 21 | protected void reset() { 22 | this.likes = 0; 23 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 24 | } 25 | public String getUsername() { 26 | return this.username; 27 | } 28 | public long getTimestamp() { 29 | return this.timestamp; 30 | } 31 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/06/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost p1 = new PhotoPost("Nancy"); 4 | p1.setPhoto("selfie.png"); 5 | p1.setCaption("Wow...."); 6 | p1.increaseLikes(); 7 | p1.increaseLikes(); 8 | p1.increaseLikes(); 9 | 10 | TextPost p2 = new TextPost("Goerge", "Here's a thought.."); 11 | p2.increaseLikes(); 12 | p2.increaseLikes(); 13 | 14 | MyWall wall = new MyWall(); 15 | wall.addPost(p1); 16 | wall.addPost(p2); 17 | 18 | wall.showPosts(); 19 | } 20 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/06/MyWall.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayDeque; 2 | public class MyWall { 3 | private ArrayDeque posts= new ArrayDeque<>(); 4 | 5 | public void addPost(Post p) { 6 | this.posts.push(p); 7 | } 8 | 9 | public void showPosts() { 10 | for( Post p: posts) { 11 | System.out.println(p.toString()); 12 | System.out.println("================="); 13 | } 14 | } 15 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/06/PhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class PhotoPost extends Post { 6 | private String filename; 7 | private String caption; 8 | 9 | public PhotoPost(String username) { 10 | super(username); 11 | } 12 | 13 | public void setPhoto(String filename) { 14 | this.filename = filename; 15 | super.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 16 | super.resetLikes(); 17 | } 18 | public String getPhoto() { 19 | return this.filename; 20 | } 21 | 22 | public void setCaption(String caption) { 23 | this.caption = caption; 24 | } 25 | public String getCaption() { 26 | return this.caption; 27 | } 28 | 29 | public String toString() { 30 | return "Photo ["+this.filename+"] \n"+ 31 | this.caption; 32 | } 33 | 34 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/06/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class Post { 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public void increaseLikes() { 16 | this.likes++; 17 | } 18 | public int getLikes() { 19 | return this.likes; 20 | } 21 | protected void resetLikes() { 22 | this.likes = 0; 23 | } 24 | public String getUsername() { 25 | return this.username; 26 | } 27 | public long getTimestamp() { 28 | return this.timestamp; 29 | } 30 | 31 | public String toString() { 32 | return "by "+ this.username + 33 | " @ "+this.timestamp+"\n"+ 34 | "Liked "+this.likes+" times."; 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/06/TextPost.java: -------------------------------------------------------------------------------- 1 | public class TextPost extends Post { 2 | private String text; 3 | 4 | public TextPost(String username, String text) { 5 | super(username); 6 | this.text = text; 7 | } 8 | 9 | public String toString() { 10 | return this.text; 11 | } 12 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/07/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost p1 = new PhotoPost("Nancy"); 4 | p1.setPhoto("selfie.png"); 5 | p1.setCaption("Wow...."); 6 | p1.increaseLikes(); 7 | p1.increaseLikes(); 8 | p1.increaseLikes(); 9 | 10 | TextPost p2 = new TextPost("Goerge", "Here's a thought.."); 11 | p2.increaseLikes(); 12 | p2.increaseLikes(); 13 | 14 | MyWall wall = new MyWall(); 15 | wall.addPost(p1); 16 | wall.addPost(p2); 17 | 18 | wall.showPosts(); 19 | 20 | // superclass reference - superclass object: allowed 21 | Post p3 = new Post("George"); 22 | // subclass reference - subclass object: allowed 23 | TextPost p4 = new TextPost("Nancy", "Testing post.."); 24 | // superclass reference - subclass object: 25 | // allowed, but can only access superclass members 26 | // Post p5 = new PhotoPost("George"); 27 | Post p5 = new TextPost("George", "Hre's a thought.."); // allowed, but casting to PhotoPost will fail! 28 | //p5.setPhoto("photo.jpg"); // COMPILER ERROR 29 | ((PhotoPost)p5).setPhoto("photo.jpg"); //EXPLICIT CASTING 30 | // subclass reference - superclass object: not allowed 31 | // PhotoPost p6 = new Post("Nancy"); // COMPILER ERROR 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/07/MyWall.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayDeque; 2 | public class MyWall { 3 | private ArrayDeque posts= new ArrayDeque<>(); 4 | private int texts, photos; 5 | 6 | public void addPost(Post p) { 7 | this.posts.push(p); 8 | if ( p instanceof TextPost ) 9 | texts++; 10 | else if ( p instanceof PhotoPost ) 11 | photos ++; 12 | } 13 | 14 | public void showPosts() { 15 | System.out.println("Total posts: "+(texts+photos)+" [Text "+texts+"][Photos "+photos+"]"); 16 | System.out.println("================="); 17 | for( Post p: posts) { 18 | System.out.println(p); 19 | System.out.println("================="); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/07/PhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class PhotoPost extends Post { 6 | private String filename; 7 | private String caption; 8 | 9 | public PhotoPost(String username) { 10 | super(username); 11 | } 12 | 13 | public void setPhoto(String filename) { 14 | this.filename = filename; 15 | super.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 16 | super.resetLikes(); 17 | } 18 | public String getPhoto() { 19 | return this.filename; 20 | } 21 | 22 | public void setCaption(String caption) { 23 | this.caption = caption; 24 | } 25 | public String getCaption() { 26 | return this.caption; 27 | } 28 | 29 | public String toString() { 30 | return "Photo ["+this.filename+"] \n"+ 31 | this.caption + "\n"+ 32 | super.toString(); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/07/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class Post { 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public void increaseLikes() { 16 | this.likes++; 17 | } 18 | public int getLikes() { 19 | return this.likes; 20 | } 21 | protected void resetLikes() { 22 | this.likes = 0; 23 | } 24 | public String getUsername() { 25 | return this.username; 26 | } 27 | public long getTimestamp() { 28 | return this.timestamp; 29 | } 30 | 31 | public String toString() { 32 | return "by "+ this.username + 33 | " @ "+this.timestamp+"\n"+ 34 | "Liked "+this.likes+" times."; 35 | 36 | } 37 | } -------------------------------------------------------------------------------- /lecture-examples/06-inheritance/examples/07/TextPost.java: -------------------------------------------------------------------------------- 1 | public class TextPost extends Post { 2 | private String text; 3 | 4 | public TextPost(String username, String text) { 5 | super(username); 6 | this.text = text; 7 | } 8 | 9 | public String toString() { 10 | return this.text + "\n"+ 11 | super.toString(); 12 | } 13 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/01/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost p1 = new PhotoPost("Nancy"); 4 | p1.setPhoto("selfie.png"); 5 | p1.setCaption("Wow...."); 6 | p1.increaseLikes(); 7 | p1.increaseLikes(); 8 | p1.increaseLikes(); 9 | 10 | TextPost p2 = new TextPost("Geοrge", "Here's a thought.."); 11 | p2.increaseLikes(); 12 | p2.increaseLikes(); 13 | 14 | Post p3 = new TextPost("George", "Here's another though.."); 15 | p3.increaseLikes(); 16 | 17 | MyWall wall = new MyWall(); 18 | wall.addPost(p1); 19 | wall.addPost(p2); 20 | wall.addPost(p3); 21 | 22 | wall.showPosts(); 23 | 24 | } 25 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/01/MyWall.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayDeque; 2 | public class MyWall { 3 | private ArrayDeque posts= new ArrayDeque<>(); 4 | private int texts, photos; 5 | 6 | public void addPost(Post p) { 7 | this.posts.push(p); 8 | if ( p instanceof TextPost ) 9 | texts++; 10 | else if ( p instanceof PhotoPost ) 11 | photos ++; 12 | } 13 | 14 | public void showPosts() { 15 | System.out.println("Total posts: "+(texts+photos)+" [Text "+texts+"][Photos "+photos+"]"); 16 | System.out.println("================="); 17 | for( Post p: posts) { 18 | System.out.println(p); 19 | System.out.println("================="); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/01/PhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class PhotoPost extends Post { 6 | private String filename; 7 | private String caption; 8 | 9 | public PhotoPost(String username) { 10 | super(username); 11 | } 12 | 13 | public void setPhoto(String filename) { 14 | this.filename = filename; 15 | super.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 16 | super.resetLikes(); 17 | } 18 | public String getPhoto() { 19 | return this.filename; 20 | } 21 | 22 | public void setCaption(String caption) { 23 | this.caption = caption; 24 | } 25 | public String getCaption() { 26 | return this.caption; 27 | } 28 | 29 | public String toString() { 30 | return "Photo ["+this.filename+"] \n"+ 31 | this.caption + "\n"+ 32 | super.toString(); 33 | } 34 | 35 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/01/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public abstract class Post { 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public void increaseLikes() { 16 | this.likes++; 17 | } 18 | public int getLikes() { 19 | return this.likes; 20 | } 21 | protected void resetLikes() { 22 | this.likes = 0; 23 | } 24 | public String getUsername() { 25 | return this.username; 26 | } 27 | public long getTimestamp() { 28 | return this.timestamp; 29 | } 30 | 31 | public String toString() { 32 | return "by "+ this.username + 33 | " @ "+this.timestamp+"\n"+ 34 | "Liked "+this.likes+" times."; 35 | 36 | } 37 | 38 | public static void main(String[] args) { 39 | // System.out.println(new Post("Dimitris")); 40 | } 41 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/01/TextPost.java: -------------------------------------------------------------------------------- 1 | public class TextPost extends Post { 2 | private String text; 3 | 4 | public TextPost(String username, String text) { 5 | super(username); 6 | this.text = text; 7 | } 8 | 9 | public String toString() { 10 | return this.text + "\n"+ 11 | super.toString(); 12 | } 13 | public static void main(String[] args) { 14 | System.out.println(new TextPost("Dimitris", "Hello world")); 15 | } 16 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/HTMLPhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class HTMLPhotoPost extends PhotoPost { 6 | 7 | public HTMLPhotoPost(String username) { 8 | super(username); 9 | } 10 | 11 | public String render() { 12 | return "
\n" 13 | +"\n" 14 | +""+super.toString()+"\n" 15 | +"
"; 16 | } 17 | 18 | /* Cheating! 19 | public void increaseLikes() { 20 | for (int i=0; i<5; i++) 21 | super.increaseLikes(); 22 | } 23 | */ 24 | 25 | public static void main(String[] args) { 26 | HTMLPhotoPost p = new HTMLPhotoPost("Dimitris"); 27 | p.setPhoto("selfie.jpg"); 28 | p.setCaption("Wow me!"); 29 | System.out.println(p.render()); 30 | } 31 | 32 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/HTMLTextPost.java: -------------------------------------------------------------------------------- 1 | public class HTMLTextPost extends TextPost { 2 | 3 | public HTMLTextPost(String username, String text) { 4 | super(username, text); 5 | } 6 | 7 | public String toString() { 8 | return super.toString(); 9 | } 10 | 11 | public String render() { 12 | return "
\n" 13 | +""+super.toString()+"\n" 14 | +"
"; 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | HTMLPhotoPost p1 = new HTMLPhotoPost("Nancy"); 4 | p1.setPhoto("selfie.png"); 5 | p1.setCaption("Wow...."); 6 | p1.increaseLikes(); 7 | 8 | HTMLTextPost p2 = new HTMLTextPost("Geοrge", "Here's a thought.."); 9 | p2.increaseLikes(); 10 | 11 | // Post p3 = new HTMLTextPost("George", "Here's another though.."); 12 | 13 | MyWall wall = new MyWall(); 14 | wall.addPost(p1); 15 | wall.addPost(p2); 16 | // wall.addPost(p3); 17 | 18 | wall.showPosts(); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/MyWall.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayDeque; 2 | public class MyWall { 3 | private ArrayDeque posts= new ArrayDeque<>(); 4 | private int texts, photos; 5 | 6 | public void addPost(Post p) { 7 | this.posts.push(p); 8 | if ( p instanceof TextPost ) 9 | texts++; 10 | else if ( p instanceof PhotoPost ) 11 | photos ++; 12 | } 13 | 14 | public void showPosts() { 15 | System.out.println("Total posts: "+(texts+photos)+" [Text "+texts+"][Photos "+photos+"]"); 16 | System.out.println("================="); 17 | for( Post p: posts) { 18 | System.out.println(p.render()); 19 | System.out.println("================="); 20 | } 21 | } 22 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/PhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public abstract class PhotoPost extends Post { 6 | private String filename; 7 | private String caption; 8 | 9 | public PhotoPost(String username) { 10 | super(username); 11 | } 12 | 13 | public void setPhoto(String filename) { 14 | this.filename = filename; 15 | super.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 16 | super.resetLikes(); 17 | } 18 | public String getPhoto() { 19 | return this.filename; 20 | } 21 | 22 | public void setCaption(String caption) { 23 | this.caption = caption; 24 | } 25 | public String getCaption() { 26 | return this.caption; 27 | } 28 | 29 | public String toString() { 30 | return "Photo ["+this.filename+"] \n"+ 31 | this.caption + "\n"+ 32 | super.toString(); 33 | } 34 | 35 | public abstract String render(); 36 | 37 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public abstract class Post { 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public void increaseLikes() { 16 | this.likes++; 17 | } 18 | public int getLikes() { 19 | return this.likes; 20 | } 21 | protected void resetLikes() { 22 | this.likes = 0; 23 | } 24 | public String getUsername() { 25 | return this.username; 26 | } 27 | public long getTimestamp() { 28 | return this.timestamp; 29 | } 30 | 31 | public String toString() { 32 | return "by "+ this.username + 33 | " @ "+this.timestamp+"\n"+ 34 | "Liked "+this.likes+" times."; 35 | 36 | } 37 | 38 | public abstract String render(); 39 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/02/TextPost.java: -------------------------------------------------------------------------------- 1 | public abstract class TextPost extends Post { 2 | private String text; 3 | 4 | public TextPost(String username, String text) { 5 | super(username); 6 | this.text = text; 7 | } 8 | 9 | public String toString() { 10 | return this.text + "\n"+ 11 | super.toString(); 12 | } 13 | 14 | public abstract String render(); 15 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/Book.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class Book implements Likable{ 6 | private String title; 7 | private int likes; 8 | 9 | public Book(String title) { 10 | this.title = title; 11 | } 12 | 13 | public final void increaseLikes() { 14 | this.likes++; 15 | } 16 | public int getLikes() { 17 | return this.likes; 18 | } 19 | public int isMoreLikableThan(Likable other) { 20 | if ( this.getLikes() == other.getLikes() ) 21 | return 0; 22 | else if ( this.getLikes() < other.getLikes() ) 23 | return -1; 24 | return 1; 25 | } 26 | public String toString() { 27 | return "Book "+ this.title + 28 | "Liked "+this.likes+" times."; 29 | 30 | } 31 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/Dislikable.java: -------------------------------------------------------------------------------- 1 | public interface Dislikable { 2 | public void decreaseLikes() ; 3 | public int getLikes() ; 4 | /** 5 | * @returns: -1 if other more likable, 1 if this more likable, 0 otherwise. 6 | */ 7 | public int isLessLikableThan(Dislikable other); 8 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/Likable.java: -------------------------------------------------------------------------------- 1 | public interface Likable { 2 | public void increaseLikes() ; 3 | public int getLikes() ; 4 | /** 5 | * @returns: -1 if other more likable, 1 if this more likable, 0 otherwise. 6 | */ 7 | public int isMoreLikableThan(Likable other); 8 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PhotoPost p1 = new PhotoPost("Nancy"); 4 | p1.setPhoto("selfie.png"); 5 | p1.setCaption("Wow...."); 6 | p1.increaseLikes(); 7 | p1.increaseLikes(); 8 | p1.increaseLikes(); 9 | 10 | TextPost p2 = new TextPost("Geοrge", "Here's a thought.."); 11 | p2.increaseLikes(); 12 | p2.increaseLikes(); 13 | 14 | Post p3 = new TextPost("George", "Here's another though.."); 15 | 16 | MyWall wall = new MyWall(); 17 | wall.addPost(p1); 18 | wall.addPost(p2); 19 | wall.addPost(p3); 20 | 21 | wall.showPosts(); 22 | System.out.println("\n\n"); 23 | wall.showExtremeLikables(); 24 | 25 | 26 | Book b = new Book("Harry Potter"); 27 | for( int i=0; i<1000; i++) 28 | b.increaseLikes(); 29 | 30 | System.out.println(p1.isMoreLikableThan(b)); 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/MyWall.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayDeque; 2 | public class MyWall { 3 | private ArrayDeque posts= new ArrayDeque<>(); 4 | private int texts, photos; 5 | 6 | public void addPost(Post p) { 7 | this.posts.push(p); 8 | if ( p instanceof TextPost ) 9 | texts++; 10 | else if ( p instanceof PhotoPost ) 11 | photos ++; 12 | } 13 | 14 | public void showPosts() { 15 | System.out.println("Total posts: "+(texts+photos)+" [Text "+texts+"][Photos "+photos+"]"); 16 | System.out.println("================="); 17 | for( Post p: posts) { 18 | System.out.println(p); 19 | System.out.println("================="); 20 | } 21 | } 22 | 23 | public void showExtremeLikables() { 24 | Likable mostLikable = (Likable)posts.getFirst(); 25 | Dislikable leastLikable = (Dislikable)mostLikable; 26 | if ( mostLikable == null ) return; 27 | Likable lk = null; 28 | Dislikable dk = null; 29 | for( Post p: posts) { 30 | lk = (Likable)p; 31 | dk = (Dislikable)p; 32 | if ( mostLikable.isMoreLikableThan(lk) < 0 ) 33 | mostLikable = lk; 34 | if ( leastLikable.isLessLikableThan(dk) < 0 ) 35 | leastLikable = dk; 36 | 37 | } 38 | System.out.println("+++++ Most likable:\n"+mostLikable); 39 | System.out.println("----- Least likable:\n"+leastLikable); 40 | } 41 | 42 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/PhotoPost.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public class PhotoPost extends Post { 6 | private String filename; 7 | private String caption; 8 | 9 | public PhotoPost(String username) { 10 | super(username); 11 | } 12 | /* Cheeting not allowed! 13 | increaseLikes() is final in Post 14 | public void increaseLikes() { 15 | super.increaseLikes(); 16 | super.increaseLikes(); 17 | } */ 18 | 19 | public void setPhoto(String filename) { 20 | this.filename = filename; 21 | super.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 22 | super.resetLikes(); 23 | } 24 | public String getPhoto() { 25 | return this.filename; 26 | } 27 | 28 | public void setCaption(String caption) { 29 | this.caption = caption; 30 | } 31 | public String getCaption() { 32 | return this.caption; 33 | } 34 | 35 | public String toString() { 36 | return "Photo ["+this.filename+"] \n"+ 37 | this.caption + "\n"+ 38 | super.toString(); 39 | } 40 | 41 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/Post.java: -------------------------------------------------------------------------------- 1 | import java.text.DateFormat; 2 | import java.text.SimpleDateFormat; 3 | import java.util.Date; 4 | 5 | public abstract class Post implements Likable, Dislikable{ 6 | private String username; 7 | protected long timestamp; 8 | private int likes; 9 | 10 | public Post(String username) { 11 | this.username = username; 12 | this.timestamp = Long.parseLong((new SimpleDateFormat("yyyyMMddHHmm")).format(new Date())); 13 | } 14 | 15 | public final void increaseLikes() { 16 | this.likes++; 17 | } 18 | public final void decreaseLikes() { 19 | this.likes--; 20 | } 21 | public int getLikes() { 22 | return this.likes; 23 | } 24 | public int isMoreLikableThan(Likable other) { 25 | if ( this.getLikes() == other.getLikes() ) 26 | return 0; 27 | else if ( this.getLikes() < other.getLikes() ) 28 | return -1; 29 | return 1; 30 | } 31 | public int isLessLikableThan(Dislikable other) { 32 | if ( this.getLikes() == other.getLikes() ) 33 | return 0; 34 | else if ( this.getLikes() < other.getLikes() ) 35 | return -1; 36 | return 1; 37 | } 38 | protected void resetLikes() { 39 | this.likes = 0; 40 | } 41 | public String getUsername() { 42 | return this.username; 43 | } 44 | public long getTimestamp() { 45 | return this.timestamp; 46 | } 47 | 48 | public String toString() { 49 | return "by "+ this.username + 50 | " @ "+this.timestamp+"\n"+ 51 | "Liked "+this.likes+" times."; 52 | 53 | } 54 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/03/TextPost.java: -------------------------------------------------------------------------------- 1 | public class TextPost extends Post { 2 | private String text; 3 | 4 | public TextPost(String username, String text) { 5 | super(username); 6 | this.text = text; 7 | } 8 | 9 | public String toString() { 10 | return this.text + "\n"+ 11 | super.toString(); 12 | } 13 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/04.1/MediaSelector.java: -------------------------------------------------------------------------------- 1 | public class MediaSelector { 2 | 3 | public MediaSelector (MediaTypes mt) { 4 | System.out.println("Selected "+mt+ " filetype: "+mt.description); 5 | } 6 | 7 | public static void main(String[] args) { 8 | 9 | MediaTypes.DVD.description ="bad description"; 10 | new MediaSelector(MediaTypes.DVD); 11 | new MediaSelector(MediaTypes.OGG); 12 | 13 | } 14 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/04.1/MediaTypes.java: -------------------------------------------------------------------------------- 1 | public enum MediaTypes { 2 | DVD("DVD Optical Disk"), 3 | OGG("Streaming Audio"), 4 | BOOK("Printed Text Media"); 5 | 6 | public final String description; 7 | MediaTypes(String descr) { 8 | this.description = descr; 9 | } 10 | 11 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/04/MediaSelector.java: -------------------------------------------------------------------------------- 1 | public class MediaSelector { 2 | 3 | public MediaSelector (MediaTypes mt) { 4 | System.out.println("Selected "+mt+ " filetype: "+mt.getDescription()); 5 | } 6 | 7 | public static void main(String[] args) { 8 | 9 | new MediaSelector(MediaTypes.DVD); 10 | new MediaSelector(MediaTypes.OGG); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-1/04/MediaTypes.java: -------------------------------------------------------------------------------- 1 | public enum MediaTypes { 2 | DVD("DVD Optical Disk"), 3 | OGG("Streaming Audio"), 4 | BOOK("Printed Text Media"); 5 | 6 | private final String description; 7 | MediaTypes(String descr) { 8 | this.description = descr; 9 | } 10 | 11 | public String getDescription() { 12 | return this.description; 13 | } 14 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/Employee.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Employee { 3 | public void goToWork(); 4 | public void doTheWork(); 5 | public void departFromWork(); 6 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/Lecturer.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Lecturer extends Person implements Employee { 4 | private LecturerWork work ; 5 | public Lecturer(String name) { 6 | super(name); 7 | } 8 | public void goToWork() { 9 | System.out.println( getName() + " goes to university."); 10 | } 11 | public void doTheWork(){ 12 | int max = work.values().length; 13 | int randomWork = (new Random()).nextInt(max); 14 | this.work = (work.values())[randomWork]; 15 | System.out.println( getName() + " current activity: " 16 | + work.getDescription() +" ("+work+") "); 17 | } 18 | public void departFromWork(){ 19 | System.out.println( getName() + " departs from university."); 20 | } 21 | 22 | public void logDay() { 23 | this.goodmorning(); 24 | this.goToWork(); 25 | for ( int i=0; i<5; i++) { 26 | this.doTheWork(); 27 | } 28 | this.departFromWork(); 29 | this.goodnight(); 30 | } 31 | 32 | public static void main(String args[]) { 33 | Lecturer p = new Lecturer("Dimitris"); 34 | p.logDay(); 35 | // Person p = new Lecturer("Dimitris"); 36 | // p.goodmorning(); 37 | // p.doTheWork(); 38 | } 39 | 40 | } 41 | 42 | -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/LecturerWithKids.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class LecturerWithKids extends Lecturer implements Parent { 4 | private String whereAreTheKids = "home"; 5 | public LecturerWithKids(String name) { super(name); } 6 | public void dropOffKids(String where) { 7 | System.out.println(getName()+" drops off kids at "+where); 8 | this.whereAreTheKids = where; 9 | } 10 | public void pickUpKids() 11 | { 12 | System.out.println(getName()+" picks up kids from "+whereAreTheKids); 13 | this.whereAreTheKids = "home"; 14 | } 15 | 16 | public void attendParentsMeeting() { 17 | System.out.println(getName()+" attends parents meeting "); 18 | } 19 | 20 | public void logDay() { 21 | this.goodmorning(); 22 | this.dropOffKids("school"); 23 | this.goToWork(); 24 | for ( int i=0; i<5; i++) { 25 | this.doTheWork(); 26 | } 27 | this.departFromWork(); 28 | this.pickUpKids(); 29 | this.goodnight(); 30 | } 31 | 32 | public static void main(String args[]) { 33 | // LecturerWithKids l = new LecturerWithKids("Dimitris"); 34 | // Lecturer l = new LecturerWithKids("Dimitris"); 35 | Person l = new LecturerWithKids("Dimitris"); 36 | l.logDay(); 37 | Employee e = new LecturerWithKids("George"); 38 | e.doTheWork(); 39 | } 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/LecturerWork.java: -------------------------------------------------------------------------------- 1 | public enum LecturerWork { 2 | TEACH ("Be inspiring"), 3 | RESEARCH ("Appear thoughtful"); 4 | 5 | private final String descr; 6 | LecturerWork(String d) { this.descr = d; } 7 | public String getDescription() { return this.descr; } 8 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | private static Set people = new HashSet(); 5 | private static Set parents = new HashSet(); 6 | 7 | public static void main(String args[]) { 8 | Person p1 = new LecturerWithKids("Dimitris"); 9 | people.add(p1); 10 | parents.add((Parent)p1); 11 | 12 | Person p2 = new LecturerWithKids("George"); 13 | people.add(p2); 14 | parents.add((Parent)p2); 15 | 16 | Person p3 = new Lecturer("Kostas"); 17 | people.add(p3); 18 | //parents.add((Parent)p3); 19 | 20 | } 21 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/Parent.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Parent { 3 | public void dropOffKids(String where); 4 | public void pickUpKids(); 5 | public void attendParentsMeeting(); 6 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/01/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public abstract class Person { 3 | private String name; 4 | public Person(String name) { 5 | this.name = name; 6 | } 7 | public void goodmorning() { 8 | System.out.println(name + " says Gooooodmorning!..."); 9 | } 10 | public void goodnight() { 11 | System.out.println(name + " says Goodnight.."); 12 | } 13 | public String getName() { return name; } 14 | 15 | public abstract void logDay(); 16 | 17 | public static void main(String[] args) { 18 | //new Person("Dimitris"); 19 | System.out.println("Hello world"); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/Employee.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Employee { 3 | public void goToWork(); 4 | public void doTheWork(); 5 | public void departFromWork(); 6 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/Lecturer.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Lecturer extends Person implements Employee { 4 | private LecturerWork work ; 5 | public Lecturer(String name) { 6 | super(name); 7 | } 8 | public void goToWork() { 9 | System.out.println( getName() + " goes to university."); 10 | } 11 | public void doTheWork(){ 12 | int max = work.values().length; 13 | int randomWork = (new Random()).nextInt(max); 14 | this.work = (work.values())[randomWork]; 15 | System.out.println( getName() + " current activity: " 16 | + work.getDescription() +" ("+work+") "); 17 | } 18 | public void departFromWork(){ 19 | System.out.println( getName() + " departs from university."); 20 | } 21 | 22 | public void logDay() { 23 | this.goodmorning(); 24 | this.goToWork(); 25 | for ( int i=0; i<5; i++) { 26 | this.doTheWork(); 27 | } 28 | this.departFromWork(); 29 | this.goodnight(); 30 | } 31 | 32 | public static void main(String args[]) { 33 | Lecturer l = new Lecturer("Dimitris"); 34 | l.logDay(); 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/LecturerWithKids.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class LecturerWithKids extends Lecturer implements Parent { 4 | private String whereAreTheKids = "home"; 5 | public LecturerWithKids(String name) { super(name); } 6 | public void dropOffKids(String where) { 7 | System.out.println(getName()+" drops off kids at "+where); 8 | this.whereAreTheKids = where; 9 | } 10 | public void pickUpKids() 11 | { 12 | System.out.println(getName()+" picks up kids from "+whereAreTheKids); 13 | this.whereAreTheKids = "home"; 14 | } 15 | 16 | public void attendParentsMeeting() { 17 | System.out.println(getName()+" attends parents meeting "); 18 | } 19 | 20 | public void logDay() { 21 | this.goodmorning(); 22 | this.dropOffKids("school"); 23 | this.goToWork(); 24 | for ( int i=0; i<5; i++) { 25 | this.doTheWork(); 26 | } 27 | this.departFromWork(); 28 | this.pickUpKids(); 29 | this.goodnight(); 30 | } 31 | 32 | 33 | public static void main(String args[]) { 34 | LecturerWithKids l = new LecturerWithKids("Dimitris"); 35 | l.logDay(); 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/LecturerWork.java: -------------------------------------------------------------------------------- 1 | public enum LecturerWork { 2 | TEACH ("Be inspiring"), 3 | RESEARCH ("Appear thoughtful"); 4 | 5 | private final String descr; 6 | LecturerWork(String d) { this.descr = d; } 7 | public String getDescription() { return this.descr; } 8 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | 3 | public class Main { 4 | private static Set people = new HashSet(); 5 | private static Set parents = new HashSet(); 6 | 7 | public static void main(String args[]) { 8 | Person p1 = new LecturerWithKids("Dimitris"); 9 | people.add(p1); 10 | parents.add((Parent)p1); 11 | 12 | Person p2 = new ProgrammerWithKids("George"); 13 | people.add(p2); 14 | parents.add((Parent)p2); 15 | 16 | Person p3 = new Lecturer("Kostas"); 17 | people.add(p3); 18 | // parents.add((Parent)p3); // Not allowed! 19 | 20 | Person p4 = new Programmer("Peter"); 21 | people.add(p4); 22 | // parents.add((Parent)p2); 23 | 24 | int i=1; 25 | for (Person p: people) { 26 | System.out.println("\nPerson "+(i++)+"\n"+p); 27 | p.logDay(); 28 | } 29 | 30 | i=1; 31 | for (Parent p: parents) { 32 | System.out.println("\nParent "+(i++)+"\n"+p); 33 | ((Person)p).logDay(); // programmer's responsibility! 34 | // p.logDay(); //δεν είναι δυνατό 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/Parent.java: -------------------------------------------------------------------------------- 1 | 2 | public interface Parent { 3 | public void dropOffKids(String where); 4 | public void pickUpKids(); 5 | public void attendParentsMeeting(); 6 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/Person.java: -------------------------------------------------------------------------------- 1 | 2 | public abstract class Person { 3 | private String name; 4 | public Person(String name) { 5 | this.name = name; 6 | } 7 | public void goodmorning() { 8 | System.out.println(name + " says Gooooodmorning!..."); 9 | } 10 | public void goodnight() { 11 | System.out.println(name + " says Goodnight.."); 12 | } 13 | public String getName() { return name; } 14 | 15 | public abstract void logDay(); 16 | } -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/Programmer.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class Programmer extends Person implements Employee { 4 | private ProgrammerWork work ; 5 | public Programmer(String name) { 6 | super(name); 7 | } 8 | public void goToWork() { 9 | System.out.println( getName() + " logs in."); 10 | } 11 | public void doTheWork(){ 12 | int max = work.values().length; 13 | int randomWork = (new Random()).nextInt(max); 14 | this.work = (work.values())[randomWork]; 15 | System.out.println( getName() + " current activity: " 16 | + work.getDescription() +" ("+work+") "); 17 | } 18 | public void departFromWork(){ 19 | System.out.println( getName() + " logs off."); 20 | } 21 | 22 | public void logDay() { 23 | this.goodmorning(); 24 | this.goToWork(); 25 | for ( int i=0; i<8; i++) { 26 | this.doTheWork(); 27 | } 28 | this.departFromWork(); 29 | this.goodnight(); 30 | } 31 | 32 | public static void main(String args[]) { 33 | Programmer p = new Programmer("George"); 34 | p.logDay(); 35 | } 36 | 37 | } 38 | 39 | -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/ProgrammerWithKids.java: -------------------------------------------------------------------------------- 1 | import java.util.Random; 2 | 3 | public class ProgrammerWithKids extends Programmer implements Parent { 4 | private String whereAreTheKids = "home"; 5 | public ProgrammerWithKids(String name) { super(name); } 6 | public void dropOffKids(String where) { 7 | System.out.println(getName()+" drops off kids at "+where); 8 | this.whereAreTheKids = where; 9 | } 10 | public void pickUpKids() 11 | { 12 | System.out.println(getName()+" picks up kids from "+whereAreTheKids); 13 | this.whereAreTheKids = "home"; 14 | } 15 | 16 | public void attendParentsMeeting() { 17 | System.out.println(getName()+" attends parents meeting "); 18 | } 19 | 20 | public void logDay() { 21 | this.goodmorning(); 22 | this.dropOffKids("school"); 23 | this.goToWork(); 24 | for ( int i=0; i<5; i++) { 25 | this.doTheWork(); 26 | } 27 | this.departFromWork(); 28 | this.pickUpKids(); 29 | this.goodnight(); 30 | } 31 | 32 | 33 | public static void main(String args[]) { 34 | ProgrammerWithKids p = new ProgrammerWithKids("Dimitris"); 35 | p.logDay(); 36 | } 37 | 38 | } 39 | 40 | -------------------------------------------------------------------------------- /lecture-examples/07-interfaces-abstract/examples-2/02/ProgrammerWork.java: -------------------------------------------------------------------------------- 1 | public enum ProgrammerWork { 2 | MEET ("Discuss, Design"), 3 | CODE ("Think, Type, Test"), 4 | MAKE_COFFEE ("Make coffee"); 5 | 6 | private final String descr; 7 | ProgrammerWork(String d) { this.descr = d; } 8 | public String getDescription() { return this.descr; } 9 | } -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/01/IntDivisor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/08-exceptions/examples/01/IntDivisor.class -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/01/IntDivisor.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class IntDivisor { 4 | 5 | public static int quotient(int numerator, int denominator ) { // διαιρετέος, διαιρέτης 6 | int res = numerator/denominator; 7 | return res; 8 | } 9 | 10 | public static void main(String[] args) { 11 | Scanner sc = new Scanner(System.in); 12 | 13 | System.out.print("Δώσε διαιρετεό: "); 14 | int n = sc.nextInt(); 15 | 16 | System.out.print("Δώσε διαιρέτη: "); 17 | int d = sc.nextInt(); 18 | 19 | System.out.println("Πηλίκο: "+ quotient(n, d)); 20 | 21 | } 22 | } -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/01/Main.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | Scanner sc = new Scanner(System.in); 7 | 8 | System.out.print("Δώσε διαιρετεό: "); 9 | int n = sc.nextInt(); 10 | 11 | System.out.print("Δώσε διαιρέτη: "); 12 | int d = sc.nextInt(); 13 | 14 | System.out.println("Πηλίκο: "+ IntDivisor.quotient(n, d)); 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/02.1/IntDivisor.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class IntDivisor { 4 | 5 | public static int quotient(int numerator, int denominator ) { // διαιρετέος, διαιρέτης 6 | return numerator/denominator; 7 | } 8 | 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | boolean exceptionFlag = true; // συνέβη σφάλμα; 12 | do { 13 | try { 14 | System.out.print("Δώσε διαιρετεό: "); 15 | int n = sc.nextInt(); 16 | 17 | System.out.print("Δώσε διαιρέτη: "); 18 | int d = sc.nextInt(); 19 | 20 | System.out.println("Πηλίκο: "+ quotient(n, d)); 21 | exceptionFlag = false; 22 | } 23 | catch (java.util.InputMismatchException imex) { 24 | System.out.println("\nΣυνέβη ένα InputMismatchException: "+ imex); 25 | System.out.println("Δοκιμάστε πάλι... και αυτή τη φορά δώστε δύο ΑΚΕΡΑΙΟΥΣ!\n"); 26 | sc.nextLine(); // Παραβλέπουμε τη λάθος είσοδο. 27 | } 28 | catch (ArithmeticException aex) { 29 | System.out.println("\nΣυνέβη ένα ArithmeticException: "+ aex); 30 | System.out.println("Δοκιμάστε πάλι. Θυμηθείτε! Δεν διαιρούμε με το μηδέν.\n"); 31 | } 32 | catch (Exception ex) { 33 | System.out.println("\nΣυνέβη ένα σφάλμα, δεν έχω ιδέα τι!"); 34 | } 35 | 36 | } while (exceptionFlag); 37 | 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/02/IntDivisor.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class IntDivisor { 4 | 5 | public static int quotient(int numerator, int denominator ) { // διαιρετέος, διαιρέτης 6 | return numerator/denominator; 7 | } 8 | 9 | public static void main(String[] args) { 10 | Scanner sc = new Scanner(System.in); 11 | boolean exceptionFlag = true; // συνέβη σφάλμα; 12 | // int n=0, d=0; 13 | do { 14 | try { 15 | System.out.print("Δώσε διαιρετεό: "); 16 | int n = sc.nextInt(); 17 | 18 | System.out.print("Δώσε διαιρέτη: "); 19 | int d = sc.nextInt(); 20 | 21 | } 22 | catch (java.util.InputMismatchException imex) { 23 | System.out.println("\nΣυνέβη ένα InputMismatchException: "+ imex); 24 | System.out.println("Δοκιμάστε πάλι... και αυτή τη φορά δώστε δύο ΑΚΕΡΑΙΟΥΣ!\n"); 25 | sc.nextLine(); // Παραβλέπουμε τη λάθος είσοδο. 26 | } 27 | 28 | System.out.println("Πηλίκο: "+ quotient(n, d)); 29 | exceptionFlag = false; 30 | 31 | } while (exceptionFlag); 32 | 33 | 34 | } 35 | } -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/IntDivisor.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class IntDivisor { 4 | 5 | public static int quotient(int numerator, int denominator ) throws ArithmeticException 6 | { // διαιρετέος, διαιρέτης 7 | return numerator/denominator; 8 | } 9 | 10 | public static void main(String[] args) { 11 | Scanner sc = new Scanner(System.in); 12 | boolean exceptionFlag = true; // συνέβη σφάλμα; 13 | do { 14 | try { 15 | System.out.print("Δώσε διαιρετεό: "); 16 | int n = sc.nextInt(); 17 | 18 | System.out.print("Δώσε διαιρέτη: "); 19 | int d = sc.nextInt(); 20 | 21 | System.out.println("Πηλίκο: "+ quotient(n, d)); 22 | exceptionFlag = false; 23 | } 24 | catch (ArithmeticException aex) { 25 | System.out.println("\nΣυνέβη ένα ArithmeticException: "+ aex); 26 | System.out.println("Δοκιμάστε πάλι. Θυμηθείτε! Δεν διαιρούμε με το μηδέν.\n"); 27 | } 28 | catch (java.util.InputMismatchException imex) { 29 | System.out.println("\nΣυνέβη ένα InputMismatchException: "+ imex); 30 | System.out.println("Δοκιμάστε πάλι... και αυτή τη φορά δώστε δύο ΑΚΕΡΑΙΟΥΣ!\n"); 31 | sc.nextLine(); // Παραβλέπουμε τη λάθος είσοδο. 32 | } 33 | finally { 34 | System.out.println("Μη ξεχνάτε ότι το σύνολο των ακεραίων δεν είναι κλειστό ως προς τη διαίρεση, μπορεί να υπάρχει και υπόλοιπο!"); 35 | } 36 | 37 | } while (exceptionFlag); 38 | 39 | 40 | } 41 | } -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/allclasses-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | All Classes 7 | 8 | 9 | 10 | 11 |

All Classes

12 |
13 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/allclasses-noframe.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | All Classes 7 | 8 | 9 | 10 | 11 |

All Classes

12 |
13 | 16 |
17 | 18 | 19 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/deprecated-list.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Deprecated List 7 | 8 | 9 | 10 | 11 | 21 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 38 |
39 | 66 | 67 |
68 |

Deprecated API

69 |

Contents

70 |
71 | 72 |
73 | 74 | 75 | 76 | 77 | 85 |
86 | 113 | 114 | 115 | 116 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | Generated Documentation (Untitled) 7 | 60 | 61 | 62 | 63 | 64 | 65 | <noscript> 66 | <div>JavaScript is disabled on your browser.</div> 67 | </noscript> 68 | <h2>Frame Alert</h2> 69 | <p>This document is designed to be viewed using the frames feature. If you see this message, you are using a non-frame-capable web client. Link to <a href="IntDivisor.html">Non-frame version</a>.</p> 70 | 71 | 72 | 73 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/package-frame.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | &lt;Unnamed&gt; 7 | 8 | 9 | 10 | 11 |

<Unnamed>

12 |
13 |

Classes

14 | 17 |
18 | 19 | 20 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/package-list: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/resources/background.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/08-exceptions/examples/03/resources/background.gif -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/resources/tab.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/08-exceptions/examples/03/resources/tab.gif -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/resources/titlebar.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/08-exceptions/examples/03/resources/titlebar.gif -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/03/resources/titlebar_end.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/08-exceptions/examples/03/resources/titlebar_end.gif -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/04/IntDivisor.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class IntDivisor { 4 | 5 | public static int quotient(int numerator, int denominator ) throws ArithmeticException 6 | { 7 | if ( denominator == 0 ) 8 | throw new ArithmeticException("Ο διαιρέτης δεν μπορεί να είναι μηδέν (/0)!"); 9 | // διαιρετέος, διαιρέτης 10 | return numerator/denominator; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | boolean exceptionFlag = true; // συνέβη σφάλμα; 16 | do { 17 | try { 18 | System.out.print("Δώσε διαιρετεό: "); 19 | int n = sc.nextInt(); 20 | 21 | System.out.print("Δώσε διαιρέτη: "); 22 | int d = sc.nextInt(); 23 | 24 | System.out.println("Πηλίκο: "+ quotient(n, d)); 25 | exceptionFlag = false; 26 | } 27 | catch (ArithmeticException aex) { 28 | System.out.println("\nΣυνέβη ένα ArithmeticException: "+ aex); 29 | System.out.println("Δοκιμάστε πάλι. Θυμηθείτε! Δεν διαιρούμε με το μηδέν.\n"); 30 | } 31 | catch (java.util.InputMismatchException imex) { 32 | System.out.println("\nΣυνέβη ένα InputMismatchException: "+ imex); 33 | System.out.println("Δοκιμάστε πάλι... και αυτή τη φορά δώστε δύο ΑΚΕΡΑΙΟΥΣ!\n"); 34 | sc.nextLine(); // Παραβλέπουμε τη λάθος είσοδο. 35 | } 36 | finally { 37 | System.out.println("Μη ξεχνάτε ότι το σύνολο των ακεραίων δεν είναι κλειστό ως προς τη διαίρεση, μπορεί να υπάρχει και υπόλοιπο!"); 38 | } 39 | 40 | } while (exceptionFlag); 41 | 42 | 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/05/IntDivisor.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/08-exceptions/examples/05/IntDivisor.class -------------------------------------------------------------------------------- /lecture-examples/08-exceptions/examples/05/IntDivisor.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | public class IntDivisor { 4 | 5 | public static int quotient(int numerator, int denominator ) throws ArithmeticException 6 | { 7 | if ( denominator == 0 ) 8 | throw new ArithmeticException("Ο διαιρέτης δεν μπορεί να είναι μηδέν (/0)!"); 9 | // διαιρετέος, διαιρέτης 10 | return numerator/denominator; 11 | } 12 | 13 | public static void main(String[] args) { 14 | Scanner sc = new Scanner(System.in); 15 | boolean exceptionFlag = true; // συνέβη σφάλμα; 16 | do { 17 | try { 18 | System.out.print("Δώσε διαιρετεό: "); 19 | int n = sc.nextInt(); 20 | 21 | System.out.print("Δώσε διαιρέτη: "); 22 | int d = sc.nextInt(); 23 | 24 | System.out.println("Πηλίκο: "+ quotient(n, d)); 25 | exceptionFlag = false; 26 | } 27 | catch (ArithmeticException aex) { 28 | System.out.println("\nΣυνέβη ένα ArithmeticException: "+ aex); 29 | System.out.println("Δοκιμάστε πάλι. Θυμηθείτε! Δεν διαιρούμε με το μηδέν.\n"); 30 | System.exit(-1); // Is finally block executed - Does do..while iteration continue? 31 | return; // Is finally block executed - Does do..while iteration continue? 32 | } 33 | /* Not catching InputMismatchExceptions; try a floating point input - is finally block executed? 34 | catch (java.util.InputMismatchException imex) { 35 | System.out.println("\nΣυνέβη ένα InputMismatchException: "+ imex); 36 | System.out.println("Δοκιμάστε πάλι... και αυτή τη φορά δώστε δύο ΑΚΕΡΑΙΟΥΣ!\n"); 37 | sc.nextLine(); // Παραβλέπουμε τη λάθος είσοδο. 38 | }*/ 39 | finally { 40 | System.out.println("Μη ξεχνάτε ότι το σύνολο των ακεραίων δεν είναι κλειστό ως προς τη διαίρεση, μπορεί να υπάρχει και υπόλοιπο!"); 41 | } 42 | 43 | } while (exceptionFlag); 44 | 45 | 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/01-Intro-copy.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/01/01-Intro-copy.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/01-Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/01/01-Intro.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/09-Exceptions-copy.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/01/09-Exceptions-copy.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/09-Exceptions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/01/09-Exceptions.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/CopyBytes.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.FileOutputStream; 3 | import java.io.IOException; 4 | 5 | public class CopyBytes { 6 | public static void main(String[] args) throws IOException { 7 | 8 | FileInputStream in = null; 9 | FileOutputStream out = null; 10 | 11 | try { 12 | in = new FileInputStream(args[0]); 13 | out = new FileOutputStream(args[1]); 14 | int c; // c is actually byte (8bit) 15 | 16 | while ((c = in.read()) != -1) { 17 | out.write(c); 18 | } 19 | // in.close(); 20 | // out.close(); 21 | } catch (java.io.FileNotFoundException e) { 22 | System.out.println("Αδυναμία ανάγνωσης αρχείου:"+args[0]); 23 | } catch (IOException e) { 24 | System.out.println("Σφάλμα εισόδου-εξόδου."); 25 | } catch (ArrayIndexOutOfBoundsException e) { 26 | System.out.println("Δεν δόθηκαν αρκετές παράμετροι. "+e.getMessage()); 27 | } finally { 28 | System.out.println("Cleaning up files streams."); 29 | if (in != null) { 30 | in.close(); 31 | } 32 | if (out != null) { 33 | out.close(); 34 | } 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/in.txt: -------------------------------------------------------------------------------- 1 | In Xanadu did Kubla Khan 2 | A stately pleasure-dome decree: 3 | Where Alph, the sacred river, ran 4 | Through caverns measureless to man 5 | Down to a sunless sea. -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/01/out.txt: -------------------------------------------------------------------------------- 1 | In Xanadu did Kubla Khan 2 | A stately pleasure-dome decree: 3 | Where Alph, the sacred river, ran 4 | Through caverns measureless to man 5 | Down to a sunless sea. -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/02/CopyToUppercase.java: -------------------------------------------------------------------------------- 1 | import java.io.FileReader; 2 | import java.io.FileWriter; 3 | import java.io.IOException; 4 | 5 | public class CopyToUppercase { 6 | public static void main(String[] args) throws IOException { 7 | 8 | FileReader inputStream = null; 9 | FileWriter outputStream = null; 10 | 11 | try { 12 | inputStream = new FileReader(args[0]); 13 | outputStream = new FileWriter(args[1]); 14 | 15 | int c; // c is now 16bit 16 | while ((c = inputStream.read()) != -1) { 17 | char[] ch =Character.toChars(c); 18 | outputStream.write(Character.toUpperCase(ch[0])); 19 | } 20 | } finally { 21 | if (inputStream != null) { 22 | inputStream.close(); 23 | } 24 | if (outputStream != null) { 25 | outputStream.close(); 26 | } 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/02/in.txt: -------------------------------------------------------------------------------- 1 | In Xanadu did Kubla Khan 2 | A stately pleasure-dome decree: 3 | Where Alph, the sacred river, ran 4 | Through caverns measureless to man 5 | Down to a sunless sea. -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/02/out.txt: -------------------------------------------------------------------------------- 1 | IN XANADU DID KUBLA KHAN 2 | A STATELY PLEASURE-DOME DECREE: 3 | WHERE ALPH, THE SACRED RIVER, RAN 4 | THROUGH CAVERNS MEASURELESS TO MAN 5 | DOWN TO A SUNLESS SEA. -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/02/up.txt: -------------------------------------------------------------------------------- 1 | IN XANADU DID KUBLA KHAN 2 | A STATELY PLEASURE-DOME DECREE: 3 | WHERE ALPH, THE SACRED RIVER, RAN 4 | THROUGH CAVERNS MEASURELESS TO MAN 5 | DOWN TO A SUNLESS SEA. -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/03/CopyLinesReversed.java: -------------------------------------------------------------------------------- 1 | import java.io.FileReader; 2 | import java.io.FileWriter; 3 | import java.io.BufferedReader; 4 | import java.io.PrintWriter; 5 | import java.io.IOException; 6 | import java.util.Stack; 7 | 8 | public class CopyLinesReversed { 9 | public static void main(String[] args) throws IOException { 10 | 11 | BufferedReader inputStream = null; 12 | PrintWriter outputStream = null; 13 | Stack lines = new Stack(); 14 | 15 | try { 16 | inputStream = new BufferedReader(new FileReader(args[0])); 17 | outputStream = new PrintWriter(new FileWriter(args[1])); 18 | 19 | String l; 20 | while ((l = inputStream.readLine()) != null) { 21 | lines.push(l); 22 | } 23 | while( !lines.empty() ) { 24 | l = lines.pop(); 25 | outputStream.println(l); 26 | } 27 | } finally { 28 | if (inputStream != null) { 29 | inputStream.close(); 30 | } 31 | if (outputStream != null) { 32 | outputStream.close(); 33 | } 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/03/in.txt: -------------------------------------------------------------------------------- 1 | In Xanadu did Kubla Khan 2 | A stately pleasure-dome decree: 3 | Where Alph, the sacred river, ran 4 | Through caverns measureless to man 5 | Down to a sunless sea. -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/03/out.txt: -------------------------------------------------------------------------------- 1 | Down to a sunless sea. 2 | Through caverns measureless to man 3 | Where Alph, the sacred river, ran 4 | A stately pleasure-dome decree: 5 | In Xanadu did Kubla Khan 6 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/03/rev.txt: -------------------------------------------------------------------------------- 1 | Down to a sunless sea. 2 | Through caverns measureless to man 3 | Where Alph, the sacred river, ran 4 | A stately pleasure-dome decree: 5 | In Xanadu did Kubla Khan 6 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/04/01-Intro-copy.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/04/01-Intro-copy.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/04/01-Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/04/01-Intro.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/04/09-Exceptions-copy.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/04/09-Exceptions-copy.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/04/09-Exceptions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/04/09-Exceptions.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/04/CopyBuffered.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.FileOutputStream; 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.IOException; 6 | 7 | public class CopyBuffered { 8 | public static void main(String[] args) throws IOException { 9 | 10 | FileInputStream in = null; 11 | BufferedInputStream bis = null; 12 | FileOutputStream out = null; 13 | BufferedOutputStream bos = null; 14 | 15 | byte[] buffer = new byte[100*1024]; 16 | 17 | try { 18 | in = new FileInputStream(args[0]); 19 | bis = new BufferedInputStream(in); 20 | out = new FileOutputStream(args[1]); 21 | bos = new BufferedOutputStream(out); 22 | 23 | int numBytes; 24 | while ((numBytes = bis.read(buffer))!= -1) 25 | { 26 | bos.write(buffer); 27 | } 28 | 29 | } finally { 30 | if (bis != null) { 31 | bis.close(); 32 | } 33 | if (bos != null) { 34 | bos.close(); 35 | } 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/05/Scanning.java: -------------------------------------------------------------------------------- 1 | import java.io.*; 2 | import java.util.Scanner; 3 | 4 | public class Scanning { 5 | public static void main(String[] args) 6 | throws IOException { 7 | 8 | Scanner sc = null; 9 | 10 | try { 11 | sc = new Scanner( 12 | new BufferedReader( 13 | new FileReader(args[0]))); 14 | int doubles=0, strings=0; 15 | while (sc.hasNext()) { 16 | if (sc.hasNextDouble()) { 17 | sc.nextDouble(); 18 | doubles++; 19 | } else { 20 | sc.next(); 21 | strings++; 22 | } 23 | } 24 | System.out.format("Κείμενα: \t\t\t%10d\n", strings); 25 | System.out.format("Αριθμοί κινητής υποδιαστολής: \t%10d\n\n", doubles); 26 | } finally { 27 | if (sc != null) { 28 | sc.close(); 29 | } 30 | } 31 | } 32 | } 33 | 34 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/06/StockDataReader.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.BufferedInputStream; 3 | import java.io.DataInputStream; 4 | import java.io.IOException; 5 | import java.io.EOFException; 6 | 7 | public class StockDataReader { 8 | public static void main(String[] args) throws IOException { 9 | 10 | DataInputStream in = null; 11 | 12 | try { 13 | in = new DataInputStream( 14 | new BufferedInputStream( 15 | new FileInputStream(args[0]))); 16 | 17 | while (true) { 18 | String stock = in.readUTF(); 19 | double lastPrice = in.readDouble(); 20 | int units = in.readInt(); 21 | 22 | System.out.format("Stock %s \tlast price %5.3f\tunits %d\n", stock, lastPrice, units); 23 | } 24 | } 25 | catch (EOFException e) { 26 | // This is normal, End Of File reached 27 | } 28 | finally { 29 | if (in != null) { 30 | in.close(); 31 | } 32 | } 33 | } 34 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/06/StockDataWriter.java: -------------------------------------------------------------------------------- 1 | import java.io.FileOutputStream; 2 | import java.io.BufferedOutputStream; 3 | import java.io.DataOutputStream; 4 | import java.io.IOException; 5 | 6 | public class StockDataWriter { 7 | public static void main(String[] args) throws IOException { 8 | 9 | String[] stocks = { "DJI", "GDAXI", "FCHI" }; 10 | double[] lastPrices = { 19.99, 9.99, 15.99 }; 11 | int[] units = { 12, 8, 13 }; 12 | 13 | DataOutputStream out = null; 14 | 15 | try { 16 | out = new DataOutputStream( 17 | new BufferedOutputStream( 18 | new FileOutputStream(args[0]))); 19 | 20 | for (int i = 0; i < stocks.length; i ++) { 21 | out.writeUTF(stocks[i]); 22 | out.writeDouble(lastPrices[i]); 23 | out.writeInt(units[i]); 24 | } 25 | } finally { 26 | if (out != null) { 27 | out.close(); 28 | } 29 | } 30 | } 31 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/06/stock.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/06/stock.out -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/07/Stock.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Stock implements java.io.Serializable{ 4 | 5 | private String symbol; 6 | private String name; 7 | private float currentValue; 8 | private float openningValue; 9 | 10 | public Stock(String symbol , String name){ 11 | this.symbol = symbol; 12 | this.name = name; 13 | } 14 | 15 | public void setCurrentValue(float currentValue){ 16 | this.currentValue = currentValue; 17 | } 18 | 19 | public void setOpenningValue(float openningValue){ 20 | this.openningValue = openningValue; 21 | } 22 | 23 | public String getSymbol(){ 24 | return this.symbol; 25 | } 26 | 27 | public String getName(){ 28 | return this.name; 29 | } 30 | 31 | public String toString(){ 32 | float def = currentValue - openningValue; 33 | return this.symbol + "(" + this.name + "):$" + this.currentValue + "($" + def + ")"; 34 | 35 | } 36 | 37 | public int compareTo(Stock other){ 38 | 39 | if(other.currentValue < this.currentValue){ 40 | return -1; 41 | }else if (other.currentValue > this.currentValue){ 42 | return 1; 43 | }else{ 44 | return 0; 45 | } 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/07/StockReader.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.ObjectInputStream; 3 | import java.io.IOException; 4 | import java.io.EOFException; 5 | 6 | public class StockReader { 7 | public static void main(String[] args) throws IOException { 8 | 9 | ObjectInputStream in = null; 10 | 11 | try { 12 | in = new ObjectInputStream( 13 | new FileInputStream(args[0])); 14 | 15 | while (true) { 16 | Stock stock = (Stock)in.readObject(); 17 | System.out.println(stock); 18 | } 19 | } 20 | catch (EOFException ex) { 21 | // This is normal, End Of File reached 22 | } 23 | catch (ClassNotFoundException ex) { 24 | System.out.println("Cannot instantiate Stock.class object (class file not found)."); 25 | } 26 | finally { 27 | if (in != null) { 28 | in.close(); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/07/StockWriter.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | import java.util.ArrayList; 4 | import java.io.FileOutputStream; 5 | import java.io.ObjectOutputStream; 6 | import java.io.IOException; 7 | 8 | public class StockWriter { 9 | 10 | public static void main(String[] args) throws IOException{ 11 | 12 | List stocks = new ArrayList(); 13 | 14 | Stock Obj1 = new Stock("FB","Facebook"); 15 | Obj1.setCurrentValue(100); 16 | Obj1.setOpenningValue(263); 17 | 18 | Stock Obj2 = new Stock("YT","Youtube"); 19 | Obj2.setCurrentValue(678); 20 | Obj2.setOpenningValue(453); 21 | 22 | 23 | 24 | Stock Obj3 = new Stock("MC","Microsoft"); 25 | Obj3.setCurrentValue(555); 26 | Obj3.setOpenningValue(444); 27 | 28 | Stock Obj4 = new Stock("YH","Yahoo"); 29 | Obj4.setCurrentValue(1833); 30 | Obj4.setOpenningValue(4523); 31 | 32 | Stock Obj5 = new Stock("COCA","COCA COLA"); 33 | Obj5.setCurrentValue(347); 34 | Obj5.setOpenningValue(68); 35 | 36 | stocks.add(Obj1); 37 | stocks.add(Obj2); 38 | stocks.add(Obj3); 39 | stocks.add(Obj4); 40 | stocks.add(Obj5); 41 | 42 | ObjectOutputStream out = null; 43 | 44 | try { 45 | out = new ObjectOutputStream( 46 | new FileOutputStream(args[0])); 47 | 48 | for (Stock s: stocks) { 49 | out.writeObject(s); 50 | } 51 | } finally { 52 | if (out != null) { 53 | out.close(); 54 | } 55 | } 56 | 57 | } 58 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/07/mystock.txt: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/07/mystock.txt -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/07/stocks.out: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/07/stocks.out -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.1/Stock.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Stock implements java.io.Serializable{ 4 | 5 | private String symbol; 6 | private String name; 7 | private float currentValue; 8 | private float openningValue; 9 | 10 | public Stock(String symbol , String name){ 11 | this.symbol = symbol; 12 | this.name = name; 13 | } 14 | 15 | public void setCurrentValue(float currentValue){ 16 | this.currentValue = currentValue; 17 | } 18 | 19 | public void setOpenningValue(float openningValue){ 20 | this.openningValue = openningValue; 21 | } 22 | 23 | public String getSymbol(){ 24 | return this.symbol; 25 | } 26 | 27 | public String getName(){ 28 | return this.name; 29 | } 30 | 31 | public String toString(){ 32 | float def = currentValue - openningValue; 33 | return this.symbol + "(" + this.name + "):$" + this.currentValue + "($" + def + ")"; 34 | 35 | } 36 | 37 | public int compareTo(Stock other){ 38 | 39 | if(other.currentValue < this.currentValue){ 40 | return -1; 41 | }else if (other.currentValue > this.currentValue){ 42 | return 1; 43 | }else{ 44 | return 0; 45 | } 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.1/StockJsonReader.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.io.Reader; 3 | import java.io.InputStreamReader; 4 | import java.io.IOException; 5 | import java.lang.reflect.Type; 6 | import com.google.gson.Gson; 7 | import com.google.gson.GsonBuilder; 8 | import com.google.gson.reflect.TypeToken; 9 | 10 | public class StockJsonReader { 11 | public static void main(String[] args) throws IOException { 12 | 13 | Reader in = null; 14 | 15 | try { 16 | in = new InputStreamReader(StockJsonReader.class.getResourceAsStream(args[0])); 17 | 18 | Gson gson = new GsonBuilder().create(); 19 | Type listType = new TypeToken>() {}.getType(); 20 | List stocks = new Gson().fromJson(in, listType); 21 | 22 | for (Stock s: stocks) { 23 | System.out.println(s); 24 | } 25 | } 26 | finally { 27 | if (in != null) { 28 | in.close(); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.1/StockJsonWriter.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | import java.util.ArrayList; 4 | import java.io.FileWriter; 5 | import java.io.IOException; 6 | import java.io.Writer; 7 | import com.google.gson.Gson; 8 | import com.google.gson.GsonBuilder; 9 | 10 | public class StockJsonWriter { 11 | 12 | public static void main(String[] args) throws IOException{ 13 | 14 | List stocks = new ArrayList(); 15 | 16 | Stock Obj1 = new Stock("FB","Facebook"); 17 | Obj1.setCurrentValue(100); 18 | Obj1.setOpenningValue(263); 19 | 20 | Stock Obj2 = new Stock("YT","Youtube"); 21 | Obj2.setCurrentValue(678); 22 | Obj2.setOpenningValue(453); 23 | 24 | Stock Obj3 = new Stock("MC","Microsoft"); 25 | Obj3.setCurrentValue(555); 26 | Obj3.setOpenningValue(444); 27 | 28 | Stock Obj4 = new Stock("YH","Yahoo"); 29 | Obj4.setCurrentValue(1833); 30 | Obj4.setOpenningValue(4523); 31 | 32 | Stock Obj5 = new Stock("COCA","COCA COLA"); 33 | Obj5.setCurrentValue(347); 34 | Obj5.setOpenningValue(68); 35 | 36 | stocks.add(Obj1); 37 | stocks.add(Obj2); 38 | stocks.add(Obj3); 39 | stocks.add(Obj4); 40 | stocks.add(Obj5); 41 | 42 | Writer out = null; 43 | Gson gson = new GsonBuilder().create(); 44 | 45 | try { 46 | out = new FileWriter(args[0]); 47 | gson.toJson(stocks, out); 48 | } finally { 49 | if (out != null) { 50 | out.close(); 51 | } 52 | } 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.1/gson-2.8.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/08.1/gson-2.8.0.jar -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.1/stocks.json: -------------------------------------------------------------------------------- 1 | [{"symbol":"FB","name":"Facebook","currentValue":100.0,"openningValue":263.0},{"symbol":"YT","name":"Youtube","currentValue":678.0,"openningValue":453.0},{"symbol":"MC","name":"Microsoft","currentValue":555.0,"openningValue":444.0},{"symbol":"YH","name":"Yahoo","currentValue":1833.0,"openningValue":4523.0},{"symbol":"COCA","name":"COCA COLA","currentValue":347.0,"openningValue":68.0}] -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.2/Stock.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Stock implements java.io.Serializable{ 4 | 5 | private String symbol; 6 | private String name; 7 | private float currentValue; 8 | private float openningValue; 9 | 10 | public Stock(String symbol , String name){ 11 | this.symbol = symbol; 12 | this.name = name; 13 | } 14 | 15 | public void setCurrentValue(float currentValue){ 16 | this.currentValue = currentValue; 17 | } 18 | 19 | public void setOpenningValue(float openningValue){ 20 | this.openningValue = openningValue; 21 | } 22 | 23 | public String getSymbol(){ 24 | return this.symbol; 25 | } 26 | 27 | public String getName(){ 28 | return this.name; 29 | } 30 | 31 | public String toString(){ 32 | float def = currentValue - openningValue; 33 | return this.symbol + "(" + this.name + "):$" + this.currentValue + "($" + def + ")"; 34 | 35 | } 36 | 37 | public int compareTo(Stock other){ 38 | 39 | if(other.currentValue < this.currentValue){ 40 | return -1; 41 | }else if (other.currentValue > this.currentValue){ 42 | return 1; 43 | }else{ 44 | return 0; 45 | } 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.2/StockJsonReader.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.io.Reader; 3 | import java.io.InputStreamReader; 4 | import java.io.IOException; 5 | import java.lang.reflect.Type; 6 | import com.google.gson.Gson; 7 | import com.google.gson.GsonBuilder; 8 | import com.google.gson.reflect.TypeToken; 9 | 10 | public class StockJsonReader { 11 | public static void main(String[] args) throws IOException { 12 | 13 | Reader in = null; 14 | 15 | try { 16 | in = new InputStreamReader(StockJsonReader.class.getResourceAsStream(args[0])); 17 | 18 | Gson gson = new GsonBuilder().create(); 19 | Type listType = new TypeToken>() {}.getType(); 20 | List stocks = new Gson().fromJson(in, listType); 21 | 22 | for (Stock s: stocks) { 23 | System.out.println(s); 24 | } 25 | } 26 | finally { 27 | if (in != null) { 28 | in.close(); 29 | } 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.2/StockJsonWriter.java: -------------------------------------------------------------------------------- 1 | import java.util.Collections; 2 | import java.util.List; 3 | import java.util.ArrayList; 4 | import java.io.FileWriter; 5 | import java.io.IOException; 6 | import java.io.Writer; 7 | import com.google.gson.Gson; 8 | import com.google.gson.GsonBuilder; 9 | 10 | public class StockJsonWriter { 11 | 12 | public static void main(String[] args) throws IOException{ 13 | 14 | List stocks = new ArrayList(); 15 | 16 | Stock Obj1 = new Stock("FB","Facebook"); 17 | Obj1.setCurrentValue(100); 18 | Obj1.setOpenningValue(263); 19 | 20 | Stock Obj2 = new Stock("YT","Youtube"); 21 | Obj2.setCurrentValue(678); 22 | Obj2.setOpenningValue(453); 23 | 24 | Stock Obj3 = new Stock("MC","Microsoft"); 25 | Obj3.setCurrentValue(555); 26 | Obj3.setOpenningValue(444); 27 | 28 | Stock Obj4 = new Stock("YH","Yahoo"); 29 | Obj4.setCurrentValue(1833); 30 | Obj4.setOpenningValue(4523); 31 | 32 | Stock Obj5 = new Stock("COCA","COCA COLA"); 33 | Obj5.setCurrentValue(347); 34 | Obj5.setOpenningValue(68); 35 | 36 | stocks.add(Obj1); 37 | stocks.add(Obj2); 38 | stocks.add(Obj3); 39 | stocks.add(Obj4); 40 | stocks.add(Obj5); 41 | 42 | Writer out = null; 43 | Gson gson = new GsonBuilder().create(); 44 | 45 | try { 46 | out = new FileWriter(args[0]); 47 | gson.toJson(stocks, out); 48 | } finally { 49 | if (out != null) { 50 | out.close(); 51 | } 52 | } 53 | 54 | } 55 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.2/gson-2.8.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/08.2/gson-2.8.0.jar -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.2/stocks.json: -------------------------------------------------------------------------------- 1 | [{"symbol":"FB","name":"Facebook","currentValue":100.0,"openningValue":263.0},{"symbol":"YT","name":"Youtube","currentValue":678.0,"openningValue":453.0},{"symbol":"MC","name":"Microsoft","currentValue":555.0,"openningValue":444.0},{"symbol":"YH","name":"Yahoo","currentValue":1833.0,"openningValue":4523.0},{"symbol":"COCA","name":"COCA COLA","currentValue":347.0,"openningValue":68.0}] -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08.2/stocks_parser.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | with open('stocks.json') as json_file: 4 | data = json.load(json_file) 5 | for stock in data: 6 | #print (stock['name'], '(', stock['symbol'], ') $', stock['currentValue']) 7 | print stock['name'], '(', stock['symbol'], ') $', stock['currentValue'] 8 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/OrderJsonWriter.java: -------------------------------------------------------------------------------- 1 | import java.io.FileWriter; 2 | import java.io.IOException; 3 | import java.io.Writer; 4 | import com.google.gson.Gson; 5 | import com.google.gson.GsonBuilder; 6 | 7 | public class OrderJsonWriter { 8 | 9 | public static void main(String[] args) throws IOException{ 10 | 11 | Order obj1 = new Order("Bike", 100, 1); 12 | 13 | Writer out = null; 14 | Gson gson = new GsonBuilder().create(); 15 | 16 | try { 17 | out = new FileWriter(args[0]); 18 | gson.toJson(obj1, out); 19 | } finally { 20 | if (out != null) { 21 | out.close(); 22 | } 23 | } 24 | 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/Stock.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Stock implements java.io.Serializable{ 4 | 5 | private String symbol; 6 | private String name; 7 | private float currentValue; 8 | private float openningValue; 9 | 10 | public Stock(String symbol , String name){ 11 | this.symbol = symbol; 12 | this.name = name; 13 | } 14 | 15 | public void setCurrentValue(float currentValue){ 16 | this.currentValue = currentValue; 17 | } 18 | 19 | public void setOpenningValue(float openningValue){ 20 | this.openningValue = openningValue; 21 | } 22 | 23 | public String getSymbol(){ 24 | return this.symbol; 25 | } 26 | 27 | public String getName(){ 28 | return this.name; 29 | } 30 | 31 | public String toString(){ 32 | float def = currentValue - openningValue; 33 | return this.symbol + "(" + this.name + "):$" + this.currentValue + "($" + def + ")"; 34 | 35 | } 36 | 37 | public int compareTo(Stock other){ 38 | 39 | if(other.currentValue < this.currentValue){ 40 | return -1; 41 | }else if (other.currentValue > this.currentValue){ 42 | return 1; 43 | }else{ 44 | return 0; 45 | } 46 | 47 | } 48 | 49 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/StockJsonReader.java: -------------------------------------------------------------------------------- 1 | import java.io.Reader; 2 | import java.io.InputStreamReader; 3 | import java.io.IOException; 4 | import com.google.gson.Gson; 5 | import com.google.gson.GsonBuilder; 6 | 7 | public class StockJsonReader { 8 | public static void main(String[] args) throws IOException { 9 | 10 | Reader in = null; 11 | 12 | try { 13 | in = new InputStreamReader(StockJsonReader.class.getResourceAsStream(args[0])); 14 | 15 | Gson gson = new GsonBuilder().create(); 16 | Stock s = new Gson().fromJson(in, Stock.class); 17 | 18 | System.out.println(s); 19 | } 20 | finally { 21 | if (in != null) { 22 | in.close(); 23 | } 24 | } 25 | } 26 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/StockJsonWriter.java: -------------------------------------------------------------------------------- 1 | import java.io.FileWriter; 2 | import java.io.IOException; 3 | import java.io.Writer; 4 | import com.google.gson.Gson; 5 | import com.google.gson.GsonBuilder; 6 | 7 | public class StockJsonWriter { 8 | 9 | public static void main(String[] args) throws IOException{ 10 | 11 | Stock obj1 = new Stock("FB","Facebook"); 12 | obj1.setCurrentValue(100); 13 | obj1.setOpenningValue(263); 14 | 15 | Writer out = null; 16 | Gson gson = new GsonBuilder().create(); 17 | 18 | try { 19 | out = new FileWriter(args[0]); 20 | gson.toJson(obj1, out); 21 | } finally { 22 | if (out != null) { 23 | out.close(); 24 | } 25 | } 26 | 27 | } 28 | } -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/gson-2.8.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/08/gson-2.8.0.jar -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/myorder.txt: -------------------------------------------------------------------------------- 1 | {"itemName":"Bike","netValue":100.0,"quantity":1,"discount":0.0} -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/out.js: -------------------------------------------------------------------------------- 1 | {"symbol":"FB","name":"Facebook","currentValue":90.0,"openningValue":263.0} -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/08/stock.json: -------------------------------------------------------------------------------- 1 | {"symbol":"FB","name":"Facebook","currentValue":100.0,"openningValue":263.0} -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.1/01-Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.1/01-Intro.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.1/09-Exceptions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.1/09-Exceptions.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.1/CopyBuffered.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.1/CopyBuffered.class -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.1/CopyBuffered.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.FileOutputStream; 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.IOException; 6 | 7 | public class CopyBuffered { 8 | public static void main(String[] args) throws IOException { 9 | 10 | byte[] buffer = new byte[100*1024]; 11 | 12 | try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0])); 13 | BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[1]))) { 14 | 15 | int numBytes; 16 | while ((numBytes = bis.read(buffer))!= -1) 17 | { 18 | bos.write(buffer); 19 | } 20 | } 21 | catch (java.io.FileNotFoundException fnfe){ 22 | System.out.println("File not found: "+fnfe.getMessage()); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/01-Intro-copy.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.2/01-Intro-copy.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/01-Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.2/01-Intro.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/09-Exceptions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.2/09-Exceptions.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/CopyBuffered.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.2/CopyBuffered.class -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/CopyBuffered.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.FileOutputStream; 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.IOException; 6 | 7 | public class CopyBuffered { 8 | public static void main(String[] args) throws IOException { 9 | 10 | byte[] buffer = new byte[100*1024]; 11 | 12 | try (BufferedInputStream bis = new MyBufferedInputStream(new FileInputStream(args[0])); 13 | BufferedOutputStream bos = new MyBufferedOutputStream(new FileOutputStream(args[1]))) { 14 | 15 | int numBytes; 16 | while ((numBytes = bis.read(buffer))!= -1) 17 | { 18 | bos.write(buffer); 19 | } 20 | } 21 | catch (java.io.FileNotFoundException fnfe){ 22 | System.out.println("File not found: "+fnfe.getMessage()); 23 | } 24 | } 25 | } 26 | 27 | class MyBufferedInputStream extends BufferedInputStream{ 28 | public MyBufferedInputStream(java.io.InputStream is){ 29 | super(is); 30 | } 31 | @Override 32 | public void close() throws IOException{ 33 | System.out.println("MyBufferedInputStream closing"); 34 | super.close(); 35 | } 36 | } 37 | class MyBufferedOutputStream extends BufferedOutputStream{ 38 | public MyBufferedOutputStream(java.io.OutputStream os){ 39 | super(os); 40 | } 41 | @Override 42 | public void close() throws IOException{ 43 | System.out.println("MyBufferedOutputStream closing"); 44 | super.close(); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/MyBufferedInputStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.2/MyBufferedInputStream.class -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09.2/MyBufferedOutputStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09.2/MyBufferedOutputStream.class -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09/01-Intro.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09/01-Intro.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09/09-Exceptions.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09/09-Exceptions.pdf -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09/CopyBuffered.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/09/CopyBuffered.class -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/09/CopyBuffered.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.FileOutputStream; 3 | import java.io.BufferedInputStream; 4 | import java.io.BufferedOutputStream; 5 | import java.io.IOException; 6 | 7 | public class CopyBuffered { 8 | public static void main(String[] args) throws IOException { 9 | 10 | byte[] buffer = new byte[100*1024]; 11 | 12 | try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(args[0])); 13 | BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(args[1]))) { 14 | 15 | int numBytes; 16 | while ((numBytes = bis.read(buffer))!= -1) 17 | { 18 | bos.write(buffer); 19 | } 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/10/FilesPaths.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/09-streams/examples/10/FilesPaths.class -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/10/FilesPaths.java: -------------------------------------------------------------------------------- 1 | import java.nio.file.*; 2 | 3 | public class FilesPaths{ 4 | 5 | public static void main(String[] args) throws java.io.IOException{ 6 | Path file = Paths.get("./file.txt"); 7 | System.out.println(file); 8 | System.out.println(file.toAbsolutePath()); 9 | System.out.println(file.getParent()); 10 | System.out.println(file.toAbsolutePath().getParent()); 11 | System.out.println(file.getFileName()); 12 | System.out.println(Files.size(file)); 13 | System.out.println(new String(Files.readAllBytes(file), "UTF-8")); 14 | Path tmpFile = Files.createTempFile("java-lab-09", ".txt"); 15 | Files.copy(file, tmpFile, StandardCopyOption.REPLACE_EXISTING); 16 | System.out.println(tmpFile.getFileName()); 17 | System.out.println(Files.size(tmpFile)); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /lecture-examples/09-streams/examples/10/file.txt: -------------------------------------------------------------------------------- 1 | mpla mpla 2 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/01/CountCountries.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/01/CountCountries.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/01/CountCountries.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | 7 | public class CountCountries{ 8 | public static void main(String[] args){ 9 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 10 | // Load all countries into list 11 | List countries = new LinkedList(); 12 | while( sc.hasNext() ){ 13 | countries.add(sc.next()); 14 | } 15 | // Count countries begining with E 16 | int count = 0; 17 | for (String s: countries){ 18 | if ( s.toUpperCase().charAt(0) == 'E' ) 19 | count++; 20 | } 21 | System.out.println("Countries beginning with 'E' count: "+count); 22 | } 23 | catch (IOException ioex){ 24 | System.out.println(ioex); 25 | } 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/01/CountCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/01/CountCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/01/CountCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | 7 | public class CountCountriesStream{ 8 | public static void main(String[] args){ 9 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 10 | // Load all countries into list 11 | List countries = new LinkedList(); 12 | while( sc.hasNext() ){ 13 | countries.add(sc.next()); 14 | } 15 | // Count countries begining with E 16 | long count = countries.stream() 17 | .filter(s -> s.toUpperCase().charAt(0) == 'E') 18 | .count(); 19 | System.out.println("Countries beginning with 'E' count: "+count); 20 | } 21 | catch (IOException ioex){ 22 | System.out.println(ioex); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/01/ListCountries.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/01/ListCountries.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/02/LimitCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/02/LimitCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/02/LimitCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | 7 | public class LimitCountriesStream{ 8 | public static void main(String[] args){ 9 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 10 | // Load all countries into list 11 | List countries = new LinkedList(); 12 | while( sc.hasNext() ){ 13 | countries.add(sc.next()); 14 | } 15 | // Print first 3 countries begining with E 16 | countries.stream() 17 | .filter(s -> s.toUpperCase().charAt(0) == 'E') 18 | .limit(3) 19 | .forEach(s -> System.out.println(s)); 20 | } 21 | catch (IOException ioex){ 22 | System.out.println(ioex); 23 | } 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/03/PrintCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/03/PrintCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/03/PrintCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.stream.Stream; 2 | 3 | public class PrintCountriesStream{ 4 | public static void main(String[] args){ 5 | // Print first 3 countries begining with E 6 | Stream countries = Stream.of("East Timor","Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia"); 7 | countries.filter(s -> s.toUpperCase().charAt(0) == 'E') 8 | .limit(3) 9 | .forEach(s -> System.out.println(s)); 10 | 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/04/ConcatCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/04/ConcatCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/04/ConcatCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.stream.Stream; 2 | import java.util.stream.Collectors; 3 | import java.util.Scanner; 4 | import java.net.URL; 5 | import java.io.IOException; 6 | import java.util.List; 7 | import java.util.LinkedList; 8 | 9 | 10 | public class ConcatCountriesStream{ 11 | public static void main(String[] args){ 12 | // Print first 3 countries begining with E 13 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 14 | List countries = new LinkedList(); 15 | while( sc.hasNext() ){ 16 | countries.add(sc.next()); 17 | } 18 | 19 | String str = countries.stream().filter(s -> s.toUpperCase().charAt(0) == 'E') 20 | .limit(3) 21 | .collect(Collectors.joining(", ")); 22 | System.out.println(str); 23 | } 24 | catch (IOException ioex){ 25 | System.out.println(ioex); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/04/GroupByCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/04/GroupByCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/04/GroupByCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | import java.util.Map; 7 | import java.util.stream.Stream; 8 | import java.util.stream.Collectors; 9 | 10 | public class GroupByCountriesStream{ 11 | public static void main(String[] args){ 12 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 13 | // Load all countries into list 14 | List countries = new LinkedList(); 15 | while( sc.hasNext() ){ 16 | countries.add(sc.next()); 17 | } 18 | // Group countries by begining letter 19 | Map> countriesGrouped = 20 | countries.stream().collect( 21 | Collectors.groupingBy(s -> s.toUpperCase().charAt(0))); 22 | System.out.println("Countries beginning by letter: \n"+countriesGrouped); 23 | // Count countries by begining letter 24 | Map countriesGroupedCounted = 25 | countries.stream().collect( 26 | Collectors.groupingBy(s -> s.toUpperCase().charAt(0), 27 | Collectors.counting())); 28 | System.out.println("Number of countries beginning by letter: \n"+countriesGroupedCounted); 29 | 30 | } 31 | catch (IOException ioex){ 32 | System.out.println(ioex); 33 | } 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/05/ProcessCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/05/ProcessCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/05/ProcessCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | import java.util.Map; 7 | import java.util.stream.Stream; 8 | import java.util.stream.Collectors; 9 | 10 | public class ProcessCountriesStream{ 11 | public static void main(String[] args){ 12 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 13 | // Load all countries into list 14 | List countries = new LinkedList(); 15 | while( sc.hasNext() ){ 16 | countries.add(sc.nextLine()); 17 | } 18 | 19 | String str = countries.stream() 20 | .filter(s -> s.length()>3) // Keep longer than 3 21 | .map(s -> s.replaceAll(" ", "_")) 22 | .sorted() 23 | .distinct() 24 | .skip(20) 25 | .limit(10) 26 | .collect(Collectors.joining(", ")); 27 | System.out.println(str); 28 | } 29 | catch (IOException ioex){ 30 | System.out.println(ioex); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/06/ProcessCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/06/ProcessCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/06/ProcessCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | import java.util.Map; 7 | import java.util.stream.Stream; 8 | import java.util.stream.Collectors; 9 | 10 | public class ProcessCountriesStream{ 11 | public static void main(String[] args){ 12 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 13 | // Load all countries into list 14 | List countries = new LinkedList(); 15 | while( sc.hasNext() ){ 16 | countries.add(sc.nextLine()); 17 | } 18 | 19 | String str = countries.stream() 20 | .filter(s -> s.length()>1) // Keep longer than 3 21 | .map(s -> s.replaceAll(" ", "_")) 22 | .sorted( (s1, s2) -> s1.length() - s2.length() ) 23 | .limit(20) 24 | .collect(Collectors.joining(", ")); 25 | System.out.println(str); 26 | } 27 | catch (IOException ioex){ 28 | System.out.println(ioex); 29 | } 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/07/ProcessOptionalCountriesStream.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/riggas-ionio/java/522f9c472933e5b670c5d863190969767e0d4b02/lecture-examples/10-streams/examples/07/ProcessOptionalCountriesStream.class -------------------------------------------------------------------------------- /lecture-examples/10-streams/examples/07/ProcessOptionalCountriesStream.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | import java.net.URL; 3 | import java.io.IOException; 4 | import java.util.List; 5 | import java.util.LinkedList; 6 | import java.util.Map; 7 | import java.util.stream.Stream; 8 | import java.util.stream.Collectors; 9 | import java.util.Optional; 10 | 11 | public class ProcessOptionalCountriesStream{ 12 | public static void main(String[] args){ 13 | try(Scanner sc = new Scanner(new URL("https://gist.githubusercontent.com/kalinchernev/486393efcca01623b18d/raw/daa24c9fea66afb7d68f8d69f0c4b8eeb9406e83/countries").openStream())){ 14 | // Load all countries into list 15 | List countries = new LinkedList(); 16 | while( sc.hasNext() ){ 17 | countries.add(sc.nextLine()); 18 | } 19 | final int MIN_LENGTH = 100; 20 | Optional str = countries.stream() 21 | .filter(s -> s.length()>MIN_LENGTH) // Keep longer than 3 22 | .map(s -> s.replaceAll(" ", "_")) 23 | .sorted( (s1, s2) -> s1.length() - s2.length() ) 24 | .findFirst(); 25 | 26 | System.out.println(str.orElse("No country with length "+MIN_LENGTH)); 27 | } 28 | catch (IOException ioex){ 29 | System.out.println(ioex); 30 | } 31 | } 32 | } 33 | --------------------------------------------------------------------------------