Presentation is loading. Please wait.

Presentation is loading. Please wait.

Advanced Arithmetic, Conditionals, and Loops INFSY 535.

Similar presentations


Presentation on theme: "Advanced Arithmetic, Conditionals, and Loops INFSY 535."— Presentation transcript:

1 Advanced Arithmetic, Conditionals, and Loops INFSY 535

2 The Math Class Examples (each returns a number of type double) Math.sqrt (double); Math.pow((double),double power); Math.random (); And more!

3 Java Math Class Math also defines two symbolic constants: E = 2.71... PI = 3.14... Methods in Math are not instantiated? methods are static !

4 static Class Methods static methods are invoked on behalf of an entire class, not on a specific instance of class. Typically each instance of a class has different copies of instance variables No instance variables allowed to be used for access In Math class, methods are declared: public static double sin(double a) {... }

5 When calling static method: do not instantiate class, send message to class _number = (int)Math.round(x); Double expressionIntegerCasting

6 public class ContrivedExample { private static int _numberOfInstances; public ContrivedExample() { // we want to keep a tally of all the // ContrivedExamples that we create _numberOfInstances++; } // now every instance of ContrivedExample // can access _numberOfInstances to see how // many contrived examples have been // created }

7 static Class Methods Other examples: (int)(math.random () * (high-low)) _randomNumber= (int)(Math.random() * (limit)) +1;

8 Mixing Integers and Fractional #s public class MyMathClass { public void reassign() { int myInt = 4; double myDouble = 2.64; myInt = myDouble; // can’t assign a double to an int } } Change above assignment to: myDouble = myInt; May use different types of numbers together, but be careful! Go from less to more precise, not the other way around!

9 Mixing Integers and Fractional #s We can force Java to convert double to int called casting or coercion loss of precision myInt = (int) myDouble; // myInt is now 2

10 Casting: general form of conversion What is Casting? Format: (data type) expression; Example: int money; ans=(float) money;

11 Arithmetic Conversion Advice! Do not assign a floating point expression to an int Convert the float to an int through casting: int (varName); Rounding in conversion: int x = int (varName +.5);

12 Using Interfaces with Constants interfaces to declare methods interfaces to declare constants What if many classes use same constants? Can put constants in an interface and have classes implement it! then public, static, and final are implicit

13 public interface PhysicsConstants { // speed of light (hundred-million meters // per second double LIGHT_SPEED = 2.998; // more constants, if we want... } Now all classes that implement the PhysicsConstants interface have access to LIGHT_SPEED

14 Complex Choices: Nested if statements Each Expression is evaluated in sequence, until some Expression is found that is true Only the specific Statement following that particular true Expression is executed If no Expression is true, the statement following the final else is executed

15 Example: Nested if Statement if (weight >= 30) { if (item == ‘L’) { System.out.println(“You have a heavy load of laundry.”); } else { System.out.println(“ It is heavy, but not laundry”); } else { System.out.println(“ It is not that heavy.”); }

16 if (weight >= 30) { if (item == ‘L’) { System.out.println(“You have a heavy load of laundry.”); } else { System.out.println(“ It is heavy, but not laundry”); } else { System.out.println(“ It is not that heavy.”); } Given: weight=30; item=“M” What is printed on the console?

17 Nested Statements Always line up if with corresponding else Compiler always matches else with nearest previous if Indentation will not make a difference except for logical understanding of code! Braces/brackets work like parentheses in logical expressions; forces statement to be evaluated the way you want

18 Looping

19  while loop  for loop

20 LOOP Statement... false true WHILE Condition Condition

21 Iteration Loop: A portion of a program that repeats itself a number of times Body: Repeated group of statements Iteration: Each repetition

22 boolean variable or expression: boolean expression: evaluates to true when it is satisfied evaluates to false when it is not satisfied returns an integer value; 0 represents false and any non-zero value represents true

23 When the expression is tested and found to be false, the loop is exited and control passes to the statement that follows the loop body false true body statement Boolean Expression While loop

24 While revisited: sentinel controlled loop final int LIMIT = 10; int count = 1; while (count <= limit) { System.out.println (count); count + +; } Control variable Changed as last Statement Use of increment Will be true or false

25 25 Infinite Loops The body of a while loop eventually must make the condition false If not, it is an infinite loop, i.e. will not end Always double check to ensure that your loops will terminate normally

26 Nested Loops Similar to nested if statements The body of the loop contains another loop Each time through the outer loop, the inner loop goes through its full set of iterations

27 Nested loop design Example X=6; while (x >=4) { y = 3; while (y <=6) { y++; System.out.println(y + “ “ + x); } x--; } What will be printed on the console?

28 The for Statement A for loop is functionally equivalent to the following while loop structure: initialization; while ( condition ) { statement; increment; } for (init exp; test; increment) { statement(s); }

29 For Loop Continued for ( ; ; ; ;<increment/decrement) { stmt(s) stmt(s) } n Initializing expression, is executed n Expression evaluated for true/false; terminates if false n Otherwise statements are executed n After execution, Increment/decrement is executed

30 Iteration: for loop example for (init exp; test; increment) { statement(s); } sum=1; for (int i = 10;i > 0; i- -) { sum=sum + i }

31 Choosing a Loop Structure When you can’t determine how many times you want to execute the loop body, use a while statement If you can determine how many times you want to execute the loop body, use a for statement


Download ppt "Advanced Arithmetic, Conditionals, and Loops INFSY 535."

Similar presentations


Ads by Google