Programo Issues Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009.

Slides:



Advertisements
Similar presentations
Identity and Equality Based on material by Michael Ernst, University of Washington.
Advertisements

Singleton vs utility class  at first glance, the singleton pattern does not seem to offer any advantages to using a utility class  i.e., a utility class.
Effective Java, Chapter 3: Methods Common to All Objects.
1 Class Design CS 3331 Fall Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode method.
Effective Java, Chapter 3: Methods Common to All Objects Paul Ammann.
C8: Understanding Inheritance. Intuitive description Intuitive: FLORISTS are SHOPKEEPERS, inheriting various shopkeeper behaviors Tension in OOP languages:
== equals ? CSE 331 SOFTWARE DESIGN & IMPLEMENTATION EQUALITY Autumn 2011.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching and Sorting I 1 Searching and Sorting 1.
CS2200 Software Development Lecture: Object class A. O’Riordan, 2008.
ICS201 Lecture 20 : Searching King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science Department.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Searching. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
Searching. Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an element in it static.
CSE 373 Data Structures and Algorithms Lecture 18: Hashing III.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
1 Inheritance and Polymorphism Chapter 9. 2 Polymorphism, Dynamic Binding and Generic Programming public class Test { public static void main(String[]
Searching Also: Logarithms. 2 Searching an array of integers If an array is not sorted, there is no better algorithm than linear search for finding an.
Announcements  I will discuss the labtest and the written test #2 common mistakes, solution, etc. in the next class  not today as I am still waiting.
Test Automation For Web-Based Applications Portnov Computer School Presenter: Ellie Skobel.
Object-oriented Programming in Java. What is OOP?  The goal is (subtype) polymorphism  Achieved by Classes (user-defined types) Classes (user-defined.
A Singleton Puzzle: What is Printed? 1 public class Elvis { public static final Elvis INSTANCE = new Elvis(); private final int beltSize; private static.
Puzzle 3 1  Write the class Enigma, which extends Object, so that the following program prints false: public class Conundrum { public static void main(String[]
Non-static classes Part 2 1. Methods  like constructors, all non-static methods have an implicit parameter named this  for methods, this refers to the.
Programo Coding Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009.
CSE 331 Software Design & Implementation Hal Perkins Winter 2013 ==, equals(), and all that (Slides by David Notkin and Mike Ernst) 1.
1 TCSS 143, Autumn 2004 Lecture Notes Creating Java Classes.
Overriding toString()
Inheritance (Part 5) Odds and ends 1. Static Methods and Inheritance  there is a significant difference between calling a static method and calling a.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Chapter 5 Objects and Classes Inheritance. Solution Assignments 3 & 4 Review in class…..
Symbol Tables From “Algorithms” (4 th Ed.) by R. Sedgewick and K. Wayne.
Effective Java: Methods Common to All Objects SWE 619: Fall 2008 Paul Ammann.
Java Type System and Object Model Horstmann ch , 7.7.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
1 Class Design CS 3331 Sec 6.1 & 6.3 of [Jia03]. 2 Outline  Organizing classes  Design guidelines  Canonical forms of classes equals method hashCode.
CS 151: Object-Oriented Design November 12 Class Meeting Department of Computer Science San Jose State University Fall 2013 Instructor: Ron Mak
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
CSI 3125, Preliminaries, page 1 Inheritance. CSI 3125, Preliminaries, page 2 Inheritance Using inheritance, can create a general class that defines traits.
1 Object-Oriented Programming Inheritance. 2 Superclasses and Subclasses Superclasses and Subclasses  Superclasses and subclasses Object of one class.
CSC142 NN 1 CSC 142 Overriding methods from the Object class: equals, toString.
COMP 110 Some notes on inheritance, review Luv Kohli December 1, 2008 MWF 2-2:50 pm Sitterson 014.
COMP Inheritance and Polymorphism Yi Hong June 09, 2015.
 2016, Marcus Biel, Marcus Biel, Software Craftsman Identity vs Equality in Java
Software Construction Lab 10 Unit Testing with JUnit
Searching.
Object-oriented Programming in Java
CSC 205 Java Programming II
CS 302 Week 11 Jim Williams, PhD.
COMP 110 Some notes on inheritance, review
Non-static classes.
Overloading and Constructors
Testing, cont., Equality and Identity
null, true, and false are also reserved.
CSE 1030: Implementing Non-Static Features
CS/ENGRD 2110 Fall 2018 The fattest knight at King Arthur's round table was Sir Cumference. He acquired his size from too much pi. Lecture 6: Consequence.
Effective Java, Chapter 3, 3rd Edition: Methods Common to All Objects
Overriding methods from the Object class: equals, toString
Searching.
CS/ENGRD 2110 Spring 2019 Lecture 6: Consequence of type, casting; function equals
Overloading Each method has a signature: its name together with the number and types of its parameters Methods Signatures String toString()
Review for Midterm 3.
Chapter 7 Java Object Model
CS 240 – Advanced Programming Concepts
Presentation transcript:

Programo Issues Cora Pérez Ariza ~ DECSAI ~ UGR Granada, January 28 th & 29 th, 2009

Index JUnit ◦Introduction to the topic ◦Tags ◦Example Design Decisions ◦equals and hashCode methods ◦toString method 2

JUnit Introduction 3 Why a JUnit? They reassure us that the expected behavior of our work is the actual behavior (and will stay like that in the future) How to use it? Creation is easy: short methods that test critical parts of the project’s classes. Integrated on NetBeans Few “tags” to learn

JUnit Tags

JUnit Example: Beginning 5 package programo.core.potential; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.Ignore; import static org.junit.Assert.*; import programo.core.variable.*; import programo.core.assignation.*; You should import as many classes as tags you are going to use

JUnit Example: data members 6 public class PotentialTableTest { PotentialTable pot1, pot2, resNorm, resMarg, resComb, resProj; CategoricalVariable X1, X2, X3, X4; CategoricalAssignation configurationToProject; VariableSet setOfVars1, setOfVars2; You can declare variables as class members, to use as global variables for your tests

JUnit Example: before/after the tests public static void setUpClass() throws Exception { …. public static void tearDownClass() throws Exception { … } These methods will run before/after the bunch of tests (only once)

JUnit Example: before/after each test public void setUp() { … public void tearDown() { … } These methods will run before/after each test (so one time per test)

JUnit Example: auxiliar methods 9 public void initializeVariables(){ … } You can define auxiliar methods to perform specific operations within your JUnit file

JUnit Example: test cases public void testNormalizePotential() { initializeVariables(); pot1.normalizePotential(); assertTrue(resNorm.equalValues(pot1)); tag marks the method as a test Body of the test case Assertion to check if the test has worked

JUnit Example: test cases ready to public void testSumMarginalize() { initializeVariables(); Potential result = pot1.sumMarginalize(setOfVars2); assertTrue(resMarg.equalValues(result)); } You may not want to execute one or more test cases:

JUnit Result in NetBeans 12

JUnit Other useful utilities 13 Other utilities: Exception Handling Use “expected” parameter tag for test cases that expect = ArithmeticException.class) Timeout Define a timeout period in = 1000)

Design Decisions Overview: equals General Contract 14 public boolean equals(Object obj): Implements an equivalence relation: It is reflexive o For any reference value x, x.equals(x) should return true It is symmetric o For any x and y, x.equals(y) == y.equals(x) It is transitive o For any x, y, z, if x.equals(y) and y.equals(z), then x.equals(z) It is consistent o For any x and y, x.equals(y) should return the same in every invocation if both remains inmutable For any non-null reference value x, x.equals(null) should return false Equal objects must have equal hash codes

Design Decisions Overview: hashCode General Contract 15 public int hashCode(): Must be consistent during the same execution of the application Equal objects must produce equal hash codes, however unequal objects need not produce distinct hash codes o Different hash codes for different objects may improve the performance of hashtables

Design Decisions equals and hashCode: when to override 16 Do not override: Each instance of the class is inherently unique You don’t care whether the class provides a “logical equality” test A super class has already overridden equals appropriately for this class The class is private or package private, and you are certain that its equals method will never be invoked Override: When a class has a notion of logical equality that differs from mere object identity, and a super class has not already overridden equals to implement the desired behavior

Design Decisions equals and hashCode: possible problems 17 Problems with sets and hash maps: Overriding equals enables instances of the class to serve as map keys or set elements with predictable, desirable behavior

Design Decisions equals and hashCode: possible problems: Example 18 Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; } … HashMap tabla = new HashMap(); tabla.put(new Point(1,3), 1); if(tabla.containsKey(new Point(1,3))) System.out.println("both points are the same"); else System.out.println("two different objects");

Design Decisions equals and hashCode: possible problems: Example 19 Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; public boolean equals(Object obj){ if(this == obj) return true; if(obj == null) || (obj.getClass() != this.getClass()) return false; Point p = (Point) obj; return (p.x == this.x && p.y == this.y) }

Design Decisions equals and hashCode: possible problems: Example 20 Problems with sets and hash maps: public class Point{ private final int x; private final int y; public Point(int x, int y){ this.x = x; this.y = y; public boolean equals(Object obj){ if(this == obj) return true; if(obj == null) || (obj.getClass() != this.getClass()) return false; Point p = (Point) obj; return (p.x == this.x && p.y == this.y) public int hashCode(){ int hash = 7; hash = 31 * hash + this.x; hash = 31 * hash + this.y; return hash; }

Design Decisions equals and hashCode: general advice 21 equals: Do not change the type of the argument, it takes java.lang.Object Review your method to verify that it fulfills all the requirements stated by the general contract Do not forget to override hashCode method whenever you override equals method Primitives can be compared directly with equality operator (==) float to Float.floatToIntBits double to Double.doubleToLongBits instance of VS getClass Instance of checks if same class or subclass May break symmetry requirement Use only if class final getClass checks if same class exclusively

Design Decisions equals and hashCode: general advice 22 hashCode: Involve significant variables of your object in the calculation of the hash code Review your hashCode method and check if it is returning equal hash codes for equals objects Different hash codes for different objects is not mandatory, but advisable

Design Decisions equals method in ProGraMo 23 When should two objects be equals? When they refer to the same object? Shallow comparison When although they are different objects, their data members’ values are the same? Deep comparison

Design Decisions toString method 24 We should always override toString() Providing a good toString implementation makes a class much more pleasant to use toString method should return all of the interesting information contained in the object

Thanks! DECSAI ~ UGR “Effective Java”, by Joshua Bloch. Addison Wesley “Equals and HashCode”, by Manish Hatwalne “Junit 4 in 60 Seconds”, by cavdar.net