Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes.

Slides:



Advertisements
Similar presentations
CSci 1130 Intro to Programming in Java
Advertisements

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.
Constants and Data Types Constants Data Types Reading for this class: L&L,
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.
2 Data and Expressions Software Solutions Lewis & Loftus java
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
ECE122 L2: Program Development February 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 2 Program Development.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
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.
ECE122 L3: Expression Evaluation February 6, 2007 ECE 122 Engineering Problem Solving with Java Lecture 3 Expression Evaluation and Program Interaction.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie June 27, 2005.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
String Escape Sequences
1 Course Lectures Available on line:
1 Variables, Constants, and Data Types Primitive Data Types Variables, Initialization, and Assignment Constants Characters Strings Reading for this class:
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
CSCI 1100/1202 January 16, Why do we need variables? To store intermediate results in a long computation. To store a value that is used more than.
Outline Questions / Review Predefined Objects Variables Primitive Data Arithmetic Expressions Interactive Programs Decision Making Assignments.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-WesleyCopyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
CSE 1301 Lecture 2 Data Types Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
Assignment Statements Operator Precedence. ICS111-Java Programming Blanca Polo 2 Assignment, not Equals  An assignment statement changes the value of.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
Chapter 2 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
Assignment An assignment statement changes the value of a variable The assignment operator is the = sign total = 55; Copyright © 2012 Pearson Education,
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
Chapter 2 Data and Expressions Part One. © 2004 Pearson Addison-Wesley. All rights reserved2-2/29 Data and Expressions Let's explore some other fundamental.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Chapter 2: Data and Expressions String and String Concatenation Escape Sequences Variables Primitive Date Types Expressions Interactive Programs.
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 Data and Expressions. © 2004 Pearson Addison-Wesley. All rights reserved2-2 Data and Expressions Let's explore some other fundamental programming.
2-1 Character Strings A string of characters can be represented as a string literal by putting double quotes around the text: Examples: "This is a string.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
Primitive Data Types 1 In PowerPoint, point at the speaker icon, then click the "Play" button.
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.
Chapter 2 Data and Expressions 5 TH EDITION Lewis & Loftus java Software Solutions Foundations of Program Design © 2007 Pearson Addison-Wesley. All rights.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Data and Expressions. Let's explore some other fundamental programming concepts Chapter 2 focuses on: Character Strings Primitive Data The Declaration.
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.
© 2006 Pearson Education Chapter 2: Objects and Primitive Data Presentation slides for Java Software Solutions for AP* Computer Science A 2nd Edition by.
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Fundamentals 2.
Chapter 02 Data and Expressions.
Lecture 2 Data Types Richard Gesick.
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
CSC 1051 – Data Structures and Algorithms I
Multiple variables can be created in one declaration
Chapter 2 Data and Expressions
Escape Sequences What if we wanted to print the quote character?
Data and Expressions Part One
IDENTIFIERS CSC 111.
Chapter 2 Data and Expressions.
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Variables Numbers can be stored and retrieved while a program is running if they are given a home. The way that integers and decimal numbers are stored.
Objects and Primitive Data
Chapter 2 Variables.
Fundamentals 2.
Chapter 2: Java Fundamentals
Chapter 2 Data and Expressions.
Chapter 2: Java Fundamentals
Chap 2. Identifiers, Keywords, and Types
Chapter 2: Objects and Primitive Data
Instructor: Alexander Stoytchev
Presentation transcript:

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Variables and assignment Variable A name for a location in memory holding data value Every variable has a type It depends on the intended use Example:  use the int type for a variable storing integer values A variable declaration => reserve portion of memory large enough to hold the value

Variable declaration A variable must be declared before using it by specifying The variable’s name And the type of information that it will hold int total; int count, temp, result; Multiple variables can be created in one declaration data type variable name

Variables and assignment: general rules A variable can be given an initial value in the declaration When a variable is referenced in a program, its current value is used You can change the value of an existing variable Using the assignment operator (=) lucky_number=13; (if the variable has been declared) See PianoKeys.java int sum = 0; int base = 32, max = 149;

Assignment An assignment statement Changes the value of a variable The assignment operator is the = sign The expression on the right is evaluated And the result is stored in the variable on the left The value that was in total is overwritten See Geometry.java total = 55;

Sample programs /* Input: Geometry.JAVA */ public class Geometry { public static void main (String[ ] args) { int sides = 5; // declaration with intialization System.out.println(“ A pentagon has ”+ sides + “sides.”); sides = 10; // assignment statement System.out.println(“A decagon has ” + sides + “sides.”); } // Output: A pentagon has 5 sides A decagon has 10 sides

Constants A constant is an identifier that is similar to a variable except that it holds the same value During its entire existence By giving the value a name => explain role in program is named using uppercase letters To distinguish them from regular variables In Java, we use the final modifier to declare a variable final int MIN_HEIGHT = 69;

Constants (cont’d) The compiler Produces an error message if you attempt to change the value of a constant This prevents coding errors Because the only valid place to change their value is the initial assignment

Why to use constants? Three reasons for using constants Giving a constant a value a name helps explain its role Give meaning to otherwise unclear values  For example, MAX_LOAD means more than the literal 250 Compiler protects constant values Avoid inadvertent errors by other programmers Prevent changing a constant value throughout a program They facilitate program maintenance If a constant is used in multiple places Its value need only be updated in one place

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes

Primitive data types There are eight primitive data types in Java Four of them represent integers byte, short, int, long Two of them represents floating point numbers float, double One of them represents characters char And one of them represent Boolean values boolean

JAVA Primitive data types All the numeric types differ By the amount of memory space used To store a value of that type Design programs so that space is not wasted Type byte short int long float double Storage 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value ,768 -2,147,483,648 < -9 x /- 3.4 x with 7 significant digits +/- 1.7 x with 15 significant digits Max Value ,767 2,147,483,647 > 9 x 10 18

Integers and floating point types By default JAVA assumes all integer literals are of type int To define a literal of type long L or l is appended to the end of the value Example: long counted_Stars = L ; JAVA assumes floating point literals are of type double If we need to treat a floating point as a float we append f or F to the end of the value Example: float ratio = F;

characters A char variable stores a single character Character literals are delimited by single quotes Example Data type char represents a single character Example: char topGrade = ‘A’; Characters include Uppercase, lowercase letters; punctuation ; etc.. 'a' 'X' '7' '$' ',' '\n'

Boolean type Declaration Example: boolean flag = true; Boolean variables have only two valid values true and false This type is used to represent situation with 2 states Example: a light bulb being on (true) or off (false)

Integers and floating point types By default JAVA assumes all integer literals are of type int To define a literal of type long L or l is appended to the end of the value Example: long counted_Stars = L ; JAVA assumes floating point literals are of type double If we need to treat a floating point as a float we append f or F to the end of the value Example: float ratio = F;

Outline Character Strings Variables and Assignment Primitive Data Types Expressions Data Conversion Interactive Programs Graphics Applets Drawing Shapes