David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation.

Slides:



Advertisements
Similar presentations
Cs205: engineering software university of virginia fall 2006 Specifying Procedures David Evans
Advertisements

Introduction to Programming G51PRG University of Nottingham Revision 1
Air Force Institute of Technology Electrical and Computer Engineering
Recursion CS 367 – Introduction to Data Structures.
Lecture 9: More on objects, classes, strings discuss hw3 assign hw4 default values for variables scope of variables and shadowing null reference and NullPointerException.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 6: Reasoning about Data Abstractions.
Written by: Dr. JJ Shepherd
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Cs2220: Engineering Software Class 5: Validation Fall 2010 University of Virginia David Evans.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Aalborg Media Lab 21-Jun-15 Software Design Lecture 2 “ Data and Expressions”
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Describing Syntax and Semantics
The switch Statement, DecimalFormat, and Introduction to Looping
MIT AITI 2003 Lecture 7 Class and Object - Part I.
Cs205: engineering software university of virginia fall 2006 Semantics and Specifying Procedures David Evans
Comp 248 Introduction to Programming Chapter 4 - Defining Classes Part A Dr. Aiman Hanna Department of Computer Science & Software Engineering Concordia.
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Cs205: engineering software university of virginia fall 2006 Validation David Evans
DIRC Workshop on Software Quality and the Legal System 13 February 2004, Gray's Inn, London LEGAL ASPECTS OF SOFTWARE PROCUREMENT Jos Dumortier University.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
CSC 480 Software Engineering Lecture 14 Oct 16, 2002.
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Introduction to Testing 1. Testing  testing code is a vital part of the development process  the goal of testing is to find defects in your code  Program.
EECE 310: Software Engineering Testing / Validation.
Spring 2008 Mark Fontenot CSE 1341 Principles of Computer Science I Note Set 2.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
 JAVA Compilation and Interpretation  JAVA Platform Independence  Building First JAVA Program  Escapes Sequences  Display text with printf  Data.
Cs2220: Engineering Software Class 6: Defensive Programming Fall 2010 University of Virginia David Evans.
David Evans These slides: Introduction to Static Analysis.
David Evans CS551/651: Dependable Computing University of Virginia Computer Science Static Analysis.
ITEC 370 Lecture 23 Maintenance. Review Questions? Project update on F, next F give prototype demonstration Maintenance –Reactive –Proactive.
 The if statement and the switch statement are types of conditional/decision controls that allow your program.  Java also provides three different looping.
Looping and Counting Lecture 3 Hartmut Kaiser
Introduction to Java Java Translation Program Structure
Changing Databases This presentation gives a quick overview on how to change databases in Osprey.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs -
© 2004 Pearson Addison-Wesley. All rights reserved ComS 207: Programming I Instructor: Alexander Stoytchev
(1) A beginners guide to testing Philip Johnson Collaborative Software Development Laboratory Information and Computer Sciences University of Hawaii Honolulu.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Cs205: engineering software university of virginia fall 2006 Introducing Java David Evans Don’t forget to your registration.
© 2004 Pearson Addison-Wesley. All rights reserved September 14, 2007 Anatomy of a Method ComS 207: Programming I (in Java) Iowa State University, FALL.
Cs2220: Engineering Software Class 3: Java Semantics Fall 2010 University of Virginia David Evans.
1 Program Development  The creation of software involves four basic activities: establishing the requirements creating a design implementing the code.
Written by: Dr. JJ Shepherd
CSC 142 F 1 CSC 142 References and Primitives. CSC 142 F 2 Review: references and primitives  Reference: the name of an object. The type of the object.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Programming in Java (COP 2250) Lecture 12 & 13 Chengyong Yang Fall, 2005.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 7: A Tale of Two Graphs (and.
David Evans CS201J: Engineering Software University of Virginia Computer Science Lecture 5: Implementing Data Abstractions.
© 2004 Pearson Addison-Wesley. All rights reserved August 27, 2007 Primitive Data Types ComS 207: Programming I (in Java) Iowa State University, FALL 2007.
Information and Computer Sciences University of Hawaii, Manoa
Repetition (While-Loop) version]
Primitive Data Types August 28, 2006 ComS 207: Programming I (in Java)
Programming Language Concepts (CIS 635)
Structural testing, Path Testing
Section 3.2c Strings and Method Signatures
Java Software Structures: John Lewis & Joseph Chase
Testing Recap Testing can find problems, but cannot prove your program works Since exhaustive testing is impossible, select test cases with maximum likelihood.
Lecture 3: Abstraction by Specification CS201j: Engineering Software?
Data Abstraction David Evans cs205: engineering software
Lecture 7: A Tale of Two Graphs CS201j: Engineering Software
Lecture 4: Data Abstraction CS201j: Engineering Software
CSE403 Software Engineering Autumn 2000 More Testing
Lecture 2: Java Semantics, Validation CS201j: Engineering Software?
Presentation transcript:

David Evans CS201j: Engineering Software? University of Virginia Computer Science Lecture 2: Java Semantics, Validation

2 September 2003CS 201J Fall Menu Objects in Java: Heap and Stack Validation: Testing and Analysis

2 September 2003CS 201J Fall Java Semantics

2 September 2003CS 201J Fall The Stack and Heap String s = new String (“hello”); s “hello” java.lang.String Objects live on the Heap new creates an Object on the Heap Local variables live on the Stack Point to objects on the Heap String is a type in the Java API for representing sequences of characters

2 September 2003CS 201J Fall String s = new String (“hello”); s “hello” java.lang.String String t = s; t

2 September 2003CS 201J Fall String s = new String (“hello”); s “hello” java.lang.String String t = s; t s = new String (“goodbye”); “goodbye” java.lang.String

2 September 2003CS 201J Fall Primitive Types Not everything in Java is an Object Some types are primitive types –boolean, byte, char, double, float, int, long, short Values of primitive types are stored directly on the stack

2 September 2003CS 201J Fall String s = new String (“hello”); s “hello” java.lang.String String t = s; t int i = 201; i 201 int j = i; j 201 How can we see the difference between primitive types and objects?

2 September 2003CS 201J Fall Equality x == y Object Types: same objects Primitive Types: same value x.equals (y) Object Types: method that compares values of objects Primitive Types: doesn’t exist

2 September 2003CS 201J Fall “hi” “high” Mutability If an object is mutated, all references to the object see the new value sb java.lang.StringBuffer tb StringBuffer sb = new (“hi”); StringBuffer tb = sb; tb.append (“gh”);

2 September 2003CS 201J Fall Immutable/Mutable Types Types can be mutable or immutable –Objects of an immutable type never change value after they are created String is immutable, StringBuffer is mutable –String.concat creates a new String object –StringBuffer.append mutates the old object

2 September 2003CS 201J Fall Java Semantics Question public class Strings { public static void test (String [] args) { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); // What are the values of s, t, sb and tb now? // Which of these are true: // a) s == t b) s1 == t1 c) s == s1 d) s.equals (t) e) sb == tb f) t.equals (tb) }

2 September 2003CS 201J Fall Java Semantics Question public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String String spec is not enough to determine if s, t, s1 and t1 are the same objects! This is what Sun’s JDK 1.4 does. Other implementations could correctly do different things. Note (added Feb 2005): Nora Sovarel noticed that this isn’t actually true. The JLS section on String literals specifies the behavior as shown.

2 September 2003CS 201J Fall Java Semantics Question public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello”

2 September 2003CS 201J Fall Java Semantics Question public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!”

2 September 2003CS 201J Fall Java Semantics Question public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!” java.lang.String

2 September 2003CS 201J Fall public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!” java.lang.String “hello goodbye!” java.lang.String

2 September 2003CS 201J Fall public class Strings { public static void test () { String s = new String ("hello"); String t = new String ("hello"); StringBuffer sb = new StringBuffer ("he"); StringBuffer tb = sb; String s1 = "hello"; String t1 = "hello"; sb.append (“llo"); tb.append (" goodbye!"); s.concat (" goodbye!"); t = s.concat (" goodbye!"); } } s “hello” java.lang.String t sb tb “hello” java.lang.String “he” java.lang.StringBuffer s1 t1 “hello” java.lang.String “hello goodbye!” java.lang.String “hello goodbye!” java.lang.String After test returns?

2 September 2003CS 201J Fall Validation

2 September 2003CS 201J Fall Dictionary Definition val·i·date 1.To declare or make legally valid. 2.To mark with an indication of official sanction. 3.To establish the soundness of; corroborate. Can we do any of these with software?

2 September 2003CS 201J Fall Java’s License READ THE TERMS OF THIS AGREEMENT AND ANY PROVIDED SUPPLEMENTAL LICENSE TERMS (COLLECTIVELY "AGREEMENT") CAREFULLY BEFORE OPENING THE SOFTWARE MEDIA PACKAGE. BY OPENING THE SOFTWARE MEDIA PACKAGE, YOU AGREE TO THE TERMS OF THIS AGREEMENT. IF YOU ARE ACCESSING THE SOFTWARE ELECTRONICALLY, INDICATE YOUR ACCEPTANCE OF THESE TERMS BY SELECTING THE "ACCEPT" BUTTON AT THE END OF THIS AGREEMENT. IF YOU DO NOT AGREE TO ALL THESE TERMS, PROMPTLY RETURN THE UNUSED SOFTWARE TO YOUR PLACE OF PURCHASE FOR A REFUND OR, IF THE SOFTWARE IS ACCESSED ELECTRONICALLY, SELECT THE "DECLINE" BUTTON AT THE END OF THIS AGREEMENT.

2 September 2003CS 201J Fall Java’s License 5. LIMITATION OF LIABILITY. TO THE EXTENT NOT PROHIBITED BY LAW, IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR SPECIAL, INDIRECT, CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF OR RELATED TO THE USE OF OR INABILITY TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. …

2 September 2003CS 201J Fall Java’s License 2. RESTRICTIONS. … Unless enforcement is prohibited by applicable law, you may not modify, decompile, or reverse engineer Software. You acknowledge that Software is not designed, licensed or intended for use in the design, construction, operation or maintenance of any nuclear facility. Sun disclaims any express or implied warranty of fitness for such uses.

2 September 2003CS 201J Fall Software Validation Process designed to increase our confidence that a program works as intended For complex programs, cannot often make guarantees This is why typical software licenses don’t make any claims about their program working

2 September 2003CS 201J Fall Increasing Confidence Testing –Run the program on set of inputs and check the results Verification –Argue formally or informally that the program always works as intended Analysis –Poor programmer’s verification: examine the source code to increase confidence that it works as intended

2 September 2003CS 201J Fall Testing If all the test cases produce the correct results, you know that a particular execution of the program on each of the test cases produced the correct result Concluding that this means the program is correct is like concluding there are no fish in the river because you didn’t catch one!

2 September 2003CS 201J Fall Exhaustive Testing Test all possible inputs PS1: 50x50 grid, all cells can be either dead or alive before starting = But that’s not all: all possible start stop step interactions, different platforms, how long to you need to run it, etc.

2 September 2003CS 201J Fall Selective Testing We can’t test everything, pick test cases with high probability of finding flaws Black-Box Testing: design tests looking only at specification Glass-Box Testing: design tests looking at code –Path-complete: at least one test to exercise each path through code

2 September 2003CS 201J Fall Black-Box Testing Test all paths through the specification: 1.currently dead, three live neighbors 2.currently alive, two live neighbors 3.currently alive, three live neighbors 4.currently dead, < 3 live neighbors 5.currently dead, > 3 live neighbors 6.currently alive, < 2 live neighbors 7.currently alive, > 3 live neighbors public CellState getNextState () // MODIFIES: this // EFFECTS: Returns the next state for this cell. If a cell is currently // dead cell and has three live neighbors, then it becomes a live cell. // If a cell is currently alive and has two or three live neighbors it // remains alive. Otherwise, the cell dies.

2 September 2003CS 201J Fall Black-Box Testing Test all paths through the specification (7 tests) Test boundary conditions 1.all neighbors are dead 2.all neighbors are alive 3.cell is at a corner of the grid 4.cell is at an edge of the grid public CellState getNextState () // MODIFIES: this // EFFECTS: Returns the next state for this cell. If a cell is currently // dead cell and has three live neighbors, then it becomes a live cell. // If a cell is currently alive and has two or three live neighbors it // remains alive. Otherwise, the cell dies.

2 September 2003CS 201J Fall Glass-Box Testing public CellState getNextState () // MODIFIES: this // EFFECTS: Returns the next state for this cell. If a cell is currently // dead cell and has three live neighbors, then it becomes a live cell. // If a cell is currently alive and has two or three live neighbors it // remains alive. Otherwise, the cell dies. { if (countAliveNeighbors () == 3) { return CellState.createAlive (); } else if (getState ().isAlive () && countAliveNeighbors () == 2) { return CellState.createAlive (); } else { return CellState.createDead (); } Test all paths through the code (4)

2 September 2003CS 201J Fall Path-Complete Testing Insufficient –Often, bugs are missing paths Impossible –Most programs have essentially infinite number of paths –Loops and recursion Test with zero, one and several iterations

2 September 2003CS 201J Fall Testing Recap Testing can find problems, not to prove your program works –Since exhaustive testing is impossible, select test cases with maximum probability of finding bugs –A successful test case is one that reveals a bug in your program! Typically at least 40% of cost of software project is testing, often ~80% of cost for safety-critical software

2 September 2003CS 201J Fall Charge Increase confidence a program works by: –Testing: sample possible executions, trying to find ones that don’t work –Analysis: check properties about all possible executions by examining code PS2: a lot longer and harder than PS1