Utilities (Part 3) Implementing static features 1.

Slides:



Advertisements
Similar presentations
 Both System.out and System.err are streams—a sequence of bytes.  System.out (the standard output stream) displays output  System.err (the standard.
Advertisements

16-Jun-15 javadoc. 2 Javadoc placement javadoc comments begin with /** and end with */ In a javadoc comment, a * at the beginning of the line is not part.
Object-Oriented Enterprise Application Development Javadoc Last Updated: 06/30/2001.
Scott Grissom, copyright 2004Ch 3: Java Features Slide 1 Why Java? It is object-oriented provides many ready to use classes platform independent modern.
26-Jun-15 Methods. About methods A method is a named group of declarations and statements If a method is in the same class, you execute those declarations.
1 Doc Comment Conventions. 2 Write for your audience Rule 32: Write documentation for– those who must use your code Users should not need to care how.
1 More on Arrays Arrays of objects Command line arguments The ArrayList class Javadoc Review Lecture 8 notes and L&L 7.1 – 7.2 Reading for this lecture:
Concordia University Department of Computer Science and Software Engineering Click to edit Master title style ADVANCED PROGRAMING PRACTICES API documentation.
Computer Science 340 Software Design & Testing Design By Contract.
3.1 Documentation & Java Language Elements Purpose of documentation Assist the programmer with developing the program Assist other programers who.
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.
JavaDoc1 JavaDoc DEPARTMENT OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING CONCORDIA UNIVERSITY July 24, 2006 by Emil Vassev & Joey Paquet revision 1.2 –
07 Coding Conventions. 2 Demonstrate Developing Local Variables Describe Separating Public and Private Members during Declaration Explore Using System.exit.
Writing JavaDocs Mimi Opkins CECS 274 Copyright (c) Pearson All rights reserved.
The Java Programming Language
Program documentation using the Javadoc tool 1 Program documentation Using the Javadoc tool.
Classes CS 21a: Introduction to Computing I First Semester,
Question of the Day  On a game show you’re given the choice of three doors: Behind one door is a car; behind the others, goats. After you pick a door,
Utilities (Part 2) Implementing static features 1.
Low-Level Detailed Design SAD (Soft Arch Design) Mid-level Detailed Design Low-Level Detailed Design Design Finalization Design Document.
INTERFACES More OO Concepts. Interface Topics Using an interface Interface details –syntax –restrictions Create your own interface Remember polymorphism.
Documentation and Programming Style Appendix A © 2015 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Javadoc Comments.  Java API has a documentation tool called javadoc  The javadoc tool is used on the source code embedded with javadoc-style comments.
Software Documentation Section 5.5 ALBING’s Section JIA’s Appendix B JIA’s.
Documentation javadoc. Documentation not a programmer's first love lives in a separate file somewhere usually a deliverable on the schedule often not.
Javadoc A very short tutorial. What is it A program that automatically generates documentation of your Java classes in a standard format For each X.java.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Documentation Dr. Andrew Wallace PhD BEng(hons) EurIng
1 JAVA API & Packages Starring: Java Documentation Co-Starring: BlueJ IDE.
CSC 212 – Data Structures Prof. Matthew Hertz WTC 207D /
JavaDoc and Contracts Spring Documenting Contracts with JavaDoc Contract model for methods Preconditions Postconditions JavaDoc Industry standard.
Inheritance (Part 2) KomondorBloodHound PureBreedMix Dog Object.
Java 3: Odds & Ends Advanced Programming Techniques.
A brief introduction to javadoc and doxygen. What’s in a program file? 1. Comments 2. Code.
1  lecture slides online
Page 1 – Autumn 2009Steffen Vissing Andersen SDJ I1, Autumn 2009 Agenda: Java API Documentation Code Documenting (in javadoc format) Debugging.
Computer Science 209 Software Development Handing Errors and Creating Documentation.
CS305j Introduction to Computing Classes II 1 Topic 24 Classes Part II "Object-oriented programming as it emerged in Simula 67 allows software structure.
Java Doc Guideline R.SANTHANA GOPALAN. Java Doc Guideline Audience Internal Developers PQA - who write test plans PPT – who write the documentation Customers.
ANU COMP2110 Software Design in 2003 Lecture 10Slide 1 COMP2110 Software Design in 2004 Lecture 12 Documenting Detailed Design How to write down detailed.
SWE 4743 Abstract Data Types Richard Gesick. SWE Abstract Data Types Object-oriented design is based on the theory of abstract data types Domain.
Documentation Javadocs. Design/Documentation An essential ingredient of good Object Oriented programming is known as design by contract. This means that.
CSE 1030: Implementing Static Features Mark Shtern.
Puzzle 2 1  what does the following program print? public class Puzzle02 { public static void main(String[] args) { final long MICROS_PER_DAY = 24 * 60.
Utilities (Part 2) Implementing static features 1.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
Winter 2006CISC121 - Prof. McLeod1 Stuff We had better discuss a midterm date… –27 Feb. to 3 March or –6 to 10 March.
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Today Javadoc. Packages and static import. Viewing API source code. Upcoming Topics: –protected access modifier –Using the debugger in Eclipse –JUnit testing.
3-July-2002cse142-D2-Methods © 2002 University of Washington1 Methods CSE 142, Summer 2002 Computer Programming 1
Today Encapsulation. Build a fully encapsulated Halloween class, going from Halloween1 to Halloween6 (eventually!): –The final version will have overloaded.
More on Arrays Review of Arrays of ints, doubles, chars
Advanced Programing practices
More Sophisticated Behavior
Software Development Handing Errors and Creating Documentation
Introduction to javadoc
Design by Contract Fall 2016 Version.
Advanced Object Oriented Programming
Object initialization: constructors
CSE 1030: Implementing Non-Static Features
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.
Slides by Steve Armstrong LeTourneau University Longview, TX
JavaDoc and Contracts Fall 2008.
Anatomy of a Java Program
Advanced Programing practices
Introduction to javadoc
Classes CS 21a: Introduction to Computing I
Computer Science 340 Software Design & Testing
Software Specifications
Presentation transcript:

Utilities (Part 3) Implementing static features 1

Goals for Today 2  learn about preconditions versus validation  introduction to documentation  introduction to testing

Yahtzee class so far  recall our implementation of the Yahtzee class so far  private constructor to prevent instantiation  public constant field that represents the number of dice  public method that determines if a list of dice represents a roll of three-of-a-kind 3

4 import java.util.Collections; import java.util.ArrayList; import java.util.List; public class Yahtzee { private Yahtzee() { // private and empty by design } public static final int NUMBER_OF_DICE = 5; public static boolean isThreeOfAKind(List dice) { List copy = new ArrayList (dice); Collections.sort(copy); boolean result = copy.get(0).getValue() == copy.get(2).getValue() || copy.get(1).getValue() == copy.get(3).getValue() || copy.get(2).getValue() == copy.get(4).getValue(); return result; }

Yahtzee client: Not enough dice  consider the following client program that tries to use our utility class using fewer than 5 dice 5

6 import java.util.ArrayList; import java.util.List; public class YahtzeeClient { public static void main(String[] args) { final int N_DICE = 3; // NOT ENOUGH DICE List dice = new ArrayList (); for (int i = 0; i < N_DICE; i++) { dice.add(new Die()); } System.out.print("Dice: " + dice.get(0).getValue()); for (int i = 1; i < N_DICE; i++) { System.out.print(", " + dice.get(i).getValue()); } System.out.println(); boolean isThree = Yahtzee.isThreeOfAKind(dice); System.out.println("three of a kind?: " + isThree); }

Yahtzee client: Not enough dice  the output of the program is: Dice: 5, 4, 4 Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3 at java.util.ArrayList.RangeCheck(Unknown Source) at java.util.ArrayList.get(Unknown Source) at Yahtzee.isThreeOfAKind(Yahtzee.java:38) at YahtzeeClient.main(YahtzeeClient.java:19) 7

Yahtzee client: Too many dice  consider the following client program that tries to use our utility class using more than 5 dice 8

9 import java.util.ArrayList; import java.util.List; public class YahtzeeClient { public static void main(String[] args) { final int N_DICE = 7; // TOO MANY DICE List dice = new ArrayList (); for (int i = 0; i < N_DICE; i++) { dice.add(new Die()); } System.out.print("Dice: " + dice.get(0).getValue()); for (int i = 1; i < N_DICE; i++) { System.out.print(", " + dice.get(i).getValue()); } System.out.println(); boolean isThree = Yahtzee.isThreeOfAKind(dice); System.out.println("three of a kind?: " + isThree); }

Yahtzee client: Too many dice  the program seems to work sometimes: Dice: 3, 2, 2, 5, 2, 4, 1 three of a kind?: true  but fails sometimes: Dice: 6, 3, 3, 6, 6, 5, 5 three of a kind?: false 10

Preconditions and postconditions  recall the meaning of method pre- and postconditions  precondition  a condition that the client must ensure is true immediately before a method is invoked  postcondtion  a condition that the method must ensure is true immediately after the method is invoked 11

Who is responsible?  our method isThreeOfAKind clearly fails if the client uses the wrong number of dice  we say that the method cannot satisfy its postcondition if the client uses the wrong number of dice  as the implementer, we should advertise this fact as part of the method API  as the implementer, we also need to decide who is responsible if a client uses the wrong number of dice 12

Client is responsible: Preconditions  as the implementer, we can choose to make the client responsible for errors caused by using the wrong number of dice  we do this by stating in the API that the method has a precondition  we'll see exactly how to do this in Java shortly 13

Client is responsible: Preconditions  recall that a method precondition is a condition that the client must ensure is true immediately before invoking a method  if the precondition is not true, then the client has no guarantees of what the method will do  for utility class methods, preconditions are conditions on the values of the arguments passed to the method  e.g., in our current implementation of isThreeOfAKind the number of dice must be 5 14

Implementer is responsible: Validation  as the implementer, we can choose to specify precisely what happens if the method cannot satisfy its postcondition given the arguments provided by the client  this often requires that the method implementation validate its parameters  e.g., isThreeOfAKind would have to check that the client has used a list argument containing 5 dice 15

16 public static boolean isThreeOfAKind(List dice) { if (dice.size() != Yahtzee.NUMBER_OF_DICE) { throw new IllegalArgumentException("wrong number of dice: " + dice.size()); } List copy = new ArrayList (dice); Collections.sort(copy); boolean result = copy.get(0).getValue() == copy.get(2).getValue() || copy.get(1).getValue() == copy.get(3).getValue() || copy.get(2).getValue() == copy.get(4).getValue(); return result; }

Documenting  documenting code was not a new idea when Java was invented  however, Java was the first major language to embed documentation in the code and extract the documentation into readable electronic APIs  the tool that generates API documents from comments embedded in the code is called Javadoc 17

Documenting  Javadoc processes doc comments that immediately precede a class, attribute, constructor or method declaration  doc comments delimited by /** and */  doc comment written in HTML and made up of two parts 1. a description  first sentence of description gets copied to the summary section  only one description block; can use to create separate paragraphs 2. block tags  @throws and many others) is a non-standard (custom tag used in EECS1030) for documenting preconditions 18

Method documentation example /** * dice */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 19 Eclipse will generate an empty Javadoc comment for you if you right-click on the method header and choose Source  Generate Element Comment

Method documentation example /** * Returns true if the list dice contains a three-of-a-kind. * dice */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 20 The first sentence of the documentation should be short summary of the method; this sentence appears in the method summary section.

Method documentation example /** * Returns true if the list dice contains a three-of-a-kind. * * A three of a kind is defined as at least three dice having * the same value. * dice */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 21 If you want separate paragraphs in your documentation, you need to use the html paragraph tag to start a new paragraph.

Method documentation example /** * Returns true if the list dice contains a three-of-a-kind. * * A three of a kind is defined as at least three dice having * the same value. * dice list of dice representing the roll */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 22 You should provide a brief description of each parameter.

Method documentation example /** * Returns true if the list dice contains a three-of-a-kind. * * A three of a kind is defined as at least three dice having * the same value. * dice list of dice representing the roll true if dice contains three-of-a-kind, false otherwise */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 23 Provide a brief description of the return value if the return type is not void. This description often describes a postcondition of the method.

Method documentation example  if a method has one or more preconditions, you should use the EECS1011 tag to document them  e.g., if we were documenting our original version of isThreeOfAKind we would use tag to document the precondition dice.size() == Yahtzee.NUMBER_OF_DICE 24

Method documentation example /** * Returns true if the list dice contains a three-of-a-kind. * * A three of a kind is defined as at least three dice having * the same value. * dice list of dice representing the roll dice.size() == Yahtzee.NUMBER_OF_DICE true if dice contains three-of-a-kind, false otherwise */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 25 Describe any preconditions using the EECS1011 tag.

Method documentation example  if a method throws an exception (perhaps as a result of failing to validate a parameter) then you should use tag to document the exception  e.g., if we were documenting our second version of isThreeOfAKind we would use tag to document the exception that is thrown if dice.size() != Yahtzee.NUMBER_OF_DICE 26

Method documentation example /** * Returns true if the list dice contains a three-of-a-kind. * * A three of a kind is defined as at least three dice having * the same value. * dice list of dice representing the roll true if dice contains three-of-a-kind, false otherwise IllegalArgumentException if dice.size() != Yahtzee.NUMBER_OF_DICE */ public static boolean isThreeOfAKind(List dice) { // implementation not shown } 27 Use tag to document each exception that might be thrown by your method.

Documenting fields  all public fields should have a Javadoc comment describing the field  Eclipse will generate an empty Javadoc comment for you if you right-click on the field declaration and choose Source  Generate Element Comment 28

Field documentation example public class Yahtzee { /** * The number of six-sided dice used in a standard game * of Yahtzee. */ public static final int NUMBER_OF_DICE = 5; 29

Documenting classes  all classes should contain a description of the class  Eclipse will generate an empty Javadoc comment for you if you right-click on the field declaration and choose Source  Generate Element Comment  the description of a class can be quite detailed for sophisticated classes  e.g., java.lang.String  you should describe the purpose of the class and any other information that might be important to clients  but normally you do not describe the implementation details of the class 30

Class documentation example /** * A utility class that encodes a subset of the rules for * the game Yahtzee. * * A description of the scoring categories can be * found on the * Yahtzee Wikipedia web page. * EECS1011E_W15 * */ public class Yahtzee { // implementation not shown } 31

javadoc Documentation  Oracle's how-to page  html html  also see the examples in the course notes 32