Variables and Constants Chapter 4 Review. Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs.

Slides:



Advertisements
Similar presentations
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Advertisements

L2:CSC © Dr. Basheer M. Nasef Lecture #2 By Dr. Basheer M. Nasef.
Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5; a literal (5) is.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 2 1 Chapter 2 Primitive.
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.
Chapter 2: Using Data.
Primitive Data Types and Operations. Introducing Programming with an Example public class ComputeArea { /** Main method */ public static void main(String[]
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved. 1 Chapter 2 Elementary Programming.
Variables and Constants
Java Building Elements Lecture 2 Instructors: Fu-Chiung Cheng ( 鄭福炯 ) Associate Professor Computer Science & Engineering Tatung University
Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
1 Chapter 2 Primitive Data Types and Operations F Introduce Programming with an Example  The MyInput class F Identifiers, Variables, and Constants F Primitive.
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,
Chapter 4 Variables and Constants. Goals and Objectives 1. Understand simple data types (int, boolean, double). 2. Declare and initialize variables using.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2: Using Data.
Computer Programming TCP1224 Chapter 4 Variables, Constants, and Arithmetic Operators.
Chapter 2 Elementary Programming
Data & Data Types & Simple Math Operation 1 Data and Data Type Standard I/O Simple Math operation.
Variables, Data Types, & Constants. Topics & Objectives Declaring Variables Assignment Statement Reserve Words Data Types Constants Packages & Libraries.
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.
Using Data Within a Program Chapter 2.  Classes  Methods  Statements  Modifiers  Identifiers.
Java Concepts Chapter 2 - Using Objects Mr. Smith AP Computer Science A.
1 Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Chapter 3 Syntax, Errors, and Debugging Fundamentals of Java.
Chapter 2 Variables.
Chapter 4 Variables and constants. 4.1 Variables -Use of variables is good programming style -easier to modify -easier for a programmer to understand.
COMP Primitive and Class Types Yi Hong May 14, 2015.
Java Variables, Types, and Math Getting Started Complete and Turn in Address/Poem.
Variables and Constants Objectives F To understand Identifiers, Variables, and Constants.
Variables and Types Java Part 3. Class Fields  Aka member variable, field variable, instance variable, class variable.  These are the descriptors of.
Announcements Quiz 1 Next Monday. int : Integer Range of Typically –2,147,483,648 to 2,147,483,647 (machine and compiler dependent) float : Real Number.
© 2005 Lawrenceville Press Slide 1 Chapter 4 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms:
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Java – Variables and Constants By: Dan Lunney. Declaring Variables All variables must be declared before they can be used A declaration takes the form:
Primitive Data Types int is a primitive data type A primitive data type is one that stores only a single piece of data. TypeStorageDescription int 4 bytes+ve.
Java Programming: From Problem Analysis to Program Design, Second Edition 1 Lecture 1 Objectives  Become familiar with the basic components of a Java.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Elementary Programming.
1 1 Chapter 2 Elementary Programming. 2 2 Motivations In the preceding chapter, you learned how to create, compile, and run a Java program. Starting from.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Making Interactive Programs with Visual Basic .NET
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are used in programs so that values can be represented with.
© 2006 Lawrenceville Press Slide 1 Chapter 4 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration.
Slide 1 Chapter 3 Variables  A variable is a name for a value stored in memory.  Variables are created using a declaration statement. For example: Dim.
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.
Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept
Bill Tucker Austin Community College COSC 1315
CompSci 230 S Programming Techniques
Topic 2 Elementary Programming
A variable is a name for a value stored in memory.
Chapter 2 Variables.
Chapter 4 Assignment Statement
Elementary Programming
Chapter 3 Assignment Statement
Java Programming: From Problem Analysis to Program Design, 4e
Introduction to C++ Programming
مساق: خوارزميات ومبادئ البرمجة الفصل الدراسي الثاني 2016/2015
Chapter 2 Edited by JJ Shepherd
Chapter 2: Basic Elements of Java
Chapter 2 Variables.
Lecture Notes - Week 2 Lecture-1. Lecture Notes - Week 2 Lecture-1.
Primitive Types and Expressions
Chapter 2 Variables.
Chapter 2 Primitive Data Types and Operations
Presentation transcript:

Variables and Constants Chapter 4 Review

Declaring Variables A variable is a name for a value stored in memory. Variables and constants are used in programs so that values can be represented with meaningful names. Both variables and constants are created with a declaration statement. A variable must be declared before it is used.

Declaring Variables Cont’d A variable declaration includes the data type and identifier. int length; A constant declaration also includes the keyword final. i.e.) final double PI = 3.14; Identifiers are case sensitive and cannot be the same as a Java keyword. Constant identifiers are typically all UPPERCASE. The value of a variable can be changed throughout program execution with assignment statements while a constant cannot. IDENTIFIER DATA TYPE

Using Variables Variable declarations should be grouped at the beginning of a method. The value of a variable is changed through an assignment statement. i.e.) length=5; The equal sign (=) is an operator that indicates that the variable on the left is to receive the value on the right. You can read it (=) as “is assigned the value” A variable can store only 1 value at any 1 time.

Primitive Data Types A primitive data type stores a single piece of data and can include int, double, char, and boolean. TYPEBYTESDATA RANGE int4A positive or negative integer. double8A positive or negative number with a decimal portion. char2A single character boolean2true of false

Abstract Data Types Abstract data types include classes. Each class defines not just a single piece of data like a primitive data type, but a set of data along with methods for performing actions on that data. Variables declared with an abstract data type are called objects. An object declaration is called instantiation. An object is instantiated with the keyword new.

Abstract Data Types Cont’d The code below creates a new object using a class named Circle: Circle spot = new Circle(4); In this statement, the variable spot refers to a Circle object that has been initialized with a radius of 4. To access a member of a class, such as a method, use the object name followed by a dot (.) and then the member name. i.e.) spot.getRadius()

Java Packages Java includes many packages. Package members are accessible with an import statement. i.e.) import java.util.Scanner; The class name starts with an uppercase letter, following the appropriate naming convention for class names. If several classes in a package are to be accessible, then a statement that imports the entire package may be used: i.e.) import java.util.*;

Obtaining a value from user Data can be read from the user at run time by using the Scanner class. This class processes data from the input stream. Objects that read data from the keyboard are initialized with System.in. When obtaining data from the user, a prompt should be included so that the user knows what information is expected.

Obtaining a value from user Scanner input = new Scanner(System.in); In this example, a Scanner object is declared, instantiated, and initialized. The calls to the nextInt() method are used to obtain the values typed by the user. When a call to nextInt() occurs, the application waits until the user types a value and presses enter before the next statement is executed.

Scanner class methods (java.util.Scanner) Methods next()returns a string from the input stream. nextLine()returns the string up to the end of line character from the input stream. nextInt()returns the int read from the input stream. nextDouble()returns the double read from the input stream. nextBoolean()returns the boolean read from the input stream. close()closes the input stream.

Numeric Expressions Java includes built-in operators that are used to form numeric expressions. Arithmetic operators include +, -, *, /, and %. The / operator performs integer division. When both operands are type int, the decimal portion of the quotient to result in an integer. The % operator performs modulus division. It returns the remainder from division.

Type Casting Type casting converts a number of one type to a number of a different type. Casting is useful when real division with integers is preferred. i.e.) result= (double)i) / double)j;

Formatting Numeric Output The NumberFormat class, which is part of the java.text package, is used to create objects that format numbers. i.e.) import java.text.NumberFormat; NumberFormat money = NumberFormat.getCurrencyInstance(); NumberFormat number = NumberFormat.getIntegerInstance(); NumberFormat decimal = NumberFormat.getNumberInstance(); NumberFormat percent = NumberFormat.getPercentInstance(); System.out.println(money.format(dollars)); System.out.println(number.format(num)); System.out.println(decimal.format(numWithDecimal)); System.out.println(percent.format(sale));

Assignment Operators In addition to the = assignment operator, Java recognizes the +=, -=, *=, /=, and %= assignment operators. operatorSame as: numPlayers -= 3;numPlayers = numPlayers - 3 numCopies *= 5;numCopies = numCopies * 5 total /= 2;total = total / 2 remainder %= 6;remainder = remainder % 6

Programming Errors A syntax error violates the rules of Java. A logic error is also called a semantic error and is more difficult to detect because statements are syntactically correct, but produce unexpected results. A run-time error is also called an exception. An exception halts program execution.