Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013
Using Variables Named Storing Location Stores a value Defined by type and identifier Type Type of data being stored – Indicates kind of variable to store
Using Variables Identifier – -Name of variable – - how value will be referred to in a program Example: double radius; –Variable named radius –Type double Number containing decimal points.
Using Variables Assignment - = operator -process of giving a variable a value - stores the value in the memory location referred to by radius 1. double radius = 12.3; 2. double radius; radius = 12.3; Variable declaration Assignment
-Variable name must be on the left -radius = 12.3; // correct = radius; // incorrect - can NOT include variable assignment in output statements - System...(radius = 12.3); // incorrect
Variable Example int variable = 5; System.out.println(variable); variable = 10; System.out.println(variable);
Variable Definitions Can define multiple variables int x, y z; Variable definitions can include assigments int x = 7;
Naming Constants defined in a statement Key word final Type identifier Assignment Example: final double PI = 3.14 Use all UPPERCASE for constants
Naming Constants Easier to understand Easier to modify Only have to change once Cannot change value of constant with an assigment PI = 22/7; // Error
Choosing Identifiers Must start with a letter Contain letters, digits, and the underscore Can be any length Case-sensitive Quickly and clearly understandable Begin with lowercase letters
Choosing Identifiers Keywords – turn blue Identifiers reserved by java Special meaning to compiler Examples double int float
Built-In Data Types double Positive or negative real numbers Uses scientific notation Rounded to 6 significant digits
PDN September 23, 2015 What variables would you create for the following output: Bob worked 17 hours for $ Wages = l7*13.75 Bob makes $ for the 17 hours he works.
When do you use variables? - Repeated values - User Input - Calculations and/or formulas - things that can be changed
What are the benefits to using variables? - Easily understood by developer - Easy to change - Less typos - Less code - Organized
int and long Positive or negative integers Can't store decimals - Error
char Stores a single character Letters, digits, symbols and spaces Requires single quotation marks EX: char ch; ch = ‘A’
char Char 4 and int 4 are NOT the same Arithmetic using char results in wrong answers
Caution: int x = 12; int y = 2*x; x = 4; System.out.println(y); Display reads: 24
What are the steps to accepting user input? -Import library -Create scanner -Prompt user -Get data -Store in a variable -Use/display data
String Library String sequence of characters access library by: #include using namespace std;
Using String string name; sets the type string allows user to enter strings (not numbers) and to output those strings
Case Study Specification 1 st step in programming defines what the program is to accomplish Design tells how the computer will be constructed
Coding writing the program Debugging process of getting a program to work correctly Testing process of selecting different possibilities and to reveal bugs