Lecture Set 4: More About Methods and More About Operators

Similar documents
Lecture Set 4: More About Methods and More About Operators

Programming with Java

Prof. Navrati Saxena TA: Rochak Sachan

Operators and Expressions

Basics of Java Programming

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Program Fundamentals

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Java Primer 1: Types, Classes and Operators

Object-Oriented Programming

Chapter 7. Expressions and Assignment Statements ISBN

CONTENTS: Compilation Data and Expressions COMP 202. More on Chapter 2

Lecture 3 Operators MIT AITI

CS111: PROGRAMMING LANGUAGE II

Expressions & Assignment Statements

Two Types of Types. Primitive Types in Java. Using Primitive Variables. Class #07: Java Primitives. Integer types.

Ex: If you use a program to record sales, you will want to remember data:

AYBUKE BUYUKCAYLI KORAY OZUYAR MUSTAFA SOYLU. Week 21/02/ /02/2007 Lecture Notes: ASCII

JAVA OPERATORS GENERAL

Operators in java Operator operands.

Java enum, casts, and others (Select portions of Chapters 4 & 5)

ECE 122 Engineering Problem Solving with Java

Lecture 2: Variables and Operators. AITI Nigeria Summer 2012 University of Lagos.

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

COMP 110 Introduction to Programming. What did we discuss?

CS313D: ADVANCED PROGRAMMING LANGUAGE

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Chapter 7. Expressions and Assignment Statements (updated edition 11) ISBN

Chapter 7. Expressions and Assignment Statements

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Operators. Lecture 3 COP 3014 Spring January 16, 2018

The Order of Operations. Unit 1 Lesson 3

Lesson 5: Introduction to the Java Basics: Java Arithmetic THEORY. Arithmetic Operators

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

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

More Programming Constructs -- Introduction

LECTURE 3 C++ Basics Part 2

Data and Variables. Data Types Expressions. String Concatenation Variables Declaration Assignment Shorthand operators. Operators Precedence

Chapter 3: Operators, Expressions and Type Conversion

9/21/17. Outline. Expression Evaluation and Control Flow. Arithmetic Expressions. Operators. Operators. Notation & Placement

CSC 1214: Object-Oriented Programming

1.00 Lecture 4. Promotion

Unit 2: Java in the small

Accelerating Information Technology Innovation

Object-Oriented Programming. Topic 2: Fundamental Programming Structures in Java

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

Lesson 3: Basic Programming Concepts

CIS133J. Working with Numbers in Java

INDEX. A SIMPLE JAVA PROGRAM Class Declaration The Main Line. The Line Contains Three Keywords The Output Line

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

The Arithmetic Operators

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Expressions and Assignment Statements

Full file at

COMP-202: Foundations of Programming. Lecture 3: Boolean, Mathematical Expressions, and Flow Control Sandeep Manjanna, Summer 2015

PROGRAMMING FUNDAMENTALS

Java provides a rich set of operators to manipulate variables. We can divide all the Java operators into the following groups:

Chapter 2: Using Data

Data Conversion & Scanner Class

CMPT 125: Lecture 3 Data and Expressions

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

CSCI 2010 Principles of Computer Science. Data and Expressions 08/09/2013 CSCI

Operators and Expressions:

3. Java - Language Constructs I

4 Programming Fundamentals. Introduction to Programming 1 1

Building Java Programs

Important Java terminology

Software II: Principles of Programming Languages. Why Expressions?

Operators. Java operators are classified into three categories:

COMP-202 Unit 2: Java Basics. CONTENTS: Using Expressions and Variables Types Strings Methods

Introduction to Programming Using Java (98-388)

CS 112 Introduction to Programming

Admin. CS 112 Introduction to Programming. Recap: Java Static Methods. Recap: Decomposition Example. Recap: Static Method Example

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Values and Variables 1 / 30

Example: Tax year 2000

Chapter 2 Elementary Programming

Chapter 2. Elementary Programming

1.00 Introduction to Computers and Engineering Problem Solving. Quiz 1 March 7, 2003


Type Conversion. and. Statements

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

Informatics Ingeniería en Electrónica y Automática Industrial

ESc101 : Fundamental of Computing

DATA TYPES AND EXPRESSIONS

Exercises Software Development I. 05 Conversions and Promotions; Lifetime, Scope, Shadowing. November 5th, 2014

Declaration and Memory

Expressions and Casting

Java for Python Programmers. Comparison of Python and Java Constructs Reading: L&C, App B

Zheng-Liang Lu Java Programming 45 / 79

Chapter 2 Primitive Data Types and Operations. Objectives

Chapter 2: Data and Expressions

Lecture 2. COMP1406/1006 (the Java course) Fall M. Jason Hinek Carleton University

Expression Evaluation and Control Flow. Outline

Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Visual C# Instructor s Manual Table of Contents

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

CS2141 Software Development using C/C++ C++ Basics

Transcription:

Lecture Set 4: More About Methods and More About Operators Methods Definitions Invocations More arithmetic operators Operator Side effects Operator Precedence Short-circuiting main method public static void main(string args[]){ // statements here All projects and examples have defined this method No explicit call needed Parts of the line Name = main Parameter List = String args[] Return type = void Access = public -- more on this later Modifier = static Other public static methods A static method is associated with a class not an individual instance (object) Must have all of the same parts as the main public static returntype name(arglist){ body For example defining a method to print a number of stars public static void printstars(int count){ for (int curr = 0; curr < count; curr++){ System.out.print( * ); For example defining a method to print a number of stars printstars(3) System.out.println(); printstars(77); 1

method information: parameters and arguments parameter list type name for each item in the list e.g. (MyGrid grid, char where) argument list expression for each item in the list e.g. (grid, t ) Matched between the arguments and the parameters based on position in the list 3 Non-main static public methods: defining, invoking and commenting Defined based on a name and a list of parameters public static void name(parameterlist){ body Invoked by stating its name and giving an argument for each element of the parameter list name(argumentlist); Each method must have a well defined purpose That information goes into a comment before the method definition Each parameter s purpose should be explained Return value s purpose should be explained 4 Expressions Java expressions that yield values e.g. x x + 1 - y x == y && z == 0 foo.equals ( cat ) Expressions have values of a specific type (int, boolean, etc.) Expressions can be assigned to variables, appear inside other expressions, etc. 5 2

Expressions and Side Effects Some expressions can also alter the values of variables e.g. x=1 x=1 is an expression? Yes! Value is result of evaluation right-hand side of = It also alters the value of x Such alterations are called side effects 6 Are the Following Legal? int x, y; x = y = 1; Yes. Result assigns 1 to x and to y int x = 0, y = 1; boolean b = false; if (b = (x <= y)){ x = y; Yes. Result assigns true to b and 1 to x 7 Other Expressions with Side Effects Java includes abbreviations for common forms of assignment Example: increment operations (Basically equivalent to x = x + 1 ++x Pre-increment Increments x, returns the new value of x x++ Post-increment Increments x, returns the old value of x Same or Different x == x++ x == ++x Compare x++ * y++ ++x * ++y ++x * y++ x++ * ++y always true never true 8 3

Other Assignment Operators Example: decrement operations (Basically equivalent to x = x - 1 --x Pre-decrement Decrements x, returns the new value of x x-- Post-decrement Decrements x, returns the old value of x General modification by constant General form: <var> <op with=> <constant> Examples x += 2 equivalent to x = x+2 x -= 2 equivalent to x = x-2 x *= 2 equivalent to x = x*2 x /= 2 equivalent to x = x/2 9 Precedence Explains how to evaluate expressions What is value of 1 2 + 3 * 4? Precedence rules answer this question Higher-precedence operators evaluated first Example from math: Please, Excuse my Dear Aunt Sally or PEMDAS Multiple and divide (higher precedence) before you add and subtract (lower precedence) Java follows Aunt Sally s Rules but what about other operators? 10 Java Precedence Rules parentheses: ( ) unary ops: +x -x ++x -x x++ x--!x multiply/divide: * / % add/subtract: + - comparisons: < > <= >= equality: ==!= logical and: && logical or: assignments: = += *= /= %= (only these are right to left associative) increasing precedence 11 4

Examples x * y + -z Equivalent to (x*y) + (-z) (x <= y && y <= z w > z) Equivalent to ((x <= y) && (y <= z)) (w > z) What is value of 1 2 + 3 * 4? 1-2 + 3 * 4 = (1-2) + (3*4) = (1-2) + 12 = -1 + 12 = 11 12 Should You Rely on Precedence? No! The only ones people can remember are Please Excuse My Dear Aunt Sally PEMDAS Bad if (2 * x++ < 5 * z + 3 && -w!= x / 2) Better if (2 * (x++) < ((5 * z) + 3)) && ((-w)!= (x / 2)) 13 Short-circuiting Example As soon as Java knows an answer it quits evaluating the expression. int x = 0, y = 1; if ((y > 1) && (++x == 0)){ --y; System.out.println (x); 0 Why? y > 1 is false The result of && will be false, regardless of second expression Java therefore does not evaluate second expression of && This treatment of &&, is called short-circuiting Subexpressions evaluated from left to right Evaluation stops when value of over-all expression is determined 14 5

Examples int x = 0, y = 1; if ((y >= 1) && (++x == 0)) { --y; System.out.println (x); 1 int x = 0, y = 1; if ( ((y > 1) && (++x == 0)) ((y == 1) && (x++ == 0)) ) { --y; System.out.println (y); System.out.println (x); 0 1 15 Examples (cont.) int x = 0, y = 0; while (x++ <= 4){ y += x; System.out.println (y); 15 16 Programming with Side-Effects Generally: Side effects in conditions are hard to understand Good programming practice Conditions should be side-effect-free Side effects should be in stand-alone statements Major Goal: Strive to create the most readable and maintainable code. 17 6

Primitive Types and their Hierarchy double float long int short byte int x = 7.2; double y = 6; Changing to something else Further Up this list is acceptable called Widening Conversion Changing to Something else Further Down this list is not acceptable called Narrowing Conversion Explicit casting needed for when you want to go lower in the list 18 Type Casting Which of the following are legal? int x = 3.5; Illegal: 3.5 is not an int float x = 3; Legal: 3 is an int, which is also a float long i = 3; Legal: 3 is an int, which is also a long byte x = 155; Illlegal: 155 is to big to be a byte (> 127) double d = 3.14159F; Legal: 3.14159F is a float, which is also a double 19 Mixed Expressions What is result of float x = 3 / 4; x assigned value 0.0F Why? 3, 4 are ints So integer / operation is used, yielding 0, before upcasting is performed To get floating point result, use explicit casting float x = (float) 3 / (float) 4; Assigns x the value 0.75F Can also do following float x = (float) 3 / 4; Why? (float) 3 returns a value type float (3.0F) 4 is an int In this case, Java compiler uses widening conversion on lower type (here, int) to obtain values in same type before computing operation 20 7