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 names? Like it or not? Change to google or top hat
Conversions & Casting
Data Conversions
Widening Conversions 25 25 int byte 1289.0 1289 double int
Narrowing Conversions 123456 123 byte int 123456.789 123456 double int
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
Conversions can occur in three ways Assignment conversion 1. Arithmetic conversion 2. Casting 3.
Assignment conversion memory int x = 1234; double y; x 1234 y = x; y Only widening conversions can happen via assignment 1234.0 ?
Arithmetic conversion 15.0 15 4.0 / int gets promoted to 3.5 a double Operands are always promoted to the “ wider ” type
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
Casting Practice 1. (double) 10 / 4 2. (double) (125 / 10) + 1 3. (int) ((double) 10 / (double) 4 ) + 1.0 4. (double) (10 / 4)
Casting Practice (double) 10 / 4 casting 10.0 / 4 arithmetic promotion 10.0 / 4.0 double division 2.5
Casting Practice (double) (10 / 4) int division (double) 2 casting 2.0
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
So what?
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
Pair Practice
practice…
Carbon footprint for air travel short 250 medium 800 long 2500 extended 5000 Source: http://www.climatecrisis.net/takeaction/carboncalculator/
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/
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
Using the Scanner class to make programs more flexible
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.
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 ” ); } }
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 ” ); } }
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 ” ); } }
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 ” ); } }
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
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
Check the website for announcement and lab1! Using The Scanner for Input
Recommend
More recommend