Chapter 4 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 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?
JAVA Tokens. Introduction A token is an individual element in a program. More than one token can appear in a single line separated by white spaces.
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 มหาวิทยาลัยเนชั่น.
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.
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:
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:
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.
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.
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
Working with Java.
Elementary Programming
BASIC ELEMENTS OF A COMPUTER PROGRAM
Lecture 2: Data Types, Variables, Operators, and Expressions
CS180 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Multiple variables can be created in one declaration
Variables and Arithmetic Operators in JavaScript
University of Central Florida COP 3330 Object Oriented Programming
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Starting JavaProgramming
null, true, and false are also reserved.
Introduction to C++ Programming
Variables and Arithmetic Operators in JavaScript
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
Chap 2. Identifiers, Keywords, and Types
Chapter 2 Variables.
Agenda Types and identifiers Practice Assignment Keywords in Java
Presentation transcript:

Chapter 4 Assignment Statement 5/19/2018 12:31 AM 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 Refer to page 78 in the text. Assignment requires an equal sign (=). The value on the right is "given to" or "assigned" the value of the variable on the left. © 2007 Lawrenceville Press

Chapter 4 Variable Assignment 5/19/2018 12:31 AM Chapter 4 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. Refer to page 78 in the text. 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. © 2007 Lawrenceville Press

Chapter 4 Primitive Data Types 5/19/2018 12:31 AM Chapter 4 Primitive Data Types Type Storage Required int 4 bytes double 8 bytes char 2 bytes boolean 1 bit Refer to page 79 in the text. 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. © 2007 Lawrenceville Press

Chapter 4 Abstract Data Types 5/19/2018 12:31 AM Chapter 4 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. Refer to page 80 in the text. The instantiation of object spot creates a reference to the object's data and method. <press space bar> © 2007 Lawrenceville Press

Numerous packages are included with JDK Packages contain classes 5/19/2018 12:31 AM Chapter 4 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. Refer to pages 80 and 81 in the text. 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. © 2007 Lawrenceville Press

Chapter 4 The Scanner Class 5/19/2018 12:31 AM Chapter 4 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() Refer to page 81 in the text. The Scanner class is new to Java 5. It is used to read input from the user. The Scanner method that should be used depends on the data expected from the user. © 2007 Lawrenceville Press

Chapter 4 Integer Division 5/19/2018 12:31 AM Chapter 4 Integer Division Integer division (/) is performed when both operands are integers. Only the integer portion of the quotient is returned: Refer to page 83 in the text. © 2007 Lawrenceville Press

5/19/2018 12:31 AM 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 Refer to page 83 in the text. © 2007 Lawrenceville Press

Chapter 4 Modulus Division 5/19/2018 12:31 AM Chapter 4 Modulus Division Modulus division (%) returns the remainder of a division operation: Refer to page 83 in the text. © 2007 Lawrenceville Press

Chapter 4 Operator Precedence 5/19/2018 12:31 AM 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: 5 + 6 * 4 / 2 = 17 Refer to page 84 in the text. © 2007 Lawrenceville Press

Chapter 4 Changing the Order of Operations 5/19/2018 12:31 AM 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 Refer to page 84 in the text. © 2007 Lawrenceville Press

5/19/2018 12:31 AM 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 Refer to pages 84 and 85 in the text. © 2007 Lawrenceville Press

Chapter 4 Assignment Operators 5/19/2018 12:31 AM Chapter 4 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 Refer to page 86 in the text. © 2007 Lawrenceville Press

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. Refer to page 87 in the text. © 2007 Lawrenceville Press

Chapter 4 Java Keywords abstract double int strictfp boolean else 5/19/2018 12:31 AM Chapter 4 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 Refer to page 88 in the text. Keywords have special meaning to the Java compiler and therefore cannot be used for a variable or constant identifier. © 2007 Lawrenceville Press

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. Refer to page 87 in the text. © 2007 Lawrenceville Press

Chapter 4 Flowchart Symbols 5/19/2018 12:31 AM Chapter 4 Flowchart Symbols process Refer to page 91 in the text. The rectangle flowchart symbol indicates a process. © 2007 Lawrenceville Press

Chapter 4 The BirthdayGame Flowchart Refer to page 91 in the text. The BirthdayGame flowchart illustrates the application solution. Solution steps include: 1. displaying the directions for the player to calculate the number 2. prompting the player for the calculated number 3. subtracting 165 from the number 4. using integer division to divide the number by 100 and then storing the quotient as the birth month 5. using modulus division to divide the number by 100 and then storing the remainder as the birth day 6. displaying a message containing the player's birthday © 2007 Lawrenceville Press