360 likes | 379 Views
Java basics – part 3. Where are we. Last time Constants and variables Initialization Simple expressions Casting Interactive programs Scanner This time Interactive programs Assignment Operators Assign programming assignment to compute windchill Discuss new lab.
E N D
Where are we • Last time • Constants and variables • Initialization • Simple expressions • Casting • Interactive programs • Scanner • This time • Interactive programs • Assignment • Operators • Assign programming assignment to compute windchill • Discuss new lab
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up person's characteristics double weightInPounds = 75.5; // our person’s weight double heightInFeet = 4.5; // our person’s height // convert to metric equivalents double metricWeight = weightInPounds * KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metricWeight/(metricHeight * metricHeight)); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + bmi); }
Making BMI general purpose • Change double weight = 75.5; double height = 4.5; To ????
Making BMI general purpose • Change double weight = 75.5; double height = 4.5; To Scanner stdin = new Scanner(System.in); System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble();
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up input acquistion Scanner stdin = new Scanner(System.in); // get person's characteristics System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble(); // convert to metric equivalents double metricWeight = weightInPounds * KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metricWeight / (metricHeight * metricHeight)); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.println("has a BMI of " + bmi); }
Can we display one place after the decimal? • Yes using a new out method named printf().
public static void main(String[] args) { // define constants final double KILOGRAMS_PER_POUND = 0.454; final double METERS_PER_FOOT = 0.3046; // set up input acquistion Scanner stdin = new Scanner(System.in); // get person's characteristics System.out.print("Enter weight (lbs): "); double weight = stdin.nextDouble(); System.out.print("Enter height (feet): "); double height = stdin.nextDouble(); // convert to metric equivalents double metricWeight = weightInPounds * KILOGRAMS_PER_POUND; double metricHeight = heightInFeet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metricWeight / (metricHeight * metricHeight)); // display result System.out.println("A person with"); System.out.println(" weight " + weightInPounds + " lbs"); System.out.println(" height " + heightInFeet + " feet"); System.out.printf("has a BMI of %4.1f“, bmi); }
Quick survey • I am ok with Scanner and its methods • Pretty much • With a little review, I’ll have it down • Not really • I’m so lost
Primitive variable assignment • Assignment operator = • Allows the memory location for a variable to be updated • Consider int numberOfRabbits = 11; numberOfRabbits = 121;
Primitive variable assignment • Assignment operator = • Allows the memory location for a variable to be updated • Consider int numberOfRabbits = 11; numberOfRabbits = 121;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; • Consider int i = 0; i = i + 1; • Consider int asaRating; asaRating = 400;
Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;
Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;
Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;
Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;
Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;
Quick survey • If I wanted to assign variable n the value of variable m I would do • n = m; • m = n; • int n = m; • int m = n;
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); System.out.println(i++); System.out.println(i);
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; // define ++i; System.out.println(i); System.out.print(++i); System.out.println(i++); System.out.println(i);
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; ++i; // increment System.out.println(i); System.out.print(++i); System.out.println(i++); System.out.println(i);
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; ++i; System.out.println(i); // display System.out.print(++i); System.out.println(i++); System.out.println(i); 5
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); // update then display System.out.println(i++); System.out.println(i); 5 6
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); System.out.println(i++); // update then display System.out.println(i); 5 6 6
Increment and decrement operators • ++ • Increments a number variable by 1 • -- • Decrements a numeric variable by 1 • Consider int i = 4; ++i; System.out.println(i); System.out.print(++i); System.out.println(i++); System.out.println(i); // display 5 6 6 7
Question • Does the following statement compute the average of double variables a, b, and c? Why double average = a + b + c / 3.0;
Expressions • What is the value used to initialize expression int expression = 4 + 2 * 5; • What value is displayed System.out.println(5 / 2.0); • Java rules in a nutshell • Each operator has a precedence level and an associativity • Operators with higher precedence are done first • * and / have higher precedence than + and - • Associativity indicates how to handle ties • When floating-point is used the result is floating point
Question • Does the following statement compute the average of double variables a, b, and c? Why double average = (a + b + c) / 3.0;
Escape sequences • Java provides escape sequences for printing special characters • \b backspace • \n newline • \t tab • \r carriage return • \\ backslash • \" double quote • \' single quote
Escape sequences • What do these statements output? System.out.println("Person\tHeight\tShoe size"); System.out.println("========================="); System.out.println("Hannah\t5‘1\"\t7"); System.out.println("Jenna\t5'10\"\t9"); System.out.println("JJ\t6'1\"\t14"); • Output Person Height Shoe size ========================= Hannah 5‘1" 7 Jenna 5'10" 9 JJ 6'1" 14