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.

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

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types and Arithmetic Operators
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.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
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.
Expressions, Data Conversion, and Input
Variables and Constants
 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)
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2: Using Data.
Variables, Data Types, & Constants. Topics & Objectives Declaring Variables Assignment Statement Reserve Words Data Types Constants Packages & Libraries.
OOP with Java, David J. Barnes Adding Sequential Behavior1 Adding Behavior Previous class definitions have passively maintained attributes. Active behavior.
CHAPTER 4: CONTROL STRUCTURES - SEQUENCING 10/14/2014 PROBLEM SOLVING & ALGORITHM (DCT 1123)
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Java Programming: From Problem Analysis to Program Design, 5e Chapter 2 Basic Elements of Java.
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.
Java Concepts Chapter 2 - Using Objects Mr. Smith AP Computer Science A.
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.
Primitive Variables.
Data Types Declarations Expressions Data storage C++ Basics.
Chapter 2 Variables.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
COMP Primitive and Class Types Yi Hong May 14, 2015.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
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.
© 2007 Lawrenceville Press Slide 1 Chapter 4 Review Assignment Statement An assignment statement gives a value to a variable. Assignment can take several.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
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.
Inside Class Methods Chapter 4. 4 What are variables? Variables store values within methods and may change value as the method processes data.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 2 Basic Computation
Chapter 4 Assignment Statement
Chapter 3 Syntax, Errors, and Debugging
Elementary Programming
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Unit-1 Introduction to Java
Chapter 2 Variables.
Expressions and Assignment
Primitive Types and Expressions
Chapter 2 Variables.
Presentation transcript:

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 assigned to x x = y + 2; the value of an expression (y + 2) is assigned to x x = z; the value of another variable ( z ) is assigned to x

Slide 2 Chapter 4 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 5 10

Slide 3 Chapter 4 Primitive Data Types TypeStorage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit

Slide 4 Classic Asteroids Game from Atari

Slide 5 A variable declared with a class is called an object. For example, the object a47 is type Asteroid: Asteroid a47 = new Asteroid();a47 a47.setLocation(x,y) a47.destroy()

Slide 6 Chapter 4 Java Packages  Numerous packages are included with JRE (Java Runtime Environment)  Packages contain classes  Packages can be added to an application with an import statement. For example, the statement import java.util.Scanner; makes the Scanner class and its methods accessible to the application.

Slide 7 Chapter 4 The Scanner Class  Part of the java.util package import java.util.*;  A Scanner object processes text and numbers from the input stream  Methods include: next() nextLine() nextInt() nextDouble() nextBoolean() close()

Slide 8 Chapter 4 Integer Division Integer division ( / ) is performed when both operands are integers. Only the integer portion of the quotient is returned: int answer; answer = 20 / 7; System.out.print(answer); Output 2 Source Code

Slide 9 Chapter 4 Real Division Real division ( / ) is performed when one or both operands are type double. The entire quotient, including the decimal portion is returned: double result; result = 20.0/7.0;//result is 2.857

Slide 10 Chapter 4 Modulus Division Modulus division ( % ) returns the remainder of a division operation: int answer; answer = 20 % 7; System.out.print(answer); Output 6 Source Code

Slide 11 Chapter 4 Operator Precedence Operators in Java have the following precedence: 1. multiplication and division 2. addition and subtraction Operators of the same precedence are evaluated in order from left to right. For example, multiplication is performed first, then division, and finally addition: * 4 / 2 = 17

Slide 12 Chapter 4 Changing the Order of Operations The order in which operators are evaluated can be changed by using parentheses. For example, addition is performed first, then multiplication, and finally division: (5 + 6) * 4 / 2 = 22

Slide 13 Chapter 4 Type Casting Assigning an integer value into a floating-point variable works just fine. For example: double bankAccountBalance = 1000; However assigning a floating-point value into an integer variable is like trying to fit a large object in a small box. By default, Java treats it as an illegal operation. For example, this causes an error: int temperature = 22.8;

Slide 14 Chapter 4 Type Casting Type Casting converts a number of one type to a number of a different, but compatible type. Type casting is used to: 1.make the operand types in an expression match. For example, wholeNum = (int)y * 2 2.truncate the decimal portion of a double. For example, wholeNum = (int)z 3.change the way in which a division ( / ) operation will be performed. For example, realDivision = (double)a / (double)b

Slide 15 Chapter 4 Assignment Operators OperatorOperation += addition and then assignment -= subtraction and then assignment *= multiplication and then assignment /= division and then assignment %= modulus division and then assignment

Slide 16 Chapter 4 Named Constants  A named memory location that cannot be changed from its initial value.  The keyword final is used in a constant declaration.  Constant identifiers are typically all uppercase with an underscore ( _ ) separating words within the identifier name.

Slide 17 Chapter 4 Java Keywords abstractdoubleintstrictfp booleanelseinterfacesuper breakextendslongswitch bytefinalnativesynchronized casefinallynewthis catchfloatpackagethrow charforprivatethrows classgotoprotectedtransient constifpublictry continueimplementsreturnvoid defaultimportshortvolatile doinstanceofstaticwhile

Slide 18 Chapter 4 Programming Errors  Syntax errors violate the rules of Java.  Logic errors, also called semantic errors, occur in statements that are syntactically correct, but produce undesired or unexpected results.  Run-time errors, also called exceptions, halt program execution at the statement that cannot be executed. One type of exception is called InputMismatchException.

Slide 19 Chapter 4 Flowchart Symbols process

Slide 20 Chapter 4 The BirthdayGame Flowchart