Mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation.

Slides:



Advertisements
Similar presentations
AP Computer Science Anthony Keen. Computer 101 What happens when you turn a computer on? –BIOS tries to start a system loader –A system loader tries to.
Advertisements

Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 219 Bell Hall Office hours: M-F 11:00-11:
1 Powers of Two: Trace Ex. Print powers of 2 that are  2 N. Increment i from 0 to N. Double v each time. int i = 0; int v = 1; while (i
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
General Computer Science for Engineers CISC 106 Lecture 19 Dr. John Cavazos Computer and Information Sciences 04/06/2009.
CS 106 Introduction to Computer Science I 01 / 30 / 2008 Instructor: Michael Eckmann.
Week 10 Recap CSE 115 Spring For-each loop When we have a collection and want to do something to all elements of that collection we use the for-each.
Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring.
Primitives in Java Java has eight primitive types –boolean –integral types: signed: long, int, short, byte unsigned: char –floating point types: double,
Java Syntax Primitive data types Operators Control statements.
Loops – While, Do, For Repetition Statements Introduction to Arrays
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
2015/8/221 Data Types & Operators Lecture from (Chapter 3,4)
Expressions creating information. topics  operators: precedence, associativity, parentheses, overloading  operands: side-effects, coercion  arithmetic,
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
03 August 2004 NLP-AI Java Lecture No. 4 Operators & Decision Constructs Satish Dethe.
Primitive variables Android Club types of variables: Primitive single value (starts with uppercase letter) Complex multiple value (starts with.
Week 8: Decisions Bryan Burlingame 21 October 2015.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
“Great leaders are never satisfied with current levels of performance. They are restlessly driven by possibilities and potential achievements.” – Donna.
CSE115: Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall
A: A: double “4” A: “34” 4.
1 CSC 221: Computer Programming I Fall 2005 simple conditionals and expressions  if statements, if-else  increment/decrement, arithmetic assignments.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
Array: Sequence of values of the same type Construct array: new double[10] Store in variable of type double[] : double[] data = new double[10]; When array.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
© 2004 Pearson Addison-Wesley. All rights reserved October 5, 2007 Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
IST 210: PHP Logic IST 210: Organization of Data IST2101.
APS105 Deciding 1. Comparing 2 Relational Expressions Relational expression: –Compare two values (or values of variables) Examples: x > 5 income < expenses.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Relational Operator and Operations
Chapter 7 – Arrays and Array Lists
Chapter 7: Expressions and Assignment Statements
Operators Operators are symbols such as + (addition), - (subtraction), and * (multiplication). Operators do something with values. $foo = 25; $foo – 15;
Agenda Warmup Finish 2.4 Assignments
Chapter 7: Expressions and Assignment Statements
Chapter 6 More Conditionals and Loops
Lecture 5 from (Chapter 4, pages 73 to 96)
Review Operation Bingo
OPERATORS (2) CSC 111.
Outline Altering flow of control Boolean expressions
Control Structures: for & while Loops
Conditional Logic Presentation Name Course Name
Arrays October 6, 2006 ComS 207: Programming I (in Java)
Seating “chart” Front Back 4 rows 5 rows 5 rows 4 rows 2 rows 2 rows
Java: Variables, Input and Arrays
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Primitive Data Types and Operators
Presentation transcript:

mixing types in expressions Operators such as +, -, * and / are overloaded: the same name has many different values + overloaded as String concatenation too! “Good” + “ ” + “morning!”  “Good morning!” What happens in an expression which mixes values of different types? = ???? 5 is coerced to its equivalent double value, 5.0: = 7.5 Type coercion happens only from “smaller” type to “larger” type (e.g. int  double, not double  int)

relational operators We can form expressions like: x < y which have a value of true or false (i.e. the type of this expression is boolean) relational operators: >= ==

Equality testing Equality testing: –of primitive values: == –of objects: equals method Consider: Foo a = new Foo();Foo c = a; Foo b = new Foo(); what is value ofwhat is value of (a==b)(a==c)

assignment = ; evaluate, assign value to Example: int x = 5; int y = x + 3; // what value is assigned to y?

assignment Example: int x = 5; x = x + 3; // what value is assigned to x? Notice that “=” is not “equality”, but “assignment”.

Some other operators ++ increments the variable it is applied to Example int x = 5; x++;// value of x is now 6 (Caution: the value of the expression x++ is perhaps not what you would expect. The value of x++ is the value of x prior to the increment of x.)

Control structure overview if ( ) true false

Control structure overview if ( ) else true false

Control structure overview while ( ) true false

Working with multiple objects A variable can refer to a single object: IBehavior cc = new ColorChanging(); IBehavior br = new Breathing(); A variable referring to many objects? No, but we saw how to build a composite object. IBehavior cc = new CompositeBehavior(cc,br); But it’s not too easy to add/remove items from a composite. Are there options?

Collections interface: java.util.Collection classes: –java.util.HashSet –java.util.ArrayList E is the type of element contained in the collection

Use of a collection HashSet names = new HashSet (); names.add(“Amy”);names.remove(“Bob”); names.add(“Bob”); names.add(“Cindy”); names.add(“Dave”); names.add(“Emma”); …

for-each loop for (String name : names) { System.out.println(name); } This would print out: Amy Cindy Dave Emma

Collection.shuffle Collection.shuffle(List )