OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
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.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Program Elements We can now examine the core elements of programming (as implemented in Java) We focuse on: data types variable declaration and use, constants.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Expressions, Data Conversion, and Input
 Value, Variable and Data Type  Type Conversion  Arithmetic Expression Evaluation  Scope of variable.
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
Georgia Institute of Technology Introduction to Java, and DrJava Barb Ericson Georgia Institute of Technology Aug 2005.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Chapter 2: Using Data.
BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA TYPES AND OPERATIONS.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
COMP Primitive and Class Types Yi Hong May 14, 2015.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CompSci Primitive Types  Primitive Types (base types)  Built-in data types; native to most hardware  Note: not objects (will use mostly first.
1 Chapter 2: Java Fundamentals cont’d Spring Lory Al Moakar.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Doing math In java.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CSCI 1100/1202 January 18, Arithmetic Expressions An expression is a combination of operators and operands Arithmetic expressions compute numeric.
Chapter 2: Data and Expressions. Variable Declaration In Java when you declare a variable, you must also declare the type of information it will hold.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
Lecture 3: More Java Basics Michael Hsu CSULA. Recall From Lecture Two  Write a basic program in Java  The process of writing, compiling, and running.
What will each of the following lines print? System.out.println("number" ); number645 System.out.println("number" + (6 + 4)+ 5); number105 System.out.println(6.
Chapter 2 Variables.
Chapter 4 Assignment Statement
Object Oriented Programming
Data Conversion & Scanner Class
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Chapter 3 Assignment Statement
Type Conversion, Constants, and the String Object
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Introduction to Java, and DrJava
Data Types and Expressions
Primitive Types and Expressions
Introduction to Java, and DrJava part 1
Chapter 2 Variables.
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior involves: –Sequences of actions. –Choices between alternative actions. –Repetition of one or more actions. Statements inside method bodies.

OOP with Java, David J. Barnes Adding Sequential Behavior2 Putting Things in Order Tasks commonly involve performing a sequence of steps: –First do this. –Then do that. –Finally do the other. Controlling a Ship: –move, setCourse, move, report.

OOP with Java, David J. Barnes Adding Sequential Behavior3 Order and Effect Some orders make no sense. –A ship cannot be moved before it has been constructed. Different orders may have different effects: –Sell your old car. Pay the money into a bank. Write a check to buy a new car. –Write a check to buy a new car. Sell your old car. Pay the money into a bank.

OOP with Java, David J. Barnes Adding Sequential Behavior4 Order and State Order often matters when actions are dependent upon the state of something. –Deposits and withdrawals affect the state of a bank account. –Burning fossil fuel affects the environment in which individuals live.

OOP with Java, David J. Barnes Adding Sequential Behavior5 Explicit and Implicit State Change Mutators names are chosen to show that they have explicit effects. –e.g., setField. Other methods have implicit effects. –move implicitly affects a ship’s position.

OOP with Java, David J. Barnes Adding Sequential Behavior6 Arithmetic Expressions Expressions involving numerical data types: –byte, ( char ), int, long, short. –double, float. Operators for addition, subtraction, negation, multiplication and division. Different results for integer and floating point types.

OOP with Java, David J. Barnes Adding Sequential Behavior7 The Addition Operator class PennyBankAccount {... // Credit the account with the given amount. public void credit(long amount){ // Find out current balance. long currentBalance = getBalance(); // Add in the amount to be credited. currentBalance = currentBalance+amount; // Set the attribute from the new value. setBalance(currentBalance); }... }

OOP with Java, David J. Barnes Adding Sequential Behavior8 Multiplication and Division Three operators are: * / % Division between integers results in truncation: –5/3 gives a result of 1 Division between floating point numbers: –5.0/3.0 gives a result of Modulus (%) gives remainder after division. –11%3 gives a result of 2

OOP with Java, David J. Barnes Adding Sequential Behavior9 Order of Precedence Rules governing order of evaluation in a multi-operator expression. –Parentheses take precedence over operators. –Multiplication and division. –Addition and subtraction. –Left to right for equal precedence operators.

OOP with Java, David J. Barnes Adding Sequential Behavior10 String Concatenation The addition operator has a further use, when one of its operands is a String. –Create a new String that is the concatenation of its operands: "big"+"apple" creates "bigapple". A non-String operand is converted to a String. –"version "+10 creates "version 10".

OOP with Java, David J. Barnes Adding Sequential Behavior11 The Char Type In Java, a char occupies 16 bits. Java based on the Unicode character set. Character literals enclosed between single quote characters: 'A', '9', '!', etc. Char values may be stored in integer variables, but not vice-versa.

OOP with Java, David J. Barnes Adding Sequential Behavior12 Type Conversion Implicit Type Conversion –Integer to floating point type. –Less-precise value converted to more-precise type. Primitive Type Checking –A more-precise value may not be used where a less-precise type is required: E.g., cannot store a double value into an int variable.

OOP with Java, David J. Barnes Adding Sequential Behavior13 Primitive Type Casting The normal type checking rules may be subverted by using a cast: –A type name written between parentheses: int x = (int) ; Casts usually involve loss of information, so may be risky.

OOP with Java, David J. Barnes Adding Sequential Behavior14 Combined Assignment Operators Operation and assignment are common: –balance = balance+amount; Combined assignment operators exist for all the binary arithmetic operators: –balance += amount; –mark *= scalingFactor;

OOP with Java, David J. Barnes Adding Sequential Behavior15 Reading Input Using SimpleInput SimpleInput : a class to simplify input. –Reading Numerical Values: nextInt, nextDouble. –Reading Strings: nextLine, nextWord. –Reading Boolean Values: nextBoolean. –Reading Data from a File.

OOP with Java, David J. Barnes Adding Sequential Behavior16 Review Object behavior often involves sequences of steps. Standard arithmetic operations are available. Order of precedence must be considered. Type compatibility rules must be obeyed. A cast permits normal type rules to be broken.