Java basics part 3 Where are we Last
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
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 weight. In. Pounds = 75. 5; // our person’s weight double height. In. Feet = 4. 5; // our person’s height // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " 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. next. Double(); System. out. print("Enter height (feet): "); double height = stdin. next. Double();
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. next. Double(); System. out. print("Enter height (feet): "); double height = stdin. next. Double(); // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person with"); System. out. println(" weight " + weight. In. Pounds + " lbs"); System. out. println(" height " + height. In. Feet + " 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. next. Double(); System. out. print("Enter height (feet): "); double height = stdin. next. Double(); // convert to metric equivalents double metric. Weight = weight. In. Pounds * KILOGRAMS_PER_POUND; double metric. Height = height. In. Feet * METERS_PER_FOOT; // perform bmi calculation double bmi = (int) (metric. Weight / (metric. Height * metric. Height)); // display result System. out. println("A person System. out. println(" weight System. out. println(" height System. out. printf("has a BMI } with"); " + weight. In. Pounds + " lbs"); " + height. In. Feet + " feet"); of %4. 1 f“, bmi);
Quick survey • I am ok with Scanner and its methods a. Pretty much b. With a little review, I’ll have it down c. Not really d. I’m so lost
Primitive variable assignment • Assignment operator = – Allows the memory location for a variable to be updated • Consider int number. Of. Rabbits = 11; number. Of. Rabbits = 121;
Primitive variable assignment • Assignment operator = – Allows the memory location for a variable to be updated • Consider int number. Of. Rabbits = 11; number. Of. Rabbits = 121;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider int a = 1; int a. Squared = a * a; a = 5; a. Squared = a * a; • Consider int i = 0; i = i + 1; • Consider int asa. Rating; asa. Rating = 400;
Primitive variable assignment • Consider double x = 5. 12; double y = 19. 28; double remember. X = x; x = y; y = remember. X;
Primitive variable assignment • Consider double x = 5. 12; double y = 19. 28; double remember. X = x; x = y; y = remember. X;
Primitive variable assignment • Consider double x = 5. 12; double y = 19. 28; double remember. X = x; x = y; y = remember. X;
Primitive variable assignment • Consider double x = 5. 12; double y = 19. 28; double remember. X = x; x = y; y = remember. X;
Primitive variable assignment • Consider double x = 5. 12; double y = 19. 28; double remember. X = x; x = y; y = remember. X;
Quick survey • If I wanted to assign variable n the value of variable m I would do a. n = m; b. m = n; c. int n = m; d. 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("Persont. Heightt. Shoe size"); System. out. println("============="); System. out. println("Hannaht 5‘ 1"t 7"); System. out. println("Jennat 5'10"t 9"); System. out. println("JJt 6'1"t 14"); • Output Person Height Shoe size ============= Hannah 5‘ 1" 7 Jenna 5'10" 9 JJ 6'1" 14
- Slides: 36