Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

Types and Arithmetic Operators
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
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.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Bellevue University CIS 205: Introduction to Programming Using C++ Lecture 3: Primitive Data Types.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
JavaScript, Fourth Edition
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
ASP.NET Programming with C# and SQL Server First Edition
JavaScript, Third Edition
String Escape Sequences
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.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
Java Chapter 2 Creating Classes. Class Defines structure of an object or set of objects; Includes variables (data) and methods (actions) which determine.
Chapter 2 Getting Started in C Programming
A First Book of ANSI C Fourth Edition
Operaciones y Variables
Java Primitives The Smallest Building Blocks of the Language (corresponds with Chapter 2)
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 3: Data Types and Operators JavaScript - Introductory.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
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.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Lesson 3 – Primitive Data Types Objective: Students will investigate the definition and usage of objects and primitive data types in Java in order to practice.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 3 – Variables and Arithmetic Operations. Variable Rules u Must declare all variable names –List name and type u Keep length to 31 characters –Older.
Chapter 2 Variables.
Java Programming, Second Edition Chapter Two Using Data Within a Program.
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 14 JavaScript: Part II The Web Warrior Guide to Web Design Technologies.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1Object-Oriented Program Development Using C++ Built-in Data Types Data type –Range of values –Set of operations on those values Literal: refers to acceptable.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Chapter 2: Problem Solving Using C++
ITEC113 Algorithms and Programming Techniques
Multiple variables can be created in one declaration
C++ for Engineers and Scientists Second Edition
Chapter 2 Variables.
Chapter 2: Introduction to C++.
Data Types and Expressions
Presentation transcript:

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition

2 Objectives You should be able to describe: Data Values and Arithmetic Operations Constructing a Data Declaration Section: Variables Completing the Class: Methods Assignment Operations

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition3 Objectives (continued) Program Design and Development: Object Identification Common Programming Errors

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition4 Data Values and Arithmetic Operations General data types: –Primitive –Reference Literal –Value that explicitly identifies itself –Example: 1 “xyz”

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition5 Data Values and Arithmetic Operations (continued) Figure 2.1: Primitive data types

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition6 Data Values and Arithmetic Operations (continued) Figure 2.2: Reference types

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition7 Integer Literals Zero or any positive or negative numerical value without decimal point Examples: –1 –-406 – Other data types: –Byte –Short integer

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition8 Integer Literals (continued) Table 2.1: Integer Data Types

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition9 Floating-Point Values Also called real numbers Numerical value with decimal point Examples: –-1.24 –0.0 – Data types: –Double –Float

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition10 Floating-Point Values (continued) Table 2.2: Floating-Point Data Types

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition11 Precision, Exponential Notation, and Atomic Data Precision can either refer to: –Accuracy of number –Number of significant digits in number Exponential notation: –Express very large and very small values in compact form – in exponential notation is e4 Atomic data –Complete entity by itself

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition12 Character Values Letters of alphabet –Both uppercase and lowercase 10 digits 0 through 9 Special symbols such as 1 $., - ! Stored as 16-bit unsigned values using Unicode

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition13 Escape Sequences \ before characters Tells compiler to execute differently from normal behavior

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition14 Escape Sequences (continued) Table 2.4: Escape Sequences

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition15 Boolean Values Restricted to one of two values: –True –False

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition16 Arithmetic Operations Used for mathematical calculations Operators: –Addition: + –Subtraction: - –Multiplication: * –Division: / –Modulus: %

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition17 Arithmetic Operations (continued) Simple arithmetic expression Integer expression Real expression Mixed-mode expression Overloaded operator –Symbol represents more than one operation –Execution depends on types of operands encountered

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition18 Integer Division Division of two integers c an produce strange results for unwary Integers cannot contain fractional part –It is dropped Example: –15/7 = 2

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition19 The Modulus Operator Retain remainder of division Operator symbol: –% Example: –9.2 % 4 = 1.2 –15 % 4 = 3

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition20 Negation Unary operator –One that operates on single operand Same symbol as binary subtraction (-) Negates or reverses number

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition21 Negation (continued) Table 2.6: Summary of Arithmetic Operations

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition22 Operator Precedence and Associativity Rules for working with arithmetic operators: –Two binary arithmetic operator symbols must never be placed side by side –Parentheses may be used to form groupings Expressions in parentheses are evaluated first –Parentheses can be nested –Parentheses cannot be used to indicate multiplication

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition23 Operator Precedence and Associativity (continued) Order of operations: –All negations are done first –Multiplication, division, and modulus operations are computed first –Addition and subtraction are computed last

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition24 String Concatenation Joins two or more strings into single string Only operation that directly manipulates string data in similar fashion as numerical data Has same precedence as addition operator –Always use parentheses when performing arithmetic operations in any expression that contains a string

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition25 Constructing a Data Declaration Section: Variables Values in computer program are stored and retrieved from computer’s memory unit Variable: –Name given by programmer –Refers to computer storage locations that store primitive data type value Selection of variable names is left to programmer

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition26 Declaration Statements Naming variable and specifying data type Syntax: –optionalAccessSpecifier dataType variableName; Example: –private int sum; Initial value: –Declaration and initialization in one line of code –private int num1 = 15;

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition27 Constructing a Data Declaration Section Classifications of variables: –Instance –Class –Local –Parameter Dependent on: –Variable placement within class –Presence or absence of reserved word static

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition28 Constructing a Data Declaration Section (continued) Table 2.8: Determination of Variable Types

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition29 Creating Objects Objects –Only created from instance variables declared in data declaration section or from other object types Methods –Provide operations that can be applied to created objects or create general-purpose functions Independent of any one object

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition30 Creating Objects (continued) Figure 2.6: The RoomType class

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition31 Creating Objects (continued) Using dynamic memory allocation operator also called: –Creating an instance –Instantiating an object Reference variable –Reference location in memory where actual object’s values located

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition32 Creating Objects (continued) Private –Once object is created, its private variables can only be accessed by its class methods –Safety feature provided by object-oriented languages Cleansing Memory –Memory leak problem –Objects keep track of who references them –JVM cleans unused memory

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition33 Creating Objects (continued) Figure 2.8: Instantiating an object

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition34 Specifying Storage Allocation Declaring variables protects against typos –Compiler catches errors Each data type has its own storage requirements –Compiler pre-allocates memory based on data type Definition statements –Statements that cause variables to be created

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition35 Completing the Class: Methods Classes provide methods to: –Initialize values stored in each instance variable to user-selected values –Display values –Modify values Format of method header: –public returnType methodName(parameter list) Public –Means method can be used in other classes

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition36 Constructor Methods Method that has same name as class Automatically called each time object created Purpose: –Initialize new object’s instance variables Format: public ClassName(parameter list) { method body }

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition37 Accessor Methods Provide means for reading values stored in object’s variables Referred to as get() method

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition38 Mutator Methods Provide means for changing object’s values –After object created Also known as set() methods

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition39 Convenience Can be convenient to place two classes in one file –Second class’s main() method is placed within first class’s methods section –Both classes are stored in same file public reserved word removed from class that does not contain main() method

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition40 Assignment Operations Most basic statements for initializing variables General syntax: –variable = expression; Example: –length = 25; Expression –Any combination of constants and variables that can be evaluated to yield a result

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition41 Assignment Operations (continued) All variables used in expression must previously have been given valid values Must be a variable listed immediately to left of equal sign Has lower precedence than any other arithmetic operator

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition42 Multiple Declarations Variables with same data type can be grouped –Declared using single declaration statement Frequently used in declaring method’s internal variables Coercion –Forced conversion based on data types

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition43 Assignment Variations In an assignment expression: –Variable to left of equal sign can also be used to right of equal sign Shortcut assignment operators: –+= –-= –*= –/= –%=

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition44 Accumulating Values added to program variable as program progresses Uses assignment operator: –sum = sum + 10,

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition45 Counting Counting statement form: –variable = variable + fixedNumber; Example: –i = i + 1; Increment/decrement operators: ++-- –Example : i++

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition46 Program Design and Development: Object Identification Model –Representation of problem Attributes –Define properties of interest Behaviors –Define how object reacts to its environment

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition47 Program Design and Development: Object Identification (continued) Figure 2.22: An initial object description

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition48 From Objects to Classes Class –Category of objects defined by given set of attributes and behavior State –Describes how object appears at moment –Defined by values that have been assigned to its attributes Identity –Name that uniquely identifies object

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition49 Procedural versus Object Orientations Procedure-oriented programming –Emphasis on operations to be performed Object-oriented programming –Emphasis on attributes and behavior of objects

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition50 Common Programming Errors Forgetting to declare all variables used in class Forgetting to assign or initialize values for all variables before variables are used in expression Mixing data types in same expression without clearly understanding effect produced Defining more than one default constructor for class

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition51 Summary Primitive data types: –Numerical –Boolean Every variable must be declared as to type of value it can store Class –Programmer-defined data type

Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition52 Summary (continued) Classes contain methods: –Constructor –Accessor –Mutator Expressions evaluated according to precedence and associativity of operators used Assignment symbol operator: –=