Chapter 2 Variables.

Slides:



Advertisements
Similar presentations
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
Advertisements

1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
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.
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
CMT Programming Software Applications
Data types and variables
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
Chapter 2 Data Types, Declarations, and Displays
Chapter 2: Introduction to C++.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Chapter Two Using Data. 2 Objectives Learn about variable types and how to declare variables Learn how to display variable values Learn about the integral.
Chapter 2 Programming Building Blocks — Java Basics.
Chapter 2 Data Types, Declarations, and Displays.
Objectives You should be able to describe: Data Types
A First Book of ANSI C Fourth Edition
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
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.
2440: 211 Interactive Web Programming Expressions & Operators.
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.
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.)
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 2: Using Data.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Week 1 Algorithmization and Programming Languages.
Knowledge Base C++ #include using std namespace; int main(){} return 0 ; cout
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
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.
CHAPTER 4 GC 101 Data types. DATA TYPES  For all data, assign a name (identifier) and a data type  Data type tells compiler:  How much memory to allocate.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Java Programming, Second Edition Chapter Two Using Data Within a Program.
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.
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.
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
C++ for Engineers and Scientists Second Edition
CS 106 Introduction to Computer Science I 09 / 10 / 2007 Instructor: Michael Eckmann.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
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.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Java Programming: Guided Learning with Early Objects Chapter 1 Basic Elements of Java.
Chapter 2 Variables.
Identifiers - symbolic names
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Multiple variables can be created in one declaration
Identifiers - symbolic names
Java Programming: From Problem Analysis to Program Design, 4e
Escape Sequences What if we wanted to print the quote character?
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 Variables.
Expressions and Assignment
Chapter 2: Introduction to C++.
Primitive Types and Expressions
Unit 3: Variables in Java
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Chapter 2 Variables

Objectives Describe the term variable. Create valid variable names by following Java’s syntactical and stylistic rules. Name and describe each of the primitive types used in the Java language. Declare and assign values to variables. Use comments, strings, and statements properly in coding. Describe string concatenation. Use escape codes to format output.

Objectives Continued List the rules of precedence in order. Apply precedence rules to solve arithmetic Java statements. Solve arithmetic Java statements that include a mixture of the 5 arithmetic operators. Match shorthand assignment operators with their equivalent longhand notations. Apply casting rules to coding situations.

Java Variables Constant Data – cannot be changed after the program compiles Variable Data - might change after the program compiles Variables are locations in memory in which values can be stored.  Variables have a type, name, and a value.

Variable Names… Consist of letters, digits, underscores (_) or $ Cannot have the first character as a digit Can be any length (extremely short or long names are awkward) Cannot be keywords Should begin with a lowercase letter (following words should have an initial uppercase letter such as theButton or grossPay) Should be meaningful. 

Variable Naming Practice Variable Name Valid or Invalid and Why? grossPay Hourly wages card-game total2 totalscore grand_Total java dollars$

Variable Types Type Size Range Sample byte (integer) 8 bits -128 to 127 byte  numberOfChildren; short (integer) 16 bits -32768 to 32767 short age; int (integer) 32 bits -2,147,483,648 to  2,147,483,647 int  counter; long (integer) 64 bits -9223372036854775808 to 9223372036854775807  long  debt; float (floating point) 32 bits single precision -3.4028235E+38  to +3.4028235E+38   (7 digits precision) float  rate; double (floating point) 64 bits double precision -1.7976931348623157E+308  to +1.7976931348623157E+308 (16 digits precision) double tax; char 16 bits unsigned  one character using Unicode system  (initialized to \u0000) char answer='Y'; boolean  8 bits true or false (initialized to false) boolean reply=false;

Declaring Variables Must be declared before they can be used. Should only be declared once in a program. Are usually declared at the beginning of a class. Example: int number; You can declare multiple variable names of same type by separating with commas. Example: int x, y, z;

Assigning Values to Variables You can declare a variable and initialize it (give it a value) at the same time.  Example: int number = 15; If you try to assign a number with a decimal point to an integer type variable, you will get an error.    Example: int y = 18.5;   // ERROR!!

Assigning Values to Variables Continued… By default, whole numbers are of type int and decimal point numbers are of type double.  If you declare a variable with a type other than int or double, you will need to attach a letter to the numeric constant or you will get an error.  Example: If you declare the variable "a" to be a float and want to assign it the value of 100.98 you would need to write it as: float  a = 100.98F;  OR float  a = 100.98f;  

Alice Variable Example

Alice Preferences

Statements Statements are the simplest thing you can do in Java. Forms a single Java operation. Generally ends with a semicolon. Block of statements are surrounded by curly braces { }. Samples : int i; //declares variable i to be integer i = a + 1; // this is an arithmetic statement System.out.println ("Hello World"); // this is a print statement

Strings A String is a series of characters that can be used to display messages, input text, etc. May include letters, digits, and/or special characters. A String is an object in Java. String literals are written inside double quotes. Examples : "John Doe" "219 Grant Street“

Strings Continued… String Declaration Examples: String message; // declare a string called message String message = "Go Tigers!!!!"; // declared and assigned value String Concatenation The + operator, when used with strings and other objects, creates a single string that contains the concatenation (combining) of all its operands.   double total = 50.95; System.out.println ( "The total is " + total ) ; Output: The total is 50.95

Precedence Rules Innermost parentheses are done first. If the parentheses are on same level, the formula inside of the leftmost parenthesis is done first. Multiplication (*), division (/), and modulus (%) are done from left to right in order that they are encountered. Addition (+) and subtraction (-) are all on same level and done from left to right in order that they are encountered. Assignment operator (=). Places result in variable on left side of = sign. Note: Modulus (%) is the remainder of the division.

Practice

Integer Division When dividing two integers: the quotient is an integer the remainder is truncated Dividing by zero creates a run-time error

Modulus Equation Explanation Answer 13 % 4 13 divided by 4 gives you a quotient of 3 and a remainder of 1 1 6 % 2 6 divided by 2 gives you a quotient of 3 and a remainder of 0 int y = 45 % 6 * 5 % 2; 45 divided by 6 gives you a quotient of 7 and remainder of 3.   The intermediate result of 45 % 6 is 3 and so now you multiply the 3 by 5 which gives you 15.  Now you are left with 15 % 2 which means to divide 15 by 2 giving you a quotient of 7 and remainder of 1.

Shorthand Assignment Operators   Shorthand Assignment Operators Shorthand Assignment Operator Longhand Notation x + = y;    x =  x + y; x - = y;    x =  x - y; x * = y;    x =  x * y; x  / = y;    x =  x / y; x % =y;    x =  x % y; x ++; x = x + 1; x --; x = x – 1;

  Casting z  = (int) 31.56;    // puts the integer part which is 31 into z z  = (int) (x / y);   //  divides x by y and then puts integer into z part of answer in z

Walk-Through double y = 25 % 4 + 3 * 5 / 2.0 + (6 / 5); 25 % 4 + 3 * 5 / 2.0 + 1; 1 + 3 * 5 / 2.0 + 1; 1 + 15 / 2.0 + 1; 1 + 7.5 + 1; 8.5 + 1; 9.5;

Escape Codes        \n       Newline         \t       Tab         \\       Displays a Backslash         \'       Displays a single quote         \"      Displays a double quote System.out.println("I am going to earn an \"A\" in this course"); I am going to earn an “A” in this course.