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

Σχετικά έγγραφα
ΤΕΧΝΙΚΕΣ ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΑΦΟΥΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΥ. Εισαγωγή στη Java

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

ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ - ΤΜΗΜΑ ΠΛΗΡΟΦΟΡΙΚΗΣ ΕΠΛ 133: ΑΝΤΙΚΕΙΜΕΝΟΣΤΡΕΦΗΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ ΕΡΓΑΣΤΗΡΙΟ 3 Javadoc Tutorial

CSD Univ. of Crete Φθινόπωρο Άσκηση 1 Εκφώνηση

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

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

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

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

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

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

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

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

Areas and Lengths in Polar Coordinates

Areas and Lengths in Polar Coordinates

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

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

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

4. Εισαγωγή στη Java

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

Homework 3 Solutions

Finite Field Problems: Solutions

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

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

Είσοδος και Έξοδος. (Input and Output) Διαφάνειες από τους Robert Sedgewick και Kevin Wayne. Τι θα δούµε σήµερα

Modbus basic setup notes for IO-Link AL1xxx Master Block

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

Section 9.2 Polar Equations and Graphs

The Simply Typed Lambda Calculus

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

2 Composition. Invertible Mappings

Διάλεξη 20: Αναδρομή (Recursion) Διδάσκων: Παναγιώτης Ανδρέου

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

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

EE512: Error Control Coding

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

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

7. Δομές Ελέγχου (Control Statements)

Section 7.6 Double and Half Angle Formulas

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

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

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

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

TMA4115 Matematikk 3

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

Math221: HW# 1 solutions

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

Μεταβλητές (Variables)

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

Introduction to Java. Module 6: Class Diagrams and Use case Diagrams. Prepared by Costantinos Costa for EPL 233

SOAP API. Table of Contents

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

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

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

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

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

ΚΕΦΑΛΑΙΟ 2. Περιγραφή της Κίνησης. 2.1 Κίνηση στο Επίπεδο

Section 8.3 Trigonometric Equations

9.09. # 1. Area inside the oval limaçon r = cos θ. To graph, start with θ = 0 so r = 6. Compute dr

Δομές ελέγχου ροής προγράμματος

Δυναμική μνήμη με πίνακες και λίστες

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

Instruction Execution Times

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

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

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

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

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

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

Right Rear Door. Let's now finish the door hinge saga with the right rear door

Σχεδιασµός, Εµφάνιση και Υφος

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

CHAPTER 48 APPLICATIONS OF MATRICES AND DETERMINANTS

Block Ciphers Modes. Ramki Thurimella

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

Βασικά Στοιχεία της Java

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

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

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

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

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

Εισαγωγή στον Προγραμματισμό

Εαρινό. Ύλη εργαστηρίου, Ασκήσεις Java

Γενικοί Τύποι. 13 Ιανουαρίου 2011 Αντικειμενοστρεφής Προγραμματισμός στη Java 32

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

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

Προγραμματισμός Η/Υ (ΤΛ2007 )

Elements of Information Theory

Δημιουργία Λογαριασμού Διαχείρισης Business Telephony Create a Management Account for Business Telephony

Partial Differential Equations in Biology The boundary element method. March 26, 2013

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

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

Αντικειμενοστρεφής Προγραμματισμός Διάλεξη 9 : ΑΦΗΡΗΜΕΝΕΣ ΚΛΑΣΕΙΣ

API: Applications Programming Interface

Strain gauge and rosettes

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

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

Numerical Analysis FMN011

derivation of the Laplacian from rectangular to spherical coordinates

4.6 Autoregressive Moving Average Model ARMA(1,1)

Transcript:

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

Java Input Review CONSOLE: Scanner stdin = new Scanner( System.in ); The "standard" input stream. This stream is already open and ready to supply input data. Typically this stream corresponds to keyboard input or another input source specified by the host environment or user. FILE: Scanner infile = new Scanner( new FileReader(srcFileName )); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 2

Java Output Review CONSOLE: System.out.print("To the screen"); The "standard" output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user. FILE: PrintWriter fout = new PrintWriter(new File("output.txt"); fout.print("to a file"); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 3

Java Output Review CONSOLE: System.err.print( Error to the screen"); The "standard" error output stream. This stream is already open and ready to accept output data. Typically this stream corresponds to display output or another output destination specified by the host environment or user. By convention, this output stream is used to display error messages or other information that should come to the immediate attention of a user even if the principal output stream, the value of the variableout, has been redirected to a file or other destination that is typically not continuously monitored. FILE: PrintWriter fout = new PrintWriter(new File("output.txt"); fout.print("to a file"); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 4

Example publicclasscomputearea{ public static void main(string[] args) { double radius; // Declare radius double area; // Declare area area); // Assign a radius radius = 20; // New value is radius // Compute area area = radius * radius * 3.14159; // Display results System.out.println("The area for the circle of radius "+ radius + " is "+ ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 5

Output ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 6

Reading Input from arguments public class ComputeAreaWithConsoleInput { public static void main(string[] args) { //Check the length of the input array if (args.length < 1) { System.err.println("Invalid arguments!!!usage: ComputeAreaWithConsoleInput <arg1>"); return; //Parse the first argument double radius =Double.parseDouble(args[0]); // Compute area double area = radius * radius * 3.14159; // Display result System.out.println("The area for the circle of radius " + radius + " is " + area); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 7

Output ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 8

Reading Input from arguments ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 9

Reading Input from arguments ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 10

Reading Input from the Console 1. Create a Scanner object Scanner input = new Scanner(System.in); 2. Use the methods next(), nextbyte(), nextshort(), nextint(), nextlong(), nextfloat(), nextdouble(), or nextboolean() to obtain to a string, byte, short, int, long, float, double, or boolean value. For example, System.out.print("Enter a double value: "); Scanner input = new Scanner(System.in); double d = input.nextdouble(); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 11

Reading Input from the Console import java.util.scanner; // Scanner is in the java.util package publicclasscomputeareawithconsoleinput{ public static void main(string[] args) { // Create a Scanner object Scanner input = newscanner(system.in); // Prompt the user to enter a radius System.out.print("Enter a number for radius: "); doubleradius = input.nextdouble(); // Compute area doublearea = radius * radius * 3.14159; // Display resultsystem.out.println("the area for the circle of radius "+ radius + " is "+ area); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 12

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

Introduction to Java Module 2.b: Expressions and Flow control Prepared by Costantinos Costa for EPL 233 ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 14

Operators Used as in C/C++ + -*/ = %(modulo)!(not) (or),&&(and) <,>, ==, >=, <=, <>,!= n++, n--, ++n, --n ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 15

Casting variable= (type) variable; Example: void casts() { inti = 200; long l = (long) I; long l2 = (long)200; Getting an intout of a String: Integer x=new Integer(String); int xvalue= x.intvalue(); Similar for float, double, long etc Look it up in the API documentation. ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 16

Casts in fundamental types Casting from another type to int, float, short, long, double: Create an instance of the appropriate wrapper class using the suitable constructor Get the desired value using the appropriate property, usually the.[type]value Example: (String to float) String piv= 3.14159 ; Float temp=new Float(piV); //create a new Float object float pi = temp.floatvalue(); //get the float value of the Float ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 17

Casting examples Integer Stringn = 23 ; Integer tmp = new Integer(n); inti= tmp.intvalue(); floatf = tmp.floatvalue(); double d = tmp.doublevalue(); Integer tmp2 = Integer.valueOf( 342 ); Float Float tmp = new Float(n); inti= tmp.intvalue(); floatf = tmp.floatvalue(); double d = tmp.doublevalue(); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 18

Flow control Java supports if-then-else, while, do-while, for and switch (case) statements as well as labeled breaks. The syntax for most of these statements is the same as in C/C++. (in addition variables can be declared in their conditions) If-then-else if (yoursales >=2*target) {bonus=1000; else if (yoursales >=1.5*target) {bonus=500; else if (yoursales >= target) {bonus=100; else {System.out.println( You are fired!! ); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 19

Flow control while (just like C/C++) while (condiction) {block while(count<6) { System.out.println(count); count++; do-while(just like C/C++) do {block while (condition) ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 20

Flow control for loops for(statement1;condition;statement2) {statements; for(int x=0;x<100;x++) {System.out.println( Number is + x); switch statement switch (choise) { case 1: { ;break; case 2: { ;break; default: {System.out.println( Invalid input );break; //end switch ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 21

Flow control The break keyword Used for breaking out of loops (and in the switch statement) The continue statement Used for transferring the execution to the top of the loop ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 22

Flow control Labeled break/continue Similar to GOTO Convenient for breaking out of nested loops or transferring the execution at the top of the loop. Label must be placed just outside the loop followed by : Works with all kinds of loops ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 23

Flow control Break/continue example outsideloop: //label for(intout=0; out<3; out++) { //some code for(int inner=0;inner < 5; inner++) { //some code if (..) {break outsideloop; //break out of the outer loop if (..) {continue outsideloop; //continue to outer loop //inner loop //outer loop System.out.println( All done ); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 24

Example-Exercise Implement a Java-method that prints out the day of the week for a given day (1..31), month (1..12) and year. The day of the week of dates between March 1900 and February 2100 can be calculated as follows: First, you have to calculate the total number of days from 1900/1/1 to the given date (see below, for details). Secondly, you divide this number by 7 with integer remainder: this now is the day of the week, with 0 as sunday, 1 as monday, etc. To calculate the total number of days you have to implement the following steps: Subtract 1900 from the given year and multiply the result by 365 Add the missing leaps years by adding (year -1900) / 4. If the year itself is a leap year and the month is January or February, you have to subtract 1 from the previous result. Now add the all days of the year up to the given one to the result (in case of february always 28, because the additional day for a leap year already have been added). Here some dates Sunday: 4/4/2010, 9/9/2012 Wednesday: 17/2/2010, 12/9/2012 ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 25

Solution /** * For a given date the corresponding day of the week is printed out. * * @author Costantinos Costa * */ public class DayOfTheWeek { /** * Prints out the day of the week for a given day, month, and year. */ public static void main(string[] args) { //Check the length of the input array if (args.length < 3) { System.err.println("Invalid arguments!!!usage: DayOfTheWeek <arg1> <arg2> <arg3>"); return; int day = Integer.parseInt(args[0]); int month = Integer.parseInt(args[1]); int year = Integer.parseInt(args[2]); ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 26

Solution // First we need to check that is a valid date switch (month) { case 12: case 10: case 8: case 6: case 4: if (day > 31) { System.out.println("Invalid Date"); return; break; case 11: case 9: case 7: case 5: case 3: case 1: if (day > 31) { System.out.println("Invalid Date"); Return; break; case 2: if (day > 28) { if (!(year % 4 == 0) day!= 29) { System.out.println("Invalid Date"); return; break; default: System.out.println("Invalid Date"); return; ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 27

Solution int dayoftheweek = (year - 1900) * 365 + (year - 1900) / 4; // In case that the year is a leap year and the month is smaller than // February if (year % 4 == 0 && month <= 2) { dayoftheweek--; switch (month) { case 12: dayoftheweek += 30; // NO break!!! Fall through to all cases case 11: dayoftheweek += 31; case 10: dayoftheweek += 30; case 9: dayoftheweek += 31; case 8: dayoftheweek += 31; case 7: dayoftheweek += 30; case 6: dayoftheweek += 31; case 5: dayoftheweek += 30; case 4: dayoftheweek += 31; case 3: dayoftheweek += 28; case 2: dayoftheweek += 31; ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 28

Solution // Determine the day of the week by a simple modulo operation dayoftheweek = (day + dayoftheweek) % 7; switch (dayoftheweek) { case 0: System.out.println("Sunday"); break; case 1: System.out.println("Monday"); break; case 2: System.out.println("Tuesday"); break; case 3: System.out.println("Wednesday"); break; case 4: System.out.println("Thursday"); break; case 5: System.out.println("Friday"); break; case 6: System.out.println("Saturday"); break; ΕΠΛ233 Αντικειμενοστρεφής Προγραμματισμός 29