Java Expressions & Flow Control

Similar documents
Classes and Objects. Again: Distance between points within the first quadrant. José Valente de Oliveira 4-1

Java Inheritance. Class ADT (Abstract Data Type) Interface. Classes implement the concept of ADT: Interfaces define interaction contracts: Rui Moreira

Behavioral Modeling in Verilog

CS 11 C track: lecture 1

Chapter 2. C++ Basics. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Chapter 11. Friends, Overloaded Operators, and Arrays in Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Recursion. Computer Science S-111 Harvard University David G. Sullivan, Ph.D. Review: Method Frames

Elementary Educational Computer

Abstract. Chapter 4 Computation. Overview 8/13/18. Bjarne Stroustrup Note:

Chapter 9. Pointers and Dynamic Arrays. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

CMPT 125 Assignment 2 Solutions

COSC 1P03. Ch 7 Recursion. Introduction to Data Structures 8.1

Goals of the Lecture Object Constraint Language

Chapter 10. Defining Classes. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

implement language system

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

BaanERP Tools. Programming features

Python Programming: An Introduction to Computer Science

Parabolic Path to a Best Best-Fit Line:

Analysis Metrics. Intro to Algorithm Analysis. Slides. 12. Alg Analysis. 12. Alg Analysis

Lecture 5: Recursion. Recursion Overview. Recursion is a powerful technique for specifying funclons, sets, and programs

Chapter 3. More Flow of Control. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Solution printed. Do not start the test until instructed to do so! CS 2604 Data Structures Midterm Spring, Instructions:

top() Applications of Stacks

ICS Regent. Communications Modules. Module Operation. RS-232, RS-422 and RS-485 (T3150A) PD-6002

APPLICATION NOTE PACE1750AE BUILT-IN FUNCTIONS

From last week. Lecture 5. Outline. Principles of programming languages

CSC 220: Computer Organization Unit 11 Basic Computer Organization and Design

Chapter 8. Strings and Vectors. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CS 111: Program Design I Lecture # 7: First Loop, Web Crawler, Functions

CIS 121 Data Structures and Algorithms with Java Spring Stacks and Queues Monday, February 12 / Tuesday, February 13

Exceptions. Your computer takes exception. The Exception Class. Causes of Exceptions

University of Waterloo Department of Electrical and Computer Engineering ECE 250 Algorithms and Data Structures

Evaluation scheme for Tracking in AMI

Homework 1 Solutions MA 522 Fall 2017

Chapter 4 The Datapath

Computers and Scientific Thinking

Abstract Syntax Trees. AST Data Structure. Visitor Interface. Accept methods. Visitor Methodology for AST Traversal CS412/CS413

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Abstract Data Types (ADTs) Stacks. The Stack ADT ( 4.2) Stack Interface in Java

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Chapter 8. Strings and Vectors. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

n n B. How many subsets of C are there of cardinality n. We are selecting elements for such a

Numerical Methods Lecture 6 - Curve Fitting Techniques

CS211 Fall 2003 Prelim 2 Solutions and Grading Guide

CS : Programming for Non-Majors, Summer 2007 Programming Project #3: Two Little Calculations Due by 12:00pm (noon) Wednesday June

Graphs. Minimum Spanning Trees. Slides by Rose Hoberman (CMU)

Alpha Individual Solutions MAΘ National Convention 2013

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 4. The Processor. Single-Cycle Disadvantages & Advantages

WORKED EXAMPLE 7.1. Producing a Mass Mailing. We want to automate the process of producing mass mailings. A typical letter might look as follows:

. Written in factored form it is easy to see that the roots are 2, 2, i,

! Given the following Structure: ! We can define a pointer to a structure. ! Now studentptr points to the s1 structure.

Chapter 6. I/O Streams as an Introduction to Objects and Classes. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

CMSC Computer Architecture Lecture 3: ISA and Introduction to Microarchitecture. Prof. Yanjing Li University of Chicago

n Maurice Wilkes, 1949 n Organize software to minimize errors. n Eliminate most of the errors we made anyway.

Hash Tables. Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015.

Lecture 9: Exam I Review

Last class. n Scheme. n Equality testing. n eq? vs. equal? n Higher-order functions. n map, foldr, foldl. n Tail recursion

Chapter 3 Classification of FFT Processor Algorithms

Lecture 3. RTL Design Methodology. Transition from Pseudocode & Interface to a Corresponding Block Diagram

Counting II 3, 7 3, 2 3, 9 7, 2 7, 9 2, 9

High-Order Language APPLICATION LEVEL HIGH-ORDER LANGUAGE LEVEL ASSEMBLY LEVEL OPERATING SYSTEM LEVEL INSTRUCTION SET ARCHITECTURE LEVEL

EE260: Digital Design, Spring /16/18. n Example: m 0 (=x 1 x 2 ) is adjacent to m 1 (=x 1 x 2 ) and m 2 (=x 1 x 2 ) but NOT m 3 (=x 1 x 2 )

It just came to me that I 8.2 GRAPHS AND CONVERGENCE

CIS 121. Introduction to Trees

Multiprocessors. HPC Prof. Robert van Engelen

CMSC Computer Architecture Lecture 12: Virtual Memory. Prof. Yanjing Li University of Chicago

Python Programming: An Introduction to Computer Science

Chapter 3. Floating Point Arithmetic

CHAPTER IV: GRAPH THEORY. Section 1: Introduction to Graphs

COMPUTER ORGANIZATION AND DESIGN The Hardware/Software Interface. Chapter 4. The Processor. Part A Datapath Design

Τεχνολογία Λογισμικού

Overview. Chapter 18 Vectors and Arrays. Reminder. vector. Bjarne Stroustrup

Instruction and Data Streams

Location Steps and Paths

CS553 Lecture Reuse Optimization: Common SubExpr Elim 3

End Semester Examination CSE, III Yr. (I Sem), 30002: Computer Organization

The VSS CCD photometry spreadsheet

A graphical view of big-o notation. c*g(n) f(n) f(n) = O(g(n))

Getting Started. Getting Started - 1

Overview Chapter 12 A display model

Sorting in Linear Time. Data Structures and Algorithms Andrei Bulatov

CSE 417: Algorithms and Computational Complexity

Programming with Shared Memory PART II. HPC Spring 2017 Prof. Robert van Engelen

n We have discussed classes in previous lectures n Here, we discuss design of classes n Library design considerations

3. b. Present a combinatorial argument that for all positive integers n : : 2 n

Exercise 6 (Week 42) For the foreign students only.

Tools : The Java Compiler. The Java Interpreter. The Java Debugger

Solutions to Final COMS W4115 Programming Languages and Translators Monday, May 4, :10-5:25pm, 309 Havemeyer

A New Morphological 3D Shape Decomposition: Grayscale Interframe Interpolation Method

A Taste of Maya. Character Setup

Chapter 1. Introduction to Computers and C++ Programming. Copyright 2015 Pearson Education, Ltd.. All rights reserved.

Data Structures Week #5. Trees (Ağaçlar)

The golden search method: Question 1

Chapter 3: Operators, Expressions and Type Conversion

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

Reversible Realization of Quaternary Decoder, Multiplexer, and Demultiplexer Circuits

Data Structures and Algorithms Part 1.4

1. (a) Write a C program to display the texts Hello, World! on the screen. (2 points)

Exercise Set: Implementing an Object-Oriented Design

Transcription:

Java Expressios & Flow Cotrol Rui Moreira Expressio Separators:. [ ] ( ), ; Dot used as decimal separator or to access attributes ad methods double d = 2.6; Poto poto = ew Poto(2, 3); it i = poto.x; it j = poto.gety(); Array defiitio uses [ ] it[] arrayits = ew it[10]; //Array of primitive it Poto arraypotos[] = ew Poto[8]; //Array of refereces Parethesis chage order of evaluatio of expressios float f=5*(4+8.9); Comma used to separate elemets of a expressio (o a sigle lie) log l1=18l, l2=28l, l3=38l; Semicolo is a termiator (used to termiate expressios) it fact=1; for(it i=2; i<=umber; i++){ fact *= i; Rui Moreira 2 1

Attributio ad Arithmetic: = ++ -- * / % + - (ca be combied) it i=+1, k=15, j=-20, p+=10; i++; k--; ++j; d = 2.6+5*(x-x%2)/10; System.out.prit( Hello + World! ); // + used to cocateate strigs Compariso: ==!= > >= < <= istaceof boolea bool1 = 1==10; // bool1 gets false boolea bool2 = 1!=10; // bool2 gets true boolea bool3 = 1>10; // bool3 gets false boolea bool4 = 1<=10; // bool4 gets true boolea bool5 = poto istaceof Poto; // bool5 gets true Logic: &&! (used oly with boolea) bool6 = bool1 && bool2; // bool6 gets false because bool1 is false (AND) bool6 = bool1 bool2; // bool6 gets true because bool1 is true (OR) bool6 =!bool1; // bool6 gets true, i.e, egatio of bool1 (NOT) if (x<10 && x>5!(y==18)) { // NB: short-circuit operators truth =!rect.isiside(poto); Rui Moreira 3 Bitwise: & ^ ~ (used oly with it or boolea) byte b1 = 63 & 252; // Does bit-by-bit AND 60 10 =0..00111100 2 byte b2 = 63 252; // Does bit-by-bit OR - 255 10 =0..11111111 2 byte b3 = 63 ^ 252; // Does bit-by-bit XOR - 195 10 =0..11000011 2 byte b4 = 7; // 7 10 =00000111 2 byte b5 = ~b4; // Does bit-by-bit COMPLEMENT/NOT -8 10 =11111000 2 See examples: http://www.jaeg.ca/scjp/oper/bitwise.html // 63 & 252 (AND example) true if both bits are true 63 10 = 00000000 00000000 00000000 00111111 252 10 = 00000000 00000000 00000000 11111100 ------------------------------------------ 60 10 = 00000000 00000000 00000000 00111100 // 63 ^ 252 (XOR example) - true if both bits are equal 63 10 = 00000000 00000000 00000000 00111111 252 10 = 00000000 00000000 00000000 11111100 ------------------------------------------ 195 10 = 00000000 00000000 00000000 11000011 Rui Moreira 4 2

Bitwise Arithmetic Shift: >> << (left-shift ad right-shift) // oly used with it ad log // with it the shift-value is always betwee 0..31 // with log the shift-value is always betwee 0..63 byte b1 = 128>>1; // b1 gets 64 (divide by 2)- iserts left 0 s b1 = 32<<4; // b1 gets 256 (multiply by 4) b1 = -256>>4; // b1 gets -32 (divide by 4)- copies sigal-bit See examples: http://www.jaeg.ca/scjp/oper/bitwise.html // 16 << 5 right-shift example iserts 0 s o the rigth 16 10 = 00000000 00000000 00000000 00010000 512 10 = 00000000 00000000 00000010 00000000 // 16 >> 2 right-shift example sigal bit 0 is preserved 16 10 = 00000000 00000000 00000000 00010000 4 10 = 00000000 00000000 00000000 00000100 // -16 >> 2 right-shift example sigal bit 1 is preserved -16 10 = 11111111 11111111 11111111 11110000-4 10 = 11111111 11111111 11111111 11111100 Rui Moreira 5 Bitwise Logic Shift: >>> (usiged right-shift) // oly used with it ad log it b2 = -12; // this byte is -12 10 =1..00001100 2 b2 = b2>>2; // b2 gets -3 10 =11000011 2 b2 = b2>>>2; // b2 gets +3 10 =0..00000011 2 // The operator >> iserts 0 s i the most sigificat bit See examples: http://www.jaeg.ca/scjp/oper/bitwise.html // 16 >>> 2 (usiged right-shift example) sigal bit is same 16 10 = 00000000 00000000 00000000 00010000 4 10 = 00000000 00000000 00000000 00000100 // -16 >> 2 (usiged right-shift example) sigal bit is NOT preserved -16 10 = 11111111 11111111 11111111 11110000 1.073.741.820 10 = 00111111 11111111 11111111 11111100 The last example -16 >>> 2 (-16 >> 2 ) + ( 2 << ~2 ) = 1,073,741,820 Rui Moreira 6 3

Creator: ew <ClassCostructor> Poto p1 = ew Poto(2, 3), p2 = ew Poto(5, 8); Rectagle r1 = ew Rectagle(p1, p2); Cast: (<ClassName>) Object o = ew Poto(1, 4); //o ca referece ay object Poto p2 = (Poto)o; //Casts (forces type coformace) Vector v = ew Vector(); //Vector behaves like growable array v.addelemet(ew Rectagle(p1, p2));//stores ay kid of object Rectagle r = (Rectagle)v.elemetAt(0); //Casts to give type Rui Moreira 7 Cotrol Flow Coditioal executio: q if (i<10) {...; else {...; q switch (i) { case 1:...; default:...; Loops: q for(it i; i<limit; i++){...; q while (i<limit) {...; i++; q do {...; i++; while (i<limit); Special cotrols: q break q Cotiue q label1: while(...) {...; if (...) break label1 q label2: for(...) {...; if (...) cotiue label2 Rui Moreira 8 4

if (<boolea-expressio>) { else { Sytax alteratives: q if (<boolea-expressio>) {...; q if (<boolea-expressio>) {...; else {...; q if (<boolea-expressio>) {...; else if (<boolea-expressio>) {...; q if (<boolea-expressio>) {...; else if (<boolea-expressio>) {...; else {...; Example (NB: short-circuit versus o short-circuit operads): if (i<10 && truth){ //Do ot tests 2 d operad if 1 st fails i++; else if (i<20 & truth){ //Tests all operads i+=20; Rui Moreira 9 switch (<it-expressio>) {... Sytax: switch (<it-expressio>) {// Does ot allow float or log expressios case <it-value>:...; case <it-value>:...; default:...; Example: switch (optio) { // If optio is char/byte/short it is promoted to it case 1: callopefile(); case 2: callsavefile(); default: callexitprogram(); Rui Moreira 10 5

for (<iitialisatio>; <test>; <actualisatio>) {... Sytax: for (<iitialisatio>; <test>; <actualizatio>) { //... Example: for (it i=0, j=20; i<18 && j>5; i++, j++) { // We ca use commas to separate expressios temp += i*j; Rui Moreira 11 while (<boolea-expressio>) {... Sytax: while (<boolea-expressio>) { // This cycle will ot execute if expressio is false // We must iclude code to cotrol stop/break the cycle Examples: while (i<100) { i++; // Sometime it will reach 100 while (keepdoig) { if (temp==value) keepdoig=false; // Stop cycle Rui Moreira 12 6

do {... while (<boolea-expressio>); Sytax: do { // This cycle executes at least oe time e.g., txt-based meu // We must iclude code to stop/break the cycle while (<boolea-expressio>); Examples: do { i++; // Sometime it will reach 100 while (i<100); do { if (temp==value) keepdoig=false; // Stop cycle while (keepdoig); Rui Moreira 13 Exercises Class Poto: q Create a method that moves the poit coordiates by a distace dx ad dy Class Rectagulo: q Create a method that moves the rectagle by a distace dx ad dy q Cotrol the coordiates/positio of the poits give to the Rectagulo costructor. q Create method isiside(poto poit) which checks if the give poit is iside o the Rectagulo Class Date: q Create a class Date for storig date objects (day,moth, year) ad esure that the costructor parameters are valid dates (e.g., moths are betwee 1..12, February as oly 28 or 29 days, etc.) q Provide a method dayselapsed(date d) that returs the umber of days betwee the date object ad date d passed as parameter Class Factorial: q Create a class with several static methods, each calculatig the factorial of a give umber but usig differet cycles provided by Java (cf. for, while ad do). Rui Moreira 14 7