Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013.

Similar presentations


Presentation on theme: "Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013."— Presentation transcript:

1 Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013

2 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

3 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.

4 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

5 -Variable name must be on the left -radius = 12.3; // correct -12.3 = radius; // incorrect - can NOT include variable assignment in output statements - System...(radius = 12.3); // incorrect

6 Variable Example int variable = 5; System.out.println(variable); variable = 10; System.out.println(variable);

7 Variable Definitions Can define multiple variables int x, y z; Variable definitions can include assigments int x = 7;

8 Naming Constants defined in a statement Key word final Type identifier Assignment Example: final double PI = 3.14 Use all UPPERCASE for constants

9 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

10 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

11 Choosing Identifiers Keywords – turn blue Identifiers reserved by java Special meaning to compiler Examples double int float

12 Built-In Data Types double Positive or negative real numbers Uses scientific notation Rounded to 6 significant digits

13 PDN September 23, 2015 What variables would you create for the following output: Bob worked 17 hours for $13.75. Wages = l7*13.75 Bob makes $233.75 for the 17 hours he works.

14 When do you use variables? - Repeated values - User Input - Calculations and/or formulas - things that can be changed

15 What are the benefits to using variables? - Easily understood by developer - Easy to change - Less typos - Less code - Organized

16 int and long Positive or negative integers Can't store decimals - Error

17 char Stores a single character Letters, digits, symbols and spaces Requires single quotation marks EX: char ch; ch = ‘A’

18 char Char 4 and int 4 are NOT the same Arithmetic using char results in wrong answers

19 Caution: int x = 12; int y = 2*x; x = 4; System.out.println(y); Display reads: 24

20 What are the steps to accepting user input? -Import library -Create scanner -Prompt user -Get data -Store in a variable -Use/display data

21 String Library String sequence of characters access library by: #include using namespace std;

22 Using String string name; sets the type string allows user to enter strings (not numbers) and to output those strings

23 Case Study Specification 1 st step in programming defines what the program is to accomplish Design tells how the computer will be constructed

24 Coding writing the program Debugging process of getting a program to work correctly Testing process of selecting different possibilities and to reveal bugs

25


Download ppt "Chapter 3 Using Variables, Constants, Formatting Mrs. UlshaferSept. 2013."

Similar presentations


Ads by Google