Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.

Slides:



Advertisements
Similar presentations
Programming with Alice Computing Institute for K-12 Teachers Summer 2011 Workshop.
Advertisements

11-Jun-14 The assert statement. 2 About the assert statement The purpose of the assert statement is to give you a way to catch program errors early The.
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
8-May-15 Additional control structures. 2 The if-else statement The if-else statement chooses which of two statements to execute The if-else statement.
Information Hiding and Encapsulation
Apply Sub Procedures/Methods and User Defined Functions
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.
The Java Programming Language
COP-3330: Object Oriented Programming Flow Control May 16, 2012 Eng. Hector M Lugo-Cordero, MS.
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Pre- and postconditions, Using assertions and exceptions 1 Pre- and postconditions Using assertions and exceptions.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
Exceptions and Assertions Chapter 15 – CSCI 1302.
Summer 2007CISC121 - Prof. McLeod1 CISC121 – Lecture 6 Last time: –Encapsulation –Javadoc documentation –Testing and Debugging.
Today… Python Keywords. Iteration (or “Loops”!) Winter 2016CISC101 - Prof. McLeod1.
Today… Preparation for doing Assignment 1. Invoking methods overview. Conditionals and Loops. Winter 2016CMPE212 - Prof. McLeod1.
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 next week. See next slide. Both versions of assignment 3 are posted. Due today.
Quiz 3 Topics Functions – using and writing. Lists: –operators used with lists. –keywords used with lists. –BIF’s used with lists. –list methods. Loops.
Winter 2006CISC121 - Prof. McLeod1 Last Time Reviewed class structure: –attributes –methods –(inner classes) Looked at the effects of the modifiers: –public.
Quiz 1 A sample quiz 1 is linked to the grading page on the course web site. Everything up to and including this Friday’s lecture except that conditionals.
Today… StringTokenizer class. Method Overloading. Catching Exceptions (and what they are!). Start Pointers and Aliasing. Winter 2016CMPE212 - Prof. McLeod1.
1 Problem Solving  The purpose of writing a program is to solve a problem  The general steps in problem solving are: Understand the problem Dissect the.
CSE 374 Programming Concepts & Tools Hal Perkins Fall 2015 Lecture 17 – Specifications, error checking & assert.
Chapter 3 of Programming Languages by Ravi Sethi
Last Time Unary operators: Other Assignment operators
Working with Java.
Chapter 6 CS 3370 – C++ Functions.
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Logger, Assert and Invariants
Topics: jGRASP editor ideosyncrasies assert debugger.
CSE 374 Programming Concepts & Tools
Loop Structures.
Prof: Dr. Shu-Ching Chen TA: Samira Pouyanfar Hector Cen Fall 2017
Important terms Black-box testing White-box testing Regression testing
Fall 2017 CISC124 9/21/2018 CISC124 First onQ quiz this week – write in lab. More details in last Wednesday’s lecture. Repeated: The quiz availability.
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Important terms Black-box testing White-box testing Regression testing
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC124 Assignment 4 on Inheritance due next Monday, the 12th at 7pm.
CISC101 Reminders Quiz 1 grading underway Assn 1 due Today, 9pm.
Winter 2018 CISC101 11/27/2018 CISC101 Reminders
Fall 2018 CISC124 12/1/2018 CISC124 Note that the next assignment, on encapsulation, is due next Wednesday at 7pm – not Friday. The next Quiz is not until.
Programming in Java Assertion.
CISC101 Reminders Quiz 2 graded. Assn 2 sample solution is posted.
CISC101 Reminders Assn 3 due tomorrow, 7pm.
Object Oriented Programming in java
CISC/CMPE320 - Prof. McLeod
CISC124 Labs start this week in JEFF 155.
CISC124 Labs start this week in JEFF 155. Fall 2018
CISC/CMPE320 - Prof. McLeod
Fall 2018 CISC124 2/24/2019 CISC124 Quiz 1 marking is complete. Quiz average was about 40/60 or 67%. TAs are still grading assn 1. Assn 2 due this Friday,
Focus of the Course Object-Oriented Software Development
CMPE212 – Reminders The other four assignments are now posted.
CISC101 Reminders All assignments are now posted.
Winter 2019 CISC101 4/16/2019 CISC101 Reminders
CISC101 Reminders Assignment 3 due next Friday. Winter 2019
CMPE212 – Reminders Course Web Site:
Last Time Some discussion of program “efficiency”. Arrays – 1D and 2D
Winter 2019 CISC101 4/28/2019 CISC101 Reminders
Classes, Objects and Methods
Winter 2019 CMPE212 5/3/2019 CMPE212 – Reminders
Additional control structures
Review of Previous Lesson
CMPE212 – Reminders Assignment 2 due today, 7pm.
CISC101 Reminders Assignment 3 due today.
slides created by Ethan Apter and Marty Stepp
CMPE212 – Reminders Assignment 2 due next Friday.
CMPE212 – Reminders Assignment 4 on Inheritance due next Friday.
Review for Midterm 3.
Presentation transcript:

Winter 2006CISC121 - Prof. McLeod1 Stuff Midterm exam in JEF234 on March 9th from 7- 9pm.

Winter 2006CISC121 - Prof. McLeod2 Last Time Debugging, including the use of the debugger in Eclipse.

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

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

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.

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.

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

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

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:

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

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.

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.

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!

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.

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…):

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!

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.

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)

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.

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.

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!

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!).

Winter 2006CISC121 - Prof. McLeod23 Best Reference on Assertions e/assert.html

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.

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.

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.

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

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.

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