CSCI 144 - Introduction to Computer Science
Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 20018
1
CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation
CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 20018 1 Reading questions Simple website: http://cs.plu.edu/~caora/ Enter your session number, and
1
// calculates pounds of C02 emitted by a gasoline powered automobile public class CO2Calculator { public static void main(String[] args) { int milesDriven = 360; double mpg = 24.5; double gallonsUsed, co2Used; double emissionsFactor = 19.6; // calculate gallons of fuel used gallonsUsed = milesDriven/mpg; // calculate and display pounds of C02 emitted co2Used = gallonsUsed * emissionsFactor; System.out.println("You used " + co2Used + " pounds of C02”); } }
int total; int count, temp, result;
data type variable name
0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007
memory ? ? ? ?
int total= 10;
10
int count = 5, temp = 2, result;
5 2
total = 55;
assignment operator expression on the right is evaluated value that was in total is overwritten variable must be consistent with the variable's declared type 0x000 0x001 0x002 0x003 0x004 0x005 0x006 0x007
memory
10
int total = 10;
result is stored in the variable on the left 55 10 55 variable can only hold
55.5
total = 55.5;
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
A heptagon has 7 sides. A decagon has 10 sides.
final int MIN_HEIGHT = 69; MIN_HEIGHT memory
69
MIN_HEIGHT = 72;
– give names to otherwise unclear literal values – facilitate updates of values used throughout a program – prevent inadvertent attempts to change a value
byte short int long
integers
float double
floating point numbers
char
characters
boolean
Boolean values
Type byte short int long float double St Storage ge 8 bits 16 bits 32 bits 64 bits 32 bits 64 bits Min Value
128
68
7,48 483,6 ,648 < < -9 x 1018
18
+/ +/- 3.4 x 1038
38 with 7 s
signific ifican ant t digits +/ +/- 1.7 x 10308
308 with 15 signific
fican ant digits ts Max Value 127 127 32,767 2,147,4 ,483 83,64 647 > 9 x x 1018
18
character set
character corresponds to a unique number
allowing for 65,536 unique characters
characters from many world languages
'a' 'X' '7' '$' ',' '\n'
boolean done = false;
Values Integer Floating-Point 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
Values Integer Floating-Point 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
PI - primitives
Addition + Subtraction
* Division / Remainder %
(no ^ operator)
14 / 3 equals? ls? 8 / 12 equals? ls? 4 14 % 3 equals? ls? 8 % 12 equals? ls? 2 8
PI – expression eval
a + b + c + d + e 1 4 3 2 a + b * c - d / e 3 2 4 1 a / (b + c) - d % e 2 3 4 1 a / (b * (c + (d - e))) 4 1 2 3
Indicate the order in which the operators will be evaluated by writing a number beneath each operator:
5000 2500 800 250 extended long medium short
Source: http://www.climatecrisis.net/takeaction/carboncalculator/
5000 2500 800 250 extended long medium short
.64 lbs/mile .45 lbs/mile .39 lbs/mile .39 lbs/mile Source: http://www.climatecrisis.net/takeaction/carboncalculator/
numShort * shortDist * shortEmission + numMedium * medDist * medEmission
download CO2AirTravel.java from the csci144 blog page http://www.cs.plu.edu/144 -> Class blog Section 1&2