Chapter 3 Assignment Statement

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.
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.
IntroductionIntroduction  Computer program: an ordered sequence of statements whose objective is to accomplish a task.  Programming: process of planning.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Variables and Constants
1 Identifiers  Identifiers are the words a programmer uses in a program  An identifier can be made up of letters, digits, the underscore character (
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Programming David Goldschmidt, Ph.D. Computer Science The College of Saint Rose Java Fundamentals (Comments, Variables, etc.)
The Java Programming Language
Lecture 2 Object Oriented Programming Basics of Java Language MBY.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
C Derived Languages C is the base upon which many build C++ conventions also influence others *SmallTalk is where most OOP comes Java and Javascript have.
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.
Variables, Data Types, & Constants. Topics & Objectives Declaring Variables Assignment Statement Reserve Words Data Types Constants Packages & Libraries.
Chapter 2: Java Fundamentals
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
SE-1010 Dr. Mark L. Hornick 1 Variables & Datatypes.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Copyright Curt Hill Variables What are they? Why do we need them?
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Chapter 2 Variables.
CSC 1051 – Algorithms and Data Structures I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
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.
© 2011 Pearson Education, publishing as Addison-Wesley Chapter 1: Computer Systems Presentation slides for Java Software Solutions for AP* Computer Science.
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.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 2 Basic Computation
JAVA MULTIPLE CHOICE QUESTION.
Working with Java.
Chapter 4 Assignment Statement
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2: Data Types, Variables, Operators, and Expressions
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2 Basic Computation
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to C++ Programming
Introduction to Java Programming
An overview of Java, Data types and variables
Chapter 2: Basic Elements of Java
Chapter 1: Computer Systems
Chapter 2 Variables.
JavaScript Reserved Words
CISC124 TA names and s have been added to the course web site.
Focus of the Course Object-Oriented Software Development
Primitive Types and Expressions
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Chapter 3 Assignment Statement 9/8/2018 12:43 AM Chapter 3 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 Assignment requires an equal sign (=). The value on the right is "given to" or "assigned" the value of the variable on the left. © 2012 EMC Publishing, LLC

Chapter 3 Variable Assignment 9/8/2018 12:43 AM Chapter 3 Variable Assignment A variable can store only one value at any time. int x; x = 5; x = 10; x 10 5 Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains three (3) animations. The variable declaration, int x, associates the variable name x with a memory location <press space bar> The first assignment statement, x = 5, associates the memory location for x with the value 5 <press space bar> The second assignment statement, x = 10, associates the memory location for x with the value 10 <press space bar> Note that once the variable x is associated with a new value, in this case 10, the previous value, 5, cannot longer be accessed. © 2012 EMC Publishing, LLC

Chapter 3 Primitive Data Types 9/8/2018 12:43 AM Chapter 3 Primitive Data Types Type Storage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit It is important to choose the appropriate data type when declaring variables so that the compiler allocates only the needed storage space for a variable. For example, declaring every variable a double will ensure that both ints and doubles can be represented, but unneeded storage space will be used. Additionally, the reader of the application will not be clear on what type of value a variable is expected to hold. © 2012 EMC Publishing, LLC

Chapter 3 Abstract Data Types 9/8/2018 12:43 AM Chapter 3 Abstract Data Types A variable declared with a class is called an object. For example, the object spot is type Circle: Circle spot = new Circle(4); spot getRadius() area() Note: This slide contains animations. Press the space bar or click the mouse button to display each animation. This slide contains one (1) animation. The instantiation of object spot creates a reference to the object's data and method. <press space bar> © 2012 EMC Publishing, LLC

Numerous packages are included with JDK Packages contain classes 9/8/2018 12:43 AM Chapter 3 Java Packages Numerous packages are included with JDK 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. Packages are reusable units of code that can be added to an application. An asterisk (*) can be used in place of a class name to import all the classes of a package. © 2012 EMC Publishing, LLC

Chapter 3 The Scanner Class 9/8/2018 12:43 AM Chapter 3 The Scanner Class Part of the java.util package A Scanner object processes text and numbers from the input stream Methods include: next() nextLine() nextInt() nextDouble() nextBoolean() close() The Scanner class is used to read input from the user. The Scanner method that should be used depends on the data expected from the user. © 2012 EMC Publishing, LLC

Chapter 3 Integer Division 9/8/2018 12:43 AM Chapter 3 Integer Division Integer division (/) is performed when both operands are integers. Only the integer portion of the quotient is returned: © 2012 EMC Publishing, LLC

9/8/2018 12:43 AM Chapter 3 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 © 2012 EMC Publishing, LLC

Chapter 3 Modulus Division 9/8/2018 12:43 AM Chapter 3 Modulus Division Modulus division (%) returns the remainder of a division operation: © 2012 EMC Publishing, LLC

Chapter 3 Operator Precedence 9/8/2018 12:43 AM Chapter 3 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: 5 + 6 * 4 / 2 = 17 © 2012 EMC Publishing, LLC

Chapter 3 Changing the Order of Operations 9/8/2018 12:43 AM Chapter 3 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 © 2012 EMC Publishing, LLC

9/8/2018 12:43 AM Chapter 3 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 © 2012 EMC Publishing, LLC

Chapter 3 Assignment Operators 9/8/2018 12:43 AM Chapter 3 Assignment Operators Operator Operation += addition and then assignment -= subtraction and then assignment *= multiplication and then assignment /= division and then assignment %= modulus division and then assignment © 2012 EMC Publishing, LLC

Chapter 3 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. © 2012 EMC Publishing, LLC

Chapter 3 Java Keywords abstract double int strictfp boolean else 9/8/2018 12:43 AM Chapter 3 Java Keywords abstract double int strictfp boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto protected transient const if public try continue implements return void default import short volatile do instanceof static While Keywords have special meaning to the Java compiler and therefore cannot be used for a variable or constant identifier. © 2012 EMC Publishing, LLC

Chapter 3 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. © 2012 EMC Publishing, LLC