Presentation is loading. Please wait.

Presentation is loading. Please wait.

Java basics – part 3.

Similar presentations


Presentation on theme: "Java basics – part 3."— 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 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 = ; // 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 = ; // 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 = ; // 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; Besides signaling in a variable definition that the variable is to be initialized, the symbol = is also the Java assignment operator. The assignment operator takes two operands. The left operand is the target variable; the right operand is the modifying expression. When evaluated, the assignment operator updates the value in the memory location associated with a target variable based on the modifying expression. The assignment operator works in the following manner. First, the left operand is evaluated to make sure that its value can be modified (e.g., a non-final variable). The right operand then is evaluated and its value is used to update the memory location associated with the left operand. For our particular assignment, the memory location associated with variable j is reset to the int value The assignment expression is read as “j gets 1985” or “j is assigned 1985.”

11 Primitive variable assignment
Assignment operator = Allows the memory location for a variable to be updated Consider int numberOfRabbits = 11; numberOfRabbits = 121; Besides signaling in a variable definition that the variable is to be initialized, the symbol = is also the Java assignment operator. The assignment operator takes two operands. The left operand is the target variable; the right operand is the modifying expression. When evaluated, the assignment operator updates the value in the memory location associated with a target variable based on the modifying expression. The assignment operator works in the following manner. First, the left operand is evaluated to make sure that its value can be modified (e.g., a non-final variable). The right operand then is evaluated and its value is used to update the memory location associated with the left operand. For our particular assignment, the memory location associated with variable j is reset to the int value The assignment expression is read as “j gets 1985” or “j is assigned 1985.”

12 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

13 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

14 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

15 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

16 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

17 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; int asaRating; asaRating = 400;

18 Primitive variable assignment
Consider int a = 1; int aSquared = a * a; a = 5; aSquared = a * a; int i = 0; i = i + 1; 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++);

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++);

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++);

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++); 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

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

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 = * 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 It seems that there are two possible values for expr. If the addition is performed first, then expr is initialized to 30; if instead, the multiplication is performed first, then expr is initialized to 14. N

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 '1" 14


Download ppt "Java basics – part 3."

Similar presentations


Ads by Google