Homework Assignments APP B Reference on Junit Testing.

Slides:



Advertisements
Similar presentations
OO Programming in Java Objectives for today: Overriding the toString() method Polymorphism & Dynamic Binding Interfaces Packages and Class Path.
Advertisements

Python Objects and Classes
J-Unit Framework.
INTERFACES IN JAVA 1.Java Does not support Multiple Inheritance directly. Multiple inheritance can be achieved in java by the use of interfaces. 2.We need.
METHOD OVERRIDING 1.Sub class can override the methods defined by the super class. 2.Overridden Methods in the sub classes should have same name, same.
Polymorphism.
Lilian Blot BUILDING CLASSES Java Programming Spring 2014 TPOP 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
OOP in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Polymorphism. Lecture Objectives To understand the concept of polymorphism To understand the concept of static or early binding To understand the concept.
Abstract Classes and Interfaces
OOP in Java Fawzi Emad Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
CMSC 202 Interfaces. 11/20102 Classes and Methods When a class defines its methods as public, it describes how the class user interacts with the method.
Programming Languages and Paradigms Object-Oriented Programming.
Inheritance. Inhertance Inheritance is used to indicate that one class will get most or all of its features from a parent class. class Dog(Pet): Make.
220 FINAL TEST REVIEW SESSION Omar Abdelwahab. INHERITANCE AND POLYMORPHISM Suppose you have a class FunClass with public methods show, tell, and smile.
1 Review of Java Higher Level Language Concepts –Names and Reserved Words –Expressions and Precedence of Operators –Flow of Control – Selection –Flow of.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Class Library, Formatting, Wrapper Classes, and JUnit Testing
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
Features of Object Oriented Programming Lec.4. ABSTRACTION AND ENCAPSULATION Computer programs can be very complex, perhaps the most complicated artifact.
What is inheritance? It is the ability to create a new class from an existing class.
(1) Unit Testing and Test Planning CS2110: SW Development Methods These slides design for use in lab. They supplement more complete slides used in lecture.
Chapter 12 Object Oriented Design.  Complements top-down design  Data-centered view of design  Reliable  Cost-effective.
Unit Testing with JUnit and Clover Based on material from: Daniel Amyot JUnit Web site.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Pengantar OOP Class-Java. 2 Software Development Tools Using Sun Java SDK alone Source File(s) (.java) Programmer Compiler (javac) Class File(s) (.class)
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
CIS 270—Application Development II Chapter 8—Classes and Objects: A Deeper Look.
Guide to Programming with Python Chapter Eight (Part I) Object Oriented Programming; Classes, constructors, attributes, and methods.
CMSC 202 Inheritance II. Version 10/092 Inherited Constructors? An Employee constructor cannot be used to create HourlyEmployee objects. Why not? We must.
S Ramakrishnan1 Systems V & V, Quality and Standards Dr Sita Ramakrishnan School CSSE Monash University.
1 Enhanced Class Design -- Introduction  We now examine several features of class design and organization that can improve reusability and system elegance.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
Chapter 11: Advanced Inheritance Concepts. Objectives Create and use abstract classes Use dynamic method binding Create arrays of subclass objects Use.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
SourceAnatomy1 Java Source Anatomy Barb Ericson Georgia Institute of Technology July 2008.
AP Java Ch. 4 Review Question 1  Java methods can return only primitive types (int, double, boolean, etc).
Terms and Rules II Professor Evan Korth New York University (All rights reserved)
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Testing JUnit Testing. Testing Testing can mean many different things It certainly includes running a completed program with various inputs It also includes.
CS/ENGRD 2110 FALL 2013 Lecture 3: Fields, getters and setters, constructors, testing 1.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Python / Java Rosetta Stone Bob Wilson December 16, 2015.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Inheritance Modern object-oriented (OO) programming languages provide 3 capabilities: encapsulation inheritance polymorphism which can improve the design,
Modern Programming Tools And Techniques-I
Object Oriented Programming
Lecture 12 Inheritance.
Software Development Java Classes and Methods
ADT’s, Collections/Generics and Iterators
Agenda Warmup AP Exam Review: Litvin A2
Classes and Objects 2nd Lecture
CS 302 Week 11 Jim Williams, PhD.
Inheritance 2nd Lecture
CMSC 202 Interfaces.
Extending Classes.
Java Programming Language
IFS410: Advanced Analysis and Design
Week 3 Object-based Programming: Classes and Objects
Java Inheritance.
CMSC 202 Interfaces.
Abstract Classes and Interfaces
CIS 199 Final Review.
Final and Abstract Classes
Chapter 11 Inheritance and Encapsulation and Polymorphism
CMSC 202 Interfaces.
Corresponds with Chapter 5
Presentation transcript:

Homework Assignments APP B Reference on Junit Testing

Classes Class Definition Class Definition Public Attribute class ClassName: attributes and methods Class Definition public class Classname { attributes and methods } // end of class definition Public Attribute name (optional = value) Public Attribute public (static) type name (optional = value); Private Attribute __name (optional = value) Note: A programmer convention only Access IS NOT prevented by interpreter Private Attribute private (static) type name (optional = value); Note: Access IS prevented by compiler Conventional Word “self” Used to refer to your own object in Python You may use another word, but “self” is the commonly accepted convention. Reserved Word “this” Used similarly to “self” in Python You must use the reserved word “this”. Not required in as many places in the code, e.g. not needed in method parameter lists.

Classes Constructor Method Constructor Method Public Method def __init__ (self, parameter): self.parameter = parameter Constructor Method public ClassName (parameter) { this.parameter = parameter; } // end of method Public Method def name (self, parameters): statements Public Method public type name (parameters) { statements; } // end of method Private Method def __name (self, parameters): statements Note: A programmer convention only Access IS NOT prevented by interpreter Private Method private type name (parameters) { statements; } // end of method Note: Access IS prevented by compiler

Classes Method Return Value Method Return value Method Overloading def name (self, parameters): return expression Method Return value public type name (parameters) { return expression of type; } // end of method Method Overloading def name (self, param = None): if param is None: 1st version of statements else: 2nd version of statements Method Overloading public type name ( ) // no parameter { 1st version of statements; } // end of first “name” method public type name (type param) 2nd version of statements; } // end of second “name” method

Python “Magic” Methods __str__(self) # representation __cmp__(self, other) # compare objects (Supports operator overloading for >, <, etc.) __add__(self, other) # and sub, mul, div, etc (Supports operator overloading for +, -, *, /, etc __eq__(self, other) # check equality __iter__(self) # returns an iterator (Supports “for item in items” type of loop) __del__(self) # clean up Java Equivalents public String toString() // representation public int compareTo(that) // compare objects (Supports implementing Comparable interface) Note: Operator overloading is NOT supported ) public boolean equals(that) // check equality public Iterator<T> iterator() // returns an interator (Supports “for (type item : items)” for-each loop and implementing Iterable<T> interface) protected void finalize() // clean up

Creating / Deleting Objects Instantiating an Object myObject = ClassName(. . .) # … are values for constructor’s parameters Instantiating an Object Classname myObject = new ClassName(. . . ); // … are values for constructor’s parameters Creating an Alias yourObject = myObject # … both variables refer to the same object Creating an Alias ClassName yourObject = myObject; # … both variables refer to the same object Deleting an Object myObject = None # deletes object # (if there is no alias) Deleting an Object myObject = null; // deletes object // (if there is no alias)

Inheritance / Interfaces # OO Concept: A Cat is an Animal class Cat(Animal): attributes and methods Inheritance // OO Concept: A Cat is an Animal public class Cat extends Animal { attributes and methods } // end of class Multiple Inheritance class ClassName(Class1, Class2, …): attributes and methods No Multiple Inheritance Java doesn’t support more than one parent class Interfaces Java supports implementing multiple interfaces public class ClassName implements Int1, Int2, … { } // end of class

Inheritance / Interfaces Polymorphism class Pet: # abstract parent class def makeSound(self): raise NameOfError(“text”) class Dog(Pet): # concrete child class print “Woof Woof” class Cat(Pet): # concrete child class print “Meow” spot = Dog() spot.makeSound() # Woof Woof fluffy = Cat() fluffy.makeSound() # Meow # Attempt to create/use an abstract class fubar = Pet() fubar.makeSound() # raises an Error # at run time Polymorphism In Java, a reference to any object may be saved as a reference to the type of a parent class or of any implemented interface: If Cat class and Dog class extend Pet class, we can do these “widening“ conversions: Dog d = new Dog(); Pet p = d; // our Pet is a Dog p = New Cat(); // and is now a Cat And call any Pet method on variable p: p.anyPetMethod(. . .); // on Dog/Cat If a method parameter needs to be a Pet, public void methodName(Pet p) {…} we can pass a Dog or a Cat object to it: methodName(d); // pass it a Dog methodName(new Cat()); // or Cat If Pet is an abstract class, we can’t create a Pet object (causes a compilation error) Pet p = new Pet(); // compile error

Inheritance / Interfaces Polymorphism If a method definition requires returning a reference to a class or interface, it may return a reference to an object of the class, a child class, or an implementing class. If Pet class implements Comparable<T>, Dog and Cat class also implement it. If we invoke a method with a return value of type Comparable<T>: Comparable<T> c = methodName( . . . ); It can return a Dog or a Cat object: public Comparable<T> methodName(. . .) { if (some boolean expression) return new Dog(); else return new Cat(); }

JUnit Testing Testing is critical to developing software quality Good test plans are difficult to specify but also difficult to document precisely in English Good testing must be repeatable Good testing is tedious so you will be tempted to cut corners and omit out some needed testing Testing is a good candidate for automation Some SW development methodologies such as “Extreme Programming” require daily builds and automated testing 10

JUnit Testing In Lab 2, when you develop Java code for the QuadraticSolver class, you use the CLI class itself as the “driver” to enter and execute the test cases from the table in the assignment You manually enter the test case values and visually verify whether the response provided is correct or not This is labor intensive and error prone!! The JUnit framework helps us build a “test case” class to automate testing of a “class under test” 11

JUnit Testing “junit.framework.TestCase Class” TestSolver “Driver Class” TestCase TestSolver extends + assertEquals( ) . . . + test2RealRoots( ) . . . depends on “Class Under Test” QuadraticSolver + QuadraticSolver( ) + getEquation( ) + getSolution( ) 12

JUnit Testing Useful method inherited from TestCase class: assertEquals(Object expected, Object actual) assertEquals(“expected”, cut.toString( )); The assertEquals method flags discrepancies between the “expected” value and the result returned by the “class under test” method( ) assertEquals method automatically displays the difference between the “expected value” and the actual return value received 13

JUnit Testing Other useful assert… methods assertEquals(double expected_value, double actual_value, double threshold_value) Automatically compares absolute difference between first two parameters with a threshold assertEquals(4.3, cut.getDbl(), 0.1); 14

JUnit Testing Useful assert… methods for boolean data type Automatically expects returned value is true assertTrue(cut.getBoolean()); Automatically expects returned value is false assertFalse(cut.getBoolean()); 15

JUnit Test for QuadraticSolver import junit.framework.TestCase; public class TestSolver extends TestCase { private QuadraticSolver cut; // a reference to an object of the “class under test” public TestSolver() { // nothing needed here } // First of six test case methods for the QuadraticSolver class public void test2RealRoots() cut = new QuadraticSolver(1, 0, -1); assertEquals("Solving: 1x\u00b2 + 0x -1 = 0", uut.getEquation()); assertEquals("Root 1 is 1.0\nRoot 2 is -1.0", uut.getSolution()); 16

JUnit Testing Test Case Execution 1 test failed: TestSolver test2RealRoots test2ImaginaryRoots testOnly1Root testLinear testNoSolution testAnySolution File: C:\Documents and Settings\bobw\My Documents\bobw\public_html\CS110\Project1\JUnitSolution\TestSolver.java [line: 48] Failure: expected:<......> but was:<...1...> (I removed part of “should be” string constant to create an error) 17

JUnit Testing The Java code in the TestCase class(es) precisely documents all the test cases It allows them to be run automatically It allows people other than the test designer to run them without knowing the details of how they work It prevents oversights in identification of any discrepancies in the results Caveat: When you get errors, check both the test case code and the code of the class under test The error may be caused in either or both places 18