Download presentation
Presentation is loading. Please wait.
Published byAugustus Sutton Modified over 8 years ago
1
Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.
2
Winter 2006CISC121 - Prof. McLeod2 Last Time Debugging, including the use of the debugger in Eclipse.
3
Winter 2006CISC121 - Prof. McLeod3 Today Proving Correctness. –Assertions and Invariants (If we have time, and just to demonstrate how disjointed a lecture can become – ArrayList ’s. If we don’t get to them in class, please read over these few slides.)
4
Winter 2006CISC121 - Prof. McLeod4 Another logic tool that you can use to prove that your program is “correct” is the use of assertions. Special assertions are called: –Preconditions, –Postconditions & –Invariants. Essentially, an assertion is just a statement that describes a condition that should be true at a given point in your program. –An assertion could just be a comment. –Java provides the use of the “ assert ” command, that can do something useful in the event that the condition turns out to be false. Program Correctness
5
Winter 2006CISC121 - Prof. McLeod5 Pre- & Post- Conditions These assertions are made at the beginning of a method (pre-condition) and at the end of the method (post- condition). A precondition: –Specifies what is true before the method executes. –Describes the assumptions under which the method runs. –Satisfying the preconditions is the responsibility of the calling method (or “user”). A postcondition: –Specifies what is true after the method executes. –Describes the effects of the code. –Satisfying the postconditions is the responsibility of the person coding the method.
6
Winter 2006CISC121 - Prof. McLeod6 Pre- & Post- Conditions - Cont. These conditions form part of the specifications of the method. A piece of code is said to be correct if the postconditions will be satisfied for all possible states of the preconditions. It is not always possible to completely prove correctness by this definition. –Sometimes all you can say is that the postconditions will be satisfied whenever the method terminates normally. –This is called partial correctness.
7
Winter 2006CISC121 - Prof. McLeod7 Pre- & Post- Conditions - Cont. For example, a little method to see if a number is prime: public static boolean isPrime (int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } // end is Prime method
8
Winter 2006CISC121 - Prof. McLeod8 Pre- & Post- Conditions - Cont. Add pre- and post- conditions: // pre: n is >= 2 public static boolean isPrime (int n) { for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } // end is Prime method // post: true if n is prime, false // otherwise
9
Winter 2006CISC121 - Prof. McLeod9 Invariants Invariants are assertions that are placed at strategic positions within the code. Invariants state what is true at this position. If the program logic is correct, then one insertion should lead from the one before and the last insertion should prove the post-condition to be true. Next slide has our little method with invariants put in as comments:
10
Winter 2006CISC121 - Prof. McLeod10 // pre: n is >= 2 public static boolean isPrime (int n) { for (int i = 2; i * i <= n; i++) { // inv1: n has no factors between 2 and i-1 if (n % i == 0) { // inv2: i divides n evenly, therefore // n is not prime return false; } // inv3: n has no factors between 2 and i } // end for loop // inv4: n has no factors between 2 and // n, therefore n is prime return true; } // end is Prime method // post: true if n is prime, false otherwise
11
Winter 2006CISC121 - Prof. McLeod11 Invariants - Cont. Note how the invariants below depend on the invariants above. The invariant above must be true in order to allow the invariant below to be true. Also, the invariants above the return statements, inv3 & inv4, when combined, prove the post- condition to be true. So, if you can chain together the invariants between the pre- and post- conditions, and thus prove that the post condition is true, then you have proved the “correctness” of your program.
12
Winter 2006CISC121 - Prof. McLeod12 Invariants - Cont. In essense, the process is all about forming a contract between the pre- and post- conditions. Having proved the “correctness” of your program, you are now ready to subject your program to a thorough testing with real values. Since you can not test every possible input value, your proof of correctness along with some testing must be sufficient to give you confidence that your method works.
13
Winter 2006CISC121 - Prof. McLeod13 Aside – Loop Invariants “ inv1 ” is called a “loop invariant”, because it is inside the loop. For this kind of invariant: –Make sure it is true when the loop starts. –Make sure it stays true each time through the loop. –Does the invariant lead to the desired condition when the loop terminates? –Make sure the loop will terminate!
14
Winter 2006CISC121 - Prof. McLeod14 Program Correctness - Cont. For the example above, you could have walked through the logic in your head to prove that it works. Was all this fancy stuff necessary? The use of assertions has two purposes: –It can help to provide documentation of your code: Pre- and Post- conditions are an important part of the documented specifications of your code. –More complex methods would be difficult to “walk through” without some kind of commenting to indicate the progression of the logic.
15
Winter 2006CISC121 - Prof. McLeod15 Invariants – Triangle Example Remember the example of the triangles? It contains a logic error, but no syntax errors – it compiles and runs fine. It took many random test cases to determine that there was a logic error. Here is the code with assertions (squished…):
16
Winter 2006CISC121 - Prof. McLeod16 // pre: a,b,c > 0 and form a triangle public static String testTriangle (int a, int b, int c){ if (a == b) { if (b == c) { return "equilateral";} //inv1: a=b, b=c, a=c else { return "isosceles";} //inv2: a=b, b c, a c } else { if (b == c) { return "isoscles";} //inv3: a b, b=c, a c else { return "scalene";} //inv4: a b, b c, a c or a=c } } //end testTriangle // post: all possibilities have been tested Not so!
17
Winter 2006CISC121 - Prof. McLeod17 Invariants – Triangle Example – Cont. Now it is easier to see that not all possibilities have been tested. A situation can be reached where: a b, b c, but a=c is unknown. So there is a break in the logic between inv4 and the post condition.
18
Winter 2006CISC121 - Prof. McLeod18 Program Correctness - Cont. In Java versions > 1.4, assertions can be put in the code using the “ assert ” command, where they can actually do something useful. –Use “ assert ” when you are having problems debugging a particular piece of code. –You can use a compilation switch to tell the compiler to ignore all assertions when it does the final compile on your code – this way you don’t have to take them out of the source code! –(Available in JBuilder 9 and 2005, Eclipse 3.1)
19
Winter 2006CISC121 - Prof. McLeod19 The Java assert Keyword Syntax: assert expression1; assert expression1: expression2; expression1 must evaluate to a boolean result. For the second syntax, expression2 provides a String for the AssertionError that will be thrown when expression1 evaluates to false.
20
Winter 2006CISC121 - Prof. McLeod20 Using Assertions in Code - Cont. By default, code is compiled with the assert command being treated as a comment - so they do not have to be removed when not needed. You have to tell the compiler to “enable assertions” for them to work. Make sure that your assert statement is passive. It should not “do anything” with variable contents or change how the program runs. –In this way, ignoring the assertions should not affect the program.
21
Winter 2006CISC121 - Prof. McLeod21 Using assert, Cont. How is using assert different from just throwing an exception directly? –It is easier to do, and the invoking method does not have to worry about catching an exception. –You have the option of compiling and running your program in such a way that assertions can be ignored, speeding up the execution of your code. During program development, enable assertions. Once done, disable them. You can leave all your assert statements in your source code!
22
Winter 2006CISC121 - Prof. McLeod22 Aside – Using Assertions in Eclipse To add the command line parameter: “-ea” for “enable assertions” –Go to “Run” on the menu bar, then choose “Run…”. –In the window that opens, choose the “Arguments” tab and enter “-ea” (without the quotes) in the “VM arguments” box. This parameter gets reset when you exit Eclipse. Without this setting, Eclipse will treat assert commands just like comments (it ignores them!).
23
Winter 2006CISC121 - Prof. McLeod23 Best Reference on Assertions http://java.sun.com/j2se/1.5.0/docs/guide/languag e/assert.html
24
Winter 2006CISC121 - Prof. McLeod24 ArrayList Class The size of an array always has to be declared before it can be used. Once an array has been given a size, it cannot be changed (in Java). (How would you re-size an array to make them more “dynamic”?) An ArrayList is another kind of data structure - a Collection, that does not need to be sized first.
25
Winter 2006CISC121 - Prof. McLeod25 ArrayList Class, Cont. It resides in java.util, so you will need: import java.util.ArrayList; Syntax to create an ArrayList in Java 5.0: ArrayList list_name = new ArrayList (); “ type ” must be an Object, it cannot be a primitive type.
26
Winter 2006CISC121 - Prof. McLeod26 ArrayList Class, Cont. Note - no size declaration! For example, to create an ArrayList called “myList” to hold Double’s: ArrayList myList = new ArrayList (); ArrayList itself is a “generic” type Object in Java. You must use the to give the ArrayList a type.
27
Winter 2006CISC121 - Prof. McLeod27 ArrayList Class, Cont. To add elements to the collection, for example: myList.add(new Double(456.78)); To get the size of the collection, invoke the size() method: int myListSize = myList.size();
28
Winter 2006CISC121 - Prof. McLeod28 ArrayList Class, Cont. The get(index) method returns the Object at the given index. Note that index positions are numbered from zero (of course!). The set(position, new_value) method changes the Object at the given position. To insert an element, invoke add(position, new_value). All elements are moved up, and the new value is added at the given position. The method, remove(position) removes the element at the provided position.
29
Winter 2006CISC121 - Prof. McLeod29 ArrayList Class, Cont. Once you have accessed an element of an ArrayList, you do not have to cast it back to the type of the List, it is already of the list type. For example, to get the number back out of the first element in myList: double aVal = myList.get(0).doubleValue(); (Before Java 5.0 the element would have to have been cast to be of type Double, first.)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.