Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the...
-
date post
19-Dec-2015 -
Category
Documents
-
view
218 -
download
2
Transcript of Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the...
![Page 1: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/1.jpg)
Variables
Pepper
![Page 2: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/2.jpg)
Variable
• A variable – box – holds a certain type of value– value inside the box can change
• Example– A = 2B+1– Slope = change in y / change in x– monopoly square – holds different monopoly
pieces
![Page 3: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/3.jpg)
Data types
• type: A category or set of data values.–Examples: integer, real number, string.
• Internally, the computer stores all data as 0s and 1s.
–examples: 42 101010"hi"
0110100001101001
![Page 4: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/4.jpg)
Java's primitive types• primitive types: Java's built-in simple data types for
numbers, text characters, and logic.– Java has eight primitive types.– Types that are not primitive are called object types.
• Four primitive types we will use:
Name Description Examples– int integers (whole numbers) 42, -3, 0, 926394
– double real numbers 3.14, -0.25, 9.4e3
– char single text characters 'a', 'X', '?', '\n'
– boolean logical values true, false
![Page 5: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/5.jpg)
Types
•
type kind memory range
byte integer 1 byte -128 to 127
short integer 2 bytes -32768 to 32767
int integer 4 bytes -2147483648 to 2147483647
long integer 8 bytes -9223372036854775808 to-9223372036854775807
float floating point
4 bytes ±3.40282347 x 1038 to±3.40282347 x 10-45
double floating point
8 bytes ±1.76769313486231570 x 10308 to
±4.94065645841246544 x 10-
324
char single character
2 bytes all Unicode characters
boolean true or false 1 bit
![Page 6: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/6.jpg)
Create a variable• create a variable
– In Java: <type> <variable name> ; example: int myIntVar;
– declare with correct type: int, double, char, boolean, (not capitalized)
– declare Object variables with types such as : String, Scanner, Random, (capitalized)
• You try: – Create a program that creates the following variables:
• int myQuantity• double myDollars• boolean answer• char oneLetter• String myName
![Page 7: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/7.jpg)
Change a variable• change variable contents:
– In Java: <variable name> = <some expression>; example: myIntVar = 5 ;.
– Equal sign means set equal to – Left side gets put into right side
• You try – set those variables = to something• myQuantity = 1• myDollars = 3.3• answer = true• oneLetter = ‘a’• myName = “pepper”
• Now, print those variables with: – System.out.println(myQuantity + myDollars + answer +
oneLetter + myName)• What happens when you put single or double quotes
where there were none, or take away the quotes?
![Page 8: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/8.jpg)
Use variables in a real program
• On Paper: Average 2 + 4 + 6
• Pay attention to your steps
![Page 9: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/9.jpg)
Writing the Average program
Specification – write a program which can find the average of three numbers.
Let’s list the steps that our program must perform to do this:• Add up these values
• Divide the sum by the number of values
• Print the result
Each of these steps will be a different statement.
![Page 11: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/11.jpg)
Writing the Average program
• Add up these values• Divide the sum by the number of values• Print the result
sum = 2 + 4 + 6;
sum = 2 + 4 + 6; an assignment statement
![Page 12: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/12.jpg)
Assignment Statements
• Assignment statements take the form:variable = expression
Memory location where the value is stored Combination of constants
and variables
![Page 13: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/13.jpg)
Expressions
• Expressions combine values using one of several operations.
• The operations being used is indicated by the operator:
+ Addition- Subtraction* Multiplication/ DivisionExamples:
2 + 53 4 * valuex / y
![Page 14: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/14.jpg)
Writing the Average program 1
• sum = 2 + 4 + 6;• Divide the sum by the
number of values• Print the result
average = sum / 3;
Names that describe whatthe values represent
![Page 15: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/15.jpg)
Writing the Average program 2
• sum = 2 + 4 + 6• average = sum / 3;• Print the result
System.out.println(″The average is ″ + average); The output method variable
name
![Page 16: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/16.jpg)
Writing the Average program 3
public static void main(String[] args) {
-------------------- sum = 2 + 4 + 6; average = sum / 3; System.out.println("The average is "
+ average); }
We still need to add a declare our variables. This tells the computer what they are.
![Page 17: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/17.jpg)
Writing the Average program 4
public class Average3 { public static void main(String[] args) { int sum, average; sum = 2 + 4 + 6; average = sum / 3; System.out.println("The average is " +
average); }}
Tells the computer that sum and average are integers
![Page 18: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/18.jpg)
Writing the Average program 5
public class Average3a {
public static void main(String[] args) {
int sum;
int average;
sum = 2 + 4 + 6;
average = sum / 3;
System.out.println("The average is " + average);
}
}
We could also write this as two separate declarations.
![Page 19: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/19.jpg)
You try
• Add 1 to the average and then print it again.
average = average + 1
![Page 20: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/20.jpg)
Variables and Identifiers
• Variables have names – we call these names identifiers.
• Identifiers identify various elements of a program (so far the only such element are the variables.
• Some identifiers are standard (such as System)
![Page 21: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/21.jpg)
Identifier Rules
• An identifier must begin with a letter or an underscore _
• Java is case sensitive upper case (capital) or lower case letters are considered different characters. Average, average and AVERAGE are three different identifiers.
• Numbers can also appear after the first character.
• Identifiers can be as long as you want but names that are too long usually are too cumbersome.
• Identifiers cannot be reserved words (special words like int, main, etc.)
![Page 22: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/22.jpg)
Spelling Conventions
• Name constants
• Variables start lower case
• Classes uppercase
• Word boundaries upper case (numberOfPods)
![Page 23: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/23.jpg)
Some Illegal Identifiers
timeAndAHalf& is not allowed
time&ahalf
fourTimesFive* is not allowedfour*five
times2 or twoTimes
Cannot begin with a number
2times
myAgeBlanks are not allowed
my age
Suggested IdentifierReasonIllegal Identifier
![Page 24: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/24.jpg)
Assignment
int number1 = 33;
double number2;
number2 = number1;
byteshortintlongfloatdoublechar
![Page 25: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/25.jpg)
Dividing
• int / int int (even if you assign it to a double)
• float / int float
• int / float float
Solution: Cast it
ans = n / (double) m
![Page 26: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/26.jpg)
Math Operators & PEMDAS
• + add
• - subtract
• * multiply
• - division
• % remainder
Example: base + (rate * hours)
![Page 27: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/27.jpg)
Fancy Math
variable = variable op (expression)count = count + 1 count = count + (6 / 2 * a + 3)variable op = expressioncount += 1 count += (6 / 2 * a + 3)
Example:int count = 1;count += 2;The value of count is now 3
![Page 28: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/28.jpg)
More Fancy Math
• Increment ++
• Decrment –
• ++n adds 1 before executing
• n++ adds 1 after executing
Example:
![Page 29: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/29.jpg)
Constants
Constant doesn’t change
Why use a variable if
massive changes later
show meaning
avoid Hard coding
public static final int MAX_PEOPLE = 20;
Capitalize by convention only
![Page 30: Variables Pepper. Variable A variable –box –holds a certain type of value –value inside the box can change Example –A = 2B+1 –Slope = change in y / change.](https://reader036.fdocuments.us/reader036/viewer/2022062516/56649d385503460f94a112bf/html5/thumbnails/30.jpg)
Summary
• A variable holds one value
• Variables have certain types
• Literals of different types are entered differently (i.e. quotes for String)
• The system keeps variable values until you change them.
• Constants are a special kind of variable – no changing