<<

CS 144 - Introduction to

Instructor: Renzhi Cao Computer Science Department Pacific Lutheran University Spring 2019

1 Reading questions

• Top Hat website Q1. Which of the following are valid println statements?

• A. System.out.println + "Hello World"; • B. System.out.println("Have a nice dya"); • . out.System.println(value); • . println.out(Programming is great fun); • E. None of the above. Q2. Which of the following is NOT a primitive in Java?

• A. int • B. float • C. String • D. short • E. char 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. Correct Answers 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. Q2. Which of the following is NOT a primitive data type in Java?

• A. abstract • B. float C. String • D. short • E. char 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. Variables, Assignment & Types “play computer” // 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”); } } Variables

memory data type variable name 0x000 0x001 intint total=total; 10; 0x002 0x003 10? 0x004 intint countcount, = 5 ,temp, temp = result;2, result;

0x005 ?5

0x006 ?2 0x007 ? Assignment

memory int total = 10; 0x000 0x001 assignment operator 0x002 totaltotal == 5555.5; ; 0x003 1055.51055 55 expression on the 0x004 right is evaluated 0x005 value that was in result is stored in the total is overwritten 0x006 variable on the left variable can only hold 0x007 one value at a time variable must be consistent with the variable's declared type 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 output A heptagon has 7 sides. A decagon has 10 sides. Constants

final int MIN_HEIGHT = 69;

MIN_HEIGHT = 72; memory MIN_HEIGHT 69

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 Primitive Data Types 8 primitive data types in Java

short integers int long

float floating point numbers double char characters

boolean Boolean values Numeric Primitive Data

Type Storage Min Value Max Value byte 8 -128 127 short 16 bits -32,768 32,767 int 32 bits -2,147,483,648 2,147,483,647 long 64 bits < -9 x 1018 > 9 x 1018 float 38 double 32 bits +/- 3.4 x 10 with 7 significant digits 64 bits +/- 1.7 x 10308 with 15 significant digits Characters

• A char variable stores a single from the character • 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' 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; 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 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 Arithmetic Expressions

• An expression is a combination of one or more operands and their operators • Arithmetic expressions use the

operators: Addition + Subtraction - Multiplication * Division / Remainder % (no ^ operator) • If either or both operands associated with an arithmetic operator are floating point, the result is a floating point Division and Remainder

• If both operands to the division operator (/) are integers, the result is an integer (the fractional part is discarded)

14 / 3 equals? 4 8 / 12 equals? 0 • The remainder operator (%) returns the remainder after dividing the second operand into the first

14 % 3 equals? 2 8 % 12 equals? 8 PI – expression eval Division and Remainder Practice

1. 12 / 5 5. 11 % 3

2. 6 / 12 6. 8 % 4

3. 5 / 2 7. 29 % 5

4. 5 / 2.0 8. 3 % 7 Operator Precedence

• Multiplication, division, and remainder are evaluated prior to addition, subtraction, and string concatenation • Examples:

a + b + c + d + e a + b * c - d / e 1 2 3 4 3 1 4 2

a / (b + c) - d % e a / (b * (c + (d - e))) 2 1 4 3 4 3 2 1 Expression Practice

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

1. a – b + c – d 4. a - (b – ( c – d) – e)

2. a + b * c / d 5. a + (b – c) * d – e

3. a * b / c * d 6. (a + b * c) + (d + e) % f Online practices

Go to Top Hat website 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 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 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 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 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. 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. 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. 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. 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

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 2&3