csci 144 introduction to
play

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. CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 20018 1

  2. Reading questions • Simple website: http://cs.plu.edu/~caora/ • Enter your session number, and select question number, also put your epass id. • Email me your suggestions for extra points

  3. Q1. Which of the following are valid println statements? • A. System.out.println + "Hello World"; • B. System.out.println("Have a nice dya"); • C. out.System.println(value); • D. println.out(Programming is great fun); • E. None of the above.

  4. Q2. Which of the following is NOT a primitive data type in Java? • A. int • B. float • C. String • D. short • E. char

  5. Q3. Given the following declarations, which of the assignment statements below are NOT legal? int x; double y; • A. x = 5 / 2; • B. y = 5 / 2; • C. x = 5 / 2.0; • D. y = 5 / 2.0; • E. All of the above statements are legal.

  6. Correct Answers

  7. Q1. Which of the following are valid println statements? • A. System.out.println + "Hello World"; B System.out.println("Have a nice dya"); • C. out.System.println(value); • D. println.out(Programming is great fun); • E. None of the above.

  8. Q2. Which of the following is NOT a primitive data type in Java? • A. abstract • B. float C. String • D. short • E. char

  9. Q3. Given the following declarations, which of the assignment statements below are NOT legal? int x; double y; • A. x = 5 / 2; • B. y = 5 / 2; C. x = 5 / 2.0; • D. y = 5 / 2.0; • E. All of the above statements are legal.

  10. Variables, Assignment & Types revised 09/09/2016

  11. “ play computer ”

  12. // 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 ” ); } }

  13. Variables memory data type variable name 0x000 0x001 int total; int total= 10; 0x002 ? 10 0x003 int count, temp, result; int count = 5, temp = 2, result; 0x004 ? 0x005 5 ? 0x006 2 ? 0x007

  14. Assignment memory int total = 10; 0x000 0x001 assignment operator 0x002 total = 55; total = 55.5; 10 55 55.5 10 55 0x003 expression on the right is evaluated 0x004 value that was in result is stored in the 0x005 total is overwritten variable on the left 0x006 variable can only hold one value at a time 0x007 variable must be consistent with the variable's declared type

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

  16. Constants final int MIN_HEIGHT = 69; MIN_HEIGHT = 72; memory 69 MIN_HEIGHT 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

  17. Primitive Data Types 8 primitive data types in Java byte short integers int long float floating point numbers double char characters boolean Boolean values

  18. Numeric Primitive Data Type St Storage ge Min Value Max Value byte 8 bits -128 128 127 127 short 16 bits -32,768 68 32,767 int 32 bits -2,147, 7,48 483,6 ,648 2,147,4 ,483 83,64 647 long < -9 x 10 18 18 x 10 18 18 64 bits < > 9 x 38 with 7 s float +/- 3.4 x 10 38 32 bits +/ signific ifican ant t digits 308 with 15 signific double +/- 1.7 x 10 308 64 bits +/ fican ant digits ts

  19. 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'

  20. 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;

  21. CAT: Categorization Grid 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

  22. CAT: Categorization Grid 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

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

  24. Division and Remainder • If both operands to the division operator ( / ) are integers, the result is an integer (the fractional part is discarded) equals? ls? 14 / 3 4 equals? ls? 8 / 12 0 • The remainder operator (%) returns the remainder after dividing the second operand into the first equals? ls? 14 % 3 2 equals? ls? 8 % 12 8 PI – expression eval

  25. Division and Remainder Practice 5. 11 % 3 1. 12 / 5 6. 8 % 4 2. 6 / 12 7. 29 % 5 3. 5 / 2 8. 3 % 7 4. 5 / 2.0

  26. Operator Precedence • Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation • Examples: a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2 a / (b + c) - d % e a / (b * (c + (d - e))) 2 1 4 3 4 3 2 1

  27. Expression Practice Indicate the order in which the operators will be evaluated by writing a number beneath each operator: 4. a - (b – ( c – d) – e) 1. a – b + c – d 5. a + (b – c) * d – e 2. a + b * c / d 6. (a + b * c) + (d + e) % f 3. a * b / c * d

  28. Online practices Go to website: http://cs.plu.edu/~caora/

  29. Q4. You see the following expression in some code that successfully compiles: n = 100000; What type can n not be? • A. int • B. short • C. float • D. double • E. long

  30. Q4*. You see the following expression in some code that successfully compiles: n = 100000; What type can n not be? • A. int • B. short • C. float • D. double • E. long

  31. Q6. You see the following expression in some code that successfully compiles: n = -15; What type can n not be? • A. int • B. double • C. short • D. char • E. long

  32. Q6*. You see the following expression in some code that successfully compiles: n = -15; What type can n not be? • A. int • B. double • C. short • D. char • E. long

  33. Q8. Given the code int x = 27; int y = 12; What is the value of the expression x/y? • A.2 • B. 2.25 • C. 3 • D. 3.0 • E. None of the above.

  34. Q8*. Given the code int x = 27; int y = 12; What is the value of the expression x/y? • A.2 • B. 2.25 • C. 3 • D. 3.0 • E. None of the above.

  35. Q10. Given the code int x = 27; int y = 12; What is the value of the expression x%y? • A. 2 • B. 2.25 • C. 3 • D. 3.0 • E. None of the above.

  36. Q10*. Given the code int x = 27; int y = 12; What is the value of the expression x%y? • A. 2 • B. 2.25 • C. 3 • D. 3.0 • E. None of the above.

  37. practice…

  38. Carbon footprint for air travel short 250 medium 800 long 2500 extended 5000 Source: http://www.climatecrisis.net/takeaction/carboncalculator/

  39. Carbon footprint for air travel .64 lbs/mile short 250 Emissions factors .45 lbs/mile medium 800 .39 lbs/mile long 2500 .39 lbs/mile extended 5000 Source: http://www.climatecrisis.net/takeaction/carboncalculator/

Recommend


More recommend