Expressions and Assignment Statements

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
CM Programming with Java Chapter 1 Getting Started.
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Chapter 1 Getting Started Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CS102--Object Oriented Programming
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Lecture 4 Types & Expressions COMP1681 / SE15 Introduction to Programming.
Chapter 1 Getting Started Copyright © 2008 Pearson Addison-Wesley. All rights reserved.
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.
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started 1.2 Expressions and Assignment Statement.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Copyright © 2003 Pearson Education, Inc. Slide 2-1 Problem Solving with Java™ Second Edition Elliot Koffman and Ursula Wolz Copyright © 2003 Pearson Education,
Primitive Types, Strings, and Console I/O Chapter 2.1 Variables and Values Declaration of Variables Primitive Types Assignment Statement Arithmetic Operators.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
1 Do you have a CS account? Primitive types –“ building blocks ” for more complicated types Java is strongly typed –All variables in a Java program must.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Comp 248 Introduction to Programming Chapter 1 - Getting Started Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia University,
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Slides prepared by Rose Williams, Binghamton University Chapter 1 Getting Started.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
COMP Primitive and Class Types Yi Hong May 14, 2015.
Chapter 1 Getting Started Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
CSM-Java Programming-I Spring,2005 Fundamental Data Types Lesson - 2.
1 Week 5 l Primitive Data types l Assignment l Expressions l Documentation & Style Primitive Types, Assignments, and Expressions.
Chapter 1 Getting Started Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
CMSC 202 Java Primer 1. July 24, 2007 Copyright © 2008 Pearson Addison-Wesley 2 A Sample Java Application.
LESSON 5 – Assignment Statements JAVA PROGRAMMING.
Java-02 Basic Concepts Review concepts and examine how java handles them.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
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.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 1 Getting Started
Lecture 3 Java Operators.
Chapter 1 Getting Started
Building Java Programs
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Data types and variables
Java Programming: From Problem Analysis to Program Design, 4e
Program Style Console Input and Output
OPERATORS (2) CSC 111.
CSS 161 Fundamentals of Computing Introduction to Computers & Java
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Building Java Programs
Building Java Programs Chapter 2
Expressions and Assignment
elementary programming
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Chapter 1 Getting Started
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Primitive Types and Expressions
Building Java Programs Chapter 2
CSS161: Fundamentals of Computing
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

Expressions and Assignment Statements This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Identifiers The name of a variable or other item (class, method, object, etc.) Must be a~z, A~Z, 0~9, and _. Must not start with 0~9. Can be of any length. Is case-sensitive: Rate, rate, and RATE are the names of three different variables. Excludes keywords and reserved words. public class void static Excludes system-predefined class names System String CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Naming Conventions The names of variables, methods, and objects Start with a lowercase letter topSpeed bankRate1 timeOfArrival The names of classes Start with an uppercase letter FirstProgram MyClass String Word boundaries Delimited with an uppercase letter CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Practice X 34 count main firstData %growth data-1 numberOfBeans ABC123z7 first.data while PROG.CLASS variable? x_1 data2 variable! variable for x1 5A mainProgram do doIt int Integer integer String string Choose bad identifiers Choose recommended variable names CSS161: Fundamentals of Computing

Variable Declarations Every variable must be declared before it is used. Tell the compiler what kind of data you will be storing in the variable Declare variables either just before they are used or at the start of a block Syntax Type Variable_1, Variable_2, . . .; Examples int count, numberOfDragons, numberOfTrolls; char answer; double speed, distance; CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Primitive Types CSS161: Fundamentals of Computing

Assignment Statements with Primitive Types Syntax Variable = Expression; Evaluates the expression of the right-hand side and store the value into the variable of the left-hand side. Example float distance, speed, time; speed = 15; time = 20; distance = speed * time; distance time 15 20 300 15 20 * 300 CSS161: Fundamentals of Computing

Assignment Statements with Primitive Types (Cont’d) Note that a variable can occur on both sides of the assignment operator count = count + 2; The assignment operator is automatically executed from right-to-left, so assignment statements can be chained number2 = number1 = 3; 10 12 10 + 2 3 3 CSS161: Fundamentals of Computing

Variable Initialization Syntax Type Variable_1 [= Expression_1]{, Variable_2 [= Expression_2]}; [..] can be omitted {..} can be repeated 0 or more times. Each variable is declared as well as initialized or left uninitialized. Example int a = 10, b; // initialize a but not b System.out.println( a ); System.out.println( b ); Javac Test.java Test.java:5: variable b might not have been initialized ^ 1 error CSS161: Fundamentals of Computing

Shorthand Assignment Statements The general form is Variable Op = Expression which is equivalent to Variable = Variable Op (Expression) Op can be +, -, *, /, or % Example: Equivalent To: count += 2; count = count + 2; sum -= discount; sum = sum – discount; bonus *= 2; bonus = bonus * 2; time /= rushFactor; time = time / rushFactor; change %= 100; change = change % 100; amount *= count1 + count2; amount = amount * (count1 + count2); CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook p20’s 12 ~ 16 with your neighboring classmate. CSS161: Fundamentals of Computing

Assignment Compatibility Illegal int intVarible; intVariable = 2.99; possible loss of precesion found : double required : int ^ Legal/Acceptable double doubleVariable; doubleVariable = 2; intVariable = ( int )2.99 // Type Casting CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Type Casting byteshortintlongfloatdouble Char An explicit type cast is required to assign a value of one type to a variable whose type appears to the left of it on the above list (e.g., double to int) The type resulted from arithmetic operations with combined types is adjusted to the highest type. int a = 1; double b = 2.0; doule c = a + b; // int + double -> double CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Constants Integer 0, 2, -1, 5, 1000 Decimal-fraction 0.0, 2.0, -1.5, 5.0, 1000.5 Floating-point 3.65e5 (= 3.65 * 105 = 3.65 * 10000 = 36500.0) 5.89e-4 (= 5.89 * 10-4 = 5.89 * 0.0001 = 0.00589) Character ‘A’, ‘0’, ‘Welcome to Java’ String “A”, “0”, “Welcome to Java” CSS161: Fundamentals of Computing

Arithmetic Operations Division / 15.0 / 2 // equals 7.5 15 / 2 // equals 7 int a = 15, b = 2; double c = a / b; // equals 7.0 double c = a / (double)b; // equa;s 7.5 Modulus operator % 15 % 2 = 1; Increment and decrement operators ++, -- int n = 2; int a = 3 * (n++);// a equals 6, n incremented to 3 int b = 3 * (++n);// n incremented to 4, b equals 12 CSS161: Fundamentals of Computing

Arithmetic Operations (Cont’d) Precedence Associativity rules Binary operators: left-to-right order a + b + c equals (a + b) + c Unary operators: right-to-left order +-+a equals +(-(+a)) Assignment operators: right-to-left order a = b = c equals a = (b = c) CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Practice Evaluate z, and m1 ~ m5 public class Test { public static void main( String[] args ) { int x = 1, y = 2, z = 3; z += y += x += 5; System.out.println( "z += y += x += 5; z = " + z ); int m1 = 0; x= 1; y = 2; z = 3; m1 = x +(++y); System.out.println( "m1 = x +(++y); m1 = " + m1 ); int m2 = 0; x = 1; y = 2; z = 3; m2 = x +++y; System.out.println( "m2 = x +++y; m2 = " + m2 ); int m3 = 0; x = 1; y = 2; z = 3; m3 = x ---y; System.out.println( "m3 = x ---y; m = " + m3 ); int m4 = 0; x = 1; y = 2; z = 3; m4 = x + +-++z; System.out.println( "m4 = x + +-++z; m4 = " + m4 ); int m5 = 0; x = 1; y = 2; m5 = x *-y; System.out.println( “m5 = x *-y; m5 = " + m5 ); } CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Self-Test Exercises Work on Textbook p28’s 17 ~ 21 with your neighboring classmate. Work on Textbook p32’s 22 ~ 23 with your neighboring classmate. CSS161: Fundamentals of Computing

CSS161: Fundamentals of Computing Static Methods A set of statements to repeat the same action Being called from the main( ) static void main( String[] args ) { static void main( String[] args ) { printMessage( ); System.out.println( “D” ); System.out.println( “A” ); System.out.println( “B” ); System.out.println( “C” ); System.out.println( “D” ); } static void printMessage( ) { System.out.println( “A” ); System.out.println( “B” ); System.out.println( “C” ); } } CSS161: Fundamentals of Computing

Static Methods (Cont’d) What will be printed out? public class Test2 { public static void main( String[] args ) { repeatHello( ); repeatBye( ); } static void repeatHello( ) { System.out.println( "hello" ); static void repeatBye( ) { System.out.println( "bye" ); CSS161: Fundamentals of Computing