1 Variables and Data Types. 2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed,

Slides:



Advertisements
Similar presentations
4 Control Statements: Part 1.
Advertisements

Arithmetic Calculations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Etter/Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 2 Simple C Programs.
7 Copyright © 2005, Oracle. All rights reserved. Creating Classes and Objects.
Lecture 5 Types, Expressions and Simple I/O COMP1681 / SE15 Introduction to Programming.
Programming Language Concepts
© Vinny Cahill 1 Writing a Program in Java. © Vinny Cahill 2 The Hello World Program l Want to write a program to print a message on the screen.
Excel Functions. Part 1. Introduction 2 An Excel function is a formula or a procedure that is performed in the Visual Basic environment, outside the.
Solve Multi-step Equations
REVIEW: Arthropod ID. 1. Name the subphylum. 2. Name the subphylum. 3. Name the order.
Lilian Blot TO PROGRAMMING & PYTHON Introduction Autumn 2012 TPOP 1.
Semantic Analysis and Symbol Tables
1 public class Newton { public static double sqrt(double c) { double epsilon = 1E-15; if (c < 0) return Double.NaN; double t = c; while (Math.abs(t - c/t)
This is Java Jeopardy.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
CSci 1130 Intro to Programming in Java
Model and Relationships 6 M 1 M M M M M M M M M M M M M M M M
Chapter Three Arithmetic Expressions and Assignment Statements
Types of selection structures
©Brooks/Cole, 2001 Chapter 12 Derived Types-- Enumerated, Structure and Union.
Chapter 2 JAVA FUNDAMENTALS
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
PSSA Preparation.
COMP 14 Introduction to Programming
Introduction to Programming G51PRG University of Nottingham Revision 1
Chapter 8 Improving the User Interface
DATA TYPES, VARIABLES, ARITHMETIC. Variables A variable is a “named container” that holds a value. A name for a spot in the computer’s memory This value.
L6:CSC © Dr. Basheer M. Nasef Lecture #6 By Dr. Basheer M. Nasef.
Computer Programming w/ Eng. Applications
L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Primitive Data Types There are a number of common objects we encounter and are treated specially by almost any programming language These are called basic.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
1 Data types, operations, and expressions Overview l Format of a Java Application l Primitive Data Types l Variable Declaration l Arithmetic Operations.
Fundamental Programming Structures in Java: Comments, Data Types, Variables, Assignments, Operators.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 2: Basic Elements of Java J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Chapter 2 Basic Elements of Java. Chapter Objectives Become familiar with the basic components of a Java program, including methods, special symbols,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Java Programming: From Problem Analysis to Program Design, 4e Chapter 2 Basic Elements of Java.
Types CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo de Manila University (Chapter 4, Horstmann text)
CHAPTER 2 C++ SYNTAX & SEMANTICS #include using namespace std; int main() { cout
Programming in Java (COP 2250) Lecture 4 Chengyong Yang Fall, 2005.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Java Basics Variables, Expressions, Statements, etc. CS 21a: Introduction to Computing I Department of Information Systems and Computer Science Ateneo.
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.
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.
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.
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.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Java Programming: From Problem Analysis to Program Design, 4e
Chapter 2: Basic Elements of Java
Introduction to C++ Programming
Expressions and Assignment
elementary programming
Data Types and Expressions
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Data Types and Expressions
Data Types and Expressions
Presentation transcript:

1 Variables and Data Types

2 Variable Definition a location in memory, referenced by a name (identifier), where data of a given type can be stored, changed, and retrieved Using variables establish its data type and initial value (declaration) set/change its value (assignment, input) use/display its value (expressions, output)

3 Example: Using Variables int dimecount;// declaration double dimevalue = 0.10;// declaration with initial value double totalvalue; dimecount = Input.readInt(); // input (assignment) totalvalue = dimecount*dimevalue; // assignment System.out.println(totalvalue); // output

dimevalue dimecount totalvalue dimecount = Input.readInt(); totalvalue = dimecount*dimevalue; INPUT OUTPUT System.out.println(totalvalue);

5 Back to Java Program Structure A Java program (application or applet) is a class that consists of methods main(), init(), paint(), action(), setup(), onButtonPressed(), … Each method has a body delimited by { } consists of a sequence of statements (including declarations)

6 Statements Declarations int dimecount; double dimevalue = 0.10; Assignment statements dimecount = Input.readInt(); totalvalue = dimecount*dimevalue; Output statements System.out.println(totalvalue);

7 Identifier A name in a Java program used for variables, classes, methods,... Rules in forming an identifier: consists of letters, digits, and underscores (_) should start with letter or underscore Examples: ateneo score5 total_credit BigBlue _one4three x if Some identifiers are reserved words

8 Data Type Describes a domain or pool of values Helps a compiler impose rules Programming language needs rules for constructing literals for a given data type e.g., 234 is an integer literal, ‘A’ is a character literal, 2.1e-3 is a double floating point literal Some primitive data types in Java: int, char, float, double, boolean

9 Understanding Data Types Important components of a data type: Range of values Literals Possible operations

10 The int Data Type Range: -2,147,483,648 to 2,147,483,647 applies to all system platforms Literals sequence of digits Examples: 22, 16, 1, 426, 0, Operations: usual arithmetic operations +, -, *, /, % negative numbers obtained using - as prefix

11 The double Data Type Values: decimal numbers Range: 4.94e-324 to 1.80e+308 precision: n.nnnnn... X 10 (+/-)mmm Literals (examples) 100.5, , E10 ( ), 2.1e-3 (0.0021) Operations: arithmetic ops (division?) float: lower precision

12 Constants Literal values in a program appear often enough and may be associated with an appropriate name declare as a “variable with a fixed value” Examples public static final int MAX = 100; public static final double PI = ; public static final double DIMEVALUE = 0.10;

13 Input and Output

14 I/O in Java Text output in Java System.out.print & System.out.println Input in pure Java is not straightforward need to handle exception cases uses notions of streams and files Trend in current applications perform I/O through visual components GUIs

15 Input.java A “home made” class designed to make console input simpler For use in Java applications make sure that Input.java is in your working directory use Input.xxx() for text input of ints/doubles Input.readInt(), Input.readDouble()

16 Input Statements are Assignment Statements Examples: double interestRate;... int count = Input.readInt();

17 Applets To create applets need to process GUI events need an init() method to set up visual objects, an action() method to specify associated actions use InputOutputApplet extend InputOutputApplet instead of Applet define setup() and onButtonPressed()

18 Using InputOutputApplet Make sure InputOutputApplet.class is present in your directory In the setup() method addInput(“name”) to add input objects addButton(“label”) to add a button addOutput() to add an output area In the onButtonPressed method getInt(), getDouble() for retrieving data print(), println() for printing on output area

19 Operators and Expressions

20 Operators in Java Arithmetic operators +, -, *, /, % Special operators (, ) performs grouping = (assignment) Other operators

21 Understanding Operators Operands count (binary/unary) type Calculation performed value (and type) returned Other effects

22 Example: % Binary operation Both operands are ints Returns the (int) remainder when left operand is divided by the right operand No additional effects

23 Another Example: = Binary operation Left operand must be a variable Returns the value of the right operand Effect: value of right operand is assigned to left operand * Note that a = b = c = 0; is valid

24 Other Operators Increment and decrement operators ++, -- post- or pre- Assignment operators +=, -=, *=, /=, … “Built-in” Functions not really operators (but similar) Math.abs(), Math.sqrt(), Math.pow(),...

25 Post-increment Operator: ++ Example: number++ Unary operator Operand must be a variable Returns the (original) value of the operand Effect: variable is incremented

26 Pre-increment Operator: ++ Example: ++number Unary operator Operand must be a variable Returns the incremented value of the operand Effect: variable is incremented

27 About ++ Notice that a++; and ++a; are virtually equivalent return value is ignored in both cases could be used as shorthands for a = a+1; Distinction apparent when the return value is useda = 5; b = a++;b = ++a;// values of a & b?

28 Decrement Operator: -- Analogous definitions for Post-decrementnumber-- Pre-decrement--number

29 Assignment Operators There is a shorthand for constructs such as sum = sum + number; sum += number; += is an operator: such an operator exists for virtually every arithmetic operator +=, -=, *=. /=, %=,... effect: variable is updated returned value: the updated value

30 Built-in Functions Provided in Java to provide for more complex operations Example: Math.pow() double result = Math.pow(5.5,3.0); can be viewed as a binary operation that calculates some power of a number javap java.lang.Math prints a list of available math functions

31 Operand Types vs Result Type There are cases where the type of the result differs from the types of the operands Examples division between an int and a double returns a double Math.round() has an operand (argument) that is a float but returns an int

32 Expressions Expression sequence of variables, literals, operators, and function calls Uses right operand of an assignment argument for System.out.println() Expression-statement an expression terminated by a semicolon

33 Strings

34 Variables Revisited A variable holds a value A variable may instead contain a reference 5 “Hello” int num = 5; num String s = “Hello”; s

35 String A special kind of data type called a class allows for string objects About Strings sequences of characters (letters, digits, etc) literals: formed by delimiting the sequence of characters with " " operations?

36 Operations on Strings Concatenation “Hello” + “ there” equals “Hello there” Obtain length of string String s = “Hello”; int len = s.length(); // len = 5 Obtain a substring String s = “Hello”; String t = s.substring(0,3); // t = “Hel”