Basics of Java: Expressions & Statements. Nathaniel Osgood CMPT 858 February 15, 2011

Similar documents
CSC Java Programming, Fall Java Data Types and Control Constructs

Flow Control. CSC215 Lecture

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Introduction to Programming Using Java (98-388)

Pace University. Fundamental Concepts of CS121 1

SECTION II: LANGUAGE BASICS

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

Chapter 4 Introduction to Control Statements

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

Tutorial: Functions and Functional Abstraction. Nathaniel Osgood CMPT

Binary Search. Roland Backhouse February 5th, 2001

PLT Fall Shoo. Language Reference Manual

The SPL Programming Language Reference Manual

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

Array. Prepared By - Rifat Shahriyar

BIT Java Programming. Sem 1 Session 2011/12. Chapter 2 JAVA. basic

x = 3 * y + 1; // x becomes 3 * y + 1 a = b = 0; // multiple assignment: a and b both get the value 0

FORM 2 (Please put your name and form # on the scantron!!!!)

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Control Structures in Java if-else and switch

Faculty of Engineering Computer Engineering Department Islamic University of Gaza C++ Programming Language Lab # 6 Functions

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Typed Lambda Calculus and Exception Handling

JME Language Reference Manual

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

Client-Side Web Technologies. JavaScript Part I

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

for (i=1; i<=100000; i++) { x = sqrt (y); // square root function cout << x+i << endl; }

Language Fundamentals Summary

CGS 3066: Spring 2015 JavaScript Reference

Program Fundamentals

Chapter 3: Operators, Expressions and Type Conversion

G Programming Languages - Fall 2012

Concepts Review. 2. A program is the implementation of an algorithm in a particular computer language, like C and C++.

Programming for the Web with PHP

The following expression causes a divide by zero error:

CS313D: ADVANCED PROGRAMMING LANGUAGE

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

easel LANGUAGE REFERENCE MANUAL

Unit 2: Java in the small. Prepared by: Dr. Abdallah Mohamed, AOU-KW

Sprite an animation manipulation language Language Reference Manual

Operators and Expressions

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

Functions. Lab 4. Introduction: A function : is a collection of statements that are grouped together to perform an operation.

JAVA REVIEW cs2420 Introduction to Algorithms and Data Structures Spring 2015

Unit 2: Java in the small

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

FRAC: Language Reference Manual

1007 Imperative Programming Part II

Fundamentals of Programming Session 13

SCoLang - Language Reference Manual

Lecture 8 Tao Wang 1

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Vendor: Oracle. Exam Code: 1Z Exam Name: Java SE 7 Programmer I. Version: Demo

Programming, numerics and optimization

CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

News and information! Review: Java Programs! Feedback after Lecture 2! Dead-lines for the first two lab assignment have been posted.!

CS 376b Computer Vision

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

Exam 1 Prep. Dr. Demetrios Glinos University of Central Florida. COP3330 Object Oriented Programming

PieNum Language Reference Manual

C++ (Non for C Programmer) (BT307) 40 Hours

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

Lecture 3. Lecture

boolean, char, class, const, double, else, final, float, for, if, import, int, long, new, public, return, static, throws, void, while

Chapter 1 Summary. Chapter 2 Summary. end of a string, in which case the string can span multiple lines.

The Big Python Guide

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

The Arithmetic Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

An Activation Record for Simple Subprograms. Activation Record for a Language with Stack-Dynamic Local Variables

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

AnyLogic and Java. Nathaniel Osgood. Agent-Based Modeling Bootcamp for Health Researchers

The Basic Parts of Java

A Small Interpreted Language

Information Science 1

Introduction to Bioinformatics

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

CS111: PROGRAMMING LANGUAGE II

Java Bytecode (binary file)

Java+- Language Reference Manual

Reviewing for the Midterm Covers chapters 1 to 5, 7 to 9. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

The Warhol Language Reference Manual

GBIL: Generic Binary Instrumentation Language. Language Reference Manual. By: Andrew Calvano. COMS W4115 Fall 2015 CVN

Chapter 2: Functions and Control Structures

Variables and literals

CSI33 Data Structures

Topics. Java arrays. Definition. Data Structures and Information Systems Part 1: Data Structures. Lecture 3: Arrays (1)

VARIABLES AND TYPES CITS1001

Discover how to get up and running with the Java Development Environment and with the Eclipse IDE to create Java programs.

Pierce Ch. 3, 8, 11, 15. Type Systems

Review of the C Programming Language for Principles of Operating Systems

I BSc(IT) [ Batch] Semester II Core: Object Oriented Programming With C plus plus - 212A Multiple Choice Questions.

JAC444 - Lecture 1. Introduction to Java Programming Language Segment 4. Jordan Anastasiade Java Programming Language Course

Language Reference Manual simplicity

Lecture 17. For Array Class Shenanigans

Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Transcription:

Basics of Java: Expressions & Statements Nathaniel Osgood CMPT 858 February 15, 2011

Java as a Formal Language Java supports many constructs that serve different functions Class & Interface declarations Importing references to classes from other code libraries Defining methods

Methods Methods are functions associated with a class Methods can do either or both of Computing values Performing actions Printing items Displaying things Changing the state of items Consist of two pieces Header: Says what types the method expects as arguments and returns as values, and exceptions that can be thrown Body: Describes the algorithm (code) to do the work (the implementation )

Method Bodies Method bodies consist of Variable Declarations Statements Statements are commands that do something (effect some change), for example Change the value of a variable or a field Return a value from the function Call a method Perform another set of statements a set of times Based on some condition, perform one or another set of statements

Variable Declarations Variables in Java are associated with types and can contain values The types describe the sort of values that a variable can contain (the set of possible values) E.g. double: Double precision floating point numbers int: (positive & negative): Integer values within some range String: A (reference to a) text sequence boolean: A dichotomous value, holding true, or false When we declare a variable, we indicate its name & type and possibly an initial value

Example Variable Declarations Finding location in continuous space (x,y) & in terms of Discrete vegetation Space (c,r). Poor style -- Should be In separate function Declares a variable m that initially contains a reference to the Main object Declares double-precision variables x & y Declares integer values c & r, and sets equal to the column & row for this elephant In the vegetation array

Common Java Statements if for while / do-while Try-Catch-Finally Throw (Trigger) exception An expression (typically side-effecting) Assignment Call to a function Composite statement block (multiple statements enclosed in a {} )

Common Java Expressions Literal (3.5, 1, my string, null) Causes changes Side effecting) Assignment (a=b) Left hand side is some location (variable, field, etc.) Comparison (a>b,a==b) Mathematical Operators (+,-,/,*) Can be overloaded to mean other things (e.g. + as concatenation) Method call (function call): this.get_main() Dereferencing : Looking up field or value b in an object expression a: (a.b) Ternary operator: (predicate? a : b)

Comments Comments in Java are indicated in two different ways Arbitrarily long: Begun with /* and ended with */ These can span many lines Within a line: after a // Use comments to describe your intentions!

Rerouting Around Barriers (Boundaries & Water) Poor Style entire logic, conditions (checks on boundaries, whether water) & rerouting Logic should all be in separate functions from this & from each other). Remove constants

For statements For statements iterate, repeatedly executing some inner statement many times Several variants are available for (int i = 0; i < 100; i++) statement for (int i : collection) statement

Heading Towards Resource Determining current position & Searching for quickest way to find water from that position. (should be in separate function!)

If Statements With an if statement, one tests a condition ( predicate ), and based on the result either executes one statement or another (possibly empty) statement if (condition) if (condition) true-statement or true-statement else false-statement falls through to later code if condition is false

Handling of Movement Logic Finding location in continuous space (x,y) & in terms of Discrete vegetation Space (c,r). Poor style -- Should be In separate function Handling the case of reaching water when thirsty Distinguishing the case of many & few trees

Rerouting Around Barriers (Boundaries & Water) Poor Style entire logic, conditions (checks on boundaries, whether water) & rerouting Logic should all be in separate functions from this & from each other). Remove constants A more complex condition (should really place condition in 1-2 functions that returns a boolean, and just call the functions! can reuse elsewhere)

New Direction Change Function Info

New Direction Change: Function Body Setting Agent Speed (set so as to reach target in fixed time until next target shift) Initiates movement towards (randomly chosen) destination

While / Do while loop Executes a statement as long as some condition is true The classic While loop has the test at the beginning The do while has the test at the end of the loop

While loops

Compound Statements (Delineated by { } ) Innermost is not actually needed, because Only one statement could remove { } and the statement inside would still be within The if consequent

Expression Statements Assignment expressions as an expression statements (including count++, which is equivalent to count=count+1 ) Method call Expression as an expression statement