CSCI 144 - Introduction to Computer Science
Instructor: John Goettsche Computer Science Department Pacific Lutheran University Spring 2018
1
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
25 25
byte int
1289 1289.0
int double
Widening Conversions
123456.789 123456
double int int
123456 123
byte
Narrowing Conversions
“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
1.
2.
3.
int x = 1234; double y; y = x;
1234
x
memory ?
y
1234.0 Only widening conversions can happen via assignment
/ 15 4.0 15.0 3.5
int gets promoted to a double
Operands are always promoted to the “wider” type
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
(double) 10 / 4 10.0 / 4 10.0 / 4.0 2.5
casting arithmetic promotion double division
(double) (10 / 4) (double) 2 2.0
int division casting
(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
double addition
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
Peer Instruction questions
5000 2500 800 250 extended long medium short
Source: http://www.climatecrisis.net/takeaction/carboncalculator/
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/
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
download CO2AirTravel.java from the csci144 blog page http://www.cs.plu.edu/144 -> Class blog Section 1&2
1.
2.
3.
4.
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”); } }
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”); } }
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”); } }
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”); } }
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
Revise the Java program to calculate carbon emissions so it can convert user’s floating number input to the correct int data
numShort * shortDist * shortEmission + numMedium * medDist * medEmission
display total carbon emissions
Revise CO2AirTravel.java from the previous exercise