Variables Assignment Types Notional Machine The notional machine
Variables, Assignment & Types
“Notional Machine” The notional machine is how to think about what the computer is doing.
// calculates pounds of C 02 emitted by a gasoline powered automobile public class CO 2 Calculator { public static void main(String[] args) { int miles. Driven = 360; double mpg = 24. 5; double gallons. Used, co 2 Used; double emissions. Factor = 19. 6; // calculate gallons of fuel used gallons. Used = miles. Driven/mpg; // calculate and display pounds of C 02 emitted co 2 Used = gallons. Used * emissions. Factor; System. out. println("You used " + co 2 Used + " pounds of C 02”); } }
Variables memory 0 x 000 0 x 001 variable name int total= total; 10; int 0 x 002 0 x 003 data type 10 ? 0 x 004 int count, = 5, temp, int temp = result; 2, result; 0 x 005 ? 5 0 x 006 ? 2 0 x 007 ?
Assignment memory 0 x 000 0 x 001 assignment operator 0 x 002 0 x 003 0 x 004 0 x 005 0 x 006 0 x 007 int total = 10; 10 55. 5 55 1055 total = 55; 55. 5; expression on the right is evaluated value that was in result is stored in the total is overwritten variable on the left variable can only hold one value at a time variable must be consistent with the variable's declared type
public class Geometry { // Prints the number of sides of several geometric shapes. public static void main (String[] args) { int sides = 7; // declaration with initialization System. out. println ("A heptagon has " + sides + " sides. "); sides = 10; // assignment statement System. out. println ("A decagon has " + sides + " sides. "); } } sides = 12; System. out. println ("A dodecagon has " + sides + " sides. "); memory output A heptagon has 7 sides. A decagon has 10 sides.
Constants final int MIN_HEIGHT = 69; MIN_HEIGHT = 72; memory MIN_HEIGHT 69 Uses for constants: – give names to otherwise unclear literal values – facilitate updates of values used throughout a program – prevent inadvertent attempts to change a value
Primitive Data Types 8 primitive data types in Java byte short int long integers float double char floating point numbers boolean Boolean values characters
Numeric Primitive Data Type Storage Min Value Max Value byte short int long 8 bits 16 bits 32 bits 64 bits -128 -32, 768 -2, 147, 483, 648 < -9 x 1018 127 32, 767 2, 147, 483, 647 > 9 x 1018 float double 32 bits 64 bits +/- 3. 4 x 1038 with 7 significant digits +/- 1. 7 x 10308 with 15 significant digits
Characters • A char variable stores a single character from the Unicode character set • A character set is an ordered list of characters, and each character corresponds to a unique number • The Unicode character set uses sixteen bits per character, allowing for 65, 536 unique characters • It is an international character set, containing symbols and characters from many world languages • Character literals are delimited by single quotes: 'a' 'X' '7' '$' ', ' 'n'
Boolean • A boolean value represents a true or false condition • A boolean also can be used to represent any two states, such as a light bulb being on or off • The reserved words true and false are the only valid values for a boolean type boolean done = false;
CAT: Categorization Grid Values 1. Current temperature in degrees Celsius 2. The population of lemmings 3. Your grade point average 4. A person's age in years 5. A person's weight in pounds 6. A person's height in meters 7. Miles traveled 8. Number of rainy days in the past month 9. A locker number 10. Number of seconds remaining in a game 11. The sum of a series of integers 12. The average of a series of integers Integer Floating-Point
CAT: Categorization Grid Values Integer 1. Current temperature in degrees Celsius 2. The population of lemmings 3. Your grade point average 4. A person's age in years 5. A person's weight in pounds 6. A person's height in meters 7. Miles traveled 8. Number of rainy days in the past month 9. A locker number 10. Number of seconds remaining in a game 11. The sum of a series of integers 12. The average of a series of integers Floating-Point PI - primitives
Arithmetic Expressions • An expression is a combination of one or more operands and their operators • Arithmetic expressions use the operators: Addition Subtraction Multiplication Division Remainder + * / % (no ^ operator) • If either or both operands associated with an arithmetic operator are floating point, the result is a floating point
Division and Remainder • If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded) 14 / 3 equals? 4 8 / 12 equals? 0 • The remainder operator (%) returns the remainder after dividing the second operand into the first 14 % 3 equals? 2 8 % 12 equals? 8 PI – expression eval
Division and Remainder Practice 1. 12 / 5 5. 11 % 3 2. 6 / 12 6. 8 % 4 3. 5 / 2 7. 29 % 5 4. 5 / 2. 0 8. 3 % 7
Operator Precedence • Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation • Examples: a + b + c + d + e 1 2 3 4 a + b * c - d / e 3 1 4 2 a / (b + c) - d % e 2 1 4 3 a / (b * (c + (d - e))) 4 3 2 1
Expression Practice Indicate the order in which the operators will be evaluated by writing a number beneath each operator: 1. a – b + c – d 4. a - (b – ( c – d) – e) 2. a + b * c / d 5. a + (b – c) * d – e 3. a * b / c * d 6. (a + b * c) + (d + e) % f
practice…
Carbon footprint for air travel Source: http: //www. climatecrisis. net/takeaction/carboncalculator/
Carbon footprint for air travel. 64 lbs/mile Emissions factors. 45 lbs/mile . 39 lbs/mile Source: http: //www. climatecrisis. net/takeaction/carboncalculator/
Practice Exercise Together we will complete a Java program to calculate carbon emissions for short and medium flights Algorithm assign the number of short flights taken (num. Short) assign the number of medium flights (num. Medium) calculate total carbon emissions (total. Emissions) num. Short * short. Dist * short. Emission + num. Medium * med. Dist * med. Emission display total carbon emissions download CO 2 Air. Travel. java from the csce 144 blog page
- Slides: 23