CSCI 144 - Introduction to Computer Science Instructor: John - - PowerPoint PPT Presentation

csci 144 introduction to
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

CSCI 144 - Introduction to Computer Science

Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 20018

1

slide-2
SLIDE 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
slide-3
SLIDE 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.
slide-4
SLIDE 4
  • Q2. Which of the following is

NOT a primitive data type in Java?

  • A. int
  • B. float
  • C. String
  • D. short
  • E. char
slide-5
SLIDE 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.
slide-6
SLIDE 6

Correct Answers

slide-7
SLIDE 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.
slide-8
SLIDE 8
  • Q2. Which of the following is

NOT a primitive data type in Java?

  • A. abstract
  • B. float
  • C. String
  • D. short
  • E. char
slide-9
SLIDE 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.
slide-10
SLIDE 10

Variables, Assignment & Types

revised 09/09/2016

slide-11
SLIDE 11

“play computer”

slide-12
SLIDE 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”); } }

slide-13
SLIDE 13

Variables

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

slide-14
SLIDE 14

Assignment

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

  • ne value at a time

55.5

total = 55.5;

slide-15
SLIDE 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

  • utput

A heptagon has 7 sides. A decagon has 10 sides.

slide-16
SLIDE 16

Constants

final int MIN_HEIGHT = 69; MIN_HEIGHT memory

69

MIN_HEIGHT = 72;

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

slide-17
SLIDE 17

Primitive Data Types

8 primitive data types in Java

byte short int long

integers

float double

floating point numbers

char

characters

boolean

Boolean values

slide-18
SLIDE 18

Numeric Primitive Data

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

128

  • 32,768

68

  • 2,147,

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

slide-19
SLIDE 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'

slide-20
SLIDE 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;

slide-21
SLIDE 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

slide-22
SLIDE 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

slide-23
SLIDE 23

Arithmetic Expressions

  • An expression is a combination of one
  • r more operands and their operators
  • Arithmetic expressions use the
  • perators:
  • If either or both operands associated

with an arithmetic operator are floating point, the result is a floating point

Addition + Subtraction

  • Multiplication

* Division / Remainder %

(no ^ operator)

slide-24
SLIDE 24

Division and Remainder

  • If both operands to the division operator

(/) are integers, the result is an integer (the fractional part is discarded)

  • The remainder operator (%) returns the

remainder after dividing the second

  • perand into the first

14 / 3 equals? ls? 8 / 12 equals? ls? 4 14 % 3 equals? ls? 8 % 12 equals? ls? 2 8

PI – expression eval

slide-25
SLIDE 25

Division and Remainder Practice

  • 1. 12 / 5
  • 2. 6 / 12
  • 3. 5 / 2
  • 4. 5 / 2.0
  • 5. 11 % 3
  • 6. 8 % 4
  • 7. 29 % 5
  • 8. 3 % 7
slide-26
SLIDE 26

Operator Precedence

  • Multiplication, division, and remainder are evaluated

prior to addition, subtraction, and string concatenation

  • Examples:

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

slide-27
SLIDE 27

Expression Practice

Indicate the order in which the operators will be evaluated by writing a number beneath each operator:

  • 1. a – b + c – d
  • 2. a + b * c / d
  • 3. a * b / c * d
  • 4. a - (b – ( c – d) – e)
  • 5. a + (b – c) * d – e
  • 6. (a + b * c) + (d + e) % f
slide-28
SLIDE 28

Online practices

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

slide-29
SLIDE 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
slide-30
SLIDE 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
slide-31
SLIDE 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
slide-32
SLIDE 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
slide-33
SLIDE 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.
slide-34
SLIDE 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.
slide-35
SLIDE 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.
slide-36
SLIDE 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.
slide-37
SLIDE 37

practice…

slide-38
SLIDE 38

Carbon footprint for air travel

5000 2500 800 250 extended long medium short

Source: http://www.climatecrisis.net/takeaction/carboncalculator/

slide-39
SLIDE 39

Carbon footprint for air travel

5000 2500 800 250 extended long medium short

Emissions factors

.64 lbs/mile .45 lbs/mile .39 lbs/mile .39 lbs/mile Source: http://www.climatecrisis.net/takeaction/carboncalculator/

slide-40
SLIDE 40

Together we will complete a Java program to calculate carbon emissions for short and medium flights Algorithm assign the number of short flights taken (numShort) assign the number of medium flights (numMedium) calculate total carbon emissions (totalEmissions)

numShort * shortDist * shortEmission + numMedium * medDist * medEmission

display total carbon emissions

Practice Exercise

download CO2AirTravel.java from the csci144 blog page http://www.cs.plu.edu/144 -> Class blog Section 1&2

slide-41
SLIDE 41