Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.

Slides:



Advertisements
Similar presentations
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Advertisements

Types and Arithmetic Operators
Types, Variables and Operators Computer Engineering Department Java Course Asst. Prof. Dr. Ahmet Sayar Kocaeli University - Fall 2013.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
Introduction to Programming with Java, for Beginners Primitive Types Expressions Statements Variables Strings.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
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.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
Primitive Types Java supports two kinds of types of values – objects, and – values of primitive data types variables store – either references to objects.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
The Data Element. 2 Data type: A description of the set of values and the basic set of operations that can be applied to values of the type. Strong typing:
Java Programming Constructs 1 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
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.
General Features of Java Programming Language Variables and Data Types Operators Expressions Control Flow Statements.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
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.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Final Exam Review Closed book Closed laptop One sheet of notes permitted SE-0010 Dr. Mark L. Hornick 1.
Chapter 2: Using Data.
Java Programming Constructs 3 MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation.
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.
Today’s topic:  Arithmetic expressions. Arithmetic expressions  binary (two arguments) arithmetic operators  +, -, *, /, % (mod or modulus)  unary.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
CIS 260: App Dev I. 2 Programs and Programming n Program  A sequence of steps designed to accomplish a task n Program design  A detailed _____ for implementing.
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.
November 1, 2015ICS102: Expressions & Assignment 1 Expressions and Assignment.
Programming Fundamental Slides1 Data Types, Identifiers, and Expressions Topics to cover here: Data types Variables and Identifiers Arithmetic and Logical.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Java Arrays and Methods MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
VARIABLES, CONSTANTS, OPERATORS ANS EXPRESSION
Java Language Basics By Keywords Keywords of Java are given below – abstract continue for new switch assert *** default goto * package.
Mathematical Calculations in Java Mrs. C. Furman.
A Simple Java Program //This program prints Welcome to Java! public class Welcome { public static void main(String[] args) { public static void main(String[]
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.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
JAVA Practical Unary operators 2. Using Reals 3. Conversions 4. Type Casting 5. Scope 6. Constants.
Programming Principles Operators and Expressions.
ICS102 Lecture 1 : Expressions and Assignment King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
Primitive Data Types and Operations F Introduce Programming with an Example F Identifiers, Variables, and Constants F Primitive Data Types –byte, short,
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.
1.  Algorithm: 1. Read in the radius 2. Compute the area using the following formula: area = radius x radius x PI 3. Display the area 2.
Java Fundamentals MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Data Types, Identifiers, and Expressions
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Data Types, Identifiers, and Expressions
Business Programming Concepts II
Chapter 8 JavaScript: Control Statements, Part 2
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Numerical Data Types.
Chapter 2: Basic Elements of Java
Expressions and Assignment
elementary programming
CS2011 Introduction to Programming I Elementary Programming
Fundamental OOP Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
In this class, we will cover:
Module 2 Variables, Data Types and Arithmetic
Chapter 2: Java Fundamentals cont’d
Presentation transcript:

Java Expressions MIS 3023 Business Programming Concepts II The University of Tulsa Professor: Akhilesh Bajaj All slides in this presentation ©Akhilesh Bajaj, All rights reserved

Objectives Understand what an expression is Understand how to create expressions in Java Let’s get started!

Variables We always declare the variable, by specifying the type first int anIntegerVariable; ch aCharVariable; double firstDoubleVariable,secondDoubleVariable; Variable names must begin with a letter or _ and be a sequence of digits or letters We can declare anywhere in Java, within the method. It’s a good idea to declare at the start of each method, and in one place in the class (outside of the methods of the class) Symbols like + or © cannot be used within a variable name The convention in Java is to start the variable name with a lowercase letter. Just like variable declarations, object declarations need the class first. Again, object names start with a lower case letter, but class names start with an uppercase letter. E.g., String aStringObject;

Variables We can typecast between certain types. So, a variable of type int can be assigned a value contained in another variable that is of type double, only if we explicitly typecast the value to int. anIntegerVariable = (int) firstDoubleVariable; This does not change the value in the firstDoubleVariable location.It simply takes the RHS value, and truncates it before assigning it to the LHS. The value in the RHS location is not changed. We can only typecast between certain types. For example, between a double and int. But it makes no sense to typecast between a boolean and anything else and so this is not supported. in our book has more detailed information & makes for fun reading!

Constants A constant in Java is a memory location that contains one, unchangeable value It cannot be in a method. It has to be outside of all methods. Why do we need a constant? - make one change instead of throughout the class - More meaningful name for the value E.g., In a program that converts meters to feet, the conversion ratio can be a constant. Class UsesConstants { static final double conversionMetersFeet=3.3; public static void main (String[] args) { //code in main //more code in main }//end main }//end class UsesConstants

Expressions An expression consists of: -an operator -one or more operands -a type -a value unary operator: 1 operand, binary operator: 2 operands, ternary operator: 3 operands. E.g., anIntegerVariable + firstDoubleVariable is an expression. What are its components?

Expressions Operators: + - * /arithmetic expressions Math.pow( double base, double exp ) returns the double value of base raised to the power of exp We will use this in assignment 1! %mod operator E.g. 11 % 4 is / 4 is /4 is 2 Operands can be literal values, initialized variables, or other expressions.

Expressions Operators: The increment and decrement operators: n = n + 1; Can be written as n++; Or ++n; int m = 7; int n = 7; int a = 2* ++m; // a becomes 16, m is 8 int b = 2* n++; //b is 14, n is 8

Expressions Operators that create a boolean expression: > = for comparing quantities E.g., (a>b) is an expression. What are its components? == for comparing two variables or expressions’ values E.g., (a ==b ) is an expression. What are it’s components? != stands for not equal to E.g., (a!=b) is an expression. What are its components? && stands for the AND operator || stands for the OR operator. Some examples of creating boolean expressions? TestComparisonOperators.java

Expressions boolean sameSign; sameSign = ((x > 0) == (y > 0)); What does this evaluate to? We use expressions to -build larger expressions -on the RHS of assignment statements -boolean expressions can be used in if, for, while, do-while, switch statements

We understood how to use expressions in Java More details can be found in section 2.5 of our text: 1/c2/s5.html which should be fun to read now! CONCLUSION