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

csci 144 introduction to computer
SMART_READER_LITE
LIVE PREVIEW

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

1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018 https://www.cs.plu.edu/~caora Using The Scanner for Input About the questions website Website


slide-1
SLIDE 1

CSCI 144 - Introduction to Computer Science

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

1

slide-2
SLIDE 2

Using The Scanner for Input

https://www.cs.plu.edu/~caora

About the questions website Website names? Like it or not? Change to google or top hat

slide-3
SLIDE 3

Conversions & Casting

slide-4
SLIDE 4

Data Conversions

slide-5
SLIDE 5

25 25

byte int

1289 1289.0

int double

Widening Conversions

slide-6
SLIDE 6

123456.789 123456

double int int

123456 123

byte

Narrowing Conversions

slide-7
SLIDE 7

SO WHAT?

“The failure occurred because the horizontal velocity exceeded the maximum value for a 16 bit unsigned integer when it was converted from it's signed 64 bit representation.”

Ariane 5 was a $500 million unmanned space craft designed by the European Space Agency

On June 4, 1996 it was launched for the first time

Here’s what happened

Source: http://www.vuw.ac.nz/staff/stephen_marshall/SE/Failures/SE_Ariane.html

slide-8
SLIDE 8

Conversions can occur in three ways

1.

Assignment conversion

2.

Arithmetic conversion

3.

Casting

slide-9
SLIDE 9

Assignment conversion

int x = 1234; double y; y = x;

1234

x

memory ?

y

1234.0 Only widening conversions can happen via assignment

slide-10
SLIDE 10

Arithmetic conversion

/ 15 4.0 15.0 3.5

int gets promoted to a double

Operands are always promoted to the “wider” type

slide-11
SLIDE 11

Casting

int x; double y 12345.67; x = (int) y;

?

x

memory 12345.67

y

12345

Type is put in parentheses in front of value being converted

avg = (double) sum / count;

Common use is when we are dividing two ints but want a floating point result

slide-12
SLIDE 12

Casting Practice

  • 1. (double) 10 / 4
  • 2. (double) (125 / 10) + 1
  • 3. (int) ((double) 10 / (double) 4 ) + 1.0
  • 4. (double) (10 / 4)
slide-13
SLIDE 13

Casting Practice

(double) 10 / 4 10.0 / 4 10.0 / 4.0 2.5

casting arithmetic promotion double division

slide-14
SLIDE 14

Casting Practice

(double) (10 / 4) (double) 2 2.0

int division casting

slide-15
SLIDE 15

Casting Practice

(int) ((double) 10 / (double) 4 ) + 1.0 (int) ( 10.0 / 4.0 ) + 1.0 (int) 2.5 + 1.0 2 + 1.0

casting double division casting

2.0 + 1.0

arithmetic promotion

3.0

double addition

slide-16
SLIDE 16

So what?

slide-17
SLIDE 17

double gpa; int totalPoints = 114, totalHours = 30; gpa = totalPoints / totalHours; gpa = 114 / 30

integer division

gpa = 3

assignment conversion

Note: 114.0 / 30.0 = 3.8 3.0

So what?

Peer Instruction questions

slide-18
SLIDE 18

Pair Practice

slide-19
SLIDE 19

practice…

slide-20
SLIDE 20

Carbon footprint for air travel

5000 2500 800 250 extended long medium short

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

slide-21
SLIDE 21

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

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 (10 mins pair and print out)

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

slide-23
SLIDE 23

Using the Scanner class to make programs more flexible

slide-24
SLIDE 24

1.

import the Scanner class

2.

instantiate a Scanner object

3.

prompt user for input value

4.

scan user input into a variable

To use the Scanner class:

slide-25
SLIDE 25

import

import java.util.Scanner; public class CO2Calculator2 { public static void main(String[] args) { int milesDriven; double mpg; double gallonsUsed, co2Used; double emissionsFactor = 19.6; Scanner keyboard = new Scanner(System.in); System.out.print("Enter total miles driven: "); milesDriven = keyboard.nextInt(); System.out.print("Enter miles per gallon: "); mpg = keyboard.nextDouble(); gallonsUsed = milesDriven/mpg; co2Used = gallonsUsed * emissionsFactor; System.out.println("You used " + co2Used + " pounds of C02”); } }

slide-26
SLIDE 26

instantiate import

import java.util.Scanner; public class CO2Calculator2 { public static void main(String[] args) { int milesDriven; double mpg; double gallonsUsed, co2Used; double emissionsFactor = 19.6; Scanner keyboard = new Scanner(System.in); System.out.print("Enter total miles driven: "); milesDriven = keyboard.nextInt(); System.out.print("Enter miles per gallon: "); mpg = keyboard.nextDouble(); gallonsUsed = milesDriven/mpg; co2Used = gallonsUsed * emissionsFactor; System.out.println("You used " + co2Used + " pounds of C02”); } }

slide-27
SLIDE 27

prompt instantiate import

import java.util.Scanner; public class CO2Calculator2 { public static void main(String[] args) { int milesDriven; double mpg; double gallonsUsed, co2Used; double emissionsFactor = 19.6; Scanner keyboard = new Scanner(System.in); System.out.print("Enter total miles driven: "); milesDriven = keyboard.nextInt(); System.out.print("Enter miles per gallon: "); mpg = keyboard.nextDouble(); gallonsUsed = milesDriven/mpg; co2Used = gallonsUsed * emissionsFactor; System.out.println("You used " + co2Used + " pounds of C02”); } }

slide-28
SLIDE 28

scan prompt instantiate import

import java.util.Scanner; public class CO2Calculator2 { public static void main(String[] args) { int milesDriven; double mpg; double gallonsUsed, co2Used; double emissionsFactor = 19.6; Scanner keyboard = new Scanner(System.in); System.out.print("Enter total miles driven: "); milesDriven = keyboard.nextInt(); System.out.print("Enter miles per gallon: "); mpg = keyboard.nextDouble(); gallonsUsed = milesDriven/mpg; co2Used = gallonsUsed * emissionsFactor; System.out.println("You used " + co2Used + " pounds of C02”); } }

slide-29
SLIDE 29

Revise the Java program to calculate carbon emissions so it asks the user for the number of short and medium flights Revised Algorithm ask user for number of short flights taken scan number of short flights (numShort) ask user for number of medium flights taken scan number of medium flights (numMedium) calculate total carbon emissions (totalEmissions)

numShort * shortDist * shortEmission + numMedium * medDist * medEmission

display total carbon emissions

More Practice (5 mins in pairs)

Revise CO2AirTravel.java from the previous exercise

slide-30
SLIDE 30

Revise the Java program to calculate carbon emissions so it can convert user’s floating number input to the correct int data

  • type. Print it out with both your names and submit the paper to

me. Revised Algorithm ask user for number of short flights taken scan number of short flights (numShort) Convert user’s float input to int type ask user for number of medium flights taken scan number of medium flights (numMedium) calculate total carbon emissions (totalEmissions)

numShort * shortDist * shortEmission + numMedium * medDist * medEmission

display total carbon emissions

More and more Practice

Revise CO2AirTravel.java from the previous exercise

slide-31
SLIDE 31

Using The Scanner for Input

Check the website for announcement and lab1!