Assertions Program correctness. Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean.

Slides:



Advertisements
Similar presentations
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.
Advertisements

1. Define the concept of assertions. 1 Explain the use of assertions. 2 Create Java program using assertions. 3 Run Java program using assertions. 4 2.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
Core Java Lecture 4-5. What We Will Cover Today What Are Methods Scope and Life Time of Variables Command Line Arguments Use of static keyword in Java.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
An Introduction to Java Programming and Object- Oriented Application Development Chapter 8 Exceptions and Assertions.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 13 Exception Handling.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Fall 2013 Chapter 13 Exception.
Slides prepared by Rose Williams, Binghamton University ICS201 Exception Handling University of Hail College of Computer Science and Engineering Department.
1 / 89 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 11 Programming Fundamentals using Java 1.
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.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
The Java Assert Statement. 2 Assert A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates a particle’s.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
1 Exception Handling (in a nutshell). 2 Motivations When a program runs into a runtime error, the program terminates abnormally. How can you handle the.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 17 Exceptions and.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Exception Handling in Java Course Lecture Slides 7 th July 2010 “ Admitting.
Unit Testing & Defensive Programming. F-22 Raptor Fighter.
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 14 Exception Handling and Text.
1 Chapter 18 Exception Handling. 2 Motivations F Program runs into a runtime error –program terminates abnormally F How can you handle the runtime error.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 18 Exception Handling.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Exception Handling.
Chapter 12: Exception Handling
The switch StatementtMyn1 The switch Statement Sometimes there can be a multiple-choice situation, in which you need to execute a particular set of statements.
Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved Chapter 15 Exceptions and.
Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 11 Exception Handling and Text.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
A Survey on Java Modeling Languages Gergely Kovásznai,Eszterházy Károly College Wolfgang Schreiner,Johannes Kepler University Gábor Kusper,Eszterházy Károly.
Page: 1 การโปรแกรมเชิงวัตถุด้วยภาษา JAVA บุรินทร์ รุจจนพันธุ์.. ปรับปรุง 15 มิถุนายน 2552 Keyword & Data Type มหาวิทยาลัยเนชั่น.
Programming with Assertions © Allan C. Milne v
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Programming and Problem Solving With Java Copyright 1999, James M. Slack Exceptions Handling Exceptions with try and catch The finally-block The throws.
Java Nuts and Bolts Variables and Data Types Operators Expressions Control Flow Statements Arrays and Strings.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
Exceptions and Assertions Chapter 15 – CSCI 1302.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Spring 2009 Programming Fundamentals I Java Programming XuanTung Hoang Lecture No. 8.
The Java Assertion. 2 Assertion A Java statement in JDK 1.4 & newer Intent: enables code to test assumptions. E.g., a method that calculates the a particle’s.
1 Exceptions. 2 Syntax Errors, Runtime Errors, and Logic Errors syntax errors, runtime errors, and logic errors You learned that there are three categories.
1 Chapter 15 Exceptions and Assertions. 2 Objectives F To know what is exception and what is exception handling (§15.2). F To distinguish exception types:
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 15 Exceptions and Assertions Nothing is impossible.
COP3502 Programming Fundamentals for CIS Majors 1 Instructor: Parisa Rashidi.
Defensive Programming. Good programming practices that protect you from your own programming mistakes, as well as those of others – Assertions – Parameter.
1 Lecture # 2. * Introducing Programming with an Example * Identifiers, Variables, and Constants * Primitive Data Types * Byte, short, int, long, float,
Lesson 4 Programming Techniques Exception Handling /EH/ AUBG ICoSCIS Team Assoc. Prof. Stoyan Bonev March, , 2013 SWU, Blagoevgrad.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
Chapter 12 Exceptions and File Input/Output
Chapter 13 Exception Handling
Logger, Assert and Invariants
Exception Handling in Java Reference: COS240 Syllabus
Chapter 7 Exceptions and Assertions
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
null, true, and false are also reserved.
An Introduction to Java – Part I, language basics
Programming in Java Assertion.
Chapter 11 Exception Handling and Text I/O
Chapter 12 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Lecture Notes – Week 2 Lecture-2
Chapter 12 Exception Handling and Text IO
Chapter 14 Exception Handling and Text IO
Chapter 12 Exception Handling and Text IO
Additional control structures
CS-1020 and Exception Handling
Chapter 12 Exception Handling and Text IO
Defensive Programming
Presentation transcript:

Assertions Program correctness

Assertions Java statement – enables you to assert an assumption about your program. – An assertion contains a Boolean expression that should be true during program execution. – Assertions can be used to assure program correctness and avoid logic errors. 2

3 Declaring Assertions An assertion is declared using the new Java keyword assert in JDK 1.4 as follows: assert assertion; or assert assertion : detailMessage; where assertion is a Boolean expression and detailMessage is a primitive-type or an Object value.

Executing Assertions When Java executes the assertion statement: – the boolean assertion is evaluated – If it is false, an AssertionError will be thrown. – AssertionError class constructors: no-arg constructor seven overloaded single-argument constructors of type – int, – long, – float, – double, – boolean, – char, – and Object. 4

Which constructor is used? assert assertion; – the no-arg constructor of AssertionError is used. assert assertion : detailMessage; – an appropriate AssertionError constructor is used to match the data type of the message. – Since AssertionError is a subclass of Error, when an assertion becomes false, the program displays a message on the console and exits.

7 Executing Assertions Example public class AssertionDemo { public static void main(String[] args) { int i; int sum = 0; for (i = 0; i < 10; i++) { sum += i; } assert i == 10; assert sum > 10 && sum < 5 * 10 : "sum is " + sum; }

8 Compiling Programs with Assertions Since assert is a new Java keyword introduced in JDK 1.4, you have to compile the program using a JDK 1.4 compiler. Furthermore, you need to include the switch –source 1.4 in the compiler command as follows: javac –source 1.4 AssertionDemo.java NOTE: If you use JDK 1.5, there is no need to use the –source 1.4 option in the command.

9 Running Programs with Assertions By default, the assertions are disabled at runtime. To enable it, use the switch –enableassertions, or –ea for short, as follows: java –ea AssertionDemo Assertions can be selectively enabled or disabled at class level or package level. The disable switch is – disableassertions or –da for short. For example, the following command enables assertions in package package1 and disables assertions in class Class1. java –ea:package1 –da:Class1 AssertionDemo

10 Using Exception Handling or Assertions? Assertion should not be used to replace exception handling. Exception handling deals with unusual circumstances during program execution. Assertions are to assure the correctness of the program. Exception handling addresses robustness and assertion addresses correctness. Like exception handling, assertions are not used for normal tests, but for internal consistency and validity checks. Assertions are checked at runtime and can be turned on or off at startup time.

11 Using Exception Handling or Assertions? Do not use assertions for argument checking in public methods. – Valid arguments that may be passed to a public method are considered to be part of the method’s contract. – The contract must always be obeyed whether assertions are enabled or disabled. public void setRadius(double newRadius) { assert newRadius >= 0; radius = newRadius; }// improper public void setRadius(double newRadius) { if (newRadius >=0) radius = newRadius; else throw new IllegalArgumentException( “Radius cannot be negative’); }

12 Using Exception Handling or Assertions? Use assertions to reaffirm assumptions. – This gives you more confidence to assure correctness of the program. – A common use of assertions is to replace assumptions with assertions in the code.

13 Using Exception Handling or Assertions? Another good use of assertions is place assertions in a switch statement without a default case. switch (month) { case 1:... ; break; case 2:... ; break;... case 12:... ; break; default: assert false : "Invalid month: " + month }