1 / 36

Java basics – part 3

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.

scoco
Download Presentation

Java basics – part 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Java basics – part 3

  2. 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

  3. 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); }

  4. Making BMI general purpose • Change double weight = 75.5; double height = 4.5; To ????

  5. 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();

  6. 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); }

  7. Can we display one place after the decimal? • Yes using a new out method named printf().

  8. 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); }

  9. 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

  10. Primitive variable assignment • Assignment operator = • Allows the memory location for a variable to be updated • Consider int numberOfRabbits = 11; numberOfRabbits = 121;

  11. Primitive variable assignment • Assignment operator = • Allows the memory location for a variable to be updated • Consider int numberOfRabbits = 11; numberOfRabbits = 121;

  12. 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;

  13. 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;

  14. 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;

  15. 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;

  16. 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;

  17. 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;

  18. 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;

  19. Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

  20. Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

  21. Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

  22. Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

  23. Primitive variable assignment • Consider double x = 5.12; double y = 19.28; double rememberX = x; x = y; y = rememberX;

  24. 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;

  25. 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);

  26. 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);

  27. 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);

  28. 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

  29. 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

  30. 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

  31. 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

  32. Question • Does the following statement compute the average of double variables a, b, and c? Why double average = a + b + c / 3.0;

  33. 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

  34. Question • Does the following statement compute the average of double variables a, b, and c? Why double average = (a + b + c) / 3.0;

  35. Escape sequences • Java provides escape sequences for printing special characters • \b backspace • \n newline • \t tab • \r carriage return • \\ backslash • \" double quote • \' single quote

  36. 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

More Related