csci 144 introduction to computer
play

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


  1. 1 CSCI 144 - Introduction to Computer Science Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018

  2. https://www.cs.plu.edu/~caora Using The Scanner for Input About the questions website Website names? Like it or not? Change to google or top hat

  3. Conversions & Casting

  4. Data Conversions

  5. Widening Conversions 25 25 int byte 1289.0 1289 double int

  6. Narrowing Conversions 123456 123 byte int 123456.789 123456 double int

  7. SO WHAT? 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  “ 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. ” Source: http://www.vuw.ac.nz/staff/stephen_marshall/SE/Failures/SE_Ariane.html

  8. Conversions can occur in three ways Assignment conversion 1. Arithmetic conversion 2. Casting 3.

  9. Assignment conversion memory int x = 1234; double y; x 1234 y = x; y Only widening conversions can happen via assignment 1234.0 ?

  10. Arithmetic conversion 15.0 15 4.0 / int gets promoted to 3.5 a double Operands are always promoted to the “ wider ” type

  11. Casting memory int x; double y 12345.67; x x = (int) y; ? 12345 Type is put in parentheses in front of value being converted y 12345.67 avg = (double) sum / count; Common use is when we are dividing two ints but want a floating point result

  12. Casting Practice 1. (double) 10 / 4 2. (double) (125 / 10) + 1 3. (int) ((double) 10 / (double) 4 ) + 1.0 4. (double) (10 / 4)

  13. Casting Practice (double) 10 / 4 casting 10.0 / 4 arithmetic promotion 10.0 / 4.0 double division 2.5

  14. Casting Practice (double) (10 / 4) int division (double) 2 casting 2.0

  15. Casting Practice (int) ((double) 10 / (double) 4 ) + 1.0 casting (int) ( 10.0 / 4.0 ) + 1.0 double division (int) 2.5 + 1.0 casting 2 + 1.0 arithmetic promotion 2.0 + 1.0 double addition 3.0

  16. So what?

  17. So what? double gpa; int totalPoints = 114, totalHours = 30; gpa = totalPoints / totalHours; gpa = 114 / 30 integer division gpa = 3 assignment conversion 3.0 Note: 114.0 / 30.0 = 3.8 Peer Instruction questions

  18. Pair Practice

  19. practice…

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

  21. 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/

  22. Practice Exercise (10 mins pair and print out) 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 download CO2AirTravel.java from the csci144 blog page http://www.cs.plu.edu/144 -> Class blog Section 1&2

  23. Using the Scanner class to make programs more flexible

  24. To use the Scanner class: import the Scanner class 1. instantiate a Scanner object 2. prompt user for input value 3. scan user input into a variable 4.

  25. import java.util.Scanner; import 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 ” ); } }

  26. import java.util.Scanner; import 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); instantiate 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 ” ); } }

  27. import java.util.Scanner; import 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); instantiate System.out.print("Enter total miles driven: "); prompt 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 ” ); } }

  28. import java.util.Scanner; import 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); instantiate System.out.print("Enter total miles driven: "); prompt milesDriven = keyboard.nextInt(); scan 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 ” ); } }

  29. More Practice (5 mins in pairs) 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 Revise CO2AirTravel.java from the previous exercise

  30. More and more Practice 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 Revise CO2AirTravel.java from the previous exercise

  31. Check the website for announcement and lab1! Using The Scanner for Input

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend